Skip to content

Commit d2925ef

Browse files
Update async await transpilation to avoid captures of super when using ES2015 output.
MS Edge 17 does not properly capture references to `super` in an arrow function. Closes google#3101
1 parent 826b0fc commit d2925ef

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/com/google/javascript/jscomp/RewriteAsyncFunctions.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,26 @@ private void convertAsyncFunction(NodeTraversal t, LexicalContext functionContex
243243
newBody.addChildToBack(IR.constNode(IR.name(ASYNC_ARGUMENTS), IR.name("arguments")));
244244
NodeUtil.addFeatureToScript(t.getCurrentFile(), Feature.CONST_DECLARATIONS);
245245
}
246+
boolean needsSuperTranspilation = compiler.getOptions().needsTranspilationFrom(FeatureSet.ES6);
246247
for (String replacedMethodName : functionContext.replacedSuperProperties) {
248+
// MS Edge 17 cannot properly capture references to "super" in an arrow function.
249+
// If we are not transpiling classes, switch to using Object.getPrototypeOf(this)
250+
// as a replacement for super.
251+
// If we are transpiling classes, the super reference will be handled elsewhere.
252+
Node superReference;
253+
if (needsSuperTranspilation) {
254+
superReference = IR.superNode();
255+
} else {
256+
superReference =
257+
IR.call(IR.getprop(IR.name("Object"), IR.string("getPrototypeOf")), IR.thisNode());
258+
}
259+
247260
// const super$get$x = () => super.x;
248-
Node arrowFunction = IR.arrowFunction(
249-
IR.name(""), IR.paramList(), IR.getprop(IR.superNode(), IR.string(replacedMethodName)));
261+
Node arrowFunction =
262+
IR.arrowFunction(
263+
IR.name(""),
264+
IR.paramList(),
265+
IR.getprop(superReference, IR.string(replacedMethodName)));
250266
compiler.reportChangeToChangeScope(arrowFunction);
251267
NodeUtil.addFeatureToScript(t.getCurrentFile(), Feature.ARROW_FUNCTIONS);
252268

test/com/google/javascript/jscomp/RewriteAsyncFunctionsTest.java

+33
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,39 @@ public void testInnerSuperReference() {
142142
"}"));
143143
}
144144

145+
@Test
146+
public void testInnerSuperCallEs2015Out() {
147+
setLanguageOut(LanguageMode.ECMASCRIPT_2015);
148+
test(
149+
lines(
150+
"class A {",
151+
" m() {",
152+
" return this;",
153+
" }",
154+
"}",
155+
"class X extends A {",
156+
" async m() {",
157+
" return super.m();",
158+
" }",
159+
"}"),
160+
lines(
161+
"class A {",
162+
" m() {",
163+
" return this;",
164+
" }",
165+
"}",
166+
"class X extends A {",
167+
" m() {",
168+
" const $jscomp$async$this = this;",
169+
" const $jscomp$async$super$get$m = () => Object.getPrototypeOf(this).m;",
170+
" return $jscomp.asyncExecutePromiseGeneratorFunction(",
171+
" function* () {",
172+
" return $jscomp$async$super$get$m().call($jscomp$async$this);",
173+
" });",
174+
" }",
175+
"}"));
176+
}
177+
145178
@Test
146179
public void testNestedArrowFunctionUsingThis() {
147180
test(

0 commit comments

Comments
 (0)