Skip to content

Commit 3649b9e

Browse files
Closure Teamcopybara-github
authored andcommitted
Bug fix for goog.requireDynamic() transpilation
PiperOrigin-RevId: 503175436
1 parent f59bbfa commit 3649b9e

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,10 +1314,22 @@ private void updateGoogRequireDynamicCallInThen(Node call) {
13141314
objectPatternOrNameNode.getString(), exportedNamespaceNameNode)
13151315
.srcrefTreeIfMissing(call);
13161316
}
1317-
functionBody.addChildToFront(declarationNode);
1317+
1318+
// If right hand side of the callback arrow function is an expression instead of BLOCK,
1319+
// e.g., `({Foo}) => Foo` instead of `() => { return Foo; }`, create a BLOCK to host the
1320+
// expression.
1321+
if (functionBody.isBlock()) {
1322+
functionBody.addChildToFront(declarationNode);
1323+
compiler.reportChangeToEnclosingScope(declarationNode);
1324+
} else {
1325+
Node returnStmt = this.astFactory.createReturn(functionBody.detach());
1326+
Node newBlock = this.astFactory.createBlock(returnStmt).srcrefTree(call);
1327+
newBlock.insertAfter(paramListNode);
1328+
newBlock.addChildToFront(declarationNode);
1329+
compiler.reportChangeToEnclosingScope(newBlock);
1330+
}
13181331

13191332
compiler.reportChangeToEnclosingScope(call);
1320-
compiler.reportChangeToEnclosingScope(declarationNode);
13211333
}
13221334

13231335
// Rewrite

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,46 @@ public void testGoogRequireDynamic_then_destructuringPattern() {
12161216
"}")));
12171217
}
12181218

1219+
@Test
1220+
public void testGoogRequireDynamic_then_destructuringPattern_withExpressionBody() {
1221+
test(
1222+
srcs(
1223+
lines("goog.module('a.b.c');", "exports.Foo=class{}"),
1224+
lines(
1225+
"async function test() {", //
1226+
" goog.requireDynamic('a.b.c').then(({Foo}) => Foo);",
1227+
"}")),
1228+
expected(
1229+
lines(
1230+
"/** @const */ var module$exports$a$b$c = {};",
1231+
"/** @const */ module$exports$a$b$c.Foo = class {}"),
1232+
lines(
1233+
"async function test() {", //
1234+
" goog.importHandler_('sG5M4c').then(() => { ",
1235+
" const {Foo} = module$exports$a$b$c;",
1236+
" return Foo;",
1237+
" });",
1238+
"}")));
1239+
test(
1240+
srcs(
1241+
lines("goog.module('a.b.c');", "exports.Foo=class{}"),
1242+
lines(
1243+
"async function test() {", //
1244+
" goog.requireDynamic('a.b.c').then(({Foo}) => console.log(Foo));",
1245+
"}")),
1246+
expected(
1247+
lines(
1248+
"/** @const */ var module$exports$a$b$c = {};",
1249+
"/** @const */ module$exports$a$b$c.Foo = class {}"),
1250+
lines(
1251+
"async function test() {", //
1252+
" goog.importHandler_('sG5M4c').then(() => { ",
1253+
" const {Foo} = module$exports$a$b$c;",
1254+
" return console.log(Foo);",
1255+
" });",
1256+
"}")));
1257+
}
1258+
12191259
@Test
12201260
public void testGoogRequireDynamic_then_name() {
12211261
test(

0 commit comments

Comments
 (0)