OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library analyzer.source.error_processor; |
| 6 |
| 7 import 'package:analyzer/src/generated/engine.dart'; |
| 8 import 'package:analyzer/src/generated/error.dart'; |
| 9 import 'package:analyzer/src/generated/utilities_general.dart'; |
| 10 import 'package:analyzer/src/task/options.dart'; |
| 11 import 'package:yaml/yaml.dart'; |
| 12 |
| 13 /// Error processor configuration derived from analysis (or embedder) options. |
| 14 class ErrorConfig { |
| 15 /// The processors in this config. |
| 16 final List<ErrorProcessor> processors = <ErrorProcessor>[]; |
| 17 |
| 18 /// Create an error config for the given error code map. |
| 19 /// For example: |
| 20 /// new ErrorConfig({'missing_return' : 'error'}); |
| 21 /// will create a processor config that turns `missing_return` hints into |
| 22 /// errors. |
| 23 ErrorConfig(Object codeMap) { |
| 24 _processMap(codeMap); |
| 25 } |
| 26 |
| 27 void _process(String code, Object action) { |
| 28 action = toLowerCase(action); |
| 29 code = toUpperCase(code); |
| 30 if (AnalyzerOptions.ignoreSynonyms.contains(action)) { |
| 31 processors.add(new ErrorProcessor.ignore(code)); |
| 32 } else { |
| 33 ErrorSeverity severity = _toSeverity(action); |
| 34 if (severity != null) { |
| 35 processors.add(new ErrorProcessor(code, severity)); |
| 36 } |
| 37 } |
| 38 } |
| 39 |
| 40 void _processMap(Object codes) { |
| 41 if (codes is YamlMap) { |
| 42 // TODO(pq): stop traversing nodes and unify w/ standard map handling |
| 43 codes.nodes.forEach((k, v) { |
| 44 if (k is YamlScalar && v is YamlScalar) { |
| 45 _process(k.value, v.value); |
| 46 } |
| 47 }); |
| 48 } else if (codes is Map) { |
| 49 codes.forEach((k, v) { |
| 50 if (k is String) { |
| 51 _process(k, v); |
| 52 } |
| 53 }); |
| 54 } |
| 55 } |
| 56 |
| 57 ErrorSeverity _toSeverity(String severity) { |
| 58 switch (severity) { |
| 59 case 'error': |
| 60 return ErrorSeverity.ERROR; |
| 61 case 'info': |
| 62 return ErrorSeverity.INFO; |
| 63 case 'warning': |
| 64 return ErrorSeverity.WARNING; |
| 65 } |
| 66 return null; |
| 67 } |
| 68 |
| 69 } |
| 70 |
| 71 /// Process errors by filtering or changing associated [ErrorSeverity]. |
| 72 class ErrorProcessor { |
| 73 /// The code name of the associated error. |
| 74 final String code; |
| 75 |
| 76 /// The desired severity of the processed error. |
| 77 /// |
| 78 /// If `null`, this processor will "filter" the associated error code. |
| 79 final ErrorSeverity severity; |
| 80 |
| 81 /// Create an error processor that assigns errors with this [code] the |
| 82 /// given [severity]. |
| 83 /// |
| 84 /// If [severity] is `null`, matching errors will be filtered. |
| 85 ErrorProcessor(this.code, [this.severity]); |
| 86 |
| 87 /// Create an error processor that ignores the given error by [code]. |
| 88 factory ErrorProcessor.ignore(String code) => new ErrorProcessor(code); |
| 89 |
| 90 /// Check if this processor applies to the given [error]. |
| 91 bool appliesTo(AnalysisError error) => code == error.errorCode.name; |
| 92 |
| 93 /// Return an error processor associated with this [context] for the given |
| 94 /// [error], or `null` if none is found. |
| 95 static ErrorProcessor getProcessor( |
| 96 AnalysisContext context, AnalysisError error) { |
| 97 if (context == null) { |
| 98 return null; |
| 99 } |
| 100 List<ErrorProcessor> processors = |
| 101 context.getConfigurationData(CONFIGURED_ERROR_PROCESSORS); |
| 102 return processors.firstWhere((ErrorProcessor p) => p.appliesTo(error), |
| 103 orElse: () => null); |
| 104 } |
| 105 } |
OLD | NEW |