Skip to content

Commit 890db2c

Browse files
lauraharkercopybara-github
authored andcommitted
Rollforward: Fix ClosureRewriteModule crash on unrecognized goog.requireDynamic
PiperOrigin-RevId: 618246611
1 parent 7dacf05 commit 890db2c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,11 @@ private void updateGoogForwardDeclare(NodeTraversal t, Node call) {
12221222

12231223
private void updateGoogRequireDynamicCall(Node call) {
12241224
final Node parent = call.getParent();
1225+
if (parent == null) {
1226+
// This call has been detached from the AST in unrecognized require handling. Nothing else to
1227+
// do here.
1228+
return;
1229+
}
12251230
checkState(
12261231
parent.isAwait() || (parent.isGetProp() && parent.getString().equals("then")),
12271232
"goog.requireDynamic() in only allowed in await/then expression");

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

+39
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,45 @@ public void testGoogRequireDynamic_then_name() {
12821282
"}")));
12831283
}
12841284

1285+
@Test
1286+
public void testGoogRequireDynamic_then_name_laterLoadedModule() {
1287+
test(
1288+
srcs(
1289+
lines(
1290+
"async function test() {", //
1291+
" goog.requireDynamic('a.b.c').then((foo) => {console.log(foo.Foo);});",
1292+
"}"),
1293+
lines("goog.module('a.b.c');", "exports.Foo=class{}")),
1294+
expected(
1295+
lines(
1296+
"async function test() {", //
1297+
" goog.importHandler_('sG5M4c').then(() => { ",
1298+
" const foo = module$exports$a$b$c;",
1299+
" console.log(foo.Foo);",
1300+
" });",
1301+
"}"),
1302+
lines(
1303+
"/** @const */ var module$exports$a$b$c = {};",
1304+
"/** @const */ module$exports$a$b$c.Foo = class {}")));
1305+
}
1306+
1307+
@Test
1308+
public void testGoogRequireDynamic_then_missingSources() {
1309+
test(
1310+
srcs(
1311+
lines(
1312+
"async function test() {", //
1313+
" /** @suppress {missingSourcesWarnings} */",
1314+
" goog.requireDynamic('a.b.c').then((foo) => {console.log(foo.Foo);});",
1315+
"}")),
1316+
expected(
1317+
lines(
1318+
"async function test() {", //
1319+
" /** @suppress {missingSourcesWarnings} */",
1320+
" null.then((foo) => {console.log(foo.Foo);});",
1321+
"}")));
1322+
}
1323+
12851324
@Test
12861325
public void testRequireDynamic_illegal_await_lhs() {
12871326
testError(

0 commit comments

Comments
 (0)