Search in sources :

Example 1 with ErrorWithLevel

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());
}
Also used : OriginalMapping(com.google.debugging.sourcemap.proto.Mapping.OriginalMapping) ErrorWithLevel(com.google.javascript.jscomp.SortingErrorManager.ErrorWithLevel) Node(com.google.javascript.rhino.Node) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JsonWriter(com.google.gson.stream.JsonWriter) GwtIncompatible(com.google.common.annotations.GwtIncompatible)

Aggregations

GwtIncompatible (com.google.common.annotations.GwtIncompatible)1 OriginalMapping (com.google.debugging.sourcemap.proto.Mapping.OriginalMapping)1 JsonWriter (com.google.gson.stream.JsonWriter)1 ErrorWithLevel (com.google.javascript.jscomp.SortingErrorManager.ErrorWithLevel)1 Node (com.google.javascript.rhino.Node)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1