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 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.
Expand All @@ -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);
Expand All @@ -79,27 +91,31 @@ 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
String sourceExcerpt = source == null ? null :
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);

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 {
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);

Expand All @@ -102,8 +100,5 @@ private static void lint(Path path, boolean fix) throws IOException {
compiler.disableThreads();
SourceFile externs = SourceFile.fromCode("<Linter externs>", "");
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.