| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 analyzer.src.task.options; | 5 library analyzer.src.task.options; |
| 6 | 6 |
| 7 import 'package:analyzer/analyzer.dart'; | 7 import 'package:analyzer/analyzer.dart'; |
| 8 import 'package:analyzer/source/analysis_options_provider.dart'; | 8 import 'package:analyzer/source/analysis_options_provider.dart'; |
| 9 import 'package:analyzer/src/generated/engine.dart'; | 9 import 'package:analyzer/src/generated/engine.dart'; |
| 10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
| 11 import 'package:analyzer/src/task/general.dart'; | 11 import 'package:analyzer/src/task/general.dart'; |
| 12 import 'package:analyzer/task/general.dart'; | 12 import 'package:analyzer/task/general.dart'; |
| 13 import 'package:analyzer/task/model.dart'; | 13 import 'package:analyzer/task/model.dart'; |
| 14 import 'package:source_span/source_span.dart'; | 14 import 'package:source_span/source_span.dart'; |
| 15 import 'package:yaml/yaml.dart'; |
| 15 | 16 |
| 16 /// The errors produced while parsing `.analysis_options` files. | 17 /// The errors produced while parsing `.analysis_options` files. |
| 17 /// | 18 /// |
| 18 /// The list will be empty if there were no errors, but will not be `null`. | 19 /// The list will be empty if there were no errors, but will not be `null`. |
| 19 final ListResultDescriptor<AnalysisError> ANALYSIS_OPTIONS_ERRORS = | 20 final ListResultDescriptor<AnalysisError> ANALYSIS_OPTIONS_ERRORS = |
| 20 new ListResultDescriptor<AnalysisError>( | 21 new ListResultDescriptor<AnalysisError>( |
| 21 'ANALYSIS_OPTIONS_ERRORS', AnalysisError.NO_ERRORS); | 22 'ANALYSIS_OPTIONS_ERRORS', AnalysisError.NO_ERRORS); |
| 22 | 23 |
| 23 /// A task that generates errors for an `.analysis_options` file. | 24 /// A task that generates errors for an `.analysis_options` file. |
| 24 class GenerateOptionsErrorsTask extends SourceBasedAnalysisTask { | 25 class GenerateOptionsErrorsTask extends SourceBasedAnalysisTask { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 42 | 43 |
| 43 Source get source => target.source; | 44 Source get source => target.source; |
| 44 | 45 |
| 45 @override | 46 @override |
| 46 void internalPerform() { | 47 void internalPerform() { |
| 47 String content = getRequiredInput(CONTENT_INPUT_NAME); | 48 String content = getRequiredInput(CONTENT_INPUT_NAME); |
| 48 | 49 |
| 49 List<AnalysisError> errors = <AnalysisError>[]; | 50 List<AnalysisError> errors = <AnalysisError>[]; |
| 50 | 51 |
| 51 try { | 52 try { |
| 52 optionsProvider.getOptionsFromString(content); | 53 Map<String, YamlNode> options = |
| 54 optionsProvider.getOptionsFromString(content); |
| 55 errors.addAll(_validate(options)); |
| 53 } on OptionsFormatException catch (e) { | 56 } on OptionsFormatException catch (e) { |
| 54 SourceSpan span = e.span; | 57 SourceSpan span = e.span; |
| 55 var error = new AnalysisError(source, span.start.column + 1, span.length, | 58 var error = new AnalysisError(source, span.start.column + 1, span.length, |
| 56 AnalysisOptionsErrorCode.PARSE_ERROR, [e.message]); | 59 AnalysisOptionsErrorCode.PARSE_ERROR, [e.message]); |
| 57 errors.add(error); | 60 errors.add(error); |
| 58 } | 61 } |
| 59 | 62 |
| 60 // | 63 // |
| 61 // Record outputs. | 64 // Record outputs. |
| 62 // | 65 // |
| 63 outputs[ANALYSIS_OPTIONS_ERRORS] = errors; | 66 outputs[ANALYSIS_OPTIONS_ERRORS] = errors; |
| 64 } | 67 } |
| 65 | 68 |
| 69 List<AnalysisError> _validate(Map<String, YamlNode> options) => |
| 70 new OptionsFileValidator(source).validate(options); |
| 71 |
| 66 /// Return a map from the names of the inputs of this kind of task to the | 72 /// Return a map from the names of the inputs of this kind of task to the |
| 67 /// task input descriptors describing those inputs for a task with the | 73 /// task input descriptors describing those inputs for a task with the |
| 68 /// given [target]. | 74 /// given [target]. |
| 69 static Map<String, TaskInput> buildInputs(Source source) => | 75 static Map<String, TaskInput> buildInputs(Source source) => |
| 70 <String, TaskInput>{CONTENT_INPUT_NAME: CONTENT.of(source)}; | 76 <String, TaskInput>{CONTENT_INPUT_NAME: CONTENT.of(source)}; |
| 71 | 77 |
| 72 /// Create a task based on the given [target] in the given [context]. | 78 /// Create a task based on the given [target] in the given [context]. |
| 73 static GenerateOptionsErrorsTask createTask( | 79 static GenerateOptionsErrorsTask createTask( |
| 74 AnalysisContext context, AnalysisTarget target) => | 80 AnalysisContext context, AnalysisTarget target) => |
| 75 new GenerateOptionsErrorsTask(context, target); | 81 new GenerateOptionsErrorsTask(context, target); |
| 76 } | 82 } |
| 83 |
| 84 /// Validates options defined in an `.analysis_options` file. |
| 85 class OptionsFileValidator { |
| 86 // TODO(pq): consider an extension point. |
| 87 static final List<OptionsValidator> _validators = [ |
| 88 new AnalyzerOptionsValidator() |
| 89 ]; |
| 90 |
| 91 final Source source; |
| 92 OptionsFileValidator(this.source); |
| 93 |
| 94 List<AnalysisError> validate(Map<String, YamlNode> options) { |
| 95 List<AnalysisError> errors = <AnalysisError>[]; |
| 96 _validators.forEach( |
| 97 (OptionsValidator v) => errors.addAll(v.validate(source, options))); |
| 98 return errors; |
| 99 } |
| 100 } |
| 101 |
| 102 AnalysisError _unsupportedOption( |
| 103 Source source, YamlScalar key, String pluginName) { |
| 104 SourceSpan span = key.span; |
| 105 return new AnalysisError(source, span.start.column + 1, span.length, |
| 106 AnalysisOptionsWarningCode.UNSUPPORTED_OPTION, [pluginName, key.value]); |
| 107 } |
| 108 |
| 109 /// Validates options. |
| 110 abstract class OptionsValidator { |
| 111 List<AnalysisError> validate(Source source, Map<String, YamlNode> options); |
| 112 } |
| 113 |
| 114 /// Validates `analyzer` options. |
| 115 class AnalyzerOptionsValidator extends OptionsValidator { |
| 116 static const List<String> _supportedOptions = const [ |
| 117 'exclude', |
| 118 'strong-mode' |
| 119 ]; |
| 120 |
| 121 @override |
| 122 List<AnalysisError> validate(Source source, Map<String, YamlNode> options) { |
| 123 List<AnalysisError> errors = <AnalysisError>[]; |
| 124 YamlNode node = options['analyzer']; |
| 125 if (node is YamlMap) { |
| 126 node.nodes.forEach((k, v) { |
| 127 if (k is YamlScalar) { |
| 128 if (!_supportedOptions.contains(k.value)) { |
| 129 errors.add(_unsupportedOption(source, k, 'analyzer')); |
| 130 } |
| 131 } |
| 132 }); |
| 133 } |
| 134 return errors; |
| 135 } |
| 136 } |
| OLD | NEW |