| 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'; |
| 11 import 'package:analyzer/source/analysis_options_provider.dart'; | 11 import 'package:analyzer/source/analysis_options_provider.dart'; |
| 12 import 'package:analyzer/source/error_processor.dart'; | 12 import 'package:analyzer/source/error_processor.dart'; |
| 13 import 'package:analyzer/src/generated/engine.dart'; | 13 import 'package:analyzer/src/generated/engine.dart'; |
| 14 import 'package:analyzer/src/generated/java_engine.dart'; | 14 import 'package:analyzer/src/generated/java_engine.dart'; |
| 15 import 'package:analyzer/src/generated/source.dart'; | 15 import 'package:analyzer/src/generated/source.dart'; |
| 16 import 'package:analyzer/src/generated/utilities_general.dart'; | 16 import 'package:analyzer/src/generated/utilities_general.dart'; |
| 17 import 'package:analyzer/src/lint/config.dart'; |
| 18 import 'package:analyzer/src/lint/linter.dart'; |
| 19 import 'package:analyzer/src/lint/registry.dart'; |
| 17 import 'package:analyzer/src/task/general.dart'; | 20 import 'package:analyzer/src/task/general.dart'; |
| 18 import 'package:analyzer/src/util/yaml.dart'; | 21 import 'package:analyzer/src/util/yaml.dart'; |
| 19 import 'package:analyzer/task/general.dart'; | 22 import 'package:analyzer/task/general.dart'; |
| 20 import 'package:analyzer/task/model.dart'; | 23 import 'package:analyzer/task/model.dart'; |
| 21 import 'package:source_span/source_span.dart'; | 24 import 'package:source_span/source_span.dart'; |
| 22 import 'package:yaml/yaml.dart'; | 25 import 'package:yaml/yaml.dart'; |
| 23 | 26 |
| 24 /// The errors produced while parsing an analysis options file. | 27 /// The errors produced while parsing an analysis options file. |
| 25 /// | 28 /// |
| 26 /// The list will be empty if there were no errors, but will not be `null`. | 29 /// The list will be empty if there were no errors, but will not be `null`. |
| 27 final ListResultDescriptor<AnalysisError> ANALYSIS_OPTIONS_ERRORS = | 30 final ListResultDescriptor<AnalysisError> ANALYSIS_OPTIONS_ERRORS = |
| 28 new ListResultDescriptor<AnalysisError>( | 31 new ListResultDescriptor<AnalysisError>( |
| 29 'ANALYSIS_OPTIONS_ERRORS', AnalysisError.NO_ERRORS); | 32 'ANALYSIS_OPTIONS_ERRORS', AnalysisError.NO_ERRORS); |
| 30 | 33 |
| 31 final _OptionsProcessor _processor = new _OptionsProcessor(); | 34 final _OptionsProcessor _processor = new _OptionsProcessor(); |
| 32 | 35 |
| 33 void applyToAnalysisOptions( | 36 void applyToAnalysisOptions( |
| 34 AnalysisOptionsImpl options, Map<String, Object> optionMap) { | 37 AnalysisOptionsImpl options, Map<String, Object> optionMap) { |
| 35 _processor.applyToAnalysisOptions(options, optionMap); | 38 _processor.applyToAnalysisOptions(options, optionMap); |
| 36 } | 39 } |
| 37 | 40 |
| 38 /// Configure this [context] based on configuration details specified in | |
| 39 /// the given [options]. If [options] is `null`, default values are applied. | |
| 40 void configureContextOptions( | |
| 41 AnalysisContext context, Map<String, Object> options) => | |
| 42 _processor.configure(context, options); | |
| 43 | |
| 44 /// `analyzer` analysis options constants. | 41 /// `analyzer` analysis options constants. |
| 45 class AnalyzerOptions { | 42 class AnalyzerOptions { |
| 46 static const String analyzer = 'analyzer'; | 43 static const String analyzer = 'analyzer'; |
| 47 static const String enableAssertInitializer = 'enableAssertInitializer'; | 44 static const String enableAssertInitializer = 'enableAssertInitializer'; |
| 48 static const String enableAsync = 'enableAsync'; | 45 static const String enableAsync = 'enableAsync'; |
| 49 static const String enableGenericMethods = 'enableGenericMethods'; | 46 static const String enableGenericMethods = 'enableGenericMethods'; |
| 50 static const String enableInitializingFormalAccess = | 47 static const String enableInitializingFormalAccess = |
| 51 'enableInitializingFormalAccess'; | 48 'enableInitializingFormalAccess'; |
| 52 static const String enableStrictCallChecks = 'enableStrictCallChecks'; | 49 static const String enableStrictCallChecks = 'enableStrictCallChecks'; |
| 53 static const String enableSuperMixins = 'enableSuperMixins'; | 50 static const String enableSuperMixins = 'enableSuperMixins'; |
| (...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 _applyProcessors(options, filters); | 503 _applyProcessors(options, filters); |
| 507 | 504 |
| 508 // Process language options. | 505 // Process language options. |
| 509 var language = analyzer[AnalyzerOptions.language]; | 506 var language = analyzer[AnalyzerOptions.language]; |
| 510 _applyLanguageOptions(options, language); | 507 _applyLanguageOptions(options, language); |
| 511 | 508 |
| 512 // Process excludes. | 509 // Process excludes. |
| 513 var excludes = analyzer[AnalyzerOptions.exclude]; | 510 var excludes = analyzer[AnalyzerOptions.exclude]; |
| 514 _applyExcludes(options, excludes); | 511 _applyExcludes(options, excludes); |
| 515 } | 512 } |
| 516 } | |
| 517 | 513 |
| 518 /// Configure [context] based on the given [options] (which can be `null` | 514 LintConfig config = parseConfig(optionMap); |
| 519 /// to restore [defaults]). | 515 if (config != null) { |
| 520 void configure(AnalysisContext context, Map<String, Object> options) { | 516 Iterable<LintRule> lintRules = Registry.ruleRegistry.enabled(config); |
| 521 if (options == null) { | 517 if (lintRules.isNotEmpty) { |
| 522 options = defaults; | 518 options.lint = true; |
| 523 } | 519 options.lintRules = lintRules.toList(); |
| 524 | |
| 525 var analyzer = options[AnalyzerOptions.analyzer]; | |
| 526 if (analyzer is Map) { | |
| 527 // Set strong mode (default is false). | |
| 528 var strongMode = analyzer[AnalyzerOptions.strong_mode]; | |
| 529 setStrongMode(context, strongMode); | |
| 530 | |
| 531 // Set filters. | |
| 532 var filters = analyzer[AnalyzerOptions.errors]; | |
| 533 setProcessors(context, filters); | |
| 534 | |
| 535 // Process language options. | |
| 536 var language = analyzer[AnalyzerOptions.language]; | |
| 537 setLanguageOptions(context, language); | |
| 538 | |
| 539 // Process excludes. | |
| 540 var excludes = analyzer[AnalyzerOptions.exclude]; | |
| 541 setExcludes(context, excludes); | |
| 542 } | |
| 543 } | |
| 544 | |
| 545 void setExcludes(AnalysisContext context, Object excludes) { | |
| 546 if (excludes is YamlList) { | |
| 547 List<String> excludeList = toStringList(excludes); | |
| 548 if (excludeList != null) { | |
| 549 AnalysisOptionsImpl options = | |
| 550 new AnalysisOptionsImpl.from(context.analysisOptions); | |
| 551 options.excludePatterns = excludeList; | |
| 552 context.analysisOptions = options; | |
| 553 } | 520 } |
| 554 } | 521 } |
| 555 } | 522 } |
| 556 | |
| 557 void setLanguageOption( | |
| 558 AnalysisContext context, Object feature, Object value) { | |
| 559 if (feature == AnalyzerOptions.enableAssertInitializer) { | |
| 560 if (isTrue(value)) { | |
| 561 AnalysisOptionsImpl options = | |
| 562 new AnalysisOptionsImpl.from(context.analysisOptions); | |
| 563 options.enableAssertInitializer = true; | |
| 564 context.analysisOptions = options; | |
| 565 } | |
| 566 } | |
| 567 if (feature == AnalyzerOptions.enableStrictCallChecks) { | |
| 568 if (isTrue(value)) { | |
| 569 AnalysisOptionsImpl options = | |
| 570 new AnalysisOptionsImpl.from(context.analysisOptions); | |
| 571 options.enableStrictCallChecks = true; | |
| 572 context.analysisOptions = options; | |
| 573 } | |
| 574 } | |
| 575 if (feature == AnalyzerOptions.enableSuperMixins) { | |
| 576 if (isTrue(value)) { | |
| 577 AnalysisOptionsImpl options = | |
| 578 new AnalysisOptionsImpl.from(context.analysisOptions); | |
| 579 options.enableSuperMixins = true; | |
| 580 context.analysisOptions = options; | |
| 581 } | |
| 582 } | |
| 583 } | |
| 584 | |
| 585 void setLanguageOptions(AnalysisContext context, Object configs) { | |
| 586 if (configs is YamlMap) { | |
| 587 configs.nodes.forEach((k, v) { | |
| 588 if (k is YamlScalar && v is YamlScalar) { | |
| 589 String feature = k.value?.toString(); | |
| 590 setLanguageOption(context, feature, v.value); | |
| 591 } | |
| 592 }); | |
| 593 } else if (configs is Map) { | |
| 594 configs.forEach((k, v) => setLanguageOption(context, k, v)); | |
| 595 } | |
| 596 } | |
| 597 | |
| 598 void setProcessors(AnalysisContext context, Object codes) { | |
| 599 ErrorConfig config = new ErrorConfig(codes); | |
| 600 AnalysisOptionsImpl options = | |
| 601 new AnalysisOptionsImpl.from(context.analysisOptions); | |
| 602 options.errorProcessors = config.processors; | |
| 603 context.analysisOptions = options; | |
| 604 } | |
| 605 | |
| 606 void setStrongMode(AnalysisContext context, Object strongMode) { | |
| 607 if (strongMode is Map) { | |
| 608 AnalysisOptionsImpl options = | |
| 609 new AnalysisOptionsImpl.from(context.analysisOptions); | |
| 610 _applyStrongOptions(options, strongMode); | |
| 611 context.analysisOptions = options; | |
| 612 } else { | |
| 613 strongMode = strongMode is bool ? strongMode : false; | |
| 614 if (context.analysisOptions.strongMode != strongMode) { | |
| 615 AnalysisOptionsImpl options = | |
| 616 new AnalysisOptionsImpl.from(context.analysisOptions); | |
| 617 options.strongMode = strongMode; | |
| 618 context.analysisOptions = options; | |
| 619 } | |
| 620 } | |
| 621 } | |
| 622 | 523 |
| 623 void _applyExcludes(AnalysisOptionsImpl options, Object excludes) { | 524 void _applyExcludes(AnalysisOptionsImpl options, Object excludes) { |
| 624 if (excludes is YamlList) { | 525 if (excludes is YamlList) { |
| 625 List<String> excludeList = toStringList(excludes); | 526 List<String> excludeList = toStringList(excludes); |
| 626 if (excludeList != null) { | 527 if (excludeList != null) { |
| 627 options.excludePatterns = excludeList; | 528 options.excludePatterns = excludeList; |
| 628 } | 529 } |
| 629 } | 530 } |
| 630 } | 531 } |
| 631 | 532 |
| 632 void _applyLanguageOption( | 533 void _applyLanguageOption( |
| 633 AnalysisOptionsImpl options, Object feature, Object value) { | 534 AnalysisOptionsImpl options, Object feature, Object value) { |
| 634 bool boolValue = toBool(value); | 535 bool boolValue = toBool(value); |
| 635 if (boolValue != null) { | 536 if (boolValue != null) { |
| 636 if (feature == AnalyzerOptions.enableAssertInitializer) { | 537 if (feature == AnalyzerOptions.enableAssertInitializer) { |
| 637 options.enableAssertInitializer = boolValue; | 538 options.enableAssertInitializer = boolValue; |
| 539 } else if (feature == AnalyzerOptions.enableStrictCallChecks) { |
| 540 options.enableStrictCallChecks = true; |
| 638 } else if (feature == AnalyzerOptions.enableSuperMixins) { | 541 } else if (feature == AnalyzerOptions.enableSuperMixins) { |
| 639 options.enableSuperMixins = boolValue; | 542 options.enableSuperMixins = boolValue; |
| 640 } | 543 } |
| 641 } | 544 } |
| 642 } | 545 } |
| 643 | 546 |
| 644 void _applyLanguageOptions(AnalysisOptionsImpl options, Object configs) { | 547 void _applyLanguageOptions(AnalysisOptionsImpl options, Object configs) { |
| 645 if (configs is YamlMap) { | 548 if (configs is YamlMap) { |
| 646 configs.nodes.forEach((key, value) { | 549 configs.nodes.forEach((key, value) { |
| 647 if (key is YamlScalar && value is YamlScalar) { | 550 if (key is YamlScalar && value is YamlScalar) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 682 } | 585 } |
| 683 }); | 586 }); |
| 684 } else if (config is Map) { | 587 } else if (config is Map) { |
| 685 options.strongMode = true; | 588 options.strongMode = true; |
| 686 config.forEach((k, v) => _applyStrongModeOption(options, k, v)); | 589 config.forEach((k, v) => _applyStrongModeOption(options, k, v)); |
| 687 } else { | 590 } else { |
| 688 options.strongMode = config is bool ? config : false; | 591 options.strongMode = config is bool ? config : false; |
| 689 } | 592 } |
| 690 } | 593 } |
| 691 } | 594 } |
| OLD | NEW |