Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
globalSetup: '<rootDir>/jest.globalSetup.js',
transform: {
'^.+\\.ts$': 'ts-jest',
},
Expand Down
11 changes: 11 additions & 0 deletions jest.globalSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const {execFileSync} = require('node:child_process');
const path = require('node:path');

module.exports = function globalSetup() {
// Build the bundle once for the whole test run so tests that exercise the
// published artifact can require it without each rebuilding it themselves.
execFileSync('npm', ['run', 'build'], {
cwd: path.resolve(__dirname),
stdio: 'pipe',
});
};
11 changes: 1 addition & 10 deletions src/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,9 @@ import {execFileSync} from 'node:child_process';

const requireFromTest = createRequire(__filename);

function buildBundle(): string {
execFileSync('npm', ['run', 'build'], {
cwd: path.resolve(__dirname, '..'),
stdio: 'pipe',
});

return path.resolve(__dirname, '../dist/index.cjs');
}

describe('published bundle', () => {
it('inlines metadata for bundled plugins that read their own package.json', () => {
const bundlePath = buildBundle();
const bundlePath = path.resolve(__dirname, '../dist/index.cjs');
const bundle = fs.readFileSync(bundlePath, 'utf8');

expect(bundle).not.toContain('cjsRequire("../package.json")');
Expand Down
36 changes: 36 additions & 0 deletions src/configs/typescript.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import path from 'node:path';
import {execFileSync} from 'node:child_process';

const bundlePath = path.resolve(__dirname, '../../dist/index.cjs');

function lint(filename: string, code: string): Array<{ruleId: string | null, message: string}> {
const script = `
const {Linter} = require('eslint');
const {configs} = require(${JSON.stringify(bundlePath)});
const messages = new Linter().verify(
${JSON.stringify(code)},
configs.typescript,
{filename: ${JSON.stringify(filename)}},
);
process.stdout.write(JSON.stringify(messages));
`;

const output = execFileSync('node', ['-e', script], {
cwd: path.resolve(__dirname, '../..'),
encoding: 'utf8',
});

return JSON.parse(output) as Array<{ruleId: string | null, message: string}>;
}

describe('typescript', () => {
it('should not apply type-checked rules to pure JS files', () => {
const messages = lint('file.js', 'const x = 1;\n');

const typeAwareErrors = messages.filter(
message => message.message.includes('requires type information'),
);

expect(typeAwareErrors).toEqual([]);
});
});
13 changes: 11 additions & 2 deletions src/configs/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,24 @@ const baseRules: Linter.RulesRecord = {
'no-void': 'off',
};

const typescriptFiles = ['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.cts'];

// Factory function to create TypeScript config with the plugin reference
export function createTypescriptConfig(plugin: ESLint.Plugin, javascriptConfig: Linter.Config[]): Linter.Config[] {
return [
...javascriptConfig,
...tseslint.configs.recommendedTypeChecked,
...tseslint.configs
.recommendedTypeChecked
.map(
config => ({
...config,
files: typescriptFiles,
}),
),
jest.configs['flat/recommended'],
{
name: '@croct/typescript',
files: ['**/*.ts', '**/*.tsx'],
files: typescriptFiles,
plugins: {
'@stylistic': stylistic,
'@croct': plugin,
Expand Down
Loading