Skip to content

Commit 4ee556d

Browse files
Expand ESLint exceptions for identifier length and unused variable rules (#126)
2 parents 00c25e8 + 5ced415 commit 4ee556d

File tree

6 files changed

+99
-3
lines changed

6 files changed

+99
-3
lines changed

.changeset/modern-grapes-sit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tcd-devkit/eslint-config': minor
3+
---
4+
5+
Add more exceptions for id-length rule

.changeset/tidy-zoos-taste.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tcd-devkit/eslint-config-ts': minor
3+
---
4+
5+
Add ignore pattern for unused vars that start with \_
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { Linter } from 'eslint';
2+
import { config as defineConfig } from 'typescript-eslint';
3+
import { describe, expect, it } from 'vitest';
4+
5+
import { getLintMessagesForRule } from '@tcd-devkit/internal-utils/test';
6+
import type { GetLintMessagesOptions } from '@tcd-devkit/internal-utils/test';
7+
8+
import { tsConfig } from '#ts.linter';
9+
import type { CustomRule } from '#ts.rules';
10+
11+
const safeTsConfig = defineConfig(tsConfig, {
12+
languageOptions: {
13+
parserOptions: {
14+
projectService: true,
15+
tsconfigRootDir: process.cwd(),
16+
},
17+
},
18+
}) as Linter.Config[];
19+
20+
const ruleId: CustomRule = '@typescript-eslint/no-unused-vars';
21+
22+
const lintTextOptions: GetLintMessagesOptions = {
23+
eslintOptions: {
24+
cwd: import.meta.dirname,
25+
},
26+
lintTextOptions: {
27+
filePath: './fixtures/fixture.ts',
28+
},
29+
};
30+
31+
describe(`${ruleId} rule`, () => {
32+
it('should FAIL when a variable is unused', async () => {
33+
const code = `const x = 1;`;
34+
const messages = await getLintMessagesForRule(
35+
safeTsConfig,
36+
code,
37+
ruleId,
38+
lintTextOptions,
39+
);
40+
41+
expect(messages).toHaveLength(1);
42+
});
43+
44+
it('should PASS when all variables are used', async () => {
45+
const code = `
46+
const y = 1;
47+
console.log(y);
48+
`;
49+
const messages = await getLintMessagesForRule(
50+
safeTsConfig,
51+
code,
52+
ruleId,
53+
lintTextOptions,
54+
);
55+
56+
expect(messages).toHaveLength(0);
57+
});
58+
59+
it('should PASS for unused variables with ignore pattern', async () => {
60+
const code = `const _x = 1;`;
61+
const messages = await getLintMessagesForRule(
62+
safeTsConfig,
63+
code,
64+
ruleId,
65+
lintTextOptions,
66+
);
67+
68+
expect(messages).toHaveLength(0);
69+
});
70+
});

packages/eslint/eslint-config-ts/src/ts.rules.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const tsRules = {
66
'no-loop-func': ['off'],
77
'no-use-before-define': ['off'],
88
'no-duplicate-imports': ['off'],
9+
'@typescript-eslint/no-unused-vars': ['error', { varsIgnorePattern: '^_' }],
910
'@typescript-eslint/consistent-type-imports': ['error'],
1011
'@typescript-eslint/default-param-last': ['error'],
1112
'@typescript-eslint/naming-convention': ['off'],

packages/eslint/eslint-config/src/__tests__/rules/id-length.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const ruleId: CustomRule = 'id-length';
1010
describe(`${ruleId} rule`, () => {
1111
it('should FAIL when identifier is too short', async () => {
1212
const code = `
13-
const x = 1;
14-
const y = 2;
13+
const a = 1;
14+
const b = 2;
1515
`;
1616
const messages = await getLintMessagesForRule(baseConfig, code, ruleId);
1717

@@ -27,4 +27,19 @@ describe(`${ruleId} rule`, () => {
2727

2828
expect(messages).toHaveLength(0);
2929
});
30+
31+
it('should PASS when identifiers are on exceptions list', async () => {
32+
const code = `
33+
const i = 1;
34+
const e = 2;
35+
const _ = 3;
36+
const x = 4;
37+
const y = 5;
38+
const z = 6;
39+
const q = 7;
40+
`;
41+
const messages = await getLintMessagesForRule(baseConfig, code, ruleId);
42+
43+
expect(messages).toHaveLength(0);
44+
});
3045
});

packages/eslint/eslint-config/src/base.rules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const baseRules = {
4646
'grouped-accessor-pairs': ['error', 'getBeforeSet'],
4747
'id-length': [
4848
'error',
49-
{ exceptions: ['i', 'e', '_'] },
49+
{ exceptions: ['i', 'e', '_', 'x', 'y', 'z', 'q'] },
5050
] satisfies ESLintRules['id-length'],
5151
'max-classes-per-file': ['error'],
5252
'max-depth': ['error'],

0 commit comments

Comments
 (0)