Skip to content
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

fix(eslint-plugin): [no-unused-vars] false-positive with class expressions #2833

Merged
merged 1 commit into from
Dec 1, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 16 additions & 10 deletions packages/eslint-plugin/src/util/collectUnusedVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ class UnusedVarsVisitor<
}
}

private visitClass(
node: TSESTree.ClassDeclaration | TSESTree.ClassExpression,
): void {
// skip a variable of class itself name in the class scope
const scope = this.getScope<TSESLint.Scope.Scopes.ClassScope>(node);
for (const variable of scope.variables) {
if (variable.identifiers[0] === scope.block.id) {
this.markVariableAsUsed(variable);
return;
}
}
}

private visitFunction(
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
): void {
Expand Down Expand Up @@ -201,16 +214,9 @@ class UnusedVarsVisitor<
//#region VISITORS
// NOTE - This is a simple visitor - meaning it does not support selectors

protected ClassDeclaration(node: TSESTree.ClassDeclaration): void {
// skip a variable of class itself name in the class scope
const scope = this.getScope<TSESLint.Scope.Scopes.ClassScope>(node);
for (const variable of scope.variables) {
if (variable.identifiers[0] === scope.block.id) {
this.markVariableAsUsed(variable);
return;
}
}
}
protected ClassDeclaration = this.visitClass;

protected ClassExpression = this.visitClass;

protected FunctionDeclaration = this.visitFunction;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,8 @@ interface _Foo {
// ignored by pattern, even though it's only self-referenced
options: [{ varsIgnorePattern: '^_' }],
},
// https://github.com/ocavue/typescript-eslint-issue-2831/blob/master/index.ts
'export const classes = [class C1 {}, class C2 {}, class C3 {}];',
],

invalid: [
Expand Down