| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library error_formatter; | 5 library error_formatter; |
| 6 | 6 |
| 7 import 'generated/engine.dart'; | 7 import 'generated/engine.dart'; |
| 8 import 'generated/error.dart'; | 8 import 'generated/error.dart'; |
| 9 import 'generated/source_io.dart'; | 9 import 'generated/source_io.dart'; |
| 10 import '../options.dart'; | 10 import '../options.dart'; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 compare = Comparable.compare(error1.source.fullName.toLowerCase(), error2.
source.fullName.toLowerCase()); | 39 compare = Comparable.compare(error1.source.fullName.toLowerCase(), error2.
source.fullName.toLowerCase()); |
| 40 if (compare != 0) { | 40 if (compare != 0) { |
| 41 return compare; | 41 return compare; |
| 42 } | 42 } |
| 43 // offset | 43 // offset |
| 44 return error1.offset - error2.offset; | 44 return error1.offset - error2.offset; |
| 45 }); | 45 }); |
| 46 // format errors | 46 // format errors |
| 47 int errorCount = 0; | 47 int errorCount = 0; |
| 48 int warnCount = 0; | 48 int warnCount = 0; |
| 49 int hintCount = 0; |
| 49 for (AnalysisError error in errors) { | 50 for (AnalysisError error in errors) { |
| 50 if (error.errorCode.errorSeverity == ErrorSeverity.ERROR) { | 51 var severity = error.errorCode.errorSeverity; |
| 52 if (severity == ErrorSeverity.ERROR) { |
| 51 errorCount++; | 53 errorCount++; |
| 52 } else if (error.errorCode.errorSeverity == ErrorSeverity.WARNING) { | 54 } else if (severity == ErrorSeverity.WARNING) { |
| 53 if (options.warningsAreFatal) { | 55 if (options.warningsAreFatal) { |
| 54 errorCount++; | 56 errorCount++; |
| 55 } else { | 57 } else { |
| 56 warnCount++; | 58 if (error.errorCode.type == ErrorType.HINT) { |
| 59 hintCount++; |
| 60 } else { |
| 61 warnCount++; |
| 62 } |
| 57 } | 63 } |
| 58 } | 64 } |
| 59 formatError(errorToLine, error); | 65 formatError(errorToLine, error); |
| 60 } | 66 } |
| 61 // print statistics | 67 // print statistics |
| 62 if (!options.machineFormat) { | 68 if (!options.machineFormat) { |
| 63 if (errorCount != 0 && warnCount != 0) { | 69 var hasErrors = errorCount != 0; |
| 70 var hasWarns = warnCount != 0; |
| 71 var hasHints = hintCount != 0; |
| 72 bool hasContent = false; |
| 73 if (hasErrors) { |
| 64 out.write(errorCount); | 74 out.write(errorCount); |
| 65 out.write(' '); | 75 out.write(' '); |
| 66 out.write(pluralize("error", errorCount)); | 76 out.write(pluralize("error", errorCount)); |
| 67 out.write(' and '); | 77 hasContent = true; |
| 78 } |
| 79 if (hasWarns) { |
| 80 if (hasContent) { |
| 81 if (!hasHints) { |
| 82 out.write(' and '); |
| 83 } else { |
| 84 out.write(", "); |
| 85 } |
| 86 } |
| 68 out.write(warnCount); | 87 out.write(warnCount); |
| 69 out.write(' '); | 88 out.write(' '); |
| 70 out.write(pluralize("warning", warnCount)); | 89 out.write(pluralize("warning", warnCount)); |
| 71 out.writeln(' found.'); | 90 hasContent = true; |
| 72 } else if (errorCount != 0) { | 91 } |
| 73 out.write(errorCount); | 92 if (hasHints) { |
| 93 if (hasContent) { |
| 94 out.write(" and "); |
| 95 } |
| 96 out.write(hintCount); |
| 74 out.write(' '); | 97 out.write(' '); |
| 75 out.write(pluralize("error", errorCount)); | 98 out.write(pluralize("hint", hintCount)); |
| 76 out.writeln(' found.'); | 99 hasContent = true; |
| 77 } else if (warnCount != 0) { | 100 } |
| 78 out.write(warnCount); | 101 if (hasContent) { |
| 79 out.write(' '); | 102 out.writeln(" found."); |
| 80 out.write(pluralize("warning", warnCount)); | |
| 81 out.writeln(' found.'); | |
| 82 } else { | 103 } else { |
| 83 out.writeln("No issues found."); | 104 out.writeln("No issues found"); |
| 84 } | 105 } |
| 85 } | 106 } |
| 86 } | 107 } |
| 87 | 108 |
| 88 void formatError(Map<AnalysisError, LineInfo> errorToLine, AnalysisError error
) { | 109 void formatError(Map<AnalysisError, LineInfo> errorToLine, AnalysisError error
) { |
| 89 Source source = error.source; | 110 Source source = error.source; |
| 90 LineInfo_Location location = errorToLine[error].getLocation(error.offset); | 111 LineInfo_Location location = errorToLine[error].getLocation(error.offset); |
| 91 int length = error.length; | 112 int length = error.length; |
| 92 var severity = error.errorCode.errorSeverity; | 113 var severity = error.errorCode.errorSeverity; |
| 93 if (options.machineFormat) { | 114 if (options.machineFormat) { |
| 94 if (severity == ErrorSeverity.WARNING && options.warningsAreFatal) { | 115 if (severity == ErrorSeverity.WARNING && options.warningsAreFatal) { |
| 95 severity = ErrorSeverity.ERROR; | 116 severity = ErrorSeverity.ERROR; |
| 96 } | 117 } |
| 97 out.write(severity); | 118 out.write(severity); |
| 98 out.write('|'); | 119 out.write('|'); |
| 99 out.write(error.errorCode.type); | 120 out.write(error.errorCode.type); |
| 100 out.write('|'); | 121 out.write('|'); |
| 101 out.write(error.errorCode); | 122 out.write(error.errorCode); |
| 102 out.write('|'); | 123 out.write('|'); |
| 103 out.write(escapePipe(source.fullName)); | 124 out.write(escapePipe(source.fullName)); |
| 104 out.write('|'); | 125 out.write('|'); |
| 105 out.write(location.lineNumber); | 126 out.write(location.lineNumber); |
| 106 out.write('|'); | 127 out.write('|'); |
| 107 out.write(location.columnNumber); | 128 out.write(location.columnNumber); |
| 108 out.write('|'); | 129 out.write('|'); |
| 109 out.write(length); | 130 out.write(length); |
| 110 out.write('|'); | 131 out.write('|'); |
| 111 out.write(escapePipe(error.message)); | 132 out.write(escapePipe(error.message)); |
| 112 } else { | 133 } else { |
| 134 String errorType = error.errorCode.errorSeverity.displayName; |
| 135 if (error.errorCode.type == ErrorType.HINT) { |
| 136 errorType = error.errorCode.type.displayName; |
| 137 } |
| 113 // [warning] 'foo' is not a... (/Users/.../tmp/foo.dart, line 1, col 2) | 138 // [warning] 'foo' is not a... (/Users/.../tmp/foo.dart, line 1, col 2) |
| 114 out.write('[${severity.displayName}] ${error.message} '); | 139 out.write('[$errorType] ${error.message} '); |
| 115 out.write('(${source.fullName}'); | 140 out.write('(${source.fullName}'); |
| 116 out.write(', line ${location.lineNumber}, col ${location.columnNumber})'); | 141 out.write(', line ${location.lineNumber}, col ${location.columnNumber})'); |
| 117 } | 142 } |
| 118 out.writeln(); | 143 out.writeln(); |
| 119 } | 144 } |
| 120 | 145 |
| 121 static String escapePipe(String input) { | 146 static String escapePipe(String input) { |
| 122 var result = new StringBuffer(); | 147 var result = new StringBuffer(); |
| 123 for (var c in input.codeUnits) { | 148 for (var c in input.codeUnits) { |
| 124 if (c == '\\' || c == '|') { | 149 if (c == '\\' || c == '|') { |
| 125 result.write('\\'); | 150 result.write('\\'); |
| 126 } | 151 } |
| 127 result.writeCharCode(c); | 152 result.writeCharCode(c); |
| 128 } | 153 } |
| 129 return result.toString(); | 154 return result.toString(); |
| 130 } | 155 } |
| 131 | 156 |
| 132 static String pluralize(String word, int count) { | 157 static String pluralize(String word, int count) { |
| 133 if (count == 1) { | 158 if (count == 1) { |
| 134 return word; | 159 return word; |
| 135 } else { | 160 } else { |
| 136 return word + "s"; | 161 return word + "s"; |
| 137 } | 162 } |
| 138 } | 163 } |
| 139 } | 164 } |
| OLD | NEW |