Skip to content

Commit 4f660fd

Browse files
dinofxbradzacher
andauthored
fix(scope-manager): correct analysis of inferred types in conditional types (typescript-eslint#2537)
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
1 parent 33522b4 commit 4f660fd

File tree

4 files changed

+187
-1
lines changed

4 files changed

+187
-1
lines changed

packages/eslint-plugin/tests/rules/no-shadow.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ const ruleTester = new RuleTester({
1111

1212
ruleTester.run('no-shadow TS tests', rule, {
1313
valid: [
14+
// nested conditional types
15+
`
16+
export type ArrayInput<Func> = Func extends (arg0: Array<infer T>) => any
17+
? T[]
18+
: Func extends (...args: infer T) => any
19+
? T
20+
: never;
21+
`,
1422
`
1523
function foo() {
1624
var Object = 0;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,12 @@ class TypeVisitor extends Visitor {
9494
// which are only accessible from inside the conditional parameter
9595
this.#referencer.scopeManager.nestConditionalTypeScope(node);
9696

97-
this.visitChildren(node);
97+
// type parameters inferred in the condition clause are not accessible within the false branch
98+
this.visitChildren(node, ['falseType']);
9899

99100
this.#referencer.close(node);
101+
102+
this.visit(node.falseType);
100103
}
101104

102105
protected TSConstructorType(node: TSESTree.TSConstructorType): void {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type Test<T> = T extends Array<infer U>
2+
? U
3+
: T extends Set<infer U>
4+
? U
5+
: never;
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`type-declaration conditional-nested 1`] = `
4+
ScopeManager {
5+
variables: Array [
6+
ImplicitGlobalConstTypeVariable,
7+
Variable$2 {
8+
defs: Array [
9+
TypeDefinition$1 {
10+
name: Identifier<"Test">,
11+
node: TSTypeAliasDeclaration$1,
12+
},
13+
],
14+
name: "Test",
15+
references: Array [],
16+
isValueVariable: false,
17+
isTypeVariable: true,
18+
},
19+
Variable$3 {
20+
defs: Array [
21+
TypeDefinition$2 {
22+
name: Identifier<"T">,
23+
node: TSTypeParameter$2,
24+
},
25+
],
26+
name: "T",
27+
references: Array [
28+
Reference$1 {
29+
identifier: Identifier<"T">,
30+
isRead: true,
31+
isTypeReference: true,
32+
isValueReference: false,
33+
isWrite: false,
34+
resolved: Variable$3,
35+
},
36+
Reference$4 {
37+
identifier: Identifier<"T">,
38+
isRead: true,
39+
isTypeReference: true,
40+
isValueReference: false,
41+
isWrite: false,
42+
resolved: Variable$3,
43+
},
44+
],
45+
isValueVariable: false,
46+
isTypeVariable: true,
47+
},
48+
Variable$4 {
49+
defs: Array [
50+
TypeDefinition$3 {
51+
name: Identifier<"U">,
52+
node: TSTypeParameter$3,
53+
},
54+
],
55+
name: "U",
56+
references: Array [
57+
Reference$3 {
58+
identifier: Identifier<"U">,
59+
isRead: true,
60+
isTypeReference: true,
61+
isValueReference: false,
62+
isWrite: false,
63+
resolved: Variable$4,
64+
},
65+
],
66+
isValueVariable: false,
67+
isTypeVariable: true,
68+
},
69+
Variable$5 {
70+
defs: Array [
71+
TypeDefinition$4 {
72+
name: Identifier<"U">,
73+
node: TSTypeParameter$4,
74+
},
75+
],
76+
name: "U",
77+
references: Array [
78+
Reference$6 {
79+
identifier: Identifier<"U">,
80+
isRead: true,
81+
isTypeReference: true,
82+
isValueReference: false,
83+
isWrite: false,
84+
resolved: Variable$5,
85+
},
86+
],
87+
isValueVariable: false,
88+
isTypeVariable: true,
89+
},
90+
],
91+
scopes: Array [
92+
GlobalScope$1 {
93+
block: Program$5,
94+
isStrict: false,
95+
references: Array [],
96+
set: Map {
97+
"const" => ImplicitGlobalConstTypeVariable,
98+
"Test" => Variable$2,
99+
},
100+
type: "global",
101+
upper: null,
102+
variables: Array [
103+
ImplicitGlobalConstTypeVariable,
104+
Variable$2,
105+
],
106+
},
107+
TypeScope$2 {
108+
block: TSTypeAliasDeclaration$1,
109+
isStrict: true,
110+
references: Array [],
111+
set: Map {
112+
"T" => Variable$3,
113+
},
114+
type: "type",
115+
upper: GlobalScope$1,
116+
variables: Array [
117+
Variable$3,
118+
],
119+
},
120+
ConditionalTypeScope$3 {
121+
block: TSConditionalType$6,
122+
isStrict: true,
123+
references: Array [
124+
Reference$1,
125+
Reference$2 {
126+
identifier: Identifier<"Array">,
127+
isRead: true,
128+
isTypeReference: true,
129+
isValueReference: false,
130+
isWrite: false,
131+
resolved: null,
132+
},
133+
Reference$3,
134+
],
135+
set: Map {
136+
"U" => Variable$4,
137+
},
138+
type: "conditionalType",
139+
upper: TypeScope$2,
140+
variables: Array [
141+
Variable$4,
142+
],
143+
},
144+
ConditionalTypeScope$4 {
145+
block: TSConditionalType$7,
146+
isStrict: true,
147+
references: Array [
148+
Reference$4,
149+
Reference$5 {
150+
identifier: Identifier<"Set">,
151+
isRead: true,
152+
isTypeReference: true,
153+
isValueReference: false,
154+
isWrite: false,
155+
resolved: null,
156+
},
157+
Reference$6,
158+
],
159+
set: Map {
160+
"U" => Variable$5,
161+
},
162+
type: "conditionalType",
163+
upper: TypeScope$2,
164+
variables: Array [
165+
Variable$5,
166+
],
167+
},
168+
],
169+
}
170+
`;

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