Skip to content

Commit 0d696c7

Browse files
authored
fix(eslint-plugin): [no-unused-vars] fix false positives for duplicated names in namespaces (#2659)
1 parent 77732a2 commit 0d696c7

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

Diff for: packages/eslint-plugin/src/rules/no-unused-vars.ts

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
2-
TSESTree,
3-
TSESLint,
42
AST_NODE_TYPES,
3+
TSESLint,
4+
TSESTree,
55
} from '@typescript-eslint/experimental-utils';
66
import { PatternVisitor } from '@typescript-eslint/scope-manager';
77
import baseRule from 'eslint/lib/rules/no-unused-vars';
@@ -327,18 +327,22 @@ export default util.createRule<Options, MessageIds>({
327327
break;
328328
}
329329

330-
const scope = context.getScope();
331-
const { variableScope } = scope;
332-
if (variableScope !== scope) {
333-
for (const id of identifiers) {
334-
const superVar = variableScope.set.get(id.name);
335-
if (superVar) {
336-
superVar.eslintUsed = true;
337-
}
338-
}
339-
} else {
340-
for (const id of identifiers) {
341-
context.markVariableAsUsed(id.name);
330+
let scope = context.getScope();
331+
const shouldUseUpperScope = [
332+
AST_NODE_TYPES.TSModuleDeclaration,
333+
AST_NODE_TYPES.TSDeclareFunction,
334+
].includes(node.type);
335+
336+
if (scope.variableScope !== scope) {
337+
scope = scope.variableScope;
338+
} else if (shouldUseUpperScope && scope.upper) {
339+
scope = scope.upper;
340+
}
341+
342+
for (const id of identifiers) {
343+
const superVar = scope.set.get(id.name);
344+
if (superVar) {
345+
superVar.eslintUsed = true;
342346
}
343347
}
344348
}

Diff for: packages/eslint-plugin/tests/rules/no-unused-vars.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,20 @@ export declare namespace Foo {
886886
}
887887
}
888888
`,
889+
{
890+
code: `
891+
declare namespace A {
892+
export interface A {}
893+
}
894+
`,
895+
filename: 'foo.d.ts',
896+
},
897+
{
898+
code: `
899+
declare function A(A: string): string;
900+
`,
901+
filename: 'foo.d.ts',
902+
},
889903
],
890904

891905
invalid: [

0 commit comments

Comments
 (0)