Skip to content

Commit 91bda23

Browse files
lauraharkercopybara-github
authored andcommitted
Add unit test to reproduce a bug inlining across conditionally loaded chunks
PiperOrigin-RevId: 714079146
1 parent 5f55b4d commit 91bda23

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

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

+36
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.javascript.jscomp.CompilerOptions.ChunkOutputType;
2525
import com.google.javascript.jscomp.CompilerOptions.PropertyCollapseLevel;
2626
import com.google.javascript.jscomp.deps.ModuleLoader.ResolutionMode;
27+
import com.google.javascript.jscomp.testing.JSChunkGraphBuilder;
2728
import com.google.javascript.jscomp.testing.TestExternsBuilder;
2829
import com.google.javascript.rhino.Node;
2930
import com.google.javascript.rhino.testing.NodeSubject;
@@ -3298,4 +3299,39 @@ public void testHookGetProp2() {
32983299
"var xid$internal_ = function() {};",
32993300
"var xx = ((x ? xid : abc) - def).toExponential(5);"));
33003301
}
3302+
3303+
@Test
3304+
public void conditionallyLoadedChunk() {
3305+
JSChunk[] chunks =
3306+
JSChunkGraphBuilder.forChain()
3307+
// m1, always loaded
3308+
.addChunk(
3309+
lines(
3310+
"const registry = {};",
3311+
"const factories = {};",
3312+
"factories.one = function() {};",
3313+
"",
3314+
"function build() {",
3315+
" const mod = registry.mod;",
3316+
" if (mod) mod();",
3317+
"}"))
3318+
// m2, conditionally loaded after m1
3319+
.addChunk("registry.mod = factories.one;")
3320+
.build();
3321+
3322+
// TODO - b/385132629: don't inline the definition of registry.mod into build(). If the mod
3323+
// chunk is not loaded, then registry.mod will be undefined when build() is called, so it's
3324+
// wrong to always call `factories$one` in build().
3325+
test(
3326+
srcs(chunks[0], chunks[1]),
3327+
expected(
3328+
lines(
3329+
"var factories$one = function() {};",
3330+
"",
3331+
"function build() {",
3332+
" const mod = null;",
3333+
" if (factories$one) factories$one();",
3334+
"}"),
3335+
"var registry$mod = null;"));
3336+
}
33013337
}

0 commit comments

Comments
 (0)