@@ -6,8 +6,8 @@ import 'package:analyzer/src/task/options.dart';
66import 'package:analyzer/src/util/yaml.dart' ;
77import 'package:yaml/yaml.dart' ;
88
9- /// Parses [optionsMap] into a [LintConfig] , returning the config , or `null` if
10- /// [optionsMap] does not have `linter` map.
9+ /// Parses [optionsMap] into a list of [RuleConfig] s , returning them , or `null`
10+ /// if [optionsMap] does not have `linter` map.
1111List <RuleConfig >? parseLintRuleConfigs (YamlMap optionsMap) {
1212 var options = optionsMap.valueAt ('linter' );
1313 // Quick check of basic contract.
@@ -24,29 +24,16 @@ List<RuleConfig>? parseLintRuleConfigs(YamlMap optionsMap) {
2424 return null ;
2525}
2626
27- /// Returns [scalar] as a [bool] , if it can be parsed as one.
28- bool ? _asBool (YamlNode scalar) {
29- var value = scalar is YamlScalar ? scalar.valueOrThrow : scalar;
30- return switch (value) {
31- bool () => value,
32- 'true' => true ,
33- 'false' => false ,
34- _ => null ,
35- };
36- }
37-
38- /// Returns [scalar] as a [String] , if it can be parsed as one.
39- String ? _asString (Object scalar) {
40- var value = scalar is YamlScalar ? scalar.value : scalar;
41- return value is String ? value : null ;
42- }
43-
44- Map <String , bool > _parseArgs (YamlNode args) {
45- var enabled = _asBool (args);
46- if (enabled != null ) {
47- return {'enabled' : enabled};
27+ RuleConfig ? _parseRuleConfig (dynamic configKey, YamlNode configNode,
28+ {String ? group}) {
29+ // For example: `{unnecessary_getters: false}`.
30+ if (configKey case YamlScalar (value: String ruleName)) {
31+ if (configNode case YamlScalar (value: bool isEnabled)) {
32+ return RuleConfig ._(name: ruleName, isEnabled: isEnabled, group: group);
33+ }
4834 }
49- return {};
35+
36+ return null ;
5037}
5138
5239/// Returns the [RuleConfig] s that are parsed from [value] , which can be either
@@ -59,56 +46,65 @@ List<RuleConfig> _ruleConfigs(YamlNode value) {
5946 // - camel_case_types
6047 // ```
6148 if (value is YamlList ) {
62- return [
63- for (var rule in value.nodes)
64- RuleConfig (name: _asString (rule), args: {'enabled' : true }),
65- ];
66- }
67-
68- // style_guide: {unnecessary_getters: false, camel_case_types: true}
69- if (value is YamlMap ) {
7049 var ruleConfigs = < RuleConfig > [];
71- value.nodes.cast <Object , YamlNode >().forEach ((key, value) {
72- // For example: `{unnecessary_getters: false}`.
73- var valueAsBool = _asBool (value);
74- if (valueAsBool != null ) {
75- ruleConfigs.add (RuleConfig (
76- name: _asString (key),
77- args: {'enabled' : valueAsBool},
78- ));
79- }
80-
81- // style_guide: {unnecessary_getters: false, camel_case_types: true}
82- if (value is YamlMap ) {
83- value.nodes.cast <Object , YamlNode >().forEach ((rule, args) {
84- // TODO(brianwilkerson): verify format.
85- // For example: `unnecessary_getters: false`.
86- ruleConfigs.add (RuleConfig (
87- name: _asString (rule),
88- args: _parseArgs (args),
89- group: _asString (key),
90- ));
91- });
50+ for (var ruleNode in value.nodes) {
51+ if (ruleNode case YamlScalar (value: String ruleName)) {
52+ ruleConfigs.add (RuleConfig ._(name: ruleName, isEnabled: true ));
9253 }
93- });
54+ }
9455 return ruleConfigs;
9556 }
9657
97- return const [];
58+ if (value is ! YamlMap ) {
59+ return const [];
60+ }
61+
62+ var ruleConfigs = < RuleConfig > [];
63+ value.nodes.forEach ((configKey, configValue) {
64+ if (configKey case YamlScalar (value: String configName)) {
65+ var ruleConfig = _parseRuleConfig (configKey, configValue);
66+ if (ruleConfig != null ) {
67+ ruleConfigs.add (ruleConfig);
68+ return ;
69+ }
70+
71+ if (configValue is ! YamlMap ) {
72+ return ;
73+ }
74+ // For example:
75+ //
76+ // ```yaml
77+ // style_guide: {unnecessary_getters: false, camel_case_types: true}
78+ // ```
79+ configValue.nodes.forEach ((ruleName, ruleValue) {
80+ var ruleConfig =
81+ _parseRuleConfig (ruleName, ruleValue, group: configName);
82+ if (ruleConfig != null ) {
83+ ruleConfigs.add (ruleConfig);
84+ return ;
85+ }
86+ });
87+ }
88+ });
89+ return ruleConfigs;
9890}
9991
10092/// The configuration of a single lint rule within an analysis options file.
10193class RuleConfig {
102- final Map <String , bool > args;
94+ /// Whether this rule is enabled or disabled in this configuration.
95+ final bool isEnabled;
96+
97+ /// The name of the group under which this configuration is found.
10398 final String ? group;
104- final String ? name;
10599
106- RuleConfig ({required this .name, required this .args, this .group});
100+ /// The name of the rule.
101+ final String name;
102+
103+ RuleConfig ._({required this .name, required this .isEnabled, this .group});
107104
108105 /// Returns whether [ruleName] is disabled in this configuration.
109- bool disables (String ruleName) =>
110- ruleName == name && args['enabled' ] == false ;
106+ bool disables (String ruleName) => ruleName == name && ! isEnabled;
111107
112108 /// Returns whether [ruleName] is enabled in this configuration.
113- bool enables (String ruleName) => ruleName == name && args[ 'enabled' ] == true ;
109+ bool enables (String ruleName) => ruleName == name && isEnabled ;
114110}
0 commit comments