| 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'; |
| 11 import 'package:analyzer/src/analyzer_impl.dart'; |
| 11 | 12 |
| 12 /// Returns `true` if [AnalysisError] should be printed. | 13 /// Returns `true` if [AnalysisError] should be printed. |
| 13 typedef bool _ErrorFilter(AnalysisError error); | 14 typedef bool _ErrorFilter(AnalysisError error); |
| 14 | 15 |
| 15 /// Allows any [AnalysisError]. | 16 /// Allows any [AnalysisError]. |
| 16 bool _anyError(AnalysisError error) => true; | 17 bool _anyError(AnalysisError error) => true; |
| 17 | 18 |
| 18 /** | 19 /** |
| 19 * Helper for formatting [AnalysisError]s. | 20 * Helper for formatting [AnalysisError]s. |
| 20 * The two format options are a user consumable format and a machine consumable
format. | 21 * The two format options are a user consumable format and a machine consumable
format. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 33 for (AnalysisError error in errorInfo.errors) { | 34 for (AnalysisError error in errorInfo.errors) { |
| 34 if (errorFilter(error)) { | 35 if (errorFilter(error)) { |
| 35 errors.add(error); | 36 errors.add(error); |
| 36 errorToLine[error] = errorInfo.lineInfo; | 37 errorToLine[error] = errorInfo.lineInfo; |
| 37 } | 38 } |
| 38 } | 39 } |
| 39 } | 40 } |
| 40 // sort errors | 41 // sort errors |
| 41 errors.sort((AnalysisError error1, AnalysisError error2) { | 42 errors.sort((AnalysisError error1, AnalysisError error2) { |
| 42 // severity | 43 // severity |
| 43 int compare = error2.errorCode.errorSeverity.compareTo(error1.errorCode.er
rorSeverity); | 44 ErrorSeverity severity1 = AnalyzerImpl.computeSeverity( |
| 45 error1, options.enableTypeChecks); |
| 46 ErrorSeverity severity2 = AnalyzerImpl.computeSeverity( |
| 47 error2, options.enableTypeChecks); |
| 48 int compare = severity2.compareTo(severity1); |
| 44 if (compare != 0) { | 49 if (compare != 0) { |
| 45 return compare; | 50 return compare; |
| 46 } | 51 } |
| 47 // path | 52 // path |
| 48 compare = Comparable.compare(error1.source.fullName.toLowerCase(), error2.
source.fullName.toLowerCase()); | 53 compare = Comparable.compare(error1.source.fullName.toLowerCase(), error2.
source.fullName.toLowerCase()); |
| 49 if (compare != 0) { | 54 if (compare != 0) { |
| 50 return compare; | 55 return compare; |
| 51 } | 56 } |
| 52 // offset | 57 // offset |
| 53 return error1.offset - error2.offset; | 58 return error1.offset - error2.offset; |
| 54 }); | 59 }); |
| 55 // format errors | 60 // format errors |
| 56 int errorCount = 0; | 61 int errorCount = 0; |
| 57 int warnCount = 0; | 62 int warnCount = 0; |
| 58 int hintCount = 0; | 63 int hintCount = 0; |
| 59 for (AnalysisError error in errors) { | 64 for (AnalysisError error in errors) { |
| 60 var severity = error.errorCode.errorSeverity; | 65 ErrorSeverity severity = AnalyzerImpl.computeSeverity( |
| 66 error, options.enableTypeChecks); |
| 61 if (severity == ErrorSeverity.ERROR) { | 67 if (severity == ErrorSeverity.ERROR) { |
| 62 errorCount++; | 68 errorCount++; |
| 63 } else if (severity == ErrorSeverity.WARNING) { | 69 } else if (severity == ErrorSeverity.WARNING) { |
| 64 if (options.warningsAreFatal) { | 70 if (options.warningsAreFatal) { |
| 65 errorCount++; | 71 errorCount++; |
| 66 } else { | 72 } else { |
| 67 if (error.errorCode.type == ErrorType.HINT) { | 73 if (error.errorCode.type == ErrorType.HINT) { |
| 68 hintCount++; | 74 hintCount++; |
| 69 } else { | 75 } else { |
| 70 warnCount++; | 76 warnCount++; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 } else { | 118 } else { |
| 113 out.writeln("No issues found"); | 119 out.writeln("No issues found"); |
| 114 } | 120 } |
| 115 } | 121 } |
| 116 } | 122 } |
| 117 | 123 |
| 118 void formatError(Map<AnalysisError, LineInfo> errorToLine, AnalysisError error
) { | 124 void formatError(Map<AnalysisError, LineInfo> errorToLine, AnalysisError error
) { |
| 119 Source source = error.source; | 125 Source source = error.source; |
| 120 LineInfo_Location location = errorToLine[error].getLocation(error.offset); | 126 LineInfo_Location location = errorToLine[error].getLocation(error.offset); |
| 121 int length = error.length; | 127 int length = error.length; |
| 122 var severity = error.errorCode.errorSeverity; | 128 ErrorSeverity severity = AnalyzerImpl.computeSeverity( |
| 129 error, options.enableTypeChecks); |
| 123 if (options.machineFormat) { | 130 if (options.machineFormat) { |
| 124 if (severity == ErrorSeverity.WARNING && options.warningsAreFatal) { | 131 if (severity == ErrorSeverity.WARNING && options.warningsAreFatal) { |
| 125 severity = ErrorSeverity.ERROR; | 132 severity = ErrorSeverity.ERROR; |
| 126 } | 133 } |
| 127 out.write(severity); | 134 out.write(severity); |
| 128 out.write('|'); | 135 out.write('|'); |
| 129 out.write(error.errorCode.type); | 136 out.write(error.errorCode.type); |
| 130 out.write('|'); | 137 out.write('|'); |
| 131 out.write(error.errorCode); | 138 out.write(error.errorCode); |
| 132 out.write('|'); | 139 out.write('|'); |
| 133 out.write(escapePipe(source.fullName)); | 140 out.write(escapePipe(source.fullName)); |
| 134 out.write('|'); | 141 out.write('|'); |
| 135 out.write(location.lineNumber); | 142 out.write(location.lineNumber); |
| 136 out.write('|'); | 143 out.write('|'); |
| 137 out.write(location.columnNumber); | 144 out.write(location.columnNumber); |
| 138 out.write('|'); | 145 out.write('|'); |
| 139 out.write(length); | 146 out.write(length); |
| 140 out.write('|'); | 147 out.write('|'); |
| 141 out.write(escapePipe(error.message)); | 148 out.write(escapePipe(error.message)); |
| 142 } else { | 149 } else { |
| 143 String errorType = error.errorCode.errorSeverity.displayName; | 150 String errorType = severity.displayName; |
| 144 if (error.errorCode.type == ErrorType.HINT) { | 151 if (error.errorCode.type == ErrorType.HINT) { |
| 145 errorType = error.errorCode.type.displayName; | 152 errorType = error.errorCode.type.displayName; |
| 146 } | 153 } |
| 147 // [warning] 'foo' is not a... (/Users/.../tmp/foo.dart, line 1, col 2) | 154 // [warning] 'foo' is not a... (/Users/.../tmp/foo.dart, line 1, col 2) |
| 148 out.write('[$errorType] ${error.message} '); | 155 out.write('[$errorType] ${error.message} '); |
| 149 out.write('(${source.fullName}'); | 156 out.write('(${source.fullName}'); |
| 150 out.write(', line ${location.lineNumber}, col ${location.columnNumber})'); | 157 out.write(', line ${location.lineNumber}, col ${location.columnNumber})'); |
| 151 } | 158 } |
| 152 out.writeln(); | 159 out.writeln(); |
| 153 } | 160 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 164 } | 171 } |
| 165 | 172 |
| 166 static String pluralize(String word, int count) { | 173 static String pluralize(String word, int count) { |
| 167 if (count == 1) { | 174 if (count == 1) { |
| 168 return word; | 175 return word; |
| 169 } else { | 176 } else { |
| 170 return word + "s"; | 177 return word + "s"; |
| 171 } | 178 } |
| 172 } | 179 } |
| 173 } | 180 } |
| OLD | NEW |