Skip to content

Commit 2595ccf

Browse files
authored
fix(scope-manager): handle typeParameters of TSInstantiationExpression (typescript-eslint#5355)
1 parent a0d5a70 commit 2595ccf

File tree

7 files changed

+418
-0
lines changed

7 files changed

+418
-0
lines changed

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ packages/ast-spec/src/*/*/fixtures/_error_/*/fixture.ts
1717
# Syntax not yet supported
1818
packages/scope-manager/tests/fixtures/type-declaration/type-query-with-parameters.ts
1919
packages/scope-manager/tests/fixtures/type-declaration/infer-type-constraint.ts
20+
packages/scope-manager/tests/fixtures/instantiation-expressions/type-arguments1.ts
21+
packages/scope-manager/tests/fixtures/instantiation-expressions/type-arguments2.ts
2022

2123
# Ignore CHANGELOG.md files to avoid issues with automated release job
2224
CHANGELOG.md

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,15 @@ export declare namespace Foo {
921921
}
922922
}
923923
}
924+
`,
925+
noFormat`
926+
class Foo<T> {
927+
value: T;
928+
}
929+
class Bar<T> {
930+
foo = Foo<T>;
931+
}
932+
new Bar();
924933
`,
925934
{
926935
code: `

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,13 @@ class Referencer extends Visitor {
692692
this.close(node);
693693
}
694694

695+
protected TSInstantiationExpression(
696+
node: TSESTree.TSInstantiationExpression,
697+
): void {
698+
this.visitChildren(node, ['typeParameters']);
699+
this.visitType(node.typeParameters);
700+
}
701+
695702
protected TSInterfaceDeclaration(
696703
node: TSESTree.TSInterfaceDeclaration,
697704
): void {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Foo<T> {
2+
value: T
3+
}
4+
5+
class Bar<T> {
6+
foo = Foo<T>
7+
}
8+
9+
new Bar()
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`instantiation-expressions type-arguments1 1`] = `
4+
ScopeManager {
5+
variables: Array [
6+
ImplicitGlobalConstTypeVariable,
7+
Variable$2 {
8+
defs: Array [
9+
ClassNameDefinition$1 {
10+
name: Identifier<"Foo">,
11+
node: ClassDeclaration$1,
12+
},
13+
],
14+
name: "Foo",
15+
references: Array [
16+
Reference$2 {
17+
identifier: Identifier<"Foo">,
18+
isRead: true,
19+
isTypeReference: false,
20+
isValueReference: true,
21+
isWrite: false,
22+
resolved: Variable$2,
23+
},
24+
],
25+
isValueVariable: true,
26+
isTypeVariable: true,
27+
},
28+
Variable$3 {
29+
defs: Array [
30+
ClassNameDefinition$2 {
31+
name: Identifier<"Foo">,
32+
node: ClassDeclaration$1,
33+
},
34+
],
35+
name: "Foo",
36+
references: Array [],
37+
isValueVariable: true,
38+
isTypeVariable: true,
39+
},
40+
Variable$4 {
41+
defs: Array [
42+
TypeDefinition$3 {
43+
name: Identifier<"T">,
44+
node: TSTypeParameter$2,
45+
},
46+
],
47+
name: "T",
48+
references: Array [
49+
Reference$1 {
50+
identifier: Identifier<"T">,
51+
isRead: true,
52+
isTypeReference: true,
53+
isValueReference: false,
54+
isWrite: false,
55+
resolved: Variable$4,
56+
},
57+
],
58+
isValueVariable: false,
59+
isTypeVariable: true,
60+
},
61+
Variable$5 {
62+
defs: Array [
63+
ClassNameDefinition$4 {
64+
name: Identifier<"Bar">,
65+
node: ClassDeclaration$3,
66+
},
67+
],
68+
name: "Bar",
69+
references: Array [
70+
Reference$4 {
71+
identifier: Identifier<"Bar">,
72+
isRead: true,
73+
isTypeReference: false,
74+
isValueReference: true,
75+
isWrite: false,
76+
resolved: Variable$5,
77+
},
78+
],
79+
isValueVariable: true,
80+
isTypeVariable: true,
81+
},
82+
Variable$6 {
83+
defs: Array [
84+
ClassNameDefinition$5 {
85+
name: Identifier<"Bar">,
86+
node: ClassDeclaration$3,
87+
},
88+
],
89+
name: "Bar",
90+
references: Array [],
91+
isValueVariable: true,
92+
isTypeVariable: true,
93+
},
94+
Variable$7 {
95+
defs: Array [
96+
TypeDefinition$6 {
97+
name: Identifier<"T">,
98+
node: TSTypeParameter$4,
99+
},
100+
],
101+
name: "T",
102+
references: Array [
103+
Reference$3 {
104+
identifier: Identifier<"T">,
105+
isRead: true,
106+
isTypeReference: true,
107+
isValueReference: false,
108+
isWrite: false,
109+
resolved: Variable$7,
110+
},
111+
],
112+
isValueVariable: false,
113+
isTypeVariable: true,
114+
},
115+
],
116+
scopes: Array [
117+
GlobalScope$1 {
118+
block: Program$5,
119+
isStrict: false,
120+
references: Array [
121+
Reference$4,
122+
],
123+
set: Map {
124+
"const" => ImplicitGlobalConstTypeVariable,
125+
"Foo" => Variable$2,
126+
"Bar" => Variable$5,
127+
},
128+
type: "global",
129+
upper: null,
130+
variables: Array [
131+
ImplicitGlobalConstTypeVariable,
132+
Variable$2,
133+
Variable$5,
134+
],
135+
},
136+
ClassScope$2 {
137+
block: ClassDeclaration$1,
138+
isStrict: true,
139+
references: Array [
140+
Reference$1,
141+
],
142+
set: Map {
143+
"Foo" => Variable$3,
144+
"T" => Variable$4,
145+
},
146+
type: "class",
147+
upper: GlobalScope$1,
148+
variables: Array [
149+
Variable$3,
150+
Variable$4,
151+
],
152+
},
153+
ClassScope$3 {
154+
block: ClassDeclaration$3,
155+
isStrict: true,
156+
references: Array [],
157+
set: Map {
158+
"Bar" => Variable$6,
159+
"T" => Variable$7,
160+
},
161+
type: "class",
162+
upper: GlobalScope$1,
163+
variables: Array [
164+
Variable$6,
165+
Variable$7,
166+
],
167+
},
168+
ClassFieldInitializerScope$4 {
169+
block: TSInstantiationExpression$6,
170+
isStrict: true,
171+
references: Array [
172+
Reference$2,
173+
Reference$3,
174+
],
175+
set: Map {},
176+
type: "class-field-initializer",
177+
upper: ClassScope$3,
178+
variables: Array [],
179+
},
180+
],
181+
}
182+
`;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function makeBox<T>(value: T) {
2+
return { value };
3+
}
4+
5+
type BoxFunc<T> = typeof makeBox<T>;
6+
const makeStringBox = makeBox<string>;

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