Skip to content

Commit 65a0d3a

Browse files
authoredJan 4, 2023
fix(es/parser): Fix priority of >>> (#6748)
**Related issue:** - Closes #6739.
1 parent d7081cc commit 65a0d3a

File tree

12 files changed

+246
-3
lines changed

12 files changed

+246
-3
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "typescript",
5+
"tsx": false
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(x < y >>> z);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(x < y >>> z);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(x < y >>> z);

‎crates/swc_ecma_parser/src/lexer/state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl<'a, I: Input> Iterator for Lexer<'a, I> {
329329
if c == '<' {
330330
self.input.bump();
331331
return Ok(Some(tok!('<')));
332-
} else if c == '>' {
332+
} else if c == '>' && !self.ctx.prefer_bin_op_over_type_arg_closing {
333333
self.input.bump();
334334
return Ok(Some(tok!('>')));
335335
}

‎crates/swc_ecma_parser/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ pub struct Context {
375375
ignore_else_clause: bool,
376376

377377
disallow_conditional_types: bool,
378+
379+
/// Used for parsing `>`. If true, `>>` and `>>>` are parsed as binary
380+
/// operators.
381+
prefer_bin_op_over_type_arg_closing: bool,
378382
}
379383

380384
#[derive(Debug, Clone, Copy, Default)]

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,11 @@ impl<I: Tokens> Parser<I> {
671671
return_if_arrow!(self, obj);
672672

673673
let type_args = if self.syntax().typescript() && is!(self, '<') {
674-
self.try_parse_ts_type_args()
674+
self.with_ctx(Context {
675+
prefer_bin_op_over_type_arg_closing: true,
676+
..self.ctx()
677+
})
678+
.try_parse_ts_type_args()
675679
} else {
676680
None
677681
};

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,11 @@ impl<I: Tokens> Parser<I> {
557557
pub(super) fn try_parse_ts_type_args(&mut self) -> Option<Box<TsTypeParamInstantiation>> {
558558
debug_assert!(self.input.syntax().typescript());
559559

560-
self.try_parse_ts(|p| {
560+
self.with_ctx(Context {
561+
prefer_bin_op_over_type_arg_closing: true,
562+
..self.ctx()
563+
})
564+
.try_parse_ts(|p| {
561565
let type_args = p.parse_ts_type_args()?;
562566
if is_one_of!(
563567
p, '<', // invalid syntax
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(x < y >>> z);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"type": "Script",
3+
"span": {
4+
"start": 1,
5+
"end": 26,
6+
"ctxt": 0
7+
},
8+
"body": [
9+
{
10+
"type": "ExpressionStatement",
11+
"span": {
12+
"start": 1,
13+
"end": 26,
14+
"ctxt": 0
15+
},
16+
"expression": {
17+
"type": "CallExpression",
18+
"span": {
19+
"start": 1,
20+
"end": 25,
21+
"ctxt": 0
22+
},
23+
"callee": {
24+
"type": "MemberExpression",
25+
"span": {
26+
"start": 1,
27+
"end": 12,
28+
"ctxt": 0
29+
},
30+
"object": {
31+
"type": "Identifier",
32+
"span": {
33+
"start": 1,
34+
"end": 8,
35+
"ctxt": 0
36+
},
37+
"value": "console",
38+
"optional": false
39+
},
40+
"property": {
41+
"type": "Identifier",
42+
"span": {
43+
"start": 9,
44+
"end": 12,
45+
"ctxt": 0
46+
},
47+
"value": "log",
48+
"optional": false
49+
}
50+
},
51+
"arguments": [
52+
{
53+
"spread": null,
54+
"expression": {
55+
"type": "BinaryExpression",
56+
"span": {
57+
"start": 13,
58+
"end": 24,
59+
"ctxt": 0
60+
},
61+
"operator": "<",
62+
"left": {
63+
"type": "Identifier",
64+
"span": {
65+
"start": 13,
66+
"end": 14,
67+
"ctxt": 0
68+
},
69+
"value": "x",
70+
"optional": false
71+
},
72+
"right": {
73+
"type": "BinaryExpression",
74+
"span": {
75+
"start": 17,
76+
"end": 24,
77+
"ctxt": 0
78+
},
79+
"operator": ">>>",
80+
"left": {
81+
"type": "Identifier",
82+
"span": {
83+
"start": 17,
84+
"end": 18,
85+
"ctxt": 0
86+
},
87+
"value": "y",
88+
"optional": false
89+
},
90+
"right": {
91+
"type": "Identifier",
92+
"span": {
93+
"start": 23,
94+
"end": 24,
95+
"ctxt": 0
96+
},
97+
"value": "z",
98+
"optional": false
99+
}
100+
}
101+
}
102+
}
103+
],
104+
"typeArguments": null
105+
}
106+
}
107+
],
108+
"interpreter": null
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(x < y >>> z);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"type": "Script",
3+
"span": {
4+
"start": 1,
5+
"end": 26,
6+
"ctxt": 0
7+
},
8+
"body": [
9+
{
10+
"type": "ExpressionStatement",
11+
"span": {
12+
"start": 1,
13+
"end": 26,
14+
"ctxt": 0
15+
},
16+
"expression": {
17+
"type": "CallExpression",
18+
"span": {
19+
"start": 1,
20+
"end": 25,
21+
"ctxt": 0
22+
},
23+
"callee": {
24+
"type": "MemberExpression",
25+
"span": {
26+
"start": 1,
27+
"end": 12,
28+
"ctxt": 0
29+
},
30+
"object": {
31+
"type": "Identifier",
32+
"span": {
33+
"start": 1,
34+
"end": 8,
35+
"ctxt": 0
36+
},
37+
"value": "console",
38+
"optional": false
39+
},
40+
"property": {
41+
"type": "Identifier",
42+
"span": {
43+
"start": 9,
44+
"end": 12,
45+
"ctxt": 0
46+
},
47+
"value": "log",
48+
"optional": false
49+
}
50+
},
51+
"arguments": [
52+
{
53+
"spread": null,
54+
"expression": {
55+
"type": "BinaryExpression",
56+
"span": {
57+
"start": 13,
58+
"end": 24,
59+
"ctxt": 0
60+
},
61+
"operator": "<",
62+
"left": {
63+
"type": "Identifier",
64+
"span": {
65+
"start": 13,
66+
"end": 14,
67+
"ctxt": 0
68+
},
69+
"value": "x",
70+
"optional": false
71+
},
72+
"right": {
73+
"type": "BinaryExpression",
74+
"span": {
75+
"start": 17,
76+
"end": 24,
77+
"ctxt": 0
78+
},
79+
"operator": ">>>",
80+
"left": {
81+
"type": "Identifier",
82+
"span": {
83+
"start": 17,
84+
"end": 18,
85+
"ctxt": 0
86+
},
87+
"value": "y",
88+
"optional": false
89+
},
90+
"right": {
91+
"type": "Identifier",
92+
"span": {
93+
"start": 23,
94+
"end": 24,
95+
"ctxt": 0
96+
},
97+
"value": "z",
98+
"optional": false
99+
}
100+
}
101+
}
102+
}
103+
],
104+
"typeArguments": null
105+
}
106+
}
107+
],
108+
"interpreter": null
109+
}

0 commit comments

Comments
 (0)
Please sign in to comment.