diff --git a/src/com/google/javascript/jscomp/LightweightMessageFormatter.java b/src/com/google/javascript/jscomp/LightweightMessageFormatter.java index 6d90979da5a..47ed923587f 100644 --- a/src/com/google/javascript/jscomp/LightweightMessageFormatter.java +++ b/src/com/google/javascript/jscomp/LightweightMessageFormatter.java @@ -33,6 +33,8 @@ public final class LightweightMessageFormatter extends AbstractMessageFormatter private SourceExcerpt excerpt; private static final ExcerptFormatter excerptFormatter = new LineNumberingFormatter(); + private boolean includeLocation = true; + private boolean includeLevel = true; /** * A constructor for when the client doesn't care about source information. @@ -57,6 +59,16 @@ public static LightweightMessageFormatter withoutSource() { return new LightweightMessageFormatter(); } + public LightweightMessageFormatter setIncludeLocation(boolean includeLocation) { + this.includeLocation = includeLocation; + return this; + } + + public LightweightMessageFormatter setIncludeLevel(boolean includeLevel) { + this.includeLevel = includeLevel; + return this; + } + @Override public String formatError(JSError error) { return format(error, false); @@ -79,18 +91,20 @@ private String format(JSError error, boolean warning) { String nonMappedPosition = formatPosition(sourceName, lineNumber); // Check if we can reverse-map the source. - OriginalMapping mapping = source == null ? null : source.getSourceMapping( - error.sourceName, error.lineNumber, error.getCharno()); - if (mapping == null) { - boldLine.append(nonMappedPosition); - } else { - sourceName = mapping.getOriginalFile(); - lineNumber = mapping.getLineNumber(); - charno = mapping.getColumnPosition(); - - b.append(nonMappedPosition); - b.append("\nOriginally at:\n"); - boldLine.append(formatPosition(sourceName, lineNumber)); + if (includeLocation) { + OriginalMapping mapping = source == null ? null : source.getSourceMapping( + error.sourceName, error.lineNumber, error.getCharno()); + if (mapping == null) { + boldLine.append(nonMappedPosition); + } else { + sourceName = mapping.getOriginalFile(); + lineNumber = mapping.getLineNumber(); + charno = mapping.getColumnPosition(); + + b.append(nonMappedPosition); + b.append("\nOriginally at:\n"); + boldLine.append(formatPosition(sourceName, lineNumber)); + } } // extract source excerpt @@ -98,8 +112,10 @@ private String format(JSError error, boolean warning) { excerpt.get( source, sourceName, lineNumber, excerptFormatter); - boldLine.append(getLevelName(warning ? CheckLevel.WARNING : CheckLevel.ERROR)); - boldLine.append(" - "); + if (includeLevel) { + boldLine.append(getLevelName(warning ? CheckLevel.WARNING : CheckLevel.ERROR)); + boldLine.append(" - "); + } boldLine.append(error.description); diff --git a/src/com/google/javascript/jscomp/Linter.java b/src/com/google/javascript/jscomp/Linter.java index 4903647ddac..6a0d92e3830 100644 --- a/src/com/google/javascript/jscomp/Linter.java +++ b/src/com/google/javascript/jscomp/Linter.java @@ -60,24 +60,22 @@ private void run(String[] args) throws IOException, CmdLineException { } static void lint(String filename) throws IOException { - lint(Paths.get(filename), false); + lint(Paths.get(filename), new Compiler(System.out)); } static void fix(String filename) throws IOException { - lint(Paths.get(filename), true); - } + Compiler compiler = new Compiler(System.out); + FixingErrorManager errorManager = new FixingErrorManager(); + compiler.setErrorManager(errorManager); + errorManager.setCompiler(compiler); - private static void lint(Path path, boolean fix) throws IOException { - SourceFile file = SourceFile.fromFile(path.toString()); - Compiler compiler = new Compiler(System.out); + lint(Paths.get(filename), compiler); - FixingErrorManager errorManager = null; - if (fix) { - errorManager = new FixingErrorManager(); - compiler.setErrorManager(errorManager); - errorManager.setCompiler(compiler); - } + ApplySuggestedFixes.applySuggestedFixesToFiles(errorManager.getAllFixes()); + } + static void lint(Path path, Compiler compiler) throws IOException { + SourceFile file = SourceFile.fromFile(path.toString()); CompilerOptions options = new CompilerOptions(); options.setLanguage(LanguageMode.ECMASCRIPT8); @@ -102,8 +100,5 @@ private static void lint(Path path, boolean fix) throws IOException { compiler.disableThreads(); SourceFile externs = SourceFile.fromCode("", ""); compiler.compile(ImmutableList.of(externs), ImmutableList.of(file), options); - if (fix) { - ApplySuggestedFixes.applySuggestedFixesToFiles(errorManager.getAllFixes()); - } } }