Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unused-vars] fix false positives for duplicat…
Browse files Browse the repository at this point in the history
…ed names in namespaces (#2659)
  • Loading branch information
mbpreble authored Oct 12, 2020
1 parent 77732a2 commit 0d696c7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
32 changes: 18 additions & 14 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -327,18 +327,22 @@ export default util.createRule<Options, MessageIds>({
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;
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unused-vars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down

0 comments on commit 0d696c7

Please sign in to comment.