Skip to content

Commit 0d696c7

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

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

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

Lines changed: 18 additions & 14 deletions
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
}

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

Lines changed: 14 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy