From c41c74107e5cefb05c14e2514a32e6a685e2db7e Mon Sep 17 00:00:00 2001 From: blickly Date: Fri, 2 Sep 2016 11:16:00 -0700 Subject: [PATCH] Add an option to print the source for a single file after each pass. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=132083348 --- .../google/javascript/jscomp/Compiler.java | 21 ++++++++++++++++++- .../javascript/jscomp/CompilerOptions.java | 12 ++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/com/google/javascript/jscomp/Compiler.java b/src/com/google/javascript/jscomp/Compiler.java index 5aa8d7414d4..2637864497d 100644 --- a/src/com/google/javascript/jscomp/Compiler.java +++ b/src/com/google/javascript/jscomp/Compiler.java @@ -966,7 +966,7 @@ final void beforePass(String passName) { @Override final void afterPass(String passName) { if (options.printSourceAfterEachPass) { - String currentJsSource = toSource(); + String currentJsSource = getCurrentJsSource(); if (!currentJsSource.equals(this.lastJsSource)) { System.out.println(); System.out.println("// " + passName + " yields:"); @@ -977,6 +977,25 @@ final void afterPass(String passName) { } } + final String getCurrentJsSource() { + String filename = options.fileToPrintAfterEachPass; + if (filename == null) { + return toSource(); + } else { + Node script = getScriptNode(filename); + return script != null ? toSource(script) : ("File '" + filename + "' not found"); + } + } + + final Node getScriptNode(String filename) { + for (Node file : jsRoot.children()) { + if (file.getSourceFileName() != null && file.getSourceFileName().endsWith(filename)) { + return file; + } + } + return null; + } + /** * Returns a new tracer for the given pass name. */ diff --git a/src/com/google/javascript/jscomp/CompilerOptions.java b/src/com/google/javascript/jscomp/CompilerOptions.java index f6d3139268c..a9a3c0f89f5 100644 --- a/src/com/google/javascript/jscomp/CompilerOptions.java +++ b/src/com/google/javascript/jscomp/CompilerOptions.java @@ -880,15 +880,21 @@ public void setTrustedStrings(boolean yes) { trustedStrings = yes; } - String reportPath; - - // Should only be used when debugging compiler bugs using small JS inputs. + // Should only be used when debugging compiler bugs. boolean printSourceAfterEachPass; + // Used to narrow down the printed source when overall input size is large. + String fileToPrintAfterEachPass; public void setPrintSourceAfterEachPass(boolean printSource) { this.printSourceAfterEachPass = printSource; } + public void setFileToPrintAfterEachPass(String filename) { + this.fileToPrintAfterEachPass = filename; + } + + String reportPath; + /** Where to save a report of global name usage */ public void setReportPath(String reportPath) { this.reportPath = reportPath;