Skip to content

Commit

Permalink
fix(scope-manager): correct analysis of abstract class properties (#2420
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bradzacher committed Aug 29, 2020
1 parent 3a7ec9b commit cd84549
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
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,
],
},
],
}
`;

0 comments on commit cd84549

Please sign in to comment.