From 36195e62a9c0f19122e4286bb4bc6c309207ea55 Mon Sep 17 00:00:00 2001 From: Maxim Mazurok Date: Sun, 9 Apr 2023 18:30:09 +1000 Subject: [PATCH 1/4] fix #6188 --- .../src/util/collectUnusedVariables.ts | 23 ++++++++++--------- .../no-unused-vars/no-unused-vars.test.ts | 6 +++++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/eslint-plugin/src/util/collectUnusedVariables.ts b/packages/eslint-plugin/src/util/collectUnusedVariables.ts index ba3beb6861d..c410f779850 100644 --- a/packages/eslint-plugin/src/util/collectUnusedVariables.ts +++ b/packages/eslint-plugin/src/util/collectUnusedVariables.ts @@ -423,20 +423,21 @@ function isMergableExported(variable: TSESLint.Scope.Variable): boolean { * @returns True if the variable is exported, false if not. */ function isExported(variable: TSESLint.Scope.Variable): boolean { - const definition = variable.defs[0]; + const exportedDefinition = variable.defs.find(definition => { + if (definition) { + let node = definition.node; - if (definition) { - let node = definition.node; + if (node.type === AST_NODE_TYPES.VariableDeclarator) { + node = node.parent!; + } else if (definition.type === TSESLint.Scope.DefinitionType.Parameter) { + return false; + } - if (node.type === AST_NODE_TYPES.VariableDeclarator) { - node = node.parent!; - } else if (definition.type === TSESLint.Scope.DefinitionType.Parameter) { - return false; + return node.parent!.type.indexOf('Export') === 0; } - - return node.parent!.type.indexOf('Export') === 0; - } - return false; + return false; + }); + return exportedDefinition !== undefined; } /** diff --git a/packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts b/packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts index ee2191a3f4c..31bf0e7212f 100644 --- a/packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts @@ -1072,6 +1072,12 @@ export class Foo { typescript: '4.4', }, }, + ` +interface Foo { + bar: string; +} +export const Foo = 'bar'; + `, ], invalid: [ From 7fc9a9a81cc5b719ec01580b29ae290f292b5125 Mon Sep 17 00:00:00 2001 From: Maxim Mazurok Date: Sun, 9 Apr 2023 19:06:08 +1000 Subject: [PATCH 2/4] optimize --- .../src/util/collectUnusedVariables.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/eslint-plugin/src/util/collectUnusedVariables.ts b/packages/eslint-plugin/src/util/collectUnusedVariables.ts index c410f779850..10b67877e65 100644 --- a/packages/eslint-plugin/src/util/collectUnusedVariables.ts +++ b/packages/eslint-plugin/src/util/collectUnusedVariables.ts @@ -424,18 +424,15 @@ function isMergableExported(variable: TSESLint.Scope.Variable): boolean { */ function isExported(variable: TSESLint.Scope.Variable): boolean { const exportedDefinition = variable.defs.find(definition => { - if (definition) { - let node = definition.node; + let node = definition.node; - if (node.type === AST_NODE_TYPES.VariableDeclarator) { - node = node.parent!; - } else if (definition.type === TSESLint.Scope.DefinitionType.Parameter) { - return false; - } - - return node.parent!.type.indexOf('Export') === 0; + if (node.type === AST_NODE_TYPES.VariableDeclarator) { + node = node.parent!; + } else if (definition.type === TSESLint.Scope.DefinitionType.Parameter) { + return false; } - return false; + + return node.parent!.type.indexOf('Export') === 0; }); return exportedDefinition !== undefined; } From 7353106c51bcc48f4c0744c2cf8ee1c4c54f220d Mon Sep 17 00:00:00 2001 From: Maxim Mazurok Date: Sun, 16 Apr 2023 17:07:23 +1000 Subject: [PATCH 3/4] add tests --- .../no-unused-vars/no-unused-vars.test.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts b/packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts index 31bf0e7212f..add5a31acf4 100644 --- a/packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts @@ -1078,6 +1078,12 @@ interface Foo { } export const Foo = 'bar'; `, + ` +export const Foo = 'bar'; +interface Foo { + bar: string; +} + `, ], invalid: [ @@ -1803,5 +1809,25 @@ x = foo(x); }, ], }, + { + code: ` +interface Foo { + bar: string; +} +const Foo = 'bar'; + `, + errors: [ + { + messageId: 'unusedVar', + line: 5, + column: 7, + data: { + varName: 'Foo', + action: 'assigned a value', + additional: '', + }, + }, + ], + }, ], }); From 35462fc783b379605feaf09ce9c81b0dbfacd777 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Sun, 16 Jul 2023 19:47:23 -0400 Subject: [PATCH 4/4] nit: simplify to .some --- packages/eslint-plugin/src/util/collectUnusedVariables.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/util/collectUnusedVariables.ts b/packages/eslint-plugin/src/util/collectUnusedVariables.ts index 10b67877e65..dad984c1f58 100644 --- a/packages/eslint-plugin/src/util/collectUnusedVariables.ts +++ b/packages/eslint-plugin/src/util/collectUnusedVariables.ts @@ -423,7 +423,7 @@ function isMergableExported(variable: TSESLint.Scope.Variable): boolean { * @returns True if the variable is exported, false if not. */ function isExported(variable: TSESLint.Scope.Variable): boolean { - const exportedDefinition = variable.defs.find(definition => { + return variable.defs.some(definition => { let node = definition.node; if (node.type === AST_NODE_TYPES.VariableDeclarator) { @@ -434,7 +434,6 @@ function isExported(variable: TSESLint.Scope.Variable): boolean { return node.parent!.type.indexOf('Export') === 0; }); - return exportedDefinition !== undefined; } /**