Skip to content

Commit 8318ea8

Browse files
authored
fix(es/compat): Use async and await correctly in block-scoping pass (#8056)
**Related issue:** - Closes #7989
1 parent c6e0a18 commit 8318ea8

File tree

6 files changed

+79
-13
lines changed

6 files changed

+79
-13
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "ecmascript",
5+
"jsx": false
6+
},
7+
"loose": false,
8+
"minify": {
9+
"compress": false,
10+
"mangle": false,
11+
"format": {
12+
"comments": "all"
13+
}
14+
}
15+
},
16+
"module": {
17+
"type": "es6"
18+
},
19+
"minify": false,
20+
"isModule": true,
21+
"env": {
22+
"targets": "supports async-functions"
23+
}
24+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
async function foo(a, b) {
2+
for (let i = 0; i < 5; i++) {
3+
const boo = await a();
4+
b(() => boo)
5+
}
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
async function foo(a, b) {
2+
var _loop = async function(i) {
3+
var boo = await a();
4+
b(()=>boo);
5+
};
6+
for(var i = 0; i < 5; i++)await _loop(i);
7+
}

crates/swc_ecma_transforms_compat/src/es2015/block_scoping/mod.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ impl BlockScoping {
161161
has_break: false,
162162
has_return: false,
163163
has_yield: false,
164+
has_await: false,
164165
label: IndexMap::new(),
165166
inner_label: AHashSet::default(),
166167
mutated,
@@ -222,7 +223,7 @@ impl BlockScoping {
222223
decorators: Default::default(),
223224
body: Some(body_stmt),
224225
is_generator: flow_helper.has_yield,
225-
is_async: false,
226+
is_async: flow_helper.has_await,
226227
type_params: None,
227228
return_type: None,
228229
}
@@ -243,6 +244,14 @@ impl BlockScoping {
243244
}
244245
.into();
245246

247+
if flow_helper.has_await {
248+
call = AwaitExpr {
249+
span: DUMMY_SP,
250+
arg: call.into(),
251+
}
252+
.into();
253+
}
254+
246255
if flow_helper.has_yield {
247256
call = YieldExpr {
248257
span: DUMMY_SP,
@@ -616,6 +625,7 @@ struct FlowHelper<'a> {
616625
has_break: bool,
617626
has_return: bool,
618627
has_yield: bool,
628+
has_await: bool,
619629

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

660-
fn visit_mut_yield_expr(&mut self, e: &mut YieldExpr) {
661-
e.visit_mut_children_with(self);
662-
663-
self.has_yield = true;
664-
}
665-
666-
fn visit_mut_labeled_stmt(&mut self, l: &mut LabeledStmt) {
667-
self.inner_label.insert(l.label.sym.clone());
668-
669-
l.visit_mut_children_with(self);
670-
}
671-
672670
fn visit_mut_assign_expr(&mut self, n: &mut AssignExpr) {
673671
match &n.left {
674672
PatOrExpr::Expr(e) => {
@@ -688,6 +686,12 @@ impl VisitMut for FlowHelper<'_> {
688686
n.visit_mut_children_with(self);
689687
}
690688

689+
fn visit_mut_await_expr(&mut self, e: &mut AwaitExpr) {
690+
e.visit_mut_children_with(self);
691+
692+
self.has_await = true;
693+
}
694+
691695
/// https://github.com/swc-project/swc/pull/2916
692696
fn visit_mut_do_while_stmt(&mut self, s: &mut DoWhileStmt) {
693697
let old = self.in_nested_loop;
@@ -723,6 +727,12 @@ impl VisitMut for FlowHelper<'_> {
723727
/// noop
724728
fn visit_mut_function(&mut self, _f: &mut Function) {}
725729

730+
fn visit_mut_labeled_stmt(&mut self, l: &mut LabeledStmt) {
731+
self.inner_label.insert(l.label.sym.clone());
732+
733+
l.visit_mut_children_with(self);
734+
}
735+
726736
fn visit_mut_stmt(&mut self, node: &mut Stmt) {
727737
let span = node.span();
728738

@@ -822,6 +832,12 @@ impl VisitMut for FlowHelper<'_> {
822832
s.visit_mut_children_with(self);
823833
self.in_nested_loop = old;
824834
}
835+
836+
fn visit_mut_yield_expr(&mut self, e: &mut YieldExpr) {
837+
e.visit_mut_children_with(self);
838+
839+
self.has_yield = true;
840+
}
825841
}
826842

827843
struct MutationHandler<'a> {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
async function foo(a, b) {
2+
for (let i = 0; i < 5; i++) {
3+
const boo = await a();
4+
b(() => boo)
5+
}
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
async function foo__2(a__3, b__3) {
2+
var _loop__6 = async function(i__4) {
3+
var boo__5 = await a__3();
4+
b__3(()=>boo__5);
5+
};
6+
for(var i__4 = 0; i__4 < 5; i__4++)await _loop__6(i__4);
7+
}

0 commit comments

Comments
 (0)