Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unused-vars] false-positive with class expres…
Browse files Browse the repository at this point in the history
…sions (#2833)

Fixes #2831
  • Loading branch information
bradzacher committed Dec 1, 2020
1 parent af9ab87 commit aadb39f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/eslint-plugin/src/util/collectUnusedVariables.ts
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
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

0 comments on commit aadb39f

Please sign in to comment.