Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Unified Diff: pkg/analyzer_experimental/lib/error_formatter.dart

Issue 14767014: Dart-based command-line analyzer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/analyzer_experimental/lib/analyzer.dart ('k') | pkg/analyzer_experimental/lib/options.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer_experimental/lib/error_formatter.dart
diff --git a/pkg/analyzer_experimental/lib/error_formatter.dart b/pkg/analyzer_experimental/lib/error_formatter.dart
new file mode 100644
index 0000000000000000000000000000000000000000..9aafb9fb8a79cc69088d27667cfa38980d689f49
--- /dev/null
+++ b/pkg/analyzer_experimental/lib/error_formatter.dart
@@ -0,0 +1,143 @@
+part of analyzer;
+
+/**
+ * Helper for formatting [AnalysisError]s.
+ * The two format options are a user consumable format and a machine consumable format.
+ */
+class _ErrorFormatter {
+ StringSink out;
+ CommandLineOptions options;
+
+ _ErrorFormatter(this.out, this.options);
+
+ void startAnalysis() {
+ if (!options.machineFormat) {
+ out.writeln("Analyzing ${options.sourceFiles}...");
+ }
+ }
+
+ void formatErrors(List<AnalysisErrorInfo> errorInfos) {
+ var errors = new List<AnalysisError>();
+ var errorToLine = new Map<AnalysisError, LineInfo>();
+ for (AnalysisErrorInfo errorInfo in errorInfos) {
+ for (AnalysisError error in errorInfo.errors) {
+ errors.add(error);
+ errorToLine[error] = errorInfo.lineInfo;
+ }
+ }
+ // sort errors
+ errors.sort((AnalysisError error1, AnalysisError error2) {
+ // severity
+ int compare = error2.errorCode.errorSeverity.compareTo(error1.errorCode.errorSeverity);
+ if (compare != 0) {
+ return compare;
+ }
+ // path
+ compare = Comparable.compare(error1.source.fullName.toLowerCase(), error2.source.fullName.toLowerCase());
+ if (compare != 0) {
+ return compare;
+ }
+ // offset
+ return error1.offset - error2.offset;
+ });
+ // format errors
+ int errorCount = 0;
+ int warnCount = 0;
+ for (AnalysisError error in errors) {
+ if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) {
+ errorCount++;
+ } else if (error.errorCode.errorSeverity == ErrorSeverity.WARNING) {
+ if (options.warningsAreFatal) {
+ errorCount++;
+ } else {
+ warnCount++;
+ }
+ }
+ formatError(errorToLine, error);
+ }
+ // print statistics
+ if (!options.machineFormat) {
+ if (errorCount != 0 && warnCount != 0) {
+ out.write(errorCount);
+ out.write(' ');
+ out.write(pluralize("error", errorCount));
+ out.write(' and ');
+ out.write(warnCount);
+ out.write(' ');
+ out.write(pluralize("warning", warnCount));
+ out.writeln(' found.');
+ } else if (errorCount != 0) {
+ out.write(errorCount);
+ out.write(' ');
+ out.write(pluralize("error", errorCount));
+ out.writeln(' found.');
+ } else if (warnCount != 0) {
+ out.write(warnCount);
+ out.write(' ');
+ out.write(pluralize("warning", warnCount));
+ out.writeln(' found.');
+ } else {
+ out.writeln("No issues found.");
+ }
+ }
+ }
+
+ void formatError(Map<AnalysisError, LineInfo> errorToLine, AnalysisError error) {
+ Source source = error.source;
+ LineInfo_Location location = errorToLine[error].getLocation(error.offset);
+ int length = error.length;
+ var severity = error.errorCode.errorSeverity;
+ if (options.machineFormat) {
+ if (severity == ErrorSeverity.WARNING && options.warningsAreFatal) {
+ severity = ErrorSeverity.ERROR;
+ }
+ out.write(severity);
+ out.write('|');
+ out.write(error.errorCode.type);
+ out.write('|');
+ out.write(error.errorCode);
+ out.write('|');
+ out.write(escapePipe(source.fullName));
+ out.write('|');
+ out.write(location.lineNumber);
+ out.write('|');
+ out.write(location.columnNumber);
+ out.write('|');
+ out.write(length);
+ out.write('|');
+ out.writeln(escapePipe(error.message));
+ } else {
+ // [warning] 'foo' is not a method or function (/Users/devoncarew/temp/foo.dart:-1:-1)
+ out.write('[');
+ out.write(severity.displayName);
+ out.write('] ');
+ out.write(error.message);
+ out.write(' (');
+ out.write(source.fullName);
+ out.write(':');
+ out.write(location.lineNumber);
+ out.write(':');
+ out.write(location.columnNumber);
+ out.writeln(')');
+ }
+ }
+
+ static String escapePipe(String input) {
+ var result = new StringBuffer();
+ for (var c in input.codeUnits) {
+ if (c == '\\' || c == '|') {
+ result.write('\\');
+ }
+ result.writeCharCode(c);
+ }
+ return result.toString();
+ }
+
+ static String pluralize(String word, int count) {
+ if (count == 1) {
+ return word;
+ } else {
+ return word + "s";
+ }
+ }
+}
« no previous file with comments | « pkg/analyzer_experimental/lib/analyzer.dart ('k') | pkg/analyzer_experimental/lib/options.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698