Skip to content

Commit

Permalink
fix(es/parser): Allow instantiation expression followed by a line bre…
Browse files Browse the repository at this point in the history
…ak or a binary operator (#5000)
  • Loading branch information
g-plane committed Jun 19, 2022
1 parent fb7b314 commit a62b2b3
Show file tree
Hide file tree
Showing 9 changed files with 2,927 additions and 117 deletions.
15 changes: 13 additions & 2 deletions crates/swc_ecma_parser/src/parser/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,21 @@ impl<I: Tokens> Parser<I> {

self.try_parse_ts(|p| {
let type_args = p.parse_ts_type_args()?;
if matches!(cur!(p, false), Ok(Token::BinOp(..))) || p.is_start_of_expr()? {
if is_one_of!(
p, '<', // invalid syntax
'>', '+', '-', // becomes relational expression
/* these should be type arguments in function call or template,
* not instantiation expression */
'(', '`'
) {
Ok(None)
} else {
} else if p.input.had_line_break_before_cur()
|| matches!(cur!(p, false), Ok(Token::BinOp(..)))
|| !p.is_start_of_expr()?
{
Ok(Some(type_args))
} else {
Ok(None)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f<x> & g<y>;
f<x> < g<y>;
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

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
x Expected '>', got ';'
,-[$DIR/tests/typescript-errors/instantiation-expr/case3/input.ts:1:1]
1 | f<x> & g<y>;
: ^
1 | f<x> < g<y>;
: ^
`----
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,58 @@
const x1 = f<true>
(true);

// Parsed as relational expression
// Parsed as relational expressions
const r1 = f < true > true;
const r2 = f < true > +1;
const r3 = f < true > -1;

// All of the following are parsed as instantiation expressions
const x2 = f<true>
true;

// Parsed as instantiation expression
const x3 = f<true>;
true;

// Parsed as instantiation expression
const x4 = f<true>
if (true) {}

const x5 = f<true>
let yy = 0;

const x6 = f<true>
interface I {}

let x10 = f<true>
this.bar()

let x11 = f<true>
function bar() {}

let x12 = f<true>
class C {}

let x13 = f<true>
bar()

let x14 = f<true>
void bar()

class C1 {
static specialFoo = f<string>
static bar = 123
}

class C2 {
public specialFoo = f<string>
public bar = 123
}

class C3 {
private specialFoo = f<string>
private bar = 123
}

class C4 {
protected specialFoo = f<string>
protected bar = 123
}

1 comment on commit a62b2b3

@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: a62b2b3 Previous: 53a8cb1 Ratio
es/full/minify/libraries/antd 1665910709 ns/iter (± 21418160) 1595596554 ns/iter (± 25919669) 1.04
es/full/minify/libraries/d3 420832871 ns/iter (± 13923745) 399007394 ns/iter (± 10144751) 1.05
es/full/minify/libraries/echarts 1631790816 ns/iter (± 35590098) 1607225109 ns/iter (± 35462491) 1.02
es/full/minify/libraries/jquery 94419103 ns/iter (± 7765267) 86439496 ns/iter (± 994302) 1.09
es/full/minify/libraries/lodash 126296352 ns/iter (± 5608096) 114184251 ns/iter (± 2619824) 1.11
es/full/minify/libraries/moment 55286597 ns/iter (± 5308480) 50434404 ns/iter (± 828848) 1.10
es/full/minify/libraries/react 17981371 ns/iter (± 686664) 17110091 ns/iter (± 431702) 1.05
es/full/minify/libraries/terser 613592985 ns/iter (± 20865920) 592073229 ns/iter (± 10413409) 1.04
es/full/minify/libraries/three 555181692 ns/iter (± 15511209) 532438620 ns/iter (± 18457299) 1.04
es/full/minify/libraries/typescript 3500488602 ns/iter (± 110380568) 3382711801 ns/iter (± 43927454) 1.03
es/full/minify/libraries/victory 734717102 ns/iter (± 20444119) 706384351 ns/iter (± 13735424) 1.04
es/full/minify/libraries/vue 146564335 ns/iter (± 6431667) 132783710 ns/iter (± 7842543) 1.10
es/full/codegen/es3 30940 ns/iter (± 769) 31967 ns/iter (± 2884) 0.97
es/full/codegen/es5 30987 ns/iter (± 991) 32086 ns/iter (± 647) 0.97
es/full/codegen/es2015 31079 ns/iter (± 1301) 32176 ns/iter (± 1947) 0.97
es/full/codegen/es2016 30963 ns/iter (± 534) 31969 ns/iter (± 903) 0.97
es/full/codegen/es2017 30962 ns/iter (± 414) 31957 ns/iter (± 499) 0.97
es/full/codegen/es2018 31025 ns/iter (± 550) 32160 ns/iter (± 3019) 0.96
es/full/codegen/es2019 31008 ns/iter (± 786) 31949 ns/iter (± 792) 0.97
es/full/codegen/es2020 30622 ns/iter (± 1018) 32044 ns/iter (± 1057) 0.96
es/full/all/es3 189294665 ns/iter (± 6733970) 203108890 ns/iter (± 13888393) 0.93
es/full/all/es5 176533166 ns/iter (± 6567706) 181555448 ns/iter (± 12204746) 0.97
es/full/all/es2015 144034543 ns/iter (± 4578609) 146577373 ns/iter (± 9400778) 0.98
es/full/all/es2016 142066072 ns/iter (± 5772343) 152646850 ns/iter (± 6573469) 0.93
es/full/all/es2017 142183275 ns/iter (± 6829370) 146453992 ns/iter (± 11804738) 0.97
es/full/all/es2018 140791622 ns/iter (± 4024809) 142397367 ns/iter (± 5588469) 0.99
es/full/all/es2019 140320310 ns/iter (± 6451481) 142450810 ns/iter (± 6433830) 0.99
es/full/all/es2020 135024386 ns/iter (± 5991640) 138647242 ns/iter (± 14437394) 0.97
es/full/parser 711365 ns/iter (± 15410) 702100 ns/iter (± 15477) 1.01
es/full/base/fixer 30210 ns/iter (± 558) 29211 ns/iter (± 284) 1.03
es/full/base/resolver_and_hygiene 88591 ns/iter (± 3995) 88220 ns/iter (± 4263) 1.00
serialization of ast node 200 ns/iter (± 7) 206 ns/iter (± 4) 0.97
serialization of serde 217 ns/iter (± 1) 218 ns/iter (± 1) 1.00

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

Please sign in to comment.