| OLD | NEW |
| (Empty) |
| 1 part of analyzer; | |
| 2 | |
| 3 /** | |
| 4 * Helper for formatting [AnalysisError]s. | |
| 5 * The two format options are a user consumable format and a machine consumable
format. | |
| 6 */ | |
| 7 class _ErrorFormatter { | |
| 8 StringSink out; | |
| 9 CommandLineOptions options; | |
| 10 | |
| 11 _ErrorFormatter(this.out, this.options); | |
| 12 | |
| 13 void startAnalysis() { | |
| 14 if (!options.machineFormat) { | |
| 15 out.writeln("Analyzing ${options.sourceFiles}..."); | |
| 16 } | |
| 17 } | |
| 18 | |
| 19 void formatErrors(List<AnalysisErrorInfo> errorInfos) { | |
| 20 var errors = new List<AnalysisError>(); | |
| 21 var errorToLine = new Map<AnalysisError, LineInfo>(); | |
| 22 for (AnalysisErrorInfo errorInfo in errorInfos) { | |
| 23 for (AnalysisError error in errorInfo.errors) { | |
| 24 errors.add(error); | |
| 25 errorToLine[error] = errorInfo.lineInfo; | |
| 26 } | |
| 27 } | |
| 28 // sort errors | |
| 29 errors.sort((AnalysisError error1, AnalysisError error2) { | |
| 30 // severity | |
| 31 int compare = error2.errorCode.errorSeverity.compareTo(error1.errorCode.er
rorSeverity); | |
| 32 if (compare != 0) { | |
| 33 return compare; | |
| 34 } | |
| 35 // path | |
| 36 compare = Comparable.compare(error1.source.fullName.toLowerCase(), error2.
source.fullName.toLowerCase()); | |
| 37 if (compare != 0) { | |
| 38 return compare; | |
| 39 } | |
| 40 // offset | |
| 41 return error1.offset - error2.offset; | |
| 42 }); | |
| 43 // format errors | |
| 44 int errorCount = 0; | |
| 45 int warnCount = 0; | |
| 46 for (AnalysisError error in errors) { | |
| 47 if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) { | |
| 48 errorCount++; | |
| 49 } else if (error.errorCode.errorSeverity == ErrorSeverity.WARNING) { | |
| 50 if (options.warningsAreFatal) { | |
| 51 errorCount++; | |
| 52 } else { | |
| 53 warnCount++; | |
| 54 } | |
| 55 } | |
| 56 formatError(errorToLine, error); | |
| 57 } | |
| 58 // print statistics | |
| 59 if (!options.machineFormat) { | |
| 60 if (errorCount != 0 && warnCount != 0) { | |
| 61 out.write(errorCount); | |
| 62 out.write(' '); | |
| 63 out.write(pluralize("error", errorCount)); | |
| 64 out.write(' and '); | |
| 65 out.write(warnCount); | |
| 66 out.write(' '); | |
| 67 out.write(pluralize("warning", warnCount)); | |
| 68 out.writeln(' found.'); | |
| 69 } else if (errorCount != 0) { | |
| 70 out.write(errorCount); | |
| 71 out.write(' '); | |
| 72 out.write(pluralize("error", errorCount)); | |
| 73 out.writeln(' found.'); | |
| 74 } else if (warnCount != 0) { | |
| 75 out.write(warnCount); | |
| 76 out.write(' '); | |
| 77 out.write(pluralize("warning", warnCount)); | |
| 78 out.writeln(' found.'); | |
| 79 } else { | |
| 80 out.writeln("No issues found."); | |
| 81 } | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 void formatError(Map<AnalysisError, LineInfo> errorToLine, AnalysisError error
) { | |
| 86 Source source = error.source; | |
| 87 LineInfo_Location location = errorToLine[error].getLocation(error.offset); | |
| 88 int length = error.length; | |
| 89 var severity = error.errorCode.errorSeverity; | |
| 90 if (options.machineFormat) { | |
| 91 if (severity == ErrorSeverity.WARNING && options.warningsAreFatal) { | |
| 92 severity = ErrorSeverity.ERROR; | |
| 93 } | |
| 94 out.write(severity); | |
| 95 out.write('|'); | |
| 96 out.write(error.errorCode.type); | |
| 97 out.write('|'); | |
| 98 out.write(error.errorCode); | |
| 99 out.write('|'); | |
| 100 out.write(escapePipe(source.fullName)); | |
| 101 out.write('|'); | |
| 102 out.write(location.lineNumber); | |
| 103 out.write('|'); | |
| 104 out.write(location.columnNumber); | |
| 105 out.write('|'); | |
| 106 out.write(length); | |
| 107 out.write('|'); | |
| 108 out.writeln(escapePipe(error.message)); | |
| 109 } else { | |
| 110 // [warning] 'foo' is not a method or function (/Users/devoncarew/temp/foo
.dart:-1:-1) | |
| 111 out.write('['); | |
| 112 out.write(severity.displayName); | |
| 113 out.write('] '); | |
| 114 out.write(error.message); | |
| 115 out.write(' ('); | |
| 116 out.write(source.fullName); | |
| 117 out.write(':'); | |
| 118 out.write(location.lineNumber); | |
| 119 out.write(':'); | |
| 120 out.write(location.columnNumber); | |
| 121 out.writeln(')'); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 static String escapePipe(String input) { | |
| 126 var result = new StringBuffer(); | |
| 127 for (var c in input.codeUnits) { | |
| 128 if (c == '\\' || c == '|') { | |
| 129 result.write('\\'); | |
| 130 } | |
| 131 result.writeCharCode(c); | |
| 132 } | |
| 133 return result.toString(); | |
| 134 } | |
| 135 | |
| 136 static String pluralize(String word, int count) { | |
| 137 if (count == 1) { | |
| 138 return word; | |
| 139 } else { | |
| 140 return word + "s"; | |
| 141 } | |
| 142 } | |
| 143 } | |
| OLD | NEW |