1919import static com .google .common .truth .Truth .assertThat ;
2020import static com .google .javascript .jscomp .base .JSCompStrings .lines ;
2121import static com .google .javascript .rhino .testing .NodeSubject .assertNode ;
22+ import static org .junit .Assert .assertThrows ;
2223
2324import com .google .common .collect .ImmutableList ;
2425import com .google .common .collect .ImmutableMap ;
2829import com .google .javascript .jscomp .CompilerOptions .IncrementalCheckMode ;
2930import com .google .javascript .jscomp .CompilerOptions .LanguageMode ;
3031import com .google .javascript .jscomp .CrossChunkMethodMotion ;
32+ import com .google .javascript .jscomp .DependencyOptions ;
3133import com .google .javascript .jscomp .DiagnosticGroups ;
3234import com .google .javascript .jscomp .JSChunk ;
35+ import com .google .javascript .jscomp .ModuleIdentifier ;
3336import com .google .javascript .jscomp .SourceFile ;
3437import com .google .javascript .jscomp .VariableRenamingPolicy ;
3538import com .google .javascript .jscomp .WarningLevel ;
@@ -73,6 +76,7 @@ public void simpleAlertCall() throws IOException {
7376
7477 CompilerOptions options = new CompilerOptions ();
7578 CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
79+ options .setDependencyOptions (DependencyOptions .none ());
7680
7781 Compiler compiler = compileTypedAstShards (options );
7882
@@ -93,6 +97,7 @@ public void alertCallWithCrossLibraryVarReference() throws IOException {
9397
9498 CompilerOptions options = new CompilerOptions ();
9599 CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
100+ options .setDependencyOptions (DependencyOptions .none ());
96101
97102 Compiler compiler = compileTypedAstShards (options );
98103
@@ -139,6 +144,7 @@ public void disambiguatesAndDeletesMethodsAcrossLibraries() throws IOException {
139144
140145 CompilerOptions options = new CompilerOptions ();
141146 CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
147+ options .setDependencyOptions (DependencyOptions .none ());
142148 options .setDisambiguateProperties (true );
143149
144150 Compiler compiler = compileTypedAstShards (options );
@@ -163,6 +169,7 @@ public void disambiguatesAndDeletesMethodsAcrossLibraries_withTranspilation() th
163169
164170 CompilerOptions options = new CompilerOptions ();
165171 CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
172+ options .setDependencyOptions (DependencyOptions .none ());
166173 options .setDisambiguateProperties (true );
167174 options .setLanguageOut (LanguageMode .ECMASCRIPT5 );
168175
@@ -256,6 +263,7 @@ public void externJSDocPropertiesNotRenamed() throws IOException {
256263
257264 CompilerOptions options = new CompilerOptions ();
258265 CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
266+ options .setDependencyOptions (DependencyOptions .none ());
259267
260268 Compiler compiler = compileTypedAstShards (options );
261269
@@ -281,6 +289,7 @@ public void gatherExternProperties() throws IOException {
281289
282290 CompilerOptions options = new CompilerOptions ();
283291 CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
292+ options .setDependencyOptions (DependencyOptions .none ());
284293
285294 Compiler compiler = compileTypedAstShards (options );
286295
@@ -313,6 +322,7 @@ public void protectsHiddenSideEffects() throws IOException {
313322
314323 CompilerOptions options = new CompilerOptions ();
315324 CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
325+ options .setDependencyOptions (DependencyOptions .none ());
316326
317327 Compiler compiler = compileTypedAstShards (options );
318328
@@ -329,6 +339,7 @@ public void removesRegExpCallsIfSafe() throws IOException {
329339
330340 CompilerOptions options = new CompilerOptions ();
331341 CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
342+ options .setDependencyOptions (DependencyOptions .none ());
332343
333344 Compiler compiler = compileTypedAstShards (options );
334345
@@ -348,6 +359,7 @@ public void removesRegExpCallsIfUnsafelyReferenced() throws IOException {
348359
349360 CompilerOptions options = new CompilerOptions ();
350361 CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
362+ options .setDependencyOptions (DependencyOptions .none ());
351363
352364 Compiler compiler = compileTypedAstShards (options );
353365
@@ -374,6 +386,7 @@ public void runsJ2clOptimizations() throws IOException {
374386
375387 CompilerOptions options = new CompilerOptions ();
376388 CompilationLevel .ADVANCED_OPTIMIZATIONS .setOptionsForCompilationLevel (options );
389+ options .setDependencyOptions (DependencyOptions .none ());
377390
378391 Compiler compiler = compileTypedAstShards (options );
379392
@@ -460,6 +473,27 @@ public void testCrossChunkMethodMotion() throws IOException {
460473 .isEqualTo (expectedRoot );
461474 }
462475
476+ @ Test
477+ public void dependencyModePruningForGoogModules_banned () throws IOException {
478+ precompileLibrary (
479+ extern (new TestExternsBuilder ().addClosureExterns ().build ()),
480+ code ("goog.module('keepMe'); const x = 1;" ), // input_1
481+ code ("goog.module('entryPoint'); goog.require('keepMe');" ), // input_2
482+ code ("goog.module('dropMe'); const x = 3;" )); // input_3
483+
484+ CompilerOptions options = new CompilerOptions ();
485+ options .setDependencyOptions (
486+ DependencyOptions .pruneForEntryPoints (
487+ ImmutableList .of (ModuleIdentifier .forClosure ("entryPoint" ))));
488+
489+ // TODO(b/219588952): if we decide to support this, verify that JSCompiler no longer incorrectly
490+ // prunes the 'keepMe' module.
491+ // This might be fixable by just removing module rewriting from the 'checks' phase.
492+ Exception ex =
493+ assertThrows (IllegalArgumentException .class , () -> compileTypedAstShards (options ));
494+ assertThat (ex ).hasMessageThat ().contains ("mode=PRUNE" );
495+ }
496+
463497 // use over 'compileTypedAstShards' if you want to validate reported errors or warnings in your
464498 // @Test case.
465499 private Compiler compileTypedAstShardsWithoutErrorChecks (CompilerOptions options )
0 commit comments