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 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:analyzer/analyzer.dart'; | 9 import 'package:analyzer/analyzer.dart'; |
10 import 'package:analyzer/plugin/options.dart'; | 10 import 'package:analyzer/plugin/options.dart'; |
(...skipping 12 matching lines...) Expand all Loading... |
23 /// The errors produced while parsing `.analysis_options` files. | 23 /// The errors produced while parsing `.analysis_options` files. |
24 /// | 24 /// |
25 /// The list will be empty if there were no errors, but will not be `null`. | 25 /// The list will be empty if there were no errors, but will not be `null`. |
26 final ListResultDescriptor<AnalysisError> ANALYSIS_OPTIONS_ERRORS = | 26 final ListResultDescriptor<AnalysisError> ANALYSIS_OPTIONS_ERRORS = |
27 new ListResultDescriptor<AnalysisError>( | 27 new ListResultDescriptor<AnalysisError>( |
28 'ANALYSIS_OPTIONS_ERRORS', AnalysisError.NO_ERRORS); | 28 'ANALYSIS_OPTIONS_ERRORS', AnalysisError.NO_ERRORS); |
29 | 29 |
30 final _OptionsProcessor _processor = new _OptionsProcessor(); | 30 final _OptionsProcessor _processor = new _OptionsProcessor(); |
31 | 31 |
32 /// Configure this [context] based on configuration details specified in | 32 /// Configure this [context] based on configuration details specified in |
33 /// the given [options]. | 33 /// the given [options]. If [options] is `null`, default values are applied. |
34 void configureContextOptions( | 34 void configureContextOptions( |
35 AnalysisContext context, Map<String, YamlNode> options) => | 35 AnalysisContext context, Map<String, Object> options) => |
36 _processor.configure(context, options); | 36 _processor.configure(context, options); |
37 | 37 |
38 /// `analyzer` analysis options constants. | 38 /// `analyzer` analysis options constants. |
39 class AnalyzerOptions { | 39 class AnalyzerOptions { |
40 static const String analyzer = 'analyzer'; | 40 static const String analyzer = 'analyzer'; |
41 static const String enableGenericMethods = 'enableGenericMethods'; | 41 static const String enableGenericMethods = 'enableGenericMethods'; |
42 static const String enableSuperMixins = 'enableSuperMixins'; | 42 static const String enableSuperMixins = 'enableSuperMixins'; |
43 static const String errors = 'errors'; | 43 static const String errors = 'errors'; |
44 static const String exclude = 'exclude'; | 44 static const String exclude = 'exclude'; |
45 static const String language = 'language'; | 45 static const String language = 'language'; |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 | 377 |
378 /// An error-builder that knows about `true` and `false` legal values. | 378 /// An error-builder that knows about `true` and `false` legal values. |
379 class TrueOrFalseValueErrorBuilder extends ErrorBuilder { | 379 class TrueOrFalseValueErrorBuilder extends ErrorBuilder { |
380 TrueOrFalseValueErrorBuilder() : super(AnalyzerOptions.trueOrFalse); | 380 TrueOrFalseValueErrorBuilder() : super(AnalyzerOptions.trueOrFalse); |
381 @override | 381 @override |
382 AnalysisOptionsWarningCode get pluralProposalCode => | 382 AnalysisOptionsWarningCode get pluralProposalCode => |
383 AnalysisOptionsWarningCode.UNSUPPORTED_VALUE; | 383 AnalysisOptionsWarningCode.UNSUPPORTED_VALUE; |
384 } | 384 } |
385 | 385 |
386 class _OptionsProcessor { | 386 class _OptionsProcessor { |
387 void configure(AnalysisContext context, Map<String, YamlNode> options) { | 387 static final Map<String, Object> defaults = {'analyzer': {}}; |
| 388 |
| 389 /// Configure [context] based on the given [options] (which can be `null` |
| 390 /// to restore [defaults]). |
| 391 void configure(AnalysisContext context, Map<String, Object> options) { |
388 if (options == null) { | 392 if (options == null) { |
389 return; | 393 options = defaults; |
390 } | 394 } |
391 | 395 |
392 var analyzer = options[AnalyzerOptions.analyzer]; | 396 var analyzer = options[AnalyzerOptions.analyzer]; |
393 if (analyzer is! Map) { | 397 if (analyzer is! Map) { |
394 return; | 398 return; |
395 } | 399 } |
396 | 400 |
397 // Set strong mode (default is false). | 401 // Set strong mode (default is false). |
398 var strongMode = analyzer[AnalyzerOptions.strong_mode]; | 402 var strongMode = analyzer[AnalyzerOptions.strong_mode]; |
399 setStrongMode(context, strongMode); | 403 setStrongMode(context, strongMode); |
400 | 404 |
401 // Set filters. | 405 // Set filters. |
402 var filters = analyzer[AnalyzerOptions.errors]; | 406 var filters = analyzer[AnalyzerOptions.errors]; |
403 setFilters(context, filters); | 407 setFilters(context, filters); |
404 | 408 |
405 // Process language options. | 409 // Process language options. |
406 var language = analyzer[AnalyzerOptions.language]; | 410 var language = analyzer[AnalyzerOptions.language]; |
407 setLanguageOptions(context, language); | 411 setLanguageOptions(context, language); |
408 } | 412 } |
409 | 413 |
| 414 ErrorFilter parseFilter(String code, Object enable) { |
| 415 enable = toLowerCase(enable); |
| 416 if (AnalyzerOptions.ignoreSynonyms.contains(enable)) { |
| 417 // Case-insensitive. |
| 418 code = toUpperCase(code); |
| 419 return ((AnalysisError error) => error.errorCode.name == code); |
| 420 } |
| 421 } |
| 422 |
410 void setFilters(AnalysisContext context, Object codes) { | 423 void setFilters(AnalysisContext context, Object codes) { |
411 List<ErrorFilter> filters = <ErrorFilter>[]; | 424 List<ErrorFilter> filters = <ErrorFilter>[]; |
412 // If codes are enumerated, collect them as filters; else leave filters | 425 // If codes are enumerated, collect them as filters; else leave filters |
413 // empty to overwrite previous value. | 426 // empty to overwrite previous value. |
414 if (codes is YamlMap) { | 427 if (codes is YamlMap) { |
415 String value; | 428 String value; |
| 429 // TODO(pq): stop traversing nodes and unify w/ standard map handling |
416 codes.nodes.forEach((k, v) { | 430 codes.nodes.forEach((k, v) { |
417 if (k is YamlScalar && v is YamlScalar) { | 431 if (k is YamlScalar && v is YamlScalar) { |
418 value = toLowerCase(v.value); | 432 ErrorFilter filter = parseFilter(k.value, v.value); |
419 if (AnalyzerOptions.ignoreSynonyms.contains(value)) { | 433 if (filter != null) { |
420 // Case-insensitive. | 434 filters.add(filter); |
421 String code = toUpperCase(k.value); | |
422 filters.add((AnalysisError error) => error.errorCode.name == code); | |
423 } | 435 } |
424 } | 436 } |
425 }); | 437 }); |
| 438 } else if (codes is Map) { |
| 439 codes.forEach((k, v) { |
| 440 if (k is String) { |
| 441 ErrorFilter filter = parseFilter(k, v); |
| 442 if (filter != null) { |
| 443 filters.add(filter); |
| 444 } |
| 445 } |
| 446 }); |
426 } | 447 } |
427 context.setConfigurationData(CONFIGURED_ERROR_FILTERS, filters); | 448 context.setConfigurationData(CONFIGURED_ERROR_FILTERS, filters); |
428 } | 449 } |
429 | 450 |
430 void setLanguageOption( | 451 void setLanguageOption( |
431 AnalysisContext context, Object feature, Object value) { | 452 AnalysisContext context, Object feature, Object value) { |
432 if (feature == AnalyzerOptions.enableSuperMixins) { | 453 if (feature == AnalyzerOptions.enableSuperMixins) { |
433 if (isTrue(value)) { | 454 if (isTrue(value)) { |
434 AnalysisOptionsImpl options = | 455 AnalysisOptionsImpl options = |
435 new AnalysisOptionsImpl.from(context.analysisOptions); | 456 new AnalysisOptionsImpl.from(context.analysisOptions); |
(...skipping 27 matching lines...) Expand all Loading... |
463 void setStrongMode(AnalysisContext context, Object strongMode) { | 484 void setStrongMode(AnalysisContext context, Object strongMode) { |
464 bool strong = strongMode is bool ? strongMode : false; | 485 bool strong = strongMode is bool ? strongMode : false; |
465 if (context.analysisOptions.strongMode != strong) { | 486 if (context.analysisOptions.strongMode != strong) { |
466 AnalysisOptionsImpl options = | 487 AnalysisOptionsImpl options = |
467 new AnalysisOptionsImpl.from(context.analysisOptions); | 488 new AnalysisOptionsImpl.from(context.analysisOptions); |
468 options.strongMode = strong; | 489 options.strongMode = strong; |
469 context.analysisOptions = options; | 490 context.analysisOptions = options; |
470 } | 491 } |
471 } | 492 } |
472 } | 493 } |
OLD | NEW |