Skip to content

Commit 61ebde9

Browse files
authored
fix(parser): correctly parse type assertions in extends clause (#17761)
1 parent 8920384 commit 61ebde9

File tree

3 files changed

+276
-0
lines changed

3 files changed

+276
-0
lines changed

packages/babel-parser/src/plugins/typescript/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,8 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
26602660
tokenType === tt.bitShiftR ||
26612661
// a<b>c is (a<b)>c
26622662
(tokenType !== tt.parenL &&
2663+
tokenType !== tt._as &&
2664+
tokenType !== tt._satisfies &&
26632665
tokenCanStartExpression(tokenType) &&
26642666
!this.hasPrecedingLineBreak())
26652667
) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class C extends (B<T> as D)<T> {}
2+
class C1 extends (B<Array<T>> as D) {}
3+
class C2 extends (B<T[]> as D) {}
4+
class C3 extends (B<T> satisfies D) {}
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":145,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":38,"index":145}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":145,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":38,"index":145}},
7+
"sourceType": "module",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "ClassDeclaration",
12+
"start":0,"end":33,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":33,"index":33}},
13+
"id": {
14+
"type": "Identifier",
15+
"start":6,"end":7,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":7,"index":7},"identifierName":"C"},
16+
"name": "C"
17+
},
18+
"superClass": {
19+
"type": "TSAsExpression",
20+
"start":17,"end":26,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":26,"index":26}},
21+
"expression": {
22+
"type": "TSInstantiationExpression",
23+
"start":17,"end":21,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":21,"index":21}},
24+
"expression": {
25+
"type": "Identifier",
26+
"start":17,"end":18,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":18,"index":18},"identifierName":"B"},
27+
"name": "B"
28+
},
29+
"typeArguments": {
30+
"type": "TSTypeParameterInstantiation",
31+
"start":18,"end":21,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":21,"index":21}},
32+
"params": [
33+
{
34+
"type": "TSTypeReference",
35+
"start":19,"end":20,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":20,"index":20}},
36+
"typeName": {
37+
"type": "Identifier",
38+
"start":19,"end":20,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":20,"index":20},"identifierName":"T"},
39+
"name": "T"
40+
}
41+
}
42+
]
43+
}
44+
},
45+
"typeAnnotation": {
46+
"type": "TSTypeReference",
47+
"start":25,"end":26,"loc":{"start":{"line":1,"column":25,"index":25},"end":{"line":1,"column":26,"index":26}},
48+
"typeName": {
49+
"type": "Identifier",
50+
"start":25,"end":26,"loc":{"start":{"line":1,"column":25,"index":25},"end":{"line":1,"column":26,"index":26},"identifierName":"D"},
51+
"name": "D"
52+
}
53+
},
54+
"extra": {
55+
"parenthesized": true,
56+
"parenStart": 16
57+
}
58+
},
59+
"superTypeArguments": {
60+
"type": "TSTypeParameterInstantiation",
61+
"start":27,"end":30,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":30,"index":30}},
62+
"params": [
63+
{
64+
"type": "TSTypeReference",
65+
"start":28,"end":29,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":29,"index":29}},
66+
"typeName": {
67+
"type": "Identifier",
68+
"start":28,"end":29,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":29,"index":29},"identifierName":"T"},
69+
"name": "T"
70+
}
71+
}
72+
]
73+
},
74+
"body": {
75+
"type": "ClassBody",
76+
"start":31,"end":33,"loc":{"start":{"line":1,"column":31,"index":31},"end":{"line":1,"column":33,"index":33}},
77+
"body": []
78+
}
79+
},
80+
{
81+
"type": "ClassDeclaration",
82+
"start":34,"end":72,"loc":{"start":{"line":2,"column":0,"index":34},"end":{"line":2,"column":38,"index":72}},
83+
"id": {
84+
"type": "Identifier",
85+
"start":40,"end":42,"loc":{"start":{"line":2,"column":6,"index":40},"end":{"line":2,"column":8,"index":42},"identifierName":"C1"},
86+
"name": "C1"
87+
},
88+
"superClass": {
89+
"type": "TSAsExpression",
90+
"start":52,"end":68,"loc":{"start":{"line":2,"column":18,"index":52},"end":{"line":2,"column":34,"index":68}},
91+
"expression": {
92+
"type": "TSInstantiationExpression",
93+
"start":52,"end":63,"loc":{"start":{"line":2,"column":18,"index":52},"end":{"line":2,"column":29,"index":63}},
94+
"expression": {
95+
"type": "Identifier",
96+
"start":52,"end":53,"loc":{"start":{"line":2,"column":18,"index":52},"end":{"line":2,"column":19,"index":53},"identifierName":"B"},
97+
"name": "B"
98+
},
99+
"typeArguments": {
100+
"type": "TSTypeParameterInstantiation",
101+
"start":53,"end":63,"loc":{"start":{"line":2,"column":19,"index":53},"end":{"line":2,"column":29,"index":63}},
102+
"params": [
103+
{
104+
"type": "TSTypeReference",
105+
"start":54,"end":62,"loc":{"start":{"line":2,"column":20,"index":54},"end":{"line":2,"column":28,"index":62}},
106+
"typeName": {
107+
"type": "Identifier",
108+
"start":54,"end":59,"loc":{"start":{"line":2,"column":20,"index":54},"end":{"line":2,"column":25,"index":59},"identifierName":"Array"},
109+
"name": "Array"
110+
},
111+
"typeArguments": {
112+
"type": "TSTypeParameterInstantiation",
113+
"start":59,"end":62,"loc":{"start":{"line":2,"column":25,"index":59},"end":{"line":2,"column":28,"index":62}},
114+
"params": [
115+
{
116+
"type": "TSTypeReference",
117+
"start":60,"end":61,"loc":{"start":{"line":2,"column":26,"index":60},"end":{"line":2,"column":27,"index":61}},
118+
"typeName": {
119+
"type": "Identifier",
120+
"start":60,"end":61,"loc":{"start":{"line":2,"column":26,"index":60},"end":{"line":2,"column":27,"index":61},"identifierName":"T"},
121+
"name": "T"
122+
}
123+
}
124+
]
125+
}
126+
}
127+
]
128+
}
129+
},
130+
"typeAnnotation": {
131+
"type": "TSTypeReference",
132+
"start":67,"end":68,"loc":{"start":{"line":2,"column":33,"index":67},"end":{"line":2,"column":34,"index":68}},
133+
"typeName": {
134+
"type": "Identifier",
135+
"start":67,"end":68,"loc":{"start":{"line":2,"column":33,"index":67},"end":{"line":2,"column":34,"index":68},"identifierName":"D"},
136+
"name": "D"
137+
}
138+
},
139+
"extra": {
140+
"parenthesized": true,
141+
"parenStart": 51
142+
}
143+
},
144+
"body": {
145+
"type": "ClassBody",
146+
"start":70,"end":72,"loc":{"start":{"line":2,"column":36,"index":70},"end":{"line":2,"column":38,"index":72}},
147+
"body": []
148+
}
149+
},
150+
{
151+
"type": "ClassDeclaration",
152+
"start":73,"end":106,"loc":{"start":{"line":3,"column":0,"index":73},"end":{"line":3,"column":33,"index":106}},
153+
"id": {
154+
"type": "Identifier",
155+
"start":79,"end":81,"loc":{"start":{"line":3,"column":6,"index":79},"end":{"line":3,"column":8,"index":81},"identifierName":"C2"},
156+
"name": "C2"
157+
},
158+
"superClass": {
159+
"type": "TSAsExpression",
160+
"start":91,"end":102,"loc":{"start":{"line":3,"column":18,"index":91},"end":{"line":3,"column":29,"index":102}},
161+
"expression": {
162+
"type": "TSInstantiationExpression",
163+
"start":91,"end":97,"loc":{"start":{"line":3,"column":18,"index":91},"end":{"line":3,"column":24,"index":97}},
164+
"expression": {
165+
"type": "Identifier",
166+
"start":91,"end":92,"loc":{"start":{"line":3,"column":18,"index":91},"end":{"line":3,"column":19,"index":92},"identifierName":"B"},
167+
"name": "B"
168+
},
169+
"typeArguments": {
170+
"type": "TSTypeParameterInstantiation",
171+
"start":92,"end":97,"loc":{"start":{"line":3,"column":19,"index":92},"end":{"line":3,"column":24,"index":97}},
172+
"params": [
173+
{
174+
"type": "TSArrayType",
175+
"start":93,"end":96,"loc":{"start":{"line":3,"column":20,"index":93},"end":{"line":3,"column":23,"index":96}},
176+
"elementType": {
177+
"type": "TSTypeReference",
178+
"start":93,"end":94,"loc":{"start":{"line":3,"column":20,"index":93},"end":{"line":3,"column":21,"index":94}},
179+
"typeName": {
180+
"type": "Identifier",
181+
"start":93,"end":94,"loc":{"start":{"line":3,"column":20,"index":93},"end":{"line":3,"column":21,"index":94},"identifierName":"T"},
182+
"name": "T"
183+
}
184+
}
185+
}
186+
]
187+
}
188+
},
189+
"typeAnnotation": {
190+
"type": "TSTypeReference",
191+
"start":101,"end":102,"loc":{"start":{"line":3,"column":28,"index":101},"end":{"line":3,"column":29,"index":102}},
192+
"typeName": {
193+
"type": "Identifier",
194+
"start":101,"end":102,"loc":{"start":{"line":3,"column":28,"index":101},"end":{"line":3,"column":29,"index":102},"identifierName":"D"},
195+
"name": "D"
196+
}
197+
},
198+
"extra": {
199+
"parenthesized": true,
200+
"parenStart": 90
201+
}
202+
},
203+
"body": {
204+
"type": "ClassBody",
205+
"start":104,"end":106,"loc":{"start":{"line":3,"column":31,"index":104},"end":{"line":3,"column":33,"index":106}},
206+
"body": []
207+
}
208+
},
209+
{
210+
"type": "ClassDeclaration",
211+
"start":107,"end":145,"loc":{"start":{"line":4,"column":0,"index":107},"end":{"line":4,"column":38,"index":145}},
212+
"id": {
213+
"type": "Identifier",
214+
"start":113,"end":115,"loc":{"start":{"line":4,"column":6,"index":113},"end":{"line":4,"column":8,"index":115},"identifierName":"C3"},
215+
"name": "C3"
216+
},
217+
"superClass": {
218+
"type": "TSSatisfiesExpression",
219+
"start":125,"end":141,"loc":{"start":{"line":4,"column":18,"index":125},"end":{"line":4,"column":34,"index":141}},
220+
"expression": {
221+
"type": "TSInstantiationExpression",
222+
"start":125,"end":129,"loc":{"start":{"line":4,"column":18,"index":125},"end":{"line":4,"column":22,"index":129}},
223+
"expression": {
224+
"type": "Identifier",
225+
"start":125,"end":126,"loc":{"start":{"line":4,"column":18,"index":125},"end":{"line":4,"column":19,"index":126},"identifierName":"B"},
226+
"name": "B"
227+
},
228+
"typeArguments": {
229+
"type": "TSTypeParameterInstantiation",
230+
"start":126,"end":129,"loc":{"start":{"line":4,"column":19,"index":126},"end":{"line":4,"column":22,"index":129}},
231+
"params": [
232+
{
233+
"type": "TSTypeReference",
234+
"start":127,"end":128,"loc":{"start":{"line":4,"column":20,"index":127},"end":{"line":4,"column":21,"index":128}},
235+
"typeName": {
236+
"type": "Identifier",
237+
"start":127,"end":128,"loc":{"start":{"line":4,"column":20,"index":127},"end":{"line":4,"column":21,"index":128},"identifierName":"T"},
238+
"name": "T"
239+
}
240+
}
241+
]
242+
}
243+
},
244+
"typeAnnotation": {
245+
"type": "TSTypeReference",
246+
"start":140,"end":141,"loc":{"start":{"line":4,"column":33,"index":140},"end":{"line":4,"column":34,"index":141}},
247+
"typeName": {
248+
"type": "Identifier",
249+
"start":140,"end":141,"loc":{"start":{"line":4,"column":33,"index":140},"end":{"line":4,"column":34,"index":141},"identifierName":"D"},
250+
"name": "D"
251+
}
252+
},
253+
"extra": {
254+
"parenthesized": true,
255+
"parenStart": 124
256+
}
257+
},
258+
"body": {
259+
"type": "ClassBody",
260+
"start":143,"end":145,"loc":{"start":{"line":4,"column":36,"index":143},"end":{"line":4,"column":38,"index":145}},
261+
"body": []
262+
}
263+
}
264+
],
265+
"directives": [],
266+
"extra": {
267+
"topLevelAwait": false
268+
}
269+
}
270+
}

0 commit comments

Comments
 (0)