Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: pkg/analyzer/lib/src/task/options.dart

Issue 1419673006: More options file validation (and API iteration). (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer/lib/src/generated/error.dart ('k') | pkg/analyzer/test/src/task/options_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 import 'package:yaml/yaml.dart';
16 16
17 /// The errors produced while parsing `.analysis_options` files. 17 /// The errors produced while parsing `.analysis_options` files.
18 /// 18 ///
19 /// 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`.
20 final ListResultDescriptor<AnalysisError> ANALYSIS_OPTIONS_ERRORS = 20 final ListResultDescriptor<AnalysisError> ANALYSIS_OPTIONS_ERRORS =
21 new ListResultDescriptor<AnalysisError>( 21 new ListResultDescriptor<AnalysisError>(
22 'ANALYSIS_OPTIONS_ERRORS', AnalysisError.NO_ERRORS); 22 'ANALYSIS_OPTIONS_ERRORS', AnalysisError.NO_ERRORS);
23 23
24 /// Validates `analyzer` top-level options.
25 class AnalyzerOptionsValidator extends TopLevelOptionValidator {
26 AnalyzerOptionsValidator()
27 : super('analyzer', const ['exclude', 'plugins', 'strong-mode']);
28 }
29
24 /// A task that generates errors for an `.analysis_options` file. 30 /// A task that generates errors for an `.analysis_options` file.
25 class GenerateOptionsErrorsTask extends SourceBasedAnalysisTask { 31 class GenerateOptionsErrorsTask extends SourceBasedAnalysisTask {
26 /// The name of the input whose value is the content of the file. 32 /// The name of the input whose value is the content of the file.
27 static const String CONTENT_INPUT_NAME = 'CONTENT_INPUT_NAME'; 33 static const String CONTENT_INPUT_NAME = 'CONTENT_INPUT_NAME';
28 34
29 /// The task descriptor describing this kind of task. 35 /// The task descriptor describing this kind of task.
30 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( 36 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor(
31 'GenerateOptionsErrorsTask', 37 'GenerateOptionsErrorsTask',
32 createTask, 38 createTask,
33 buildInputs, 39 buildInputs,
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 /// given [target]. 80 /// given [target].
75 static Map<String, TaskInput> buildInputs(Source source) => 81 static Map<String, TaskInput> buildInputs(Source source) =>
76 <String, TaskInput>{CONTENT_INPUT_NAME: CONTENT.of(source)}; 82 <String, TaskInput>{CONTENT_INPUT_NAME: CONTENT.of(source)};
77 83
78 /// Create a task based on the given [target] in the given [context]. 84 /// Create a task based on the given [target] in the given [context].
79 static GenerateOptionsErrorsTask createTask( 85 static GenerateOptionsErrorsTask createTask(
80 AnalysisContext context, AnalysisTarget target) => 86 AnalysisContext context, AnalysisTarget target) =>
81 new GenerateOptionsErrorsTask(context, target); 87 new GenerateOptionsErrorsTask(context, target);
82 } 88 }
83 89
90 /// Validates `linter` top-level options.
91 /// TODO(pq): move into `linter` package and plugin.
pquitslund 2015/10/22 17:48:26 Or maybe to a server-hosted linter plugin. (In an
92 class LinterOptionsValidator extends TopLevelOptionValidator {
93 LinterOptionsValidator() : super('linter', const ['rules']);
94 }
95
84 /// Validates options defined in an `.analysis_options` file. 96 /// Validates options defined in an `.analysis_options` file.
85 class OptionsFileValidator { 97 class OptionsFileValidator {
86 // TODO(pq): consider an extension point. 98 // TODO(pq): move to an extension point.
87 static final List<OptionsValidator> _validators = [ 99 static final List<OptionsValidator> _validators = [
88 new AnalyzerOptionsValidator() 100 new AnalyzerOptionsValidator(), new LinterOptionsValidator()
89 ]; 101 ];
90 102
91 final Source source; 103 final Source source;
92 OptionsFileValidator(this.source); 104 OptionsFileValidator(this.source);
93 105
94 List<AnalysisError> validate(Map<String, YamlNode> options) { 106 List<AnalysisError> validate(Map<String, YamlNode> options) {
95 List<AnalysisError> errors = <AnalysisError>[]; 107 RecordingErrorListener recorder = new RecordingErrorListener();
96 _validators.forEach( 108 ErrorReporter reporter = new ErrorReporter(recorder, source);
97 (OptionsValidator v) => errors.addAll(v.validate(source, options))); 109 _validators.forEach((OptionsValidator v) => v.validate(reporter, options));
98 return errors; 110 return recorder.errors;
99 } 111 }
100 } 112 }
101 113
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. 114 /// Validates options.
110 abstract class OptionsValidator { 115 abstract class OptionsValidator {
111 List<AnalysisError> validate(Source source, Map<String, YamlNode> options); 116 /// Validate [options], reporting any errors to the given [reporter].
117 void validate(ErrorReporter reporter, Map<String, YamlNode> options);
112 } 118 }
113 119
114 /// Validates `analyzer` options. 120 /// Validates top-level options. For example,
115 class AnalyzerOptionsValidator extends OptionsValidator { 121 /// plugin:
116 static const List<String> _supportedOptions = const [ 122 /// top-level-option: true
117 'exclude', 123 class TopLevelOptionValidator extends OptionsValidator {
118 'strong-mode' 124 final String pluginName;
119 ]; 125 final List<String> supportedOptions;
Brian Wilkerson 2015/10/22 20:00:15 Might be interesting to eventually make this a map
pquitslund 2015/10/22 20:19:26 Good idea!
120 126
127 TopLevelOptionValidator(this.pluginName, this.supportedOptions);
121 @override 128 @override
122 List<AnalysisError> validate(Source source, Map<String, YamlNode> options) { 129 void validate(ErrorReporter reporter, Map<String, YamlNode> options) {
123 List<AnalysisError> errors = <AnalysisError>[]; 130 YamlNode node = options[pluginName];
124 YamlNode node = options['analyzer'];
125 if (node is YamlMap) { 131 if (node is YamlMap) {
126 node.nodes.forEach((k, v) { 132 node.nodes.forEach((k, v) {
127 if (k is YamlScalar) { 133 if (k is YamlScalar) {
Brian Wilkerson 2015/10/22 20:00:15 Is it an error if the key is not a scalar value? (
pquitslund 2015/10/22 20:19:26 Done.
128 if (!_supportedOptions.contains(k.value)) { 134 if (!supportedOptions.contains(k.value)) {
129 errors.add(_unsupportedOption(source, k, 'analyzer')); 135 reporter.reportErrorForSpan(
136 AnalysisOptionsWarningCode.UNSUPPORTED_OPTION,
137 k.span,
138 [pluginName, k.value]);
130 } 139 }
131 } 140 }
132 }); 141 });
133 } 142 }
134 return errors;
135 } 143 }
136 } 144 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/error.dart ('k') | pkg/analyzer/test/src/task/options_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698