Skip to content

fix(eslint-plugin): [no-unused-vars] fix false positives for duplicated names in namespaces #2659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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