Skip to content

Commit 5ced415

Browse files
feat(eslint): Add ignore pattern for unused vars
1 parent 616dc2b commit 5ced415

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
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'],

0 commit comments

Comments
 (0)