Skip to content
Merged
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
78 changes: 78 additions & 0 deletions __tests__/eslintConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { execFileSync } from 'child_process';
import path from 'path';

const repoRoot = path.resolve(__dirname, '..');
const eslintBin = path.join(repoRoot, 'node_modules', 'eslint', 'bin', 'eslint.js');

type LintMessage = {
ruleId: string | null;
message: string;
severity: number;
};

jest.setTimeout(180000);

function lint(code: string, filename: string): LintMessage[] {
const stdout = execFileSync(
process.execPath,
[eslintBin, '--stdin', '--stdin-filename', filename, '--format', 'json'],
{ cwd: repoRoot, input: code, encoding: 'utf8' }
);
const [result] = JSON.parse(stdout) as { messages: LintMessage[] }[];
return result ? result.messages : [];
}

const ruleIds = (messages: LintMessage[]) => messages.map((message) => message.ruleId);

describe('ESLint ruleset', () => {
it('flags console.log while allowing console.error and console.warn', () => {
expect(ruleIds(lint('export function f() {\n console.log("noise");\n}\n', 'lib/sample.ts'))).toContain(
'no-console'
);

for (const method of ['error', 'warn']) {
const messages = lint(`export function f() {\n console.${method}("diagnostic");\n}\n`, 'lib/sample.ts');
expect(messages.filter((message) => message.ruleId === 'no-console')).toEqual([]);
}
});

it('reports unused TypeScript bindings once, through the TS-aware rule', () => {
const messages = lint('export function f() {\n const unusedValue = 1;\n return 2;\n}\n', 'lib/sample.ts');

expect(ruleIds(messages)).toEqual(['@typescript-eslint/no-unused-vars']);
expect(ruleIds(messages)).not.toContain('no-unused-vars');
});

it('accepts underscore-prefixed placeholders for arguments, catches and destructuring', () => {
const code = [
'export function read({ a, b: _b }: { a: number; b: number }, _label: string) {',
' const [, ...rest] = [1, 2, 3];',
' try {',
' return a + rest.length;',
' } catch (_error) {',
' return 0;',
' }',
'}',
].join('\n');

expect(lint(code, 'lib/sample.ts').filter((message) => message.severity === 2)).toEqual([]);
});

it('keeps checked-in and generated build output out of the lint run', () => {
expect(
lint('const stale = 1;\n', '.next/dev/stale.ts').some(
(message) => message.ruleId === null && /ignored/i.test(message.message)
)
).toBe(true);

expect(
lint('const stale = 1;\n', 'coverage/report.ts').some((message) =>
/ignored/i.test(message.message)
)
).toBe(true);

expect(
lint('export const live = 1;\n', 'lib/live.ts').some((message) => /ignored/i.test(message.message))
).toBe(false);
});
});
35 changes: 34 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import coreWebVitals from 'eslint-config-next/core-web-vitals';

// eslint-config-next already ignores `.next`, `out`, `build`, `next-env.d.ts`
// (and ESLint ignores `node_modules`/`.git`), so only what it misses is listed.
const generatedOutput = ['coverage/**', '**/*.tsbuildinfo'];

const config = [
{ ignores: generatedOutput },
...coreWebVitals,
{
rules: {
Expand All @@ -17,7 +22,35 @@ const config = [
},
},
{
ignores: ['.next/**', 'node_modules/**', 'tsconfig.tsbuildinfo'],
// The base rule misreports on TypeScript (types, enums and implements
// clauses); eslint-config-next already enables the TS-aware variant.
files: ['**/*.{ts,tsx,mts,cts}'],
rules: {
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': [
'warn',
{
varsIgnorePattern: '^_',
argsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
destructuredArrayIgnorePattern: '^_',
ignoreRestSiblings: true,
},
],
},
},
{
files: ['**/*.{js,cjs,mjs,jsx}'],
rules: {
'no-unused-vars': [
'warn',
{
varsIgnorePattern: '^_',
argsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
},
],
},
},
];

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@types/react": "^19.0.0",
"@types/react-dom": "^19.0.0",
"autoprefixer": "^10.0.1",
"eslint": "^9.0.0",
"eslint": "^9.39.5",
"eslint-config-next": "16.1.6",
"jest": "^30.3.0",
"jest-environment-jsdom": "^30.5.2",
Expand Down
Loading
Loading