Skip to content

Commit 8de997e

Browse files
authored
feat(eslint-plugin): [no-unused-vars] support explicit resource management with ignoreUsingDeclarations option (#11456)
* feat: add ignoreUsingDeclarations option * test: add test case for using and await using * feat: implement report using and await using with option * test: fix test cases * chore: generate snapshots * chore: revert snapshots and commit only necessary snapshot * Revert "chore: generate snapshots" This reverts commit 01b73c2. * chore: commit only necessary snapshot * refactor: dont use else if after the unconditional continue * test: align tc with eslint * test: fix lint err * test: fix minor diff with eslint tc
1 parent 740a63f commit 8de997e

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

‎packages/eslint-plugin/src/rules/no-unused-vars.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type Options = [
3535
destructuredArrayIgnorePattern?: string;
3636
ignoreClassWithStaticInitBlock?: boolean;
3737
ignoreRestSiblings?: boolean;
38+
ignoreUsingDeclarations?: boolean;
3839
reportUsedIgnorePattern?: boolean;
3940
vars?: 'all' | 'local';
4041
varsIgnorePattern?: string;
@@ -49,6 +50,7 @@ interface TranslatedOptions {
4950
destructuredArrayIgnorePattern?: RegExp;
5051
ignoreClassWithStaticInitBlock: boolean;
5152
ignoreRestSiblings: boolean;
53+
ignoreUsingDeclarations: boolean;
5254
reportUsedIgnorePattern: boolean;
5355
vars: 'all' | 'local';
5456
varsIgnorePattern?: RegExp;
@@ -128,6 +130,11 @@ export default createRule<Options, MessageIds>({
128130
description:
129131
'Whether to ignore sibling properties in `...` destructurings.',
130132
},
133+
ignoreUsingDeclarations: {
134+
type: 'boolean',
135+
description:
136+
'Whether to ignore using or await using declarations.',
137+
},
131138
reportUsedIgnorePattern: {
132139
type: 'boolean',
133140
description:
@@ -163,6 +170,7 @@ export default createRule<Options, MessageIds>({
163170
caughtErrors: 'all',
164171
ignoreClassWithStaticInitBlock: false,
165172
ignoreRestSiblings: false,
173+
ignoreUsingDeclarations: false,
166174
reportUsedIgnorePattern: false,
167175
vars: 'all',
168176
};
@@ -174,6 +182,9 @@ export default createRule<Options, MessageIds>({
174182
options.args = firstOption.args ?? options.args;
175183
options.ignoreRestSiblings =
176184
firstOption.ignoreRestSiblings ?? options.ignoreRestSiblings;
185+
options.ignoreUsingDeclarations =
186+
firstOption.ignoreUsingDeclarations ??
187+
options.ignoreUsingDeclarations;
177188
options.caughtErrors = firstOption.caughtErrors ?? options.caughtErrors;
178189
options.ignoreClassWithStaticInitBlock =
179190
firstOption.ignoreClassWithStaticInitBlock ??
@@ -549,6 +560,14 @@ export default createRule<Options, MessageIds>({
549560
continue;
550561
}
551562

563+
if (
564+
def.type === TSESLint.Scope.DefinitionType.Variable &&
565+
options.ignoreUsingDeclarations &&
566+
(def.parent.kind === 'await using' || def.parent.kind === 'using')
567+
) {
568+
continue;
569+
}
570+
552571
if (hasRestSpreadSibling(variable)) {
553572
continue;
554573
}

‎packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,48 @@ export {};
17151715
],
17161716
filename: 'foo.d.ts',
17171717
},
1718+
{
1719+
code: `
1720+
using resource = getResource();
1721+
`,
1722+
errors: [
1723+
{
1724+
data: {
1725+
action: 'assigned a value',
1726+
additional: '',
1727+
varName: 'resource',
1728+
},
1729+
line: 2,
1730+
messageId: 'unusedVar',
1731+
},
1732+
],
1733+
languageOptions: {
1734+
parserOptions: {
1735+
ecmaVersion: 2026,
1736+
},
1737+
},
1738+
},
1739+
{
1740+
code: `
1741+
await using resource = getResource();
1742+
`,
1743+
errors: [
1744+
{
1745+
data: {
1746+
action: 'assigned a value',
1747+
additional: '',
1748+
varName: 'resource',
1749+
},
1750+
line: 2,
1751+
messageId: 'unusedVar',
1752+
},
1753+
],
1754+
languageOptions: {
1755+
parserOptions: {
1756+
ecmaVersion: 2026,
1757+
},
1758+
},
1759+
},
17181760
],
17191761

17201762
valid: [
@@ -3018,5 +3060,38 @@ declare class Bar {}
30183060
`,
30193061
filename: 'foo.d.ts',
30203062
},
3063+
{
3064+
code: `
3065+
using resource = getResource();
3066+
resource;
3067+
`,
3068+
languageOptions: {
3069+
parserOptions: {
3070+
ecmaVersion: 2026,
3071+
},
3072+
},
3073+
},
3074+
{
3075+
code: `
3076+
using resource = getResource();
3077+
`,
3078+
languageOptions: {
3079+
parserOptions: {
3080+
ecmaVersion: 2026,
3081+
},
3082+
},
3083+
options: [{ ignoreUsingDeclarations: true }],
3084+
},
3085+
{
3086+
code: `
3087+
await using resource = getResource();
3088+
`,
3089+
languageOptions: {
3090+
parserOptions: {
3091+
ecmaVersion: 2026,
3092+
},
3093+
},
3094+
options: [{ ignoreUsingDeclarations: true }],
3095+
},
30213096
],
30223097
});

‎packages/eslint-plugin/tests/schema-snapshots/no-unused-vars.shot‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)