use of com.google.javascript.jscomp.SortingErrorManager.ErrorWithLevel in project closure-compiler by google.
the class JsonErrorReportGenerator method generateReport.
@Override
@GwtIncompatible
public void generateReport(SortingErrorManager manager) {
ByteArrayOutputStream bufferedStream = new ByteArrayOutputStream();
try (JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(bufferedStream, UTF_8))) {
jsonWriter.beginArray();
for (ErrorWithLevel message : manager.getSortedDiagnostics()) {
String sourceName = message.error.getSourceName();
int lineNumber = message.error.getLineNumber();
int charno = message.error.getCharno();
jsonWriter.beginObject();
jsonWriter.name("level").value(message.level == CheckLevel.ERROR ? "error" : "warning");
jsonWriter.name("description").value(message.error.getDescription());
jsonWriter.name("key").value(message.error.getType().key);
jsonWriter.name("source").value(sourceName);
jsonWriter.name("line").value(lineNumber);
jsonWriter.name("column").value(charno);
Node node = message.error.getNode();
if (node != null) {
jsonWriter.name("length").value(node.getLength());
}
// extract source excerpt
String sourceExcerpt = SourceExcerpt.LINE.get(sourceExcerptProvider, sourceName, lineNumber, excerptFormatter);
if (sourceExcerpt != null) {
StringBuilder b = new StringBuilder(sourceExcerpt);
b.append("\n");
// at the end of the line
if (0 <= charno && charno <= sourceExcerpt.length()) {
for (int i = 0; i < charno; i++) {
char c = sourceExcerpt.charAt(i);
if (TokenUtil.isWhitespace(c)) {
b.append(c);
} else {
b.append(' ');
}
}
if (message.error.getNode() == null) {
b.append("^");
} else {
int length = max(1, min(message.error.getNode().getLength(), sourceExcerpt.length() - charno));
for (int i = 0; i < length; i++) {
b.append("^");
}
}
}
jsonWriter.name("context").value(b.toString());
}
OriginalMapping mapping = sourceExcerptProvider.getSourceMapping(sourceName, message.error.getLineNumber(), message.error.getCharno());
if (mapping != null) {
jsonWriter.name("originalLocation").beginObject();
jsonWriter.name("source").value(mapping.getOriginalFile());
jsonWriter.name("line").value(mapping.getLineNumber());
jsonWriter.name("column").value(mapping.getColumnPosition());
jsonWriter.endObject();
}
jsonWriter.endObject();
}
StringBuilder summaryBuilder = new StringBuilder();
if (manager.getTypedPercent() > 0.0) {
summaryBuilder.append(SimpleFormat.format("%d error(s), %d warning(s), %.1f%% typed", manager.getErrorCount(), manager.getWarningCount(), manager.getTypedPercent()));
} else {
summaryBuilder.append(SimpleFormat.format("%d error(s), %d warning(s)", manager.getErrorCount(), manager.getWarningCount()));
}
jsonWriter.beginObject();
jsonWriter.name("level").value("info");
jsonWriter.name("description").value(summaryBuilder.toString());
jsonWriter.endObject();
jsonWriter.endArray();
jsonWriter.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
stream.append(bufferedStream.toString());
}
Aggregations