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' | 5 import 'package:analyzer/analyzer.dart' show AnalysisError, ErrorSeverity; |
6 show AnalysisError, ErrorSeverity, StaticWarningCode; | |
7 import 'package:analyzer/source/error_processor.dart' show ErrorProcessor; | 6 import 'package:analyzer/source/error_processor.dart' show ErrorProcessor; |
8 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; | 7 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; |
9 import 'package:path/path.dart' as path; | 8 import 'package:path/path.dart' as path; |
10 | 9 |
11 // TODO(jmesserly): this code was taken from analyzer_cli. | 10 // TODO(jmesserly): this code was taken from analyzer_cli. |
12 // It really should be in some common place so we can share it. | 11 // It really should be in some common place so we can share it. |
13 // TODO(jmesserly): this shouldn't depend on `context` but we need it to compute | 12 // TODO(jmesserly): this shouldn't depend on `context` but we need it to compute |
14 // `errorSeverity` due to some APIs that need fixing. | 13 // `errorSeverity` due to some APIs that need fixing. |
15 void sortErrors(AnalysisContext context, List<AnalysisError> errors) { | 14 void sortErrors(AnalysisContext context, List<AnalysisError> errors) { |
16 errors.sort((AnalysisError error1, AnalysisError error2) { | 15 errors.sort((AnalysisError error1, AnalysisError error2) { |
(...skipping 29 matching lines...) Expand all Loading... |
46 // [warning] 'foo' is not a... (/Users/.../tmp/foo.dart, line 1, col 2) | 45 // [warning] 'foo' is not a... (/Users/.../tmp/foo.dart, line 1, col 2) |
47 return (new StringBuffer() | 46 return (new StringBuffer() |
48 ..write('[${severity.displayName}] ') | 47 ..write('[${severity.displayName}] ') |
49 ..write(error.message) | 48 ..write(error.message) |
50 ..write(' (${path.prettyUri(error.source.uri)}') | 49 ..write(' (${path.prettyUri(error.source.uri)}') |
51 ..write(', line ${location.lineNumber}, col ${location.columnNumber})')) | 50 ..write(', line ${location.lineNumber}, col ${location.columnNumber})')) |
52 .toString(); | 51 .toString(); |
53 } | 52 } |
54 | 53 |
55 ErrorSeverity errorSeverity(AnalysisContext context, AnalysisError error) { | 54 ErrorSeverity errorSeverity(AnalysisContext context, AnalysisError error) { |
56 var code = error.errorCode; | |
57 if (code is StaticWarningCode) { | |
58 // TODO(jmesserly): many more warnings need to be promoted for soundness. | |
59 // Also code generation will blow up finding null types/elements for many | |
60 // of these, or we rely on them to produce valid optimizations. | |
61 switch (code.name) { | |
62 case 'AMBIGUOUS_IMPORT': | |
63 case 'ARGUMENT_TYPE_NOT_ASSIGNABLE': | |
64 case 'ARGUMENT_TYPE_NOT_ASSIGNABLE_STATIC_WARNING': | |
65 case 'ASSIGNMENT_TO_CONST': | |
66 case 'ASSIGNMENT_TO_FINAL': | |
67 case 'ASSIGNMENT_TO_FINAL_NO_SETTER': | |
68 case 'ASSIGNMENT_TO_FUNCTION': | |
69 case 'ASSIGNMENT_TO_METHOD': | |
70 case 'ASSIGNMENT_TO_TYPE': | |
71 case 'CASE_BLOCK_NOT_TERMINATED': | |
72 case 'CAST_TO_NON_TYPE': | |
73 case 'CONCRETE_CLASS_WITH_ABSTRACT_MEMBER': | |
74 case 'CONFLICTING_DART_IMPORT': | |
75 case 'CONFLICTING_INSTANCE_GETTER_AND_SUPERCLASS_MEMBER': | |
76 case 'CONFLICTING_INSTANCE_METHOD_SETTER': | |
77 case 'CONFLICTING_INSTANCE_SETTER_AND_SUPERCLASS_MEMBER': | |
78 case 'CONFLICTING_STATIC_GETTER_AND_INSTANCE_SETTER': | |
79 case 'CONFLICTING_STATIC_SETTER_AND_INSTANCE_MEMBER': | |
80 case 'CONST_WITH_ABSTRACT_CLASS': | |
81 case 'CONST_WITH_INVALID_TYPE_PARAMETERS': | |
82 case 'EQUAL_KEYS_IN_MAP': | |
83 case 'EXPORT_DUPLICATED_LIBRARY_NAMED': | |
84 case 'EXTRA_POSITIONAL_ARGUMENTS': | |
85 case 'FIELD_INITIALIZED_IN_INITIALIZER_AND_DECLARATION': | |
86 case 'FIELD_INITIALIZER_NOT_ASSIGNABLE': | |
87 case 'FIELD_INITIALIZING_FORMAL_NOT_ASSIGNABLE': | |
88 case 'FINAL_INITIALIZED_IN_DECLARATION_AND_CONSTRUCTOR': | |
89 case 'FUNCTION_WITHOUT_CALL': | |
90 case 'IMPORT_DUPLICATED_LIBRARY_NAMED': | |
91 case 'IMPORT_OF_NON_LIBRARY': | |
92 case 'INCONSISTENT_METHOD_INHERITANCE_GETTER_AND_METHOD': | |
93 case 'INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_STATIC': | |
94 case 'INVALID_GETTER_OVERRIDE_RETURN_TYPE': | |
95 case 'INVALID_METHOD_OVERRIDE_NAMED_PARAM_TYPE': | |
96 case 'INVALID_METHOD_OVERRIDE_NORMAL_PARAM_TYPE': | |
97 case 'INVALID_METHOD_OVERRIDE_OPTIONAL_PARAM_TYPE': | |
98 case 'INVALID_METHOD_OVERRIDE_RETURN_TYPE': | |
99 case 'INVALID_METHOD_OVERRIDE_TYPE_PARAMETERS': | |
100 case 'INVALID_METHOD_OVERRIDE_TYPE_PARAMETER_BOUND': | |
101 case 'INVALID_OVERRIDE_DIFFERENT_DEFAULT_VALUES_NAMED': | |
102 case 'INVALID_OVERRIDE_DIFFERENT_DEFAULT_VALUES_POSITIONAL': | |
103 case 'INVALID_OVERRIDE_NAMED': | |
104 case 'INVALID_OVERRIDE_POSITIONAL': | |
105 case 'INVALID_OVERRIDE_REQUIRED': | |
106 case 'INVALID_SETTER_OVERRIDE_NORMAL_PARAM_TYPE': | |
107 case 'LIST_ELEMENT_TYPE_NOT_ASSIGNABLE': | |
108 case 'MAP_KEY_TYPE_NOT_ASSIGNABLE': | |
109 case 'MAP_VALUE_TYPE_NOT_ASSIGNABLE': | |
110 case 'NEW_WITH_ABSTRACT_CLASS': | |
111 case 'NEW_WITH_INVALID_TYPE_PARAMETERS': | |
112 case 'NEW_WITH_NON_TYPE': | |
113 case 'NEW_WITH_UNDEFINED_CONSTRUCTOR': | |
114 case 'NEW_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT': | |
115 case 'NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_FIVE_PLUS': | |
116 case 'NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_FOUR': | |
117 case 'NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_ONE': | |
118 case 'NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_THREE': | |
119 case 'NON_ABSTRACT_CLASS_INHERITS_ABSTRACT_MEMBER_TWO': | |
120 case 'NON_TYPE_IN_CATCH_CLAUSE': | |
121 case 'NOT_A_TYPE': | |
122 case 'NOT_ENOUGH_REQUIRED_ARGUMENTS': | |
123 case 'PART_OF_DIFFERENT_LIBRARY': | |
124 case 'REDIRECT_TO_INVALID_FUNCTION_TYPE': | |
125 case 'REDIRECT_TO_INVALID_RETURN_TYPE': | |
126 case 'REDIRECT_TO_MISSING_CONSTRUCTOR': | |
127 case 'REDIRECT_TO_NON_CLASS': | |
128 case 'STATIC_ACCESS_TO_INSTANCE_MEMBER': | |
129 case 'SWITCH_EXPRESSION_NOT_ASSIGNABLE': | |
130 case 'TYPE_ANNOTATION_DEFERRED_CLASS': | |
131 case 'TYPE_PARAMETER_REFERENCED_BY_STATIC': | |
132 case 'TYPE_TEST_WITH_NON_TYPE': | |
133 case 'TYPE_TEST_WITH_UNDEFINED_NAME': | |
134 case 'UNDEFINED_CLASS': | |
135 case 'UNDEFINED_CLASS_BOOLEAN': | |
136 case 'UNDEFINED_GETTER': | |
137 case 'UNDEFINED_GETTER_STATIC_WARNING': | |
138 case 'UNDEFINED_IDENTIFIER': | |
139 case 'UNDEFINED_NAMED_PARAMETER': | |
140 case 'UNDEFINED_SETTER': | |
141 case 'UNDEFINED_SETTER_STATIC_WARNING': | |
142 case 'UNDEFINED_STATIC_METHOD_OR_GETTER': | |
143 case 'UNDEFINED_SUPER_GETTER': | |
144 case 'UNDEFINED_SUPER_GETTER_STATIC_WARNING': | |
145 case 'UNDEFINED_SUPER_SETTER': | |
146 case 'UNDEFINED_SUPER_SETTER_STATIC_WARNING': | |
147 case 'WRONG_NUMBER_OF_TYPE_ARGUMENTS': | |
148 return ErrorSeverity.ERROR; | |
149 | |
150 // All of the following ones are okay as warnings. | |
151 case 'FINAL_NOT_INITIALIZED': | |
152 case 'FINAL_NOT_INITIALIZED_CONSTRUCTOR_': | |
153 | |
154 // We don't rely on these for override checking, AFAIK. | |
155 case 'MISMATCHED_GETTER_AND_SETTER_TYPES': | |
156 case 'MISMATCHED_GETTER_AND_SETTER_TYPES_FROM_SUPERTYPE': | |
157 | |
158 case 'MISSING_ENUM_CONSTANT_IN_SWITCH': | |
159 case 'MIXED_RETURN_TYPES': | |
160 | |
161 // TODO(jmesserly): I think codegen already handles this for []=. | |
162 // Though we could simplify it if we didn't need to handle this case. | |
163 case 'NON_VOID_RETURN_FOR_OPERATOR': | |
164 | |
165 case 'NON_VOID_RETURN_FOR_SETTER': | |
166 case 'RETURN_WITHOUT_VALUE': | |
167 case 'STATIC_WARNING': | |
168 case 'VOID_RETURN_FOR_GETTER': | |
169 break; | |
170 } | |
171 } | |
172 | |
173 // TODO(jmesserly): this Analyzer API totally bonkers, but it's what | 55 // TODO(jmesserly): this Analyzer API totally bonkers, but it's what |
174 // analyzer_cli and server use. | 56 // analyzer_cli and server use. |
175 // | 57 // |
176 // Among the issues with ErrorProcessor.getProcessor: | 58 // Among the issues with ErrorProcessor.getProcessor: |
177 // * it needs to be called per-error, so it's a performance trap. | 59 // * it needs to be called per-error, so it's a performance trap. |
178 // * it can return null | 60 // * it can return null |
179 // * using AnalysisError directly is now suspect, it's a correctness trap | 61 // * using AnalysisError directly is now suspect, it's a correctness trap |
180 // * it requires an AnalysisContext | 62 // * it requires an AnalysisContext |
181 return ErrorProcessor.getProcessor(context, error)?.severity ?? | 63 return ErrorProcessor.getProcessor(context, error)?.severity ?? |
182 error.errorCode.errorSeverity; | 64 error.errorCode.errorSeverity; |
183 } | 65 } |
OLD | NEW |