Skip to content

Commit

Permalink
Refactoring for an internal fix.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=179763385
  • Loading branch information
tbreisacher committed Dec 21, 2017
1 parent 9cc62fa commit 8e5a203
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -2907,7 +2907,7 @@ void addToDebugLog(String... strings) {
@Override @Override
SourceFile getSourceFileByName(String sourceName) { SourceFile getSourceFileByName(String sourceName) {
// Here we assume that the source name is the input name, this // 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) { if (sourceName != null) {
CompilerInput input = inputsById.get(new InputId(sourceName)); CompilerInput input = inputsById.get(new InputId(sourceName));
if (input != null) { if (input != null) {
Expand Down
33 changes: 26 additions & 7 deletions src/com/google/javascript/jscomp/LightweightMessageFormatter.java
Expand Up @@ -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) { if (includeLevel) {
boldLine.append(getLevelName(warning ? CheckLevel.WARNING : CheckLevel.ERROR)); boldLine.append(getLevelName(warning ? CheckLevel.WARNING : CheckLevel.ERROR));
boldLine.append(" - "); boldLine.append(" - ");
Expand All @@ -121,6 +116,26 @@ private String format(JSError error, boolean warning) {


b.append(maybeEmbolden(boldLine.toString())); b.append(maybeEmbolden(boldLine.toString()));
b.append('\n'); 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) { if (sourceExcerpt != null) {
b.append(sourceExcerpt); b.append(sourceExcerpt);
b.append('\n'); b.append('\n');
Expand Down Expand Up @@ -167,9 +182,13 @@ private static String formatPosition(String sourceName, int lineNumber) {


/** /**
* Formats a region by appending line numbers in front, e.g. * Formats a region by appending line numbers in front, e.g.
* <pre> 9| if (foo) { *
* <pre>
* 9| if (foo) {
* 10| alert('bar'); * 10| alert('bar');
* 11| }</pre> * 11| }
* </pre>
*
* and return line excerpt without any modification. * and return line excerpt without any modification.
*/ */
static class LineNumberingFormatter implements ExcerptFormatter { static class LineNumberingFormatter implements ExcerptFormatter {
Expand Down

0 comments on commit 8e5a203

Please sign in to comment.