Skip to content

Commit 904464f

Browse files
lauraharkercopybara-github
authored andcommitted
Avoid using regex parser unless bundling or pruning unused inputs
This assumes all other kinds of compilations will need an actual AST for each input, and so regex parsing, while faster, is redundant. PiperOrigin-RevId: 506075616
1 parent 6990533 commit 904464f

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,11 @@ protected void setRunOptions(CompilerOptions options) throws IOException {
487487
if (config.printTree || config.printTreeJson) {
488488
options.setParseJsDocDocumentation(Config.JsDocParsing.INCLUDE_ALL_COMMENTS);
489489
}
490+
if (config.skipNormalOutputs) {
491+
// If skipping normal outputs, it's unnecessary to do a full AST parse of each input file.
492+
// The regex parser may still run if ordering/pruning inputs.
493+
compiler.setPreferRegexParser(true);
494+
}
490495
}
491496

492497
@GwtIncompatible("Unnecessary")

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ public int size() {
421421
// description.
422422
options.setParseJsDocDocumentation(JsDocParsing.INCLUDE_ALL_COMMENTS);
423423
}
424+
// If pruning many unused inputs, it's possible that it's faster to regex-parse everything and
425+
// AST-parse only the non-pruned inputs, rather than just AST-parse everything once.
426+
this.preferRegexParser = this.preferRegexParser || options.getDependencyOptions().shouldPrune();
424427
}
425428

426429
public void printConfig() {
@@ -2001,8 +2004,7 @@ void setPreferRegexParser(boolean preferRegexParser) {
20012004
this.preferRegexParser = preferRegexParser;
20022005
}
20032006

2004-
// TODO(b/264916633): make this default to false.
2005-
private boolean preferRegexParser = true;
2007+
private boolean preferRegexParser = false;
20062008

20072009
void orderInputsWithLargeStack() {
20082010
runInCompilerThread(

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,6 +2835,55 @@ public void testInstrumentCodeProductionArrayArgNotSet() {
28352835
+ "--instrument_for_coverage_option is set to Production");
28362836
}
28372837

2838+
@Test
2839+
public void testBundleOutput_bundlesGoogModule() throws IOException {
2840+
// Create a path for the final output
2841+
File bundledFile = temporaryFolder.newFile("bundle.js");
2842+
2843+
FlagEntry<JsSourceType> jsFile1 = createJsFile("test1", "var a;");
2844+
FlagEntry<JsSourceType> jsFile2 = createJsFile("test2", "goog.module('foo'); var b;");
2845+
2846+
args.add("--compilation_level=BUNDLE");
2847+
args.add("--dependency_mode=NONE");
2848+
args.add("--js_output_file");
2849+
args.add(bundledFile.getPath());
2850+
2851+
compileJsFiles("", jsFile1, jsFile2);
2852+
2853+
String bundledJs = java.nio.file.Files.readString(bundledFile.toPath());
2854+
String expected =
2855+
lines(
2856+
"//" + jsFile1.getValue(),
2857+
"var a;",
2858+
"//" + jsFile2.getValue(),
2859+
"goog.loadModule(function(exports) {'use strict';goog.module('foo'); var b;",
2860+
";return exports;});",
2861+
"\n");
2862+
assertThat(bundledJs).isEqualTo(expected);
2863+
}
2864+
2865+
@Test
2866+
public void testBundleOutput_ignoresSyntaxErrors() throws IOException {
2867+
// Verify that if bundling, the compiler doesn't run a full parse and thus doesn't report
2868+
// syntax errors.
2869+
2870+
// Create a path for the final output
2871+
File bundledFile = temporaryFolder.newFile("bundle.js");
2872+
2873+
FlagEntry<JsSourceType> jsFile = createJsFile("test1", "var a; syntax error!");
2874+
2875+
args.add("--compilation_level=BUNDLE");
2876+
args.add("--dependency_mode=NONE");
2877+
args.add("--js_output_file");
2878+
args.add(bundledFile.getPath());
2879+
2880+
compileJsFiles("", jsFile);
2881+
2882+
String bundledJs = java.nio.file.Files.readString(bundledFile.toPath());
2883+
String expected = lines("//" + jsFile.getValue(), "var a; syntax error!\n");
2884+
assertThat(bundledJs).isEqualTo(expected);
2885+
}
2886+
28382887
@Rule public TemporaryFolder folder = new TemporaryFolder();
28392888

28402889
@Test
@@ -3278,6 +3327,7 @@ private void compileArgs(String expectedOutput, @Nullable DiagnosticType expecte
32783327
assertThat(compiler.getErrors()).hasSize(1);
32793328
assertError(compiler.getErrors().get(0)).hasType(expectedError);
32803329
}
3330+
lastCommandLineRunner = runner;
32813331
}
32823332

32833333
private String compile(String inputString, List<String> args) {

0 commit comments

Comments
 (0)