| 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/plugin/options.dart'; |
| 8 import 'package:analyzer/source/analysis_options_provider.dart'; | 9 import 'package:analyzer/source/analysis_options_provider.dart'; |
| 9 import 'package:analyzer/src/generated/engine.dart'; | 10 import 'package:analyzer/src/generated/engine.dart'; |
| 10 import 'package:analyzer/src/generated/source.dart'; | 11 import 'package:analyzer/src/generated/source.dart'; |
| 11 import 'package:analyzer/src/task/general.dart'; | 12 import 'package:analyzer/src/task/general.dart'; |
| 12 import 'package:analyzer/task/general.dart'; | 13 import 'package:analyzer/task/general.dart'; |
| 13 import 'package:analyzer/task/model.dart'; | 14 import 'package:analyzer/task/model.dart'; |
| 14 import 'package:source_span/source_span.dart'; | 15 import 'package:source_span/source_span.dart'; |
| 15 import 'package:yaml/yaml.dart'; | 16 import 'package:yaml/yaml.dart'; |
| 16 | 17 |
| 17 /// The errors produced while parsing `.analysis_options` files. | 18 /// The errors produced while parsing `.analysis_options` files. |
| 18 /// | 19 /// |
| 19 /// The list will be empty if there were no errors, but will not be `null`. | 20 /// The list will be empty if there were no errors, but will not be `null`. |
| 20 final ListResultDescriptor<AnalysisError> ANALYSIS_OPTIONS_ERRORS = | 21 final ListResultDescriptor<AnalysisError> ANALYSIS_OPTIONS_ERRORS = |
| 21 new ListResultDescriptor<AnalysisError>( | 22 new ListResultDescriptor<AnalysisError>( |
| 22 'ANALYSIS_OPTIONS_ERRORS', AnalysisError.NO_ERRORS); | 23 'ANALYSIS_OPTIONS_ERRORS', AnalysisError.NO_ERRORS); |
| 23 | 24 |
| 24 /// Validates `analyzer` top-level options. | 25 /// Validates `analyzer` top-level options. |
| 25 class AnalyzerOptionsValidator extends TopLevelOptionValidator { | 26 class AnalyzerOptionsValidator extends TopLevelOptionValidator { |
| 26 AnalyzerOptionsValidator() | 27 AnalyzerOptionsValidator() |
| 27 : super('analyzer', const ['exclude', 'plugins', 'strong-mode']); | 28 : super('analyzer', const ['exclude', 'plugins', 'strong-mode']); |
| 28 } | 29 } |
| 29 | 30 |
| 31 /// Convenience class for composing validators. |
| 32 class CompositeValidator extends OptionsValidator { |
| 33 final List<OptionsValidator> validators; |
| 34 CompositeValidator(this.validators); |
| 35 |
| 36 @override |
| 37 void validate(ErrorReporter reporter, Map<String, YamlNode> options) => |
| 38 validators.forEach((v) => v.validate(reporter, options)); |
| 39 } |
| 40 |
| 30 /// A task that generates errors for an `.analysis_options` file. | 41 /// A task that generates errors for an `.analysis_options` file. |
| 31 class GenerateOptionsErrorsTask extends SourceBasedAnalysisTask { | 42 class GenerateOptionsErrorsTask extends SourceBasedAnalysisTask { |
| 32 /// The name of the input whose value is the content of the file. | 43 /// The name of the input whose value is the content of the file. |
| 33 static const String CONTENT_INPUT_NAME = 'CONTENT_INPUT_NAME'; | 44 static const String CONTENT_INPUT_NAME = 'CONTENT_INPUT_NAME'; |
| 34 | 45 |
| 35 /// The task descriptor describing this kind of task. | 46 /// The task descriptor describing this kind of task. |
| 36 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | 47 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( |
| 37 'GenerateOptionsErrorsTask', | 48 'GenerateOptionsErrorsTask', |
| 38 createTask, | 49 createTask, |
| 39 buildInputs, | 50 buildInputs, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 | 100 |
| 90 /// Validates `linter` top-level options. | 101 /// Validates `linter` top-level options. |
| 91 /// TODO(pq): move into `linter` package and plugin. | 102 /// TODO(pq): move into `linter` package and plugin. |
| 92 class LinterOptionsValidator extends TopLevelOptionValidator { | 103 class LinterOptionsValidator extends TopLevelOptionValidator { |
| 93 LinterOptionsValidator() : super('linter', const ['rules']); | 104 LinterOptionsValidator() : super('linter', const ['rules']); |
| 94 } | 105 } |
| 95 | 106 |
| 96 /// Validates options defined in an `.analysis_options` file. | 107 /// Validates options defined in an `.analysis_options` file. |
| 97 class OptionsFileValidator { | 108 class OptionsFileValidator { |
| 98 // TODO(pq): move to an extension point. | 109 // TODO(pq): move to an extension point. |
| 99 static final List<OptionsValidator> _validators = [ | 110 final List<OptionsValidator> _validators = [ |
| 100 new AnalyzerOptionsValidator(), new LinterOptionsValidator() | 111 new AnalyzerOptionsValidator(), |
| 112 new LinterOptionsValidator() |
| 101 ]; | 113 ]; |
| 102 | 114 |
| 103 final Source source; | 115 final Source source; |
| 104 OptionsFileValidator(this.source); | 116 OptionsFileValidator(this.source) { |
| 117 _validators.addAll(AnalysisEngine.instance.optionsPlugin.optionsValidators); |
| 118 } |
| 105 | 119 |
| 106 List<AnalysisError> validate(Map<String, YamlNode> options) { | 120 List<AnalysisError> validate(Map<String, YamlNode> options) { |
| 107 RecordingErrorListener recorder = new RecordingErrorListener(); | 121 RecordingErrorListener recorder = new RecordingErrorListener(); |
| 108 ErrorReporter reporter = new ErrorReporter(recorder, source); | 122 ErrorReporter reporter = new ErrorReporter(recorder, source); |
| 109 _validators.forEach((OptionsValidator v) => v.validate(reporter, options)); | 123 _validators.forEach((OptionsValidator v) => v.validate(reporter, options)); |
| 110 return recorder.errors; | 124 return recorder.errors; |
| 111 } | 125 } |
| 112 } | 126 } |
| 113 | 127 |
| 114 /// Validates options. | |
| 115 abstract class OptionsValidator { | |
| 116 /// Validate [options], reporting any errors to the given [reporter]. | |
| 117 void validate(ErrorReporter reporter, Map<String, YamlNode> options); | |
| 118 } | |
| 119 | |
| 120 /// Validates top-level options. For example, | 128 /// Validates top-level options. For example, |
| 121 /// plugin: | 129 /// plugin: |
| 122 /// top-level-option: true | 130 /// top-level-option: true |
| 123 class TopLevelOptionValidator extends OptionsValidator { | 131 class TopLevelOptionValidator extends OptionsValidator { |
| 124 final String pluginName; | 132 final String pluginName; |
| 125 final List<String> supportedOptions; | 133 final List<String> supportedOptions; |
| 126 | 134 |
| 127 TopLevelOptionValidator(this.pluginName, this.supportedOptions); | 135 TopLevelOptionValidator(this.pluginName, this.supportedOptions); |
| 128 @override | 136 @override |
| 129 void validate(ErrorReporter reporter, Map<String, YamlNode> options) { | 137 void validate(ErrorReporter reporter, Map<String, YamlNode> options) { |
| 130 YamlNode node = options[pluginName]; | 138 YamlNode node = options[pluginName]; |
| 131 if (node is YamlMap) { | 139 if (node is YamlMap) { |
| 132 node.nodes.forEach((k, v) { | 140 node.nodes.forEach((k, v) { |
| 133 if (k is YamlScalar) { | 141 if (k is YamlScalar) { |
| 134 if (!supportedOptions.contains(k.value)) { | 142 if (!supportedOptions.contains(k.value)) { |
| 135 reporter.reportErrorForSpan( | 143 reporter.reportErrorForSpan( |
| 136 AnalysisOptionsWarningCode.UNSUPPORTED_OPTION, | 144 AnalysisOptionsWarningCode.UNSUPPORTED_OPTION, |
| 137 k.span, | 145 k.span, |
| 138 [pluginName, k.value]); | 146 [pluginName, k.value]); |
| 139 } | 147 } |
| 140 } | 148 } |
| 141 //TODO(pq): consider an error if the node is not a Scalar. | 149 //TODO(pq): consider an error if the node is not a Scalar. |
| 142 }); | 150 }); |
| 143 } | 151 } |
| 144 } | 152 } |
| 145 } | 153 } |
| OLD | NEW |