Skip to content

Commit 29c1f16

Browse files
lauraharkercopybara-github
authored andcommitted
Fix TypeInference crash on goog.requireDynamic for a missing source
PiperOrigin-RevId: 617957576
1 parent 25b3c9b commit 29c1f16

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1976,9 +1976,9 @@ private FlowScope setCallNodeTypeAfterChildrenTraversed(Node n, FlowScope scopeA
19761976
private @Nullable JSType getGoogModuleDependencyCallResultType(Node callNode) {
19771977
String moduleId = callNode.getSecondChild().getString();
19781978
Module module = compiler.getModuleMap().getClosureModule(moduleId);
1979-
// Only declared module ids.
1979+
// Fall back to the `?` type if the module is unknown to the compiler
19801980
if (module == null) {
1981-
return null;
1981+
return unknownType;
19821982
}
19831983

19841984
ScopedName name = moduleImportResolver.getClosureNamespaceTypeFromCall(callNode);
@@ -1987,7 +1987,7 @@ private FlowScope setCallNodeTypeAfterChildrenTraversed(Node n, FlowScope scopeA
19871987
TypedVar otherVar =
19881988
otherModuleScope != null ? otherModuleScope.getSlot(name.getName()) : null;
19891989
if (otherVar != null) {
1990-
return otherVar.getType();
1990+
return otherVar.getType() != null ? otherVar.getType() : unknownType;
19911991
}
19921992

19931993
if (otherModuleScope != null && module.metadata().moduleType() == ModuleType.GOOG_PROVIDE) {

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

+25
Original file line numberDiff line numberDiff line change
@@ -23511,6 +23511,31 @@ public void testSymbolIteratorMethod() {
2351123511
.run();
2351223512
}
2351323513

23514+
@Test
23515+
public void testGoogRequireDynamic_missingSources() {
23516+
// regression test for case that used to throw an exception
23517+
newTest()
23518+
.addSource(
23519+
"class Test {",
23520+
" /**",
23521+
" * @return {!Object}",
23522+
" *",
23523+
" * @suppress {missingSourcesWarnings} reference to dynamically loaded",
23524+
" * namespace.",
23525+
" * @suppress {checkTypes}",
23526+
" */",
23527+
"testGoogRequireDynamicStubbedAndWithLoadedModule() {",
23528+
" goog.setImportHandlerInternalDoNotCallOrElse(() => Promise.resolve(null));",
23529+
" goog.setUncompiledChunkIdHandlerInternalDoNotCallOrElse(s => s);",
23530+
"",
23531+
" goog.loadModule('goog.module(\"a.loaded.module\"); exports.foo = 12;');",
23532+
"",
23533+
" return goog.requireDynamic('a.loaded.module')",
23534+
" .then(({foo}) => assertEquals(foo, 12));",
23535+
" } }")
23536+
.run();
23537+
}
23538+
2351423539
@Test
2351523540
public void testClassMultipleExtends_fromClosureJs() {
2351623541
// Tests a workaround for b/325489639

0 commit comments

Comments
 (0)