Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aadb39f

Browse files
authored
fix(eslint-plugin): [no-unused-vars] false-positive with class expressions (typescript-eslint#2833)
Fixes typescript-eslint#2831
1 parent af9ab87 commit aadb39f

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

packages/eslint-plugin/src/util/collectUnusedVariables.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ class UnusedVarsVisitor<
153153
}
154154
}
155155

156+
private visitClass(
157+
node: TSESTree.ClassDeclaration | TSESTree.ClassExpression,
158+
): void {
159+
// skip a variable of class itself name in the class scope
160+
const scope = this.getScope<TSESLint.Scope.Scopes.ClassScope>(node);
161+
for (const variable of scope.variables) {
162+
if (variable.identifiers[0] === scope.block.id) {
163+
this.markVariableAsUsed(variable);
164+
return;
165+
}
166+
}
167+
}
168+
156169
private visitFunction(
157170
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
158171
): void {
@@ -201,16 +214,9 @@ class UnusedVarsVisitor<
201214
//#region VISITORS
202215
// NOTE - This is a simple visitor - meaning it does not support selectors
203216

204-
protected ClassDeclaration(node: TSESTree.ClassDeclaration): void {
205-
// skip a variable of class itself name in the class scope
206-
const scope = this.getScope<TSESLint.Scope.Scopes.ClassScope>(node);
207-
for (const variable of scope.variables) {
208-
if (variable.identifiers[0] === scope.block.id) {
209-
this.markVariableAsUsed(variable);
210-
return;
211-
}
212-
}
213-
}
217+
protected ClassDeclaration = this.visitClass;
218+
219+
protected ClassExpression = this.visitClass;
214220

215221
protected FunctionDeclaration = this.visitFunction;
216222

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,8 @@ interface _Foo {
960960
// ignored by pattern, even though it's only self-referenced
961961
options: [{ varsIgnorePattern: '^_' }],
962962
},
963+
// https://github.com/ocavue/typescript-eslint-issue-2831/blob/master/index.ts
964+
'export const classes = [class C1 {}, class C2 {}, class C3 {}];',
963965
],
964966

965967
invalid: [

0 commit comments

Comments
 (0)