Skip to content

Commit

Permalink
fix(es/parser): Fix binary operator parsed as an instantiation expres…
Browse files Browse the repository at this point in the history
…sion (#4992)
  • Loading branch information
g-plane committed Jun 18, 2022
1 parent d4ecff6 commit 36ef3e4
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 1,166 deletions.
2 changes: 1 addition & 1 deletion crates/swc_ecma_parser/src/parser/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ impl<I: Tokens> Parser<I> {

self.try_parse_ts(|p| {
let type_args = p.parse_ts_type_args()?;
if p.is_start_of_expr()? {
if matches!(cur!(p, false), Ok(Token::BinOp(..))) || p.is_start_of_expr()? {
Ok(None)
} else {
Ok(Some(type_args))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f<x> & g<y>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

x Unexpected token `&`. Expected this, import, async, function, [ for array literal, { for object literal, @ for decorator, function, class, null, true, false, number, bigint, string, regexp, `
| for template literal, (, or an identifier
,-[$DIR/tests/typescript-errors/instantiation-expr/case3/input.ts:1:1]
1 | f<x> & g<y>;
: ^
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0 < 2 >> 1;
a<b>>c<d>;
a<b> + c;
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
{
"type": "Script",
"span": {
"start": 1,
"end": 33,
"ctxt": 0
},
"body": [
{
"type": "ExpressionStatement",
"span": {
"start": 1,
"end": 12,
"ctxt": 0
},
"expression": {
"type": "BinaryExpression",
"span": {
"start": 1,
"end": 11,
"ctxt": 0
},
"operator": "<",
"left": {
"type": "NumericLiteral",
"span": {
"start": 1,
"end": 2,
"ctxt": 0
},
"value": 0.0,
"raw": "0"
},
"right": {
"type": "BinaryExpression",
"span": {
"start": 5,
"end": 11,
"ctxt": 0
},
"operator": ">>",
"left": {
"type": "NumericLiteral",
"span": {
"start": 5,
"end": 6,
"ctxt": 0
},
"value": 2.0,
"raw": "2"
},
"right": {
"type": "NumericLiteral",
"span": {
"start": 10,
"end": 11,
"ctxt": 0
},
"value": 1.0,
"raw": "1"
}
}
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 13,
"end": 23,
"ctxt": 0
},
"expression": {
"type": "BinaryExpression",
"span": {
"start": 13,
"end": 22,
"ctxt": 0
},
"operator": "<",
"left": {
"type": "Identifier",
"span": {
"start": 13,
"end": 14,
"ctxt": 0
},
"value": "a",
"optional": false
},
"right": {
"type": "BinaryExpression",
"span": {
"start": 15,
"end": 22,
"ctxt": 0
},
"operator": ">>",
"left": {
"type": "Identifier",
"span": {
"start": 15,
"end": 16,
"ctxt": 0
},
"value": "b",
"optional": false
},
"right": {
"type": "TsInstantiation",
"span": {
"start": 18,
"end": 22,
"ctxt": 0
},
"expression": {
"type": "Identifier",
"span": {
"start": 18,
"end": 19,
"ctxt": 0
},
"value": "c",
"optional": false
},
"typeArguments": {
"type": "TsTypeParameterInstantiation",
"span": {
"start": 19,
"end": 22,
"ctxt": 0
},
"params": [
{
"type": "TsTypeReference",
"span": {
"start": 20,
"end": 21,
"ctxt": 0
},
"typeName": {
"type": "Identifier",
"span": {
"start": 20,
"end": 21,
"ctxt": 0
},
"value": "d",
"optional": false
},
"typeParams": null
}
]
}
}
}
}
},
{
"type": "ExpressionStatement",
"span": {
"start": 24,
"end": 33,
"ctxt": 0
},
"expression": {
"type": "BinaryExpression",
"span": {
"start": 24,
"end": 32,
"ctxt": 0
},
"operator": ">",
"left": {
"type": "BinaryExpression",
"span": {
"start": 24,
"end": 27,
"ctxt": 0
},
"operator": "<",
"left": {
"type": "Identifier",
"span": {
"start": 24,
"end": 25,
"ctxt": 0
},
"value": "a",
"optional": false
},
"right": {
"type": "Identifier",
"span": {
"start": 26,
"end": 27,
"ctxt": 0
},
"value": "b",
"optional": false
}
},
"right": {
"type": "UnaryExpression",
"span": {
"start": 29,
"end": 32,
"ctxt": 0
},
"operator": "+",
"argument": {
"type": "Identifier",
"span": {
"start": 31,
"end": 32,
"ctxt": 0
},
"value": "c",
"optional": false
}
}
}
}
],
"interpreter": null
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
f<x>, g<y>;
[f<x>];
f<x> ? g<y> : h<z>;
f<x> ^ g<y>;
f<x> & g<y>;
f<x> | g<y>;
f<x> && g<y>;
f<x> || g<y>;
{ f<x> }
f<x> ?? g<y>;
f<x> == g<y>;
f<x> === g<y>;
f<x> != g<y>;
f<x> !== g<y>;

1 comment on commit 36ef3e4

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 36ef3e4 Previous: 8e63b0b Ratio
es/full/minify/libraries/antd 1627693103 ns/iter (± 47557604) 1632070473 ns/iter (± 52580508) 1.00
es/full/minify/libraries/d3 436399415 ns/iter (± 4128628) 401797799 ns/iter (± 24230096) 1.09
es/full/minify/libraries/echarts 1652561294 ns/iter (± 27196647) 1616193879 ns/iter (± 47434892) 1.02
es/full/minify/libraries/jquery 100024966 ns/iter (± 3196061) 87624335 ns/iter (± 2447920) 1.14
es/full/minify/libraries/lodash 118319276 ns/iter (± 5933870) 114038467 ns/iter (± 4146657) 1.04
es/full/minify/libraries/moment 53323812 ns/iter (± 1231717) 51316965 ns/iter (± 955764) 1.04
es/full/minify/libraries/react 17890618 ns/iter (± 200667) 20979575 ns/iter (± 11866431) 0.85
es/full/minify/libraries/terser 611862160 ns/iter (± 11445617) 595492232 ns/iter (± 77139589) 1.03
es/full/minify/libraries/three 558827992 ns/iter (± 10348192) 539641817 ns/iter (± 25187582) 1.04
es/full/minify/libraries/typescript 3505422330 ns/iter (± 69670217) 3473601678 ns/iter (± 92341759) 1.01
es/full/minify/libraries/victory 719183432 ns/iter (± 7281857) 717503668 ns/iter (± 25697376) 1.00
es/full/minify/libraries/vue 144143393 ns/iter (± 3197838) 128903283 ns/iter (± 3812158) 1.12
es/full/codegen/es3 30564 ns/iter (± 600) 30193 ns/iter (± 2249) 1.01
es/full/codegen/es5 30666 ns/iter (± 785) 30743 ns/iter (± 746) 1.00
es/full/codegen/es2015 30679 ns/iter (± 919) 30353 ns/iter (± 1170) 1.01
es/full/codegen/es2016 30636 ns/iter (± 307) 30342 ns/iter (± 1185) 1.01
es/full/codegen/es2017 30652 ns/iter (± 228) 30304 ns/iter (± 1296) 1.01
es/full/codegen/es2018 30625 ns/iter (± 216) 30368 ns/iter (± 1231) 1.01
es/full/codegen/es2019 30617 ns/iter (± 224) 30270 ns/iter (± 927) 1.01
es/full/codegen/es2020 30715 ns/iter (± 645) 30303 ns/iter (± 578) 1.01
es/full/all/es3 184205945 ns/iter (± 5943436) 179033794 ns/iter (± 4777978) 1.03
es/full/all/es5 172605723 ns/iter (± 6080684) 167712278 ns/iter (± 6161213) 1.03
es/full/all/es2015 140347367 ns/iter (± 3413685) 136803363 ns/iter (± 7360874) 1.03
es/full/all/es2016 138677938 ns/iter (± 4194076) 135519381 ns/iter (± 4879826) 1.02
es/full/all/es2017 136906309 ns/iter (± 3505758) 135445036 ns/iter (± 4099418) 1.01
es/full/all/es2018 134900237 ns/iter (± 3782649) 134019492 ns/iter (± 4664806) 1.01
es/full/all/es2019 134189623 ns/iter (± 2407390) 133101642 ns/iter (± 5814132) 1.01
es/full/all/es2020 129311496 ns/iter (± 2512248) 128397507 ns/iter (± 3970068) 1.01
es/full/parser 688792 ns/iter (± 17077) 676644 ns/iter (± 31977) 1.02
es/full/base/fixer 29505 ns/iter (± 898) 28258 ns/iter (± 893) 1.04
es/full/base/resolver_and_hygiene 86064 ns/iter (± 988) 84994 ns/iter (± 6039) 1.01
serialization of ast node 204 ns/iter (± 1) 202 ns/iter (± 6) 1.01
serialization of serde 215 ns/iter (± 2) 210 ns/iter (± 7) 1.02

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.