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(scope-manager): correct analysis of abstract class properties #2420

Merged
merged 1 commit into from Aug 24, 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
8 changes: 8 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unused-vars.test.ts
Expand Up @@ -757,6 +757,14 @@ export default function (@Optional() value = []) {
function Optional() {
return () => {};
}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/2417
`
import { FooType } from './fileA';
export abstract class Foo {
protected abstract readonly type: FooType;
}
`,
],
Expand Down
12 changes: 9 additions & 3 deletions packages/scope-manager/src/referencer/Referencer.ts
Expand Up @@ -139,6 +139,13 @@ class Referencer extends Visitor {
this.close(node);
}

protected visitClassProperty(
node: TSESTree.TSAbstractClassProperty | TSESTree.ClassProperty,
): void {
this.visitProperty(node);
this.visitType(node.typeAnnotation);
}

protected visitForIn(
node: TSESTree.ForInStatement | TSESTree.ForOfStatement,
): void {
Expand Down Expand Up @@ -413,8 +420,7 @@ class Referencer extends Visitor {
}

protected ClassProperty(node: TSESTree.ClassProperty): void {
this.visitProperty(node);
this.visitType(node.typeAnnotation);
this.visitClassProperty(node);
}

protected ContinueStatement(): void {
Expand Down Expand Up @@ -562,7 +568,7 @@ class Referencer extends Visitor {
protected TSAbstractClassProperty(
node: TSESTree.TSAbstractClassProperty,
): void {
this.visitProperty(node);
this.visitClassProperty(node);
}

protected TSAbstractMethodDefinition(
Expand Down
@@ -0,0 +1,5 @@
type T = 1;

abstract class Foo {
protected abstract readonly prop: T;
}
@@ -0,0 +1,85 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`class declaration abstract-property 1`] = `
ScopeManager {
variables: Array [
Variable$1 {
defs: Array [
TypeDefinition$1 {
name: Identifier<"T">,
node: TSTypeAliasDeclaration$1,
},
],
name: "T",
references: Array [
Reference$1 {
identifier: Identifier<"T">,
isRead: true,
isTypeReference: true,
isValueReference: false,
isWrite: false,
resolved: Variable$1,
},
],
isValueVariable: false,
isTypeVariable: true,
},
Variable$2 {
defs: Array [
ClassNameDefinition$2 {
name: Identifier<"Foo">,
node: ClassDeclaration$2,
},
],
name: "Foo",
references: Array [],
isValueVariable: true,
isTypeVariable: true,
},
Variable$3 {
defs: Array [
ClassNameDefinition$3 {
name: Identifier<"Foo">,
node: ClassDeclaration$2,
},
],
name: "Foo",
references: Array [],
isValueVariable: true,
isTypeVariable: true,
},
],
scopes: Array [
GlobalScope$1 {
block: Program$3,
isStrict: false,
references: Array [],
set: Map {
"T" => Variable$1,
"Foo" => Variable$2,
},
type: "global",
upper: null,
variables: Array [
Variable$1,
Variable$2,
],
},
ClassScope$2 {
block: ClassDeclaration$2,
isStrict: true,
references: Array [
Reference$1,
],
set: Map {
"Foo" => Variable$3,
},
type: "class",
upper: GlobalScope$1,
variables: Array [
Variable$3,
],
},
],
}
`;