| 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/error/error.dart'; |
| 8 import 'package:analyzer/src/error/codes.dart'; |
| 9 import 'package:analyzer/src/generated/engine.dart'; |
| 10 import 'package:analyzer/src/generated/utilities_general.dart'; |
| 11 import 'package:analyzer/src/task/options.dart' |
| 12 show CONFIGURED_ERROR_PROCESSORS; |
| 13 import 'package:analyzer/src/task/options.dart'; |
| 14 import 'package:yaml/yaml.dart'; |
| 15 |
| 16 /// String identifiers mapped to associated severities. |
| 17 const Map<String, ErrorSeverity> severityMap = const { |
| 18 'error': ErrorSeverity.ERROR, |
| 19 'info': ErrorSeverity.INFO, |
| 20 'warning': ErrorSeverity.WARNING |
| 21 }; |
| 22 |
| 23 /// Error processor configuration derived from analysis (or embedder) options. |
| 24 class ErrorConfig { |
| 25 /// The processors in this config. |
| 26 final List<ErrorProcessor> processors = <ErrorProcessor>[]; |
| 27 |
| 28 /// Create an error config for the given error code map. |
| 29 /// For example: |
| 30 /// new ErrorConfig({'missing_return' : 'error'}); |
| 31 /// will create a processor config that turns `missing_return` hints into |
| 32 /// errors. |
| 33 ErrorConfig(Object codeMap) { |
| 34 _processMap(codeMap); |
| 35 } |
| 36 |
| 37 void _process(String code, Object action) { |
| 38 action = toLowerCase(action); |
| 39 code = toUpperCase(code); |
| 40 if (AnalyzerOptions.ignoreSynonyms.contains(action)) { |
| 41 processors.add(new ErrorProcessor.ignore(code)); |
| 42 } else { |
| 43 ErrorSeverity severity = _toSeverity(action); |
| 44 if (severity != null) { |
| 45 processors.add(new ErrorProcessor(code, severity)); |
| 46 } |
| 47 } |
| 48 } |
| 49 |
| 50 void _processMap(Object codes) { |
| 51 if (codes is YamlMap) { |
| 52 // TODO(pq): stop traversing nodes and unify w/ standard map handling |
| 53 codes.nodes.forEach((k, v) { |
| 54 if (k is YamlScalar && v is YamlScalar) { |
| 55 _process(k.value, v.value); |
| 56 } |
| 57 }); |
| 58 } else if (codes is Map) { |
| 59 codes.forEach((k, v) { |
| 60 if (k is String) { |
| 61 _process(k, v); |
| 62 } |
| 63 }); |
| 64 } |
| 65 } |
| 66 |
| 67 ErrorSeverity _toSeverity(String severity) => severityMap[severity]; |
| 68 } |
| 69 |
| 70 /// Process errors by filtering or changing associated [ErrorSeverity]. |
| 71 class ErrorProcessor { |
| 72 /// The code name of the associated error. |
| 73 final String code; |
| 74 |
| 75 /// The desired severity of the processed error. |
| 76 /// |
| 77 /// If `null`, this processor will "filter" the associated error code. |
| 78 final ErrorSeverity severity; |
| 79 |
| 80 /// Create an error processor that assigns errors with this [code] the |
| 81 /// given [severity]. |
| 82 /// |
| 83 /// If [severity] is `null`, matching errors will be filtered. |
| 84 ErrorProcessor(this.code, [this.severity]); |
| 85 |
| 86 /// Create an error processor that ignores the given error by [code]. |
| 87 factory ErrorProcessor.ignore(String code) => new ErrorProcessor(code); |
| 88 |
| 89 /// Check if this processor applies to the given [error]. |
| 90 bool appliesTo(AnalysisError error) => code == error.errorCode.name; |
| 91 |
| 92 /// Return an error processor associated with this [context] for the given |
| 93 /// [error], or `null` if none is found. |
| 94 static ErrorProcessor getProcessor( |
| 95 AnalysisContext context, AnalysisError error) { |
| 96 if (context == null) { |
| 97 return null; |
| 98 } |
| 99 |
| 100 // Let the user configure how specific errors are processed. |
| 101 List<ErrorProcessor> processors = |
| 102 context.getConfigurationData(CONFIGURED_ERROR_PROCESSORS); |
| 103 |
| 104 // Give strong mode a chance to upgrade it. |
| 105 if (context.analysisOptions.strongMode) { |
| 106 processors = processors.toList(); |
| 107 processors.add(_StrongModeTypeErrorProcessor.instance); |
| 108 } |
| 109 return processors.firstWhere((ErrorProcessor p) => p.appliesTo(error), |
| 110 orElse: () => null); |
| 111 } |
| 112 } |
| 113 |
| 114 /// In strong mode, this upgrades static type warnings to errors. |
| 115 class _StrongModeTypeErrorProcessor implements ErrorProcessor { |
| 116 static final instance = new _StrongModeTypeErrorProcessor(); |
| 117 |
| 118 // TODO(rnystrom): As far as I know, this is only used to implement |
| 119 // appliesTo(). Consider making it private in ErrorProcessor if possible. |
| 120 String get code => throw new UnsupportedError( |
| 121 "_StrongModeTypeErrorProcessor is not specific to an error code."); |
| 122 |
| 123 /// In strong mode, type warnings are upgraded to errors. |
| 124 ErrorSeverity get severity => ErrorSeverity.ERROR; |
| 125 |
| 126 /// Check if this processor applies to the given [error]. |
| 127 bool appliesTo(AnalysisError error) { |
| 128 ErrorCode errorCode = error.errorCode; |
| 129 if (errorCode is StaticTypeWarningCode) { |
| 130 return true; |
| 131 } |
| 132 if (errorCode is StaticWarningCode) { |
| 133 return errorCode.isStrongModeError; |
| 134 } |
| 135 return false; |
| 136 } |
| 137 } |
| OLD | NEW |