diff --git a/src/com/google/javascript/jscomp/Compiler.java b/src/com/google/javascript/jscomp/Compiler.java index 8579ef62484..c9aaa663681 100644 --- a/src/com/google/javascript/jscomp/Compiler.java +++ b/src/com/google/javascript/jscomp/Compiler.java @@ -2907,7 +2907,7 @@ void addToDebugLog(String... strings) { @Override SourceFile getSourceFileByName(String sourceName) { // Here we assume that the source name is the input name, this - // is try of JavaScript parsed from source. + // is true of JavaScript parsed from source. if (sourceName != null) { CompilerInput input = inputsById.get(new InputId(sourceName)); if (input != null) { diff --git a/src/com/google/javascript/jscomp/LightweightMessageFormatter.java b/src/com/google/javascript/jscomp/LightweightMessageFormatter.java index 7f08953adef..a50d9b7b06a 100644 --- a/src/com/google/javascript/jscomp/LightweightMessageFormatter.java +++ b/src/com/google/javascript/jscomp/LightweightMessageFormatter.java @@ -107,11 +107,6 @@ private String format(JSError error, boolean warning) { } } - // extract source excerpt - String sourceExcerpt = source == null ? null : - excerpt.get( - source, sourceName, lineNumber, excerptFormatter); - if (includeLevel) { boldLine.append(getLevelName(warning ? CheckLevel.WARNING : CheckLevel.ERROR)); boldLine.append(" - "); @@ -121,6 +116,26 @@ private String format(JSError error, boolean warning) { b.append(maybeEmbolden(boldLine.toString())); b.append('\n'); + + String sourceExcerptWithPositionIndicator = + getExcerptWithPosition(error, sourceName, lineNumber, charno); + if (sourceExcerptWithPositionIndicator != null) { + b.append(sourceExcerptWithPositionIndicator); + } + return b.toString(); + } + + String getExcerptWithPosition(JSError error) { + return getExcerptWithPosition(error, error.sourceName, error.lineNumber, error.getCharno()); + } + + String getExcerptWithPosition(JSError error, String sourceName, int lineNumber, int charno) { + StringBuilder b = new StringBuilder(); + + SourceExcerptProvider source = getSource(); + String sourceExcerpt = + source == null ? null : excerpt.get(source, sourceName, lineNumber, excerptFormatter); + if (sourceExcerpt != null) { b.append(sourceExcerpt); b.append('\n'); @@ -167,9 +182,13 @@ private static String formatPosition(String sourceName, int lineNumber) { /** * Formats a region by appending line numbers in front, e.g. - *
   9| if (foo) {
+   *
+   * 
+   *    9| if (foo) {
    *   10|   alert('bar');
-   *   11| }
+ * 11| } + *
+ * * and return line excerpt without any modification. */ static class LineNumberingFormatter implements ExcerptFormatter {