Skip to content

Commit 04333aa

Browse files
authoredJan 24, 2025··
fix(es/parser): Parse yield<T> (v: T)=>v (#9915)
**Related issue:** - Closes #9914
1 parent 5c8eaee commit 04333aa

File tree

6 files changed

+341
-24
lines changed

6 files changed

+341
-24
lines changed
 

‎.changeset/pink-kids-perform.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_parser: patch
4+
---
5+
6+
fix(es/parser): Parse `yield<T> (v: T)=>v`

‎crates/swc_ecma_parser/src/parser/expr.rs

+23-24
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,32 @@ impl<I: Tokens> Parser<I> {
3939
pub(super) fn parse_assignment_expr(&mut self) -> PResult<Box<Expr>> {
4040
trace_cur!(self, parse_assignment_expr);
4141

42-
if self.input.syntax().typescript() && self.input.syntax().jsx() {
42+
if self.input.syntax().typescript() && is!(self, JSXTagStart) {
4343
// Note: When the JSX plugin is on, type assertions (`<T> x`) aren't valid
4444
// syntax.
4545

46-
if is!(self, JSXTagStart) {
47-
let cur_context = self.input.token_context().current();
48-
debug_assert_eq!(cur_context, Some(TokenContext::JSXOpeningTag));
49-
// Only time j_oTag is pushed is right after j_expr.
46+
let cur_context = self.input.token_context().current();
47+
debug_assert_eq!(cur_context, Some(TokenContext::JSXOpeningTag));
48+
// Only time j_oTag is pushed is right after j_expr.
49+
debug_assert_eq!(
50+
self.input.token_context().0[self.input.token_context().len() - 2],
51+
TokenContext::JSXExpr
52+
);
53+
54+
let res = self.try_parse_ts(|p| p.parse_assignment_expr_base().map(Some));
55+
if let Some(res) = res {
56+
return Ok(res);
57+
} else {
5058
debug_assert_eq!(
51-
self.input.token_context().0[self.input.token_context().len() - 2],
52-
TokenContext::JSXExpr
59+
self.input.token_context().current(),
60+
Some(TokenContext::JSXOpeningTag)
5361
);
54-
55-
let res = self.try_parse_ts(|p| p.parse_assignment_expr_base().map(Some));
56-
if let Some(res) = res {
57-
return Ok(res);
58-
} else {
59-
debug_assert_eq!(
60-
self.input.token_context().current(),
61-
Some(TokenContext::JSXOpeningTag)
62-
);
63-
self.input.token_context_mut().pop();
64-
debug_assert_eq!(
65-
self.input.token_context().current(),
66-
Some(TokenContext::JSXExpr)
67-
);
68-
self.input.token_context_mut().pop();
69-
}
62+
self.input.token_context_mut().pop();
63+
debug_assert_eq!(
64+
self.input.token_context().current(),
65+
Some(TokenContext::JSXExpr)
66+
);
67+
self.input.token_context_mut().pop();
7068
}
7169
}
7270

@@ -2004,7 +2002,8 @@ impl<I: Tokens> Parser<I> {
20042002
}
20052003

20062004
if is!(self, ';')
2007-
|| (!is!(self, '*')
2005+
|| (!is!(self, '<')
2006+
&& !is!(self, '*')
20082007
&& !is!(self, '/')
20092008
&& !is!(self, "/=")
20102009
&& !cur!(self, false)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(function* () {
2+
yield <T>(v: T) => v;
3+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
{
2+
"type": "Script",
3+
"span": {
4+
"start": 1,
5+
"end": 46
6+
},
7+
"body": [
8+
{
9+
"type": "ExpressionStatement",
10+
"span": {
11+
"start": 1,
12+
"end": 46
13+
},
14+
"expression": {
15+
"type": "ParenthesisExpression",
16+
"span": {
17+
"start": 1,
18+
"end": 45
19+
},
20+
"expression": {
21+
"type": "FunctionExpression",
22+
"identifier": null,
23+
"params": [],
24+
"decorators": [],
25+
"span": {
26+
"start": 2,
27+
"end": 44
28+
},
29+
"ctxt": 0,
30+
"body": {
31+
"type": "BlockStatement",
32+
"span": {
33+
"start": 15,
34+
"end": 44
35+
},
36+
"ctxt": 0,
37+
"stmts": [
38+
{
39+
"type": "ExpressionStatement",
40+
"span": {
41+
"start": 21,
42+
"end": 42
43+
},
44+
"expression": {
45+
"type": "YieldExpression",
46+
"span": {
47+
"start": 21,
48+
"end": 41
49+
},
50+
"argument": {
51+
"type": "ArrowFunctionExpression",
52+
"span": {
53+
"start": 27,
54+
"end": 41
55+
},
56+
"ctxt": 0,
57+
"params": [
58+
{
59+
"type": "Identifier",
60+
"span": {
61+
"start": 31,
62+
"end": 35
63+
},
64+
"ctxt": 0,
65+
"value": "v",
66+
"optional": false,
67+
"typeAnnotation": {
68+
"type": "TsTypeAnnotation",
69+
"span": {
70+
"start": 32,
71+
"end": 35
72+
},
73+
"typeAnnotation": {
74+
"type": "TsTypeReference",
75+
"span": {
76+
"start": 34,
77+
"end": 35
78+
},
79+
"typeName": {
80+
"type": "Identifier",
81+
"span": {
82+
"start": 34,
83+
"end": 35
84+
},
85+
"ctxt": 0,
86+
"value": "T",
87+
"optional": false
88+
},
89+
"typeParams": null
90+
}
91+
}
92+
}
93+
],
94+
"body": {
95+
"type": "Identifier",
96+
"span": {
97+
"start": 40,
98+
"end": 41
99+
},
100+
"ctxt": 0,
101+
"value": "v",
102+
"optional": false
103+
},
104+
"async": false,
105+
"generator": false,
106+
"typeParameters": {
107+
"type": "TsTypeParameterDeclaration",
108+
"span": {
109+
"start": 27,
110+
"end": 30
111+
},
112+
"parameters": [
113+
{
114+
"type": "TsTypeParameter",
115+
"span": {
116+
"start": 28,
117+
"end": 29
118+
},
119+
"name": {
120+
"type": "Identifier",
121+
"span": {
122+
"start": 28,
123+
"end": 29
124+
},
125+
"ctxt": 0,
126+
"value": "T",
127+
"optional": false
128+
},
129+
"in": false,
130+
"out": false,
131+
"const": false,
132+
"constraint": null,
133+
"default": null
134+
}
135+
]
136+
},
137+
"returnType": null
138+
},
139+
"delegate": false
140+
}
141+
}
142+
]
143+
},
144+
"generator": true,
145+
"async": false,
146+
"typeParameters": null,
147+
"returnType": null
148+
}
149+
}
150+
}
151+
],
152+
"interpreter": null
153+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(function* () {
2+
yield <T,>(v: T) => v;
3+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
{
2+
"type": "Script",
3+
"span": {
4+
"start": 1,
5+
"end": 47
6+
},
7+
"body": [
8+
{
9+
"type": "ExpressionStatement",
10+
"span": {
11+
"start": 1,
12+
"end": 47
13+
},
14+
"expression": {
15+
"type": "ParenthesisExpression",
16+
"span": {
17+
"start": 1,
18+
"end": 46
19+
},
20+
"expression": {
21+
"type": "FunctionExpression",
22+
"identifier": null,
23+
"params": [],
24+
"decorators": [],
25+
"span": {
26+
"start": 2,
27+
"end": 45
28+
},
29+
"ctxt": 0,
30+
"body": {
31+
"type": "BlockStatement",
32+
"span": {
33+
"start": 15,
34+
"end": 45
35+
},
36+
"ctxt": 0,
37+
"stmts": [
38+
{
39+
"type": "ExpressionStatement",
40+
"span": {
41+
"start": 21,
42+
"end": 43
43+
},
44+
"expression": {
45+
"type": "YieldExpression",
46+
"span": {
47+
"start": 21,
48+
"end": 42
49+
},
50+
"argument": {
51+
"type": "ArrowFunctionExpression",
52+
"span": {
53+
"start": 27,
54+
"end": 42
55+
},
56+
"ctxt": 0,
57+
"params": [
58+
{
59+
"type": "Identifier",
60+
"span": {
61+
"start": 32,
62+
"end": 36
63+
},
64+
"ctxt": 0,
65+
"value": "v",
66+
"optional": false,
67+
"typeAnnotation": {
68+
"type": "TsTypeAnnotation",
69+
"span": {
70+
"start": 33,
71+
"end": 36
72+
},
73+
"typeAnnotation": {
74+
"type": "TsTypeReference",
75+
"span": {
76+
"start": 35,
77+
"end": 36
78+
},
79+
"typeName": {
80+
"type": "Identifier",
81+
"span": {
82+
"start": 35,
83+
"end": 36
84+
},
85+
"ctxt": 0,
86+
"value": "T",
87+
"optional": false
88+
},
89+
"typeParams": null
90+
}
91+
}
92+
}
93+
],
94+
"body": {
95+
"type": "Identifier",
96+
"span": {
97+
"start": 41,
98+
"end": 42
99+
},
100+
"ctxt": 0,
101+
"value": "v",
102+
"optional": false
103+
},
104+
"async": false,
105+
"generator": false,
106+
"typeParameters": {
107+
"type": "TsTypeParameterDeclaration",
108+
"span": {
109+
"start": 27,
110+
"end": 31
111+
},
112+
"parameters": [
113+
{
114+
"type": "TsTypeParameter",
115+
"span": {
116+
"start": 28,
117+
"end": 29
118+
},
119+
"name": {
120+
"type": "Identifier",
121+
"span": {
122+
"start": 28,
123+
"end": 29
124+
},
125+
"ctxt": 0,
126+
"value": "T",
127+
"optional": false
128+
},
129+
"in": false,
130+
"out": false,
131+
"const": false,
132+
"constraint": null,
133+
"default": null
134+
}
135+
]
136+
},
137+
"returnType": null
138+
},
139+
"delegate": false
140+
}
141+
}
142+
]
143+
},
144+
"generator": true,
145+
"async": false,
146+
"typeParameters": null,
147+
"returnType": null
148+
}
149+
}
150+
}
151+
],
152+
"interpreter": null
153+
}

0 commit comments

Comments
 (0)
Please sign in to comment.