Skip to content

Commit 63c5413

Browse files
authored
fix(es/compat): Handle super in nested arrow function (#11056)
**Related issue:** - Closes #11054
1 parent b3b0104 commit 63c5413

File tree

6 files changed

+128
-25
lines changed

6 files changed

+128
-25
lines changed

.changeset/thin-deers-happen.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_compat_es2017: patch
3+
swc_core: patch
4+
---
5+
6+
fix(es/compat): Handle `super` in nested arrow function
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"minify": false,
3+
"jsc": {
4+
"externalHelpers": true,
5+
"parser": {
6+
"tsx": true,
7+
"syntax": "typescript",
8+
"decorators": true
9+
},
10+
"experimental": {
11+
"keepImportAttributes": true
12+
},
13+
"output": {
14+
"charset": "ascii"
15+
},
16+
"transform": {
17+
"legacyDecorator": true,
18+
"decoratorMetadata": true,
19+
"useDefineForClassFields": false,
20+
"react": {
21+
"development": false,
22+
"refresh": false,
23+
"runtime": "automatic"
24+
}
25+
}
26+
},
27+
"isModule": "unknown",
28+
"env": {
29+
"targets": [
30+
"chrome >= 51",
31+
"edge >= 15",
32+
"firefox >= 54",
33+
"safari >= 10",
34+
"ios_saf >= 10"
35+
],
36+
"mode": "entry",
37+
"coreJs": "3.32"
38+
}
39+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export class A extends B {
2+
public async getFrame(ms) {
3+
const result = await new Promise((resolve) => {
4+
lvSchedulerCallback(
5+
async () => {
6+
const frame = await super.getFrame(ms);
7+
resolve(frame);
8+
},
9+
);
10+
});
11+
return result;
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2+
export class A extends B {
3+
getFrame(ms) {
4+
var _superprop_get_getFrame = ()=>super.getFrame;
5+
return _async_to_generator(function*() {
6+
var result = yield new Promise((resolve)=>{
7+
lvSchedulerCallback(()=>{
8+
var _this = this, _superprop_get_getFrame1 = ()=>_superprop_get_getFrame();
9+
_async_to_generator(function*() {
10+
var frame = yield _superprop_get_getFrame1().call(_this, ms);
11+
resolve(frame);
12+
})();
13+
});
14+
});
15+
return result;
16+
})();
17+
}
18+
}

crates/swc/tests/tsc-references/asyncMethodWithSuper_es6.1.normal.js

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,19 @@ class B extends A {
6666
// element access in arrow
6767
()=>_superprop_get("x").call(_this);
6868
// property access in async arrow
69-
()=>_async_to_generator(function*() {
70-
return super.x();
69+
()=>{
70+
var _this = this, _superprop_get_x1 = ()=>_superprop_get_x();
71+
_async_to_generator(function*() {
72+
return _superprop_get_x1().call(_this);
7173
})();
74+
};
7275
// element access in async arrow
73-
()=>_async_to_generator(function*() {
74-
return super["x"]();
76+
()=>{
77+
var _this = this, _superprop_get1 = (_prop)=>_superprop_get(_prop);
78+
_async_to_generator(function*() {
79+
return _superprop_get1("x").call(_this);
7580
})();
81+
};
7682
})();
7783
}
7884
property_access_only_read_only() {
@@ -85,9 +91,12 @@ class B extends A {
8591
// property access in arrow
8692
()=>_superprop_get_x().call(_this);
8793
// property access in async arrow
88-
()=>_async_to_generator(function*() {
89-
return super.x();
94+
()=>{
95+
var _this = this, _superprop_get_x1 = ()=>_superprop_get_x();
96+
_async_to_generator(function*() {
97+
return _superprop_get_x1().call(_this);
9098
})();
99+
};
91100
})();
92101
}
93102
property_access_only_write_only() {
@@ -110,9 +119,12 @@ class B extends A {
110119
// property access (assign) in arrow
111120
()=>_superprop_set_x(f);
112121
// property access (assign) in async arrow
113-
()=>_async_to_generator(function*() {
114-
return super.x = f;
122+
()=>{
123+
var _superprop_set_x1 = (_value)=>_superprop_set_x(_value);
124+
_async_to_generator(function*() {
125+
return _superprop_set_x1(f);
115126
})();
127+
};
116128
})();
117129
}
118130
element_access_only_read_only() {
@@ -125,9 +137,12 @@ class B extends A {
125137
// element access in arrow
126138
()=>_superprop_get("x").call(_this);
127139
// element access in async arrow
128-
()=>_async_to_generator(function*() {
129-
return super["x"]();
140+
()=>{
141+
var _this = this, _superprop_get1 = (_prop)=>_superprop_get(_prop);
142+
_async_to_generator(function*() {
143+
return _superprop_get1("x").call(_this);
130144
})();
145+
};
131146
})();
132147
}
133148
element_access_only_write_only() {
@@ -150,9 +165,12 @@ class B extends A {
150165
// element access (assign) in arrow
151166
()=>_superprop_set("x", f);
152167
// element access (assign) in async arrow
153-
()=>_async_to_generator(function*() {
154-
return super["x"] = f;
168+
()=>{
169+
var _superprop_set1 = (_prop, _value)=>_superprop_set(_prop, _value);
170+
_async_to_generator(function*() {
171+
return _superprop_set1("x", f);
155172
})();
173+
};
156174
})();
157175
}
158176
property_access_only_read_only_in_generator() {
@@ -165,9 +183,12 @@ class B extends A {
165183
// property access in arrow
166184
()=>_superprop_get_x().call(_this);
167185
// property access in async arrow
168-
()=>_async_to_generator(function*() {
169-
return super.x();
186+
()=>{
187+
var _this = this, _superprop_get_x1 = ()=>_superprop_get_x();
188+
_async_to_generator(function*() {
189+
return _superprop_get_x1().call(_this);
170190
})();
191+
};
171192
})();
172193
}
173194
property_access_only_write_only_in_generator() {
@@ -190,9 +211,12 @@ class B extends A {
190211
// property access (assign) in arrow
191212
()=>_superprop_set_x(f);
192213
// property access (assign) in async arrow
193-
()=>_async_to_generator(function*() {
194-
return super.x = f;
214+
()=>{
215+
var _superprop_set_x1 = (_value)=>_superprop_set_x(_value);
216+
_async_to_generator(function*() {
217+
return _superprop_set_x1(f);
195218
})();
219+
};
196220
})();
197221
}
198222
element_access_only_read_only_in_generator() {
@@ -205,9 +229,12 @@ class B extends A {
205229
// element access in arrow
206230
()=>_superprop_get("x").call(_this);
207231
// element access in async arrow
208-
()=>_async_to_generator(function*() {
209-
return super["x"]();
232+
()=>{
233+
var _this = this, _superprop_get1 = (_prop)=>_superprop_get(_prop);
234+
_async_to_generator(function*() {
235+
return _superprop_get1("x").call(_this);
210236
})();
237+
};
211238
})();
212239
}
213240
element_access_only_write_only_in_generator() {
@@ -230,9 +257,12 @@ class B extends A {
230257
// element access (assign) in arrow
231258
()=>_superprop_set("x", f);
232259
// element access (assign) in async arrow
233-
()=>_async_to_generator(function*() {
234-
return super["x"] = f;
260+
()=>{
261+
var _superprop_set1 = (_prop, _value)=>_superprop_set(_prop, _value);
262+
_async_to_generator(function*() {
263+
return _superprop_set1("x", f);
235264
})();
265+
};
236266
})();
237267
}
238268
}

crates/swc_ecma_compat_es2017/src/async_to_generator.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,8 @@ impl VisitMut for AsyncToGenerator {
183183
out_fn_state.use_super |= fn_state.use_super;
184184
}
185185

186-
let should_handle_super =
187-
fn_state.use_super && self.fn_state.as_ref().is_some_and(|s| !s.is_async);
188-
189186
let mut stmts = vec![];
190-
if should_handle_super {
187+
if fn_state.use_super {
191188
// slow path
192189
let mut fn_env_hoister = FnEnvHoister::new(self.unresolved_ctxt);
193190
fn_env_hoister.disable_this();
@@ -214,7 +211,7 @@ impl VisitMut for AsyncToGenerator {
214211

215212
let expr = make_fn_ref(&fn_state, vec![], body);
216213

217-
arrow_expr.body = if should_handle_super {
214+
arrow_expr.body = if fn_state.use_super {
218215
stmts.push(expr.into_stmt());
219216
BlockStmtOrExpr::BlockStmt(BlockStmt {
220217
stmts,

0 commit comments

Comments
 (0)