From 0d696c72c5c9c3446902a63509d499ee95483e81 Mon Sep 17 00:00:00 2001 From: mbpreble Date: Sun, 11 Oct 2020 19:00:33 -0500 Subject: [PATCH] fix(eslint-plugin): [no-unused-vars] fix false positives for duplicated names in namespaces (#2659) --- .../eslint-plugin/src/rules/no-unused-vars.ts | 32 +++++++++++-------- .../tests/rules/no-unused-vars.test.ts | 14 ++++++++ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-unused-vars.ts b/packages/eslint-plugin/src/rules/no-unused-vars.ts index 7239982c935..8753452dd09 100644 --- a/packages/eslint-plugin/src/rules/no-unused-vars.ts +++ b/packages/eslint-plugin/src/rules/no-unused-vars.ts @@ -1,7 +1,7 @@ import { - TSESTree, - TSESLint, AST_NODE_TYPES, + TSESLint, + TSESTree, } from '@typescript-eslint/experimental-utils'; import { PatternVisitor } from '@typescript-eslint/scope-manager'; import baseRule from 'eslint/lib/rules/no-unused-vars'; @@ -327,18 +327,22 @@ export default util.createRule({ break; } - const scope = context.getScope(); - const { variableScope } = scope; - if (variableScope !== scope) { - for (const id of identifiers) { - const superVar = variableScope.set.get(id.name); - if (superVar) { - superVar.eslintUsed = true; - } - } - } else { - for (const id of identifiers) { - context.markVariableAsUsed(id.name); + let scope = context.getScope(); + const shouldUseUpperScope = [ + AST_NODE_TYPES.TSModuleDeclaration, + AST_NODE_TYPES.TSDeclareFunction, + ].includes(node.type); + + if (scope.variableScope !== scope) { + scope = scope.variableScope; + } else if (shouldUseUpperScope && scope.upper) { + scope = scope.upper; + } + + for (const id of identifiers) { + const superVar = scope.set.get(id.name); + if (superVar) { + superVar.eslintUsed = true; } } } diff --git a/packages/eslint-plugin/tests/rules/no-unused-vars.test.ts b/packages/eslint-plugin/tests/rules/no-unused-vars.test.ts index e22f9eb2ba7..b3d9c92491d 100644 --- a/packages/eslint-plugin/tests/rules/no-unused-vars.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unused-vars.test.ts @@ -886,6 +886,20 @@ export declare namespace Foo { } } `, + { + code: ` +declare namespace A { + export interface A {} +} + `, + filename: 'foo.d.ts', + }, + { + code: ` +declare function A(A: string): string; + `, + filename: 'foo.d.ts', + }, ], invalid: [