Skip to content

Commit

Permalink
fix(es/parser): Check for line break after async (#4940)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Jun 11, 2022
1 parent bfede71 commit 381d273
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 6 deletions.
18 changes: 18 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4873/1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2022",
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false
}
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4873/1/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const async = '1';
let script = document.createElement("script")
script.async = async
script.onload = resolve(key)
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4873/1/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const async = '1';
let script = document.createElement("script");
script.async = async;
script.onload = resolve(key);
18 changes: 18 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4873/2/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2022",
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false
}
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4873/2/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const async = "1"
const foo = async
function bar() {
return 42
}
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4873/2/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const async = "1";
const foo = async;
function bar() {
return 42;
}
18 changes: 18 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4873/3/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2022",
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false
}
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4873/3/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const async = "1"
const foo = async
x => x
3 changes: 3 additions & 0 deletions crates/swc/tests/fixture/issues-4xxx/4873/3/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const async = "1";
const foo = async;
(x)=>x;
11 changes: 9 additions & 2 deletions crates/swc_ecma_parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ impl<I: Tokens> Parser<I> {
}
}

if can_be_arrow && peeked_is!(self, '(') {
if can_be_arrow
&& peeked_is!(self, '(')
&& !self.input.has_linebreak_between_cur_and_peeked()
{
expect!(self, "async");
let async_span = self.input.prev_span();
return self.parse_paren_expr_or_arrow_fn(can_be_arrow, Some(async_span));
Expand Down Expand Up @@ -380,7 +383,11 @@ impl<I: Tokens> Parser<I> {
);
}

if can_be_arrow && id.sym == js_word!("async") && is!(self, BindingIdent) {
if can_be_arrow
&& id.sym == js_word!("async")
&& !self.input.had_line_break_before_cur()
&& is!(self, BindingIdent)
{
let ident = self.parse_binding_ident()?;
if self.input.syntax().typescript()
&& ident.id.sym == js_word!("as")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo = async
() => 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

x Expected ';', '}' or <eof>
,-[$DIR/tests/errors/async-line-break/1/input.js:2:1]
2 | () => 42
: ^^
`----

Error:
> This is the expression part of an expression statement
,-[$DIR/tests/errors/async-line-break/1/input.js:1:1]
1 | ,-> foo = async
2 | `-> () => 42
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foo = async
function() {
return 42
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

x Expected ident
,-[$DIR/tests/errors/async-line-break/2/input.js:2:1]
2 | function() {
: ^
`----
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@
"*.rs": [
"rustfmt --"
],
"*.js": [
"!(**/tests/**/*)*.js": [
"prettier --write"
],
"*.ts": [
"!(**/tests/**/*)*.ts": [
"prettier --write"
],
"*.jsx": [
"!(**/tests/**/*)*.jsx": [
"prettier --write"
],
"*.tsx": [
"!(**/tests/**/*)*.tsx": [
"prettier --write"
]
},
Expand Down

1 comment on commit 381d273

@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: 381d273 Previous: a70c737 Ratio
es/full/minify/libraries/antd 1730029017 ns/iter (± 12777118) 1803387905 ns/iter (± 10731905) 0.96
es/full/minify/libraries/d3 398160500 ns/iter (± 1970498) 442339612 ns/iter (± 11302244) 0.90
es/full/minify/libraries/echarts 2160416219 ns/iter (± 12358103) 2212381443 ns/iter (± 8912272) 0.98
es/full/minify/libraries/jquery 89134250 ns/iter (± 278628) 90046860 ns/iter (± 220261) 0.99
es/full/minify/libraries/lodash 124533314 ns/iter (± 391432) 126810411 ns/iter (± 1223362) 0.98
es/full/minify/libraries/moment 52370140 ns/iter (± 351694) 52852821 ns/iter (± 178994) 0.99
es/full/minify/libraries/react 17234842 ns/iter (± 41914) 17334829 ns/iter (± 53581) 0.99
es/full/minify/libraries/terser 412639446 ns/iter (± 3364034) 446998003 ns/iter (± 5451917) 0.92
es/full/minify/libraries/three 512477045 ns/iter (± 6874615) 575775867 ns/iter (± 12171912) 0.89
es/full/minify/libraries/typescript 4132645825 ns/iter (± 20659969) 4218388674 ns/iter (± 72293123) 0.98
es/full/minify/libraries/victory 672852818 ns/iter (± 8441921) 786302102 ns/iter (± 11879969) 0.86
es/full/minify/libraries/vue 136107445 ns/iter (± 2153007) 143473547 ns/iter (± 1042879) 0.95
es/full/codegen/es3 34398 ns/iter (± 189) 34668 ns/iter (± 128) 0.99
es/full/codegen/es5 34419 ns/iter (± 135) 34685 ns/iter (± 134) 0.99
es/full/codegen/es2015 34386 ns/iter (± 443) 34704 ns/iter (± 134) 0.99
es/full/codegen/es2016 34345 ns/iter (± 150) 34699 ns/iter (± 154) 0.99
es/full/codegen/es2017 34389 ns/iter (± 247) 34709 ns/iter (± 151) 0.99
es/full/codegen/es2018 34384 ns/iter (± 150) 34666 ns/iter (± 132) 0.99
es/full/codegen/es2019 34380 ns/iter (± 146) 34730 ns/iter (± 123) 0.99
es/full/codegen/es2020 34395 ns/iter (± 146) 34691 ns/iter (± 129) 0.99
es/full/all/es3 194627119 ns/iter (± 1413985) 193509427 ns/iter (± 1263893) 1.01
es/full/all/es5 182297046 ns/iter (± 1152349) 182751894 ns/iter (± 1030950) 1.00
es/full/all/es2015 147477502 ns/iter (± 1627184) 145215766 ns/iter (± 727391) 1.02
es/full/all/es2016 145162409 ns/iter (± 1692399) 144443460 ns/iter (± 900259) 1.00
es/full/all/es2017 145145705 ns/iter (± 1699322) 143945431 ns/iter (± 1167526) 1.01
es/full/all/es2018 142831535 ns/iter (± 1443992) 142206688 ns/iter (± 960932) 1.00
es/full/all/es2019 142340888 ns/iter (± 1413232) 141332321 ns/iter (± 792448) 1.01
es/full/all/es2020 138999098 ns/iter (± 1775994) 136574555 ns/iter (± 1048986) 1.02
es/full/parser 576709 ns/iter (± 49874) 588630 ns/iter (± 57725) 0.98
es/full/base/fixer 26925 ns/iter (± 207) 28573 ns/iter (± 211) 0.94
es/full/base/resolver_and_hygiene 137687 ns/iter (± 1769) 139112 ns/iter (± 1713) 0.99
serialization of ast node 182 ns/iter (± 0) 181 ns/iter (± 0) 1.01
serialization of serde 182 ns/iter (± 0) 182 ns/iter (± 0) 1

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

Please sign in to comment.