Skip to content

Commit

Permalink
fix(es/compat): Use async and await correctly in block-scoping
Browse files Browse the repository at this point in the history
…pass (#8056)

**Related issue:**

 - Closes #7989
  • Loading branch information
kdy1 committed Oct 3, 2023
1 parent c6e0a18 commit 8318ea8
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 13 deletions.
24 changes: 24 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7989/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"loose": false,
"minify": {
"compress": false,
"mangle": false,
"format": {
"comments": "all"
}
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true,
"env": {
"targets": "supports async-functions"
}
}
6 changes: 6 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7989/input/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
async function foo(a, b) {
for (let i = 0; i < 5; i++) {
const boo = await a();
b(() => boo)
}
}
7 changes: 7 additions & 0 deletions crates/swc/tests/fixture/issues-7xxx/7989/output/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
async function foo(a, b) {
var _loop = async function(i) {
var boo = await a();
b(()=>boo);
};
for(var i = 0; i < 5; i++)await _loop(i);
}
42 changes: 29 additions & 13 deletions crates/swc_ecma_transforms_compat/src/es2015/block_scoping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl BlockScoping {
has_break: false,
has_return: false,
has_yield: false,
has_await: false,
label: IndexMap::new(),
inner_label: AHashSet::default(),
mutated,
Expand Down Expand Up @@ -222,7 +223,7 @@ impl BlockScoping {
decorators: Default::default(),
body: Some(body_stmt),
is_generator: flow_helper.has_yield,
is_async: false,
is_async: flow_helper.has_await,
type_params: None,
return_type: None,
}
Expand All @@ -243,6 +244,14 @@ impl BlockScoping {
}
.into();

if flow_helper.has_await {
call = AwaitExpr {
span: DUMMY_SP,
arg: call.into(),
}
.into();
}

if flow_helper.has_yield {
call = YieldExpr {
span: DUMMY_SP,
Expand Down Expand Up @@ -616,6 +625,7 @@ struct FlowHelper<'a> {
has_break: bool,
has_return: bool,
has_yield: bool,
has_await: bool,

// label cannot be shadowed, so it's pretty safe to use JsWord
label: IndexMap<JsWord, Label>,
Expand Down Expand Up @@ -657,18 +667,6 @@ impl VisitMut for FlowHelper<'_> {
/// noop
fn visit_mut_arrow_expr(&mut self, _n: &mut ArrowExpr) {}

fn visit_mut_yield_expr(&mut self, e: &mut YieldExpr) {
e.visit_mut_children_with(self);

self.has_yield = true;
}

fn visit_mut_labeled_stmt(&mut self, l: &mut LabeledStmt) {
self.inner_label.insert(l.label.sym.clone());

l.visit_mut_children_with(self);
}

fn visit_mut_assign_expr(&mut self, n: &mut AssignExpr) {
match &n.left {
PatOrExpr::Expr(e) => {
Expand All @@ -688,6 +686,12 @@ impl VisitMut for FlowHelper<'_> {
n.visit_mut_children_with(self);
}

fn visit_mut_await_expr(&mut self, e: &mut AwaitExpr) {
e.visit_mut_children_with(self);

self.has_await = true;
}

/// https://github.com/swc-project/swc/pull/2916
fn visit_mut_do_while_stmt(&mut self, s: &mut DoWhileStmt) {
let old = self.in_nested_loop;
Expand Down Expand Up @@ -723,6 +727,12 @@ impl VisitMut for FlowHelper<'_> {
/// noop
fn visit_mut_function(&mut self, _f: &mut Function) {}

fn visit_mut_labeled_stmt(&mut self, l: &mut LabeledStmt) {
self.inner_label.insert(l.label.sym.clone());

l.visit_mut_children_with(self);
}

fn visit_mut_stmt(&mut self, node: &mut Stmt) {
let span = node.span();

Expand Down Expand Up @@ -822,6 +832,12 @@ impl VisitMut for FlowHelper<'_> {
s.visit_mut_children_with(self);
self.in_nested_loop = old;
}

fn visit_mut_yield_expr(&mut self, e: &mut YieldExpr) {
e.visit_mut_children_with(self);

self.has_yield = true;
}
}

struct MutationHandler<'a> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
async function foo(a, b) {
for (let i = 0; i < 5; i++) {
const boo = await a();
b(() => boo)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
async function foo__2(a__3, b__3) {
var _loop__6 = async function(i__4) {
var boo__5 = await a__3();
b__3(()=>boo__5);
};
for(var i__4 = 0; i__4 < 5; i__4++)await _loop__6(i__4);
}

1 comment on commit 8318ea8

@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: 8318ea8 Previous: a751f1c Ratio
es/full/bugs-1 273215 ns/iter (± 3807) 277833 ns/iter (± 5957) 0.98
es/full/minify/libraries/antd 1308437156 ns/iter (± 19160635) 1315453138 ns/iter (± 5262929) 0.99
es/full/minify/libraries/d3 270678444 ns/iter (± 3126234) 287366028 ns/iter (± 7212246) 0.94
es/full/minify/libraries/echarts 1041894723 ns/iter (± 8806873) 1050258731 ns/iter (± 7499031) 0.99
es/full/minify/libraries/jquery 83284971 ns/iter (± 134508) 84348807 ns/iter (± 293647) 0.99
es/full/minify/libraries/lodash 96542692 ns/iter (± 935417) 97891776 ns/iter (± 505731) 0.99
es/full/minify/libraries/moment 49447384 ns/iter (± 113482) 49991198 ns/iter (± 92971) 0.99
es/full/minify/libraries/react 17884505 ns/iter (± 28511) 18044672 ns/iter (± 51497) 0.99
es/full/minify/libraries/terser 215348630 ns/iter (± 1018931) 218019297 ns/iter (± 788292) 0.99
es/full/minify/libraries/three 380737079 ns/iter (± 2328222) 384113257 ns/iter (± 2838047) 0.99
es/full/minify/libraries/typescript 2566953718 ns/iter (± 14698786) 2654667541 ns/iter (± 8719701) 0.97
es/full/minify/libraries/victory 557997694 ns/iter (± 3247628) 565656105 ns/iter (± 4851121) 0.99
es/full/minify/libraries/vue 117457437 ns/iter (± 285203) 119145169 ns/iter (± 438108) 0.99
es/full/codegen/es3 34642 ns/iter (± 132) 34990 ns/iter (± 74) 0.99
es/full/codegen/es5 34645 ns/iter (± 111) 34883 ns/iter (± 56) 0.99
es/full/codegen/es2015 34696 ns/iter (± 120) 34946 ns/iter (± 125) 0.99
es/full/codegen/es2016 34722 ns/iter (± 114) 34889 ns/iter (± 115) 1.00
es/full/codegen/es2017 34705 ns/iter (± 68) 34908 ns/iter (± 48) 0.99
es/full/codegen/es2018 34682 ns/iter (± 112) 34990 ns/iter (± 123) 0.99
es/full/codegen/es2019 34645 ns/iter (± 94) 34970 ns/iter (± 104) 0.99
es/full/codegen/es2020 34695 ns/iter (± 95) 34996 ns/iter (± 105) 0.99
es/full/all/es3 166033412 ns/iter (± 1005481) 164994483 ns/iter (± 1442444) 1.01
es/full/all/es5 157590970 ns/iter (± 1153631) 158121658 ns/iter (± 1041097) 1.00
es/full/all/es2015 116367137 ns/iter (± 774187) 117656178 ns/iter (± 1027051) 0.99
es/full/all/es2016 116207870 ns/iter (± 412454) 116266336 ns/iter (± 1367325) 1.00
es/full/all/es2017 115234523 ns/iter (± 585119) 115513112 ns/iter (± 583101) 1.00
es/full/all/es2018 112779888 ns/iter (± 737765) 114083148 ns/iter (± 1121684) 0.99
es/full/all/es2019 112824110 ns/iter (± 311244) 112838529 ns/iter (± 436015) 1.00
es/full/all/es2020 108275991 ns/iter (± 702679) 109306000 ns/iter (± 913711) 0.99
es/full/parser 488985 ns/iter (± 5137) 489804 ns/iter (± 5783) 1.00
es/full/base/fixer 20347 ns/iter (± 343) 19055 ns/iter (± 208) 1.07
es/full/base/resolver_and_hygiene 82203 ns/iter (± 126) 80324 ns/iter (± 119) 1.02

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

Please sign in to comment.