Skip to content

Commit

Permalink
Refactor Linter.lint() so it takes a compiler to allow external calle…
Browse files Browse the repository at this point in the history
…rs to control the error manager.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=143207213
  • Loading branch information
rsamuelklatchko authored and blickly committed Jan 3, 2017
1 parent b267297 commit 8cea27f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
44 changes: 30 additions & 14 deletions src/com/google/javascript/jscomp/LightweightMessageFormatter.java
Expand Up @@ -33,6 +33,8 @@ public final class LightweightMessageFormatter extends AbstractMessageFormatter
private SourceExcerpt excerpt; private SourceExcerpt excerpt;
private static final ExcerptFormatter excerptFormatter = private static final ExcerptFormatter excerptFormatter =
new LineNumberingFormatter(); new LineNumberingFormatter();
private boolean includeLocation = true;
private boolean includeLevel = true;


/** /**
* A constructor for when the client doesn't care about source information. * A constructor for when the client doesn't care about source information.
Expand All @@ -57,6 +59,16 @@ public static LightweightMessageFormatter withoutSource() {
return new LightweightMessageFormatter(); return new LightweightMessageFormatter();
} }


public LightweightMessageFormatter setIncludeLocation(boolean includeLocation) {
this.includeLocation = includeLocation;
return this;
}

public LightweightMessageFormatter setIncludeLevel(boolean includeLevel) {
this.includeLevel = includeLevel;
return this;
}

@Override @Override
public String formatError(JSError error) { public String formatError(JSError error) {
return format(error, false); return format(error, false);
Expand All @@ -79,27 +91,31 @@ private String format(JSError error, boolean warning) {
String nonMappedPosition = formatPosition(sourceName, lineNumber); String nonMappedPosition = formatPosition(sourceName, lineNumber);


// Check if we can reverse-map the source. // Check if we can reverse-map the source.
OriginalMapping mapping = source == null ? null : source.getSourceMapping( if (includeLocation) {
error.sourceName, error.lineNumber, error.getCharno()); OriginalMapping mapping = source == null ? null : source.getSourceMapping(
if (mapping == null) { error.sourceName, error.lineNumber, error.getCharno());
boldLine.append(nonMappedPosition); if (mapping == null) {
} else { boldLine.append(nonMappedPosition);
sourceName = mapping.getOriginalFile(); } else {
lineNumber = mapping.getLineNumber(); sourceName = mapping.getOriginalFile();
charno = mapping.getColumnPosition(); lineNumber = mapping.getLineNumber();

charno = mapping.getColumnPosition();
b.append(nonMappedPosition);
b.append("\nOriginally at:\n"); b.append(nonMappedPosition);
boldLine.append(formatPosition(sourceName, lineNumber)); b.append("\nOriginally at:\n");
boldLine.append(formatPosition(sourceName, lineNumber));
}
} }


// extract source excerpt // extract source excerpt
String sourceExcerpt = source == null ? null : String sourceExcerpt = source == null ? null :
excerpt.get( excerpt.get(
source, sourceName, lineNumber, excerptFormatter); source, sourceName, lineNumber, excerptFormatter);


boldLine.append(getLevelName(warning ? CheckLevel.WARNING : CheckLevel.ERROR)); if (includeLevel) {
boldLine.append(" - "); boldLine.append(getLevelName(warning ? CheckLevel.WARNING : CheckLevel.ERROR));
boldLine.append(" - ");
}


boldLine.append(error.description); boldLine.append(error.description);


Expand Down
25 changes: 10 additions & 15 deletions src/com/google/javascript/jscomp/Linter.java
Expand Up @@ -60,24 +60,22 @@ private void run(String[] args) throws IOException, CmdLineException {
} }


static void lint(String filename) throws IOException { 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 { 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 { lint(Paths.get(filename), compiler);
SourceFile file = SourceFile.fromFile(path.toString());
Compiler compiler = new Compiler(System.out);


FixingErrorManager errorManager = null; ApplySuggestedFixes.applySuggestedFixesToFiles(errorManager.getAllFixes());
if (fix) { }
errorManager = new FixingErrorManager();
compiler.setErrorManager(errorManager);
errorManager.setCompiler(compiler);
}


static void lint(Path path, Compiler compiler) throws IOException {
SourceFile file = SourceFile.fromFile(path.toString());
CompilerOptions options = new CompilerOptions(); CompilerOptions options = new CompilerOptions();
options.setLanguage(LanguageMode.ECMASCRIPT8); options.setLanguage(LanguageMode.ECMASCRIPT8);


Expand All @@ -102,8 +100,5 @@ private static void lint(Path path, boolean fix) throws IOException {
compiler.disableThreads(); compiler.disableThreads();
SourceFile externs = SourceFile.fromCode("<Linter externs>", ""); SourceFile externs = SourceFile.fromCode("<Linter externs>", "");
compiler.compile(ImmutableList.<SourceFile>of(externs), ImmutableList.of(file), options); compiler.compile(ImmutableList.<SourceFile>of(externs), ImmutableList.of(file), options);
if (fix) {
ApplySuggestedFixes.applySuggestedFixesToFiles(errorManager.getAllFixes());
}
} }
} }

0 comments on commit 8cea27f

Please sign in to comment.