Skip to content

feat(eslint-plugin): [no-unused-vars] support explicit resource management with ignoreUsingDeclarations option #11456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
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
19 changes: 19 additions & 0 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type Options = [
destructuredArrayIgnorePattern?: string;
ignoreClassWithStaticInitBlock?: boolean;
ignoreRestSiblings?: boolean;
ignoreUsingDeclarations?: boolean;
reportUsedIgnorePattern?: boolean;
vars?: 'all' | 'local';
varsIgnorePattern?: string;
Expand All @@ -49,6 +50,7 @@ interface TranslatedOptions {
destructuredArrayIgnorePattern?: RegExp;
ignoreClassWithStaticInitBlock: boolean;
ignoreRestSiblings: boolean;
ignoreUsingDeclarations: boolean;
reportUsedIgnorePattern: boolean;
vars: 'all' | 'local';
varsIgnorePattern?: RegExp;
Expand Down Expand Up @@ -127,6 +129,11 @@ export default createRule<Options, MessageIds>({
description:
'Whether to ignore sibling properties in `...` destructurings.',
},
ignoreUsingDeclarations: {
type: 'boolean',
description:
'Whether to ignore using or await using declarations.',
},
reportUsedIgnorePattern: {
type: 'boolean',
description:
Expand Down Expand Up @@ -162,6 +169,7 @@ export default createRule<Options, MessageIds>({
caughtErrors: 'all',
ignoreClassWithStaticInitBlock: false,
ignoreRestSiblings: false,
ignoreUsingDeclarations: false,
reportUsedIgnorePattern: false,
vars: 'all',
};
Expand All @@ -173,6 +181,9 @@ export default createRule<Options, MessageIds>({
options.args = firstOption.args ?? options.args;
options.ignoreRestSiblings =
firstOption.ignoreRestSiblings ?? options.ignoreRestSiblings;
options.ignoreUsingDeclarations =
firstOption.ignoreUsingDeclarations ??
options.ignoreUsingDeclarations;
options.caughtErrors = firstOption.caughtErrors ?? options.caughtErrors;
options.ignoreClassWithStaticInitBlock =
firstOption.ignoreClassWithStaticInitBlock ??
Expand Down Expand Up @@ -548,6 +559,14 @@ export default createRule<Options, MessageIds>({
continue;
}

if (
def.type === TSESLint.Scope.DefinitionType.Variable &&
options.ignoreUsingDeclarations &&
(def.parent.kind === 'await using' || def.parent.kind === 'using')
) {
continue;
}

if (hasRestSpreadSibling(variable)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,48 @@ export {};
],
filename: 'foo.d.ts',
},
{
code: `
using resource = getResource();
`,
errors: [
{
data: {
action: 'assigned a value',
additional: '',
varName: 'resource',
},
line: 2,
messageId: 'unusedVar',
},
],
languageOptions: {
parserOptions: {
ecmaVersion: 2026,
},
},
},
{
code: `
await using resource = getResource();
`,
errors: [
{
data: {
action: 'assigned a value',
additional: '',
varName: 'resource',
},
line: 2,
messageId: 'unusedVar',
},
],
languageOptions: {
parserOptions: {
ecmaVersion: 2026,
},
},
},
],

valid: [
Expand Down Expand Up @@ -3018,5 +3060,38 @@ declare class Bar {}
`,
filename: 'foo.d.ts',
},
{
code: `
using resource = getResource();
resource;
`,
languageOptions: {
parserOptions: {
ecmaVersion: 2026,
},
},
},
{
code: `
using resource = getResource();
`,
languageOptions: {
parserOptions: {
ecmaVersion: 2026,
},
},
options: [{ ignoreUsingDeclarations: true }],
},
{
code: `
await using resource = getResource();
`,
languageOptions: {
parserOptions: {
ecmaVersion: 2026,
},
},
options: [{ ignoreUsingDeclarations: true }],
},
],
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading