Skip to content

Commit cd84549

Browse files
committed
fix(scope-manager): correct analysis of abstract class properties (typescript-eslint#2420)
1 parent 3a7ec9b commit cd84549

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,14 @@ export default function (@Optional() value = []) {
757757
758758
function Optional() {
759759
return () => {};
760+
}
761+
`,
762+
// https://github.com/typescript-eslint/typescript-eslint/issues/2417
763+
`
764+
import { FooType } from './fileA';
765+
766+
export abstract class Foo {
767+
protected abstract readonly type: FooType;
760768
}
761769
`,
762770
],

packages/scope-manager/src/referencer/Referencer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ class Referencer extends Visitor {
139139
this.close(node);
140140
}
141141

142+
protected visitClassProperty(
143+
node: TSESTree.TSAbstractClassProperty | TSESTree.ClassProperty,
144+
): void {
145+
this.visitProperty(node);
146+
this.visitType(node.typeAnnotation);
147+
}
148+
142149
protected visitForIn(
143150
node: TSESTree.ForInStatement | TSESTree.ForOfStatement,
144151
): void {
@@ -413,8 +420,7 @@ class Referencer extends Visitor {
413420
}
414421

415422
protected ClassProperty(node: TSESTree.ClassProperty): void {
416-
this.visitProperty(node);
417-
this.visitType(node.typeAnnotation);
423+
this.visitClassProperty(node);
418424
}
419425

420426
protected ContinueStatement(): void {
@@ -562,7 +568,7 @@ class Referencer extends Visitor {
562568
protected TSAbstractClassProperty(
563569
node: TSESTree.TSAbstractClassProperty,
564570
): void {
565-
this.visitProperty(node);
571+
this.visitClassProperty(node);
566572
}
567573

568574
protected TSAbstractMethodDefinition(
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type T = 1;
2+
3+
abstract class Foo {
4+
protected abstract readonly prop: T;
5+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`class declaration abstract-property 1`] = `
4+
ScopeManager {
5+
variables: Array [
6+
Variable$1 {
7+
defs: Array [
8+
TypeDefinition$1 {
9+
name: Identifier<"T">,
10+
node: TSTypeAliasDeclaration$1,
11+
},
12+
],
13+
name: "T",
14+
references: Array [
15+
Reference$1 {
16+
identifier: Identifier<"T">,
17+
isRead: true,
18+
isTypeReference: true,
19+
isValueReference: false,
20+
isWrite: false,
21+
resolved: Variable$1,
22+
},
23+
],
24+
isValueVariable: false,
25+
isTypeVariable: true,
26+
},
27+
Variable$2 {
28+
defs: Array [
29+
ClassNameDefinition$2 {
30+
name: Identifier<"Foo">,
31+
node: ClassDeclaration$2,
32+
},
33+
],
34+
name: "Foo",
35+
references: Array [],
36+
isValueVariable: true,
37+
isTypeVariable: true,
38+
},
39+
Variable$3 {
40+
defs: Array [
41+
ClassNameDefinition$3 {
42+
name: Identifier<"Foo">,
43+
node: ClassDeclaration$2,
44+
},
45+
],
46+
name: "Foo",
47+
references: Array [],
48+
isValueVariable: true,
49+
isTypeVariable: true,
50+
},
51+
],
52+
scopes: Array [
53+
GlobalScope$1 {
54+
block: Program$3,
55+
isStrict: false,
56+
references: Array [],
57+
set: Map {
58+
"T" => Variable$1,
59+
"Foo" => Variable$2,
60+
},
61+
type: "global",
62+
upper: null,
63+
variables: Array [
64+
Variable$1,
65+
Variable$2,
66+
],
67+
},
68+
ClassScope$2 {
69+
block: ClassDeclaration$2,
70+
isStrict: true,
71+
references: Array [
72+
Reference$1,
73+
],
74+
set: Map {
75+
"Foo" => Variable$3,
76+
},
77+
type: "class",
78+
upper: GlobalScope$1,
79+
variables: Array [
80+
Variable$3,
81+
],
82+
},
83+
],
84+
}
85+
`;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy