OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'package:analyzer/analyzer.dart' show AnalysisError, ErrorSeverity; | 5 import 'package:analyzer/analyzer.dart' show AnalysisError, ErrorSeverity; |
6 import 'package:analyzer/source/error_processor.dart' show ErrorProcessor; | 6 import 'package:analyzer/source/error_processor.dart' show ErrorProcessor; |
7 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; | 7 import 'package:analyzer/src/generated/engine.dart' show AnalysisOptions; |
| 8 import 'package:analyzer/src/generated/source.dart'; |
8 import 'package:path/path.dart' as path; | 9 import 'package:path/path.dart' as path; |
9 | 10 |
10 // TODO(jmesserly): this code was taken from analyzer_cli. | 11 /// TODO(jmesserly): this code was taken from analyzer_cli. |
11 // It really should be in some common place so we can share it. | 12 /// It really should be in some common place so we can share it. |
12 // TODO(jmesserly): this shouldn't depend on `context` but we need it to compute | 13 /// TODO(jmesserly): this shouldn't depend on `context` but we need it to comput
e |
13 // `errorSeverity` due to some APIs that need fixing. | 14 /// `errorSeverity` due to some APIs that need fixing. |
14 void sortErrors(AnalysisContext context, List<AnalysisError> errors) { | 15 void sortErrors(AnalysisOptions analysisOptions, List<AnalysisError> errors) { |
15 errors.sort((AnalysisError error1, AnalysisError error2) { | 16 errors.sort((AnalysisError error1, AnalysisError error2) { |
16 // severity | 17 // severity |
17 var severity1 = errorSeverity(context, error1); | 18 var severity1 = errorSeverity(analysisOptions, error1); |
18 var severity2 = errorSeverity(context, error2); | 19 var severity2 = errorSeverity(analysisOptions, error2); |
19 int compare = severity2.compareTo(severity1); | 20 int compare = severity2.compareTo(severity1); |
20 if (compare != 0) return compare; | 21 if (compare != 0) return compare; |
21 | 22 |
22 // path | 23 // path |
23 compare = Comparable.compare(error1.source.fullName.toLowerCase(), | 24 compare = Comparable.compare(error1.source.fullName.toLowerCase(), |
24 error2.source.fullName.toLowerCase()); | 25 error2.source.fullName.toLowerCase()); |
25 if (compare != 0) return compare; | 26 if (compare != 0) return compare; |
26 | 27 |
27 // offset | 28 // offset |
28 compare = error1.offset - error2.offset; | 29 compare = error1.offset - error2.offset; |
29 if (compare != 0) return compare; | 30 if (compare != 0) return compare; |
30 | 31 |
31 // compare message, in worst case. | 32 // compare message, in worst case. |
32 return error1.message.compareTo(error2.message); | 33 return error1.message.compareTo(error2.message); |
33 }); | 34 }); |
34 } | 35 } |
35 | 36 |
36 // TODO(jmesserly): this was from analyzer_cli, we should factor it differently. | 37 /// TODO(jmesserly): this was from analyzer_cli, we should factor it differently
. |
37 String formatError(AnalysisContext context, AnalysisError error) { | 38 String formatError( |
38 var severity = errorSeverity(context, error); | 39 AnalysisOptions analysisOptions, LineInfo lineInfo, AnalysisError error) { |
| 40 var severity = errorSeverity(analysisOptions, error); |
39 // Skip hints, some like TODOs are not useful. | 41 // Skip hints, some like TODOs are not useful. |
40 if (severity.ordinal <= ErrorSeverity.INFO.ordinal) return null; | 42 if (severity.ordinal <= ErrorSeverity.INFO.ordinal) return null; |
41 | 43 |
42 var lineInfo = context.computeLineInfo(error.source); | |
43 var location = lineInfo.getLocation(error.offset); | 44 var location = lineInfo.getLocation(error.offset); |
44 | 45 |
45 // [warning] 'foo' is not a... (/Users/.../tmp/foo.dart, line 1, col 2) | 46 // [warning] 'foo' is not a... (/Users/.../tmp/foo.dart, line 1, col 2) |
46 return (new StringBuffer() | 47 return (new StringBuffer() |
47 ..write('[${severity.displayName}] ') | 48 ..write('[${severity.displayName}] ') |
48 ..write(error.message) | 49 ..write(error.message) |
49 ..write(' (${path.prettyUri(error.source.uri)}') | 50 ..write(' (${path.prettyUri(error.source.uri)}') |
50 ..write(', line ${location.lineNumber}, col ${location.columnNumber})')) | 51 ..write(', line ${location.lineNumber}, col ${location.columnNumber})')) |
51 .toString(); | 52 .toString(); |
52 } | 53 } |
53 | 54 |
54 ErrorSeverity errorSeverity(AnalysisContext context, AnalysisError error) { | 55 ErrorSeverity errorSeverity( |
| 56 AnalysisOptions analysisOptions, AnalysisError error) { |
55 // TODO(jmesserly): this Analyzer API totally bonkers, but it's what | 57 // TODO(jmesserly): this Analyzer API totally bonkers, but it's what |
56 // analyzer_cli and server use. | 58 // analyzer_cli and server use. |
57 // | 59 // |
58 // Among the issues with ErrorProcessor.getProcessor: | 60 // Among the issues with ErrorProcessor.getProcessor: |
59 // * it needs to be called per-error, so it's a performance trap. | 61 // * it needs to be called per-error, so it's a performance trap. |
60 // * it can return null | 62 // * it can return null |
61 // * using AnalysisError directly is now suspect, it's a correctness trap | 63 // * using AnalysisError directly is now suspect, it's a correctness trap |
62 // * it requires an AnalysisContext | 64 return ErrorProcessor.getProcessor(analysisOptions, error)?.severity ?? |
63 return ErrorProcessor | |
64 .getProcessor(context.analysisOptions, error) | |
65 ?.severity ?? | |
66 error.errorCode.errorSeverity; | 65 error.errorCode.errorSeverity; |
67 } | 66 } |
OLD | NEW |