Skip to content

Commit 03d7cc4

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Remove internal LintConfig class; static elements can be top-level.
This just simplifies the internal parsing code that parses out lint rule configurations from YAML. Change-Id: I7eb83835cff25f99b79dad9446db5215dce126fa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391700 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 37ae078 commit 03d7cc4

File tree

3 files changed

+96
-98
lines changed

3 files changed

+96
-98
lines changed

pkg/analyzer/lib/src/lint/config.dart

Lines changed: 67 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,96 +12,89 @@ List<RuleConfig>? parseLintRuleConfigs(YamlMap optionsMap) {
1212
var options = optionsMap.valueAt('linter');
1313
// Quick check of basic contract.
1414
if (options is YamlMap) {
15-
return LintConfig.parseMap(options).ruleConfigs;
16-
}
17-
18-
return null;
19-
}
20-
21-
/// The configuration of lint rules within an analysis options file.
22-
class LintConfig {
23-
final List<RuleConfig> ruleConfigs;
24-
25-
factory LintConfig.parseMap(YamlMap yaml) {
2615
var ruleConfigs = <RuleConfig>[];
27-
var rulesNode = yaml.valueAt(AnalyzerOptions.rules);
16+
var rulesNode = options.valueAt(AnalyzerOptions.rules);
2817
if (rulesNode != null) {
2918
ruleConfigs.addAll(_ruleConfigs(rulesNode));
3019
}
3120

32-
return LintConfig._(ruleConfigs);
21+
return ruleConfigs;
3322
}
3423

35-
LintConfig._(this.ruleConfigs);
24+
return null;
25+
}
3626

37-
static bool? _asBool(YamlNode scalar) {
38-
var value = scalar is YamlScalar ? scalar.valueOrThrow : scalar;
39-
return switch (value) {
40-
bool() => value,
41-
'true' => true,
42-
'false' => false,
43-
_ => null,
44-
};
45-
}
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+
}
4637

47-
static String? _asString(Object scalar) {
48-
var value = scalar is YamlScalar ? scalar.value : scalar;
49-
return value is String ? value : null;
50-
}
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+
}
5143

52-
static Map<String, bool> _parseArgs(YamlNode args) {
53-
var enabled = _asBool(args);
54-
if (enabled != null) {
55-
return {'enabled': enabled};
56-
}
57-
return {};
44+
Map<String, bool> _parseArgs(YamlNode args) {
45+
var enabled = _asBool(args);
46+
if (enabled != null) {
47+
return {'enabled': enabled};
5848
}
49+
return {};
50+
}
5951

60-
static List<RuleConfig> _ruleConfigs(YamlNode value) {
61-
// For example:
62-
//
63-
// ```yaml
64-
// - unnecessary_getters
65-
// - camel_case_types
66-
// ```
67-
if (value is YamlList) {
68-
return [
69-
for (var rule in value.nodes)
70-
RuleConfig(name: _asString(rule), args: {'enabled': true}),
71-
];
72-
}
52+
/// Returns the [RuleConfig]s that are parsed from [value], which can be either
53+
/// a YAML list or a YAML map.
54+
List<RuleConfig> _ruleConfigs(YamlNode value) {
55+
// For example:
56+
//
57+
// ```yaml
58+
// - unnecessary_getters
59+
// - camel_case_types
60+
// ```
61+
if (value is YamlList) {
62+
return [
63+
for (var rule in value.nodes)
64+
RuleConfig(name: _asString(rule), args: {'enabled': true}),
65+
];
66+
}
7367

74-
// style_guide: {unnecessary_getters: false, camel_case_types: true}
75-
if (value is YamlMap) {
76-
var ruleConfigs = <RuleConfig>[];
77-
value.nodes.cast<Object, YamlNode>().forEach((key, value) {
78-
// For example: `{unnecessary_getters: false}`.
79-
var valueAsBool = _asBool(value);
80-
if (valueAsBool != null) {
68+
// style_guide: {unnecessary_getters: false, camel_case_types: true}
69+
if (value is YamlMap) {
70+
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`.
8186
ruleConfigs.add(RuleConfig(
82-
name: _asString(key),
83-
args: {'enabled': valueAsBool},
87+
name: _asString(rule),
88+
args: _parseArgs(args),
89+
group: _asString(key),
8490
));
85-
}
86-
87-
// style_guide: {unnecessary_getters: false, camel_case_types: true}
88-
if (value is YamlMap) {
89-
value.nodes.cast<Object, YamlNode>().forEach((rule, args) {
90-
// TODO(brianwilkerson): verify format.
91-
// For example: `unnecessary_getters: false`.
92-
ruleConfigs.add(RuleConfig(
93-
name: _asString(rule),
94-
args: _parseArgs(args),
95-
group: _asString(key),
96-
));
97-
});
98-
}
99-
});
100-
return ruleConfigs;
101-
}
102-
103-
return const [];
91+
});
92+
}
93+
});
94+
return ruleConfigs;
10495
}
96+
97+
return const [];
10598
}
10699

107100
/// The configuration of a single lint rule within an analysis options file.

pkg/analyzer/test/src/lint/config_test.dart

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,34 @@ defineTests() {
3434
group('lint config', () {
3535
group('rule', () {
3636
test('configs', () {
37-
var optionsYaml = loadYamlNode('''
38-
files:
39-
include: foo # un-quoted
40-
exclude:
41-
- 'test/**' # file globs can be scalars or lists
42-
- '**/_data.dart' # unquoted stars treated by YAML as aliases
43-
rules:
44-
style_guide:
45-
unnecessary_getters: false #disable
46-
camel_case_types: true #enable
47-
pub:
48-
package_names: false
49-
''');
50-
var config = LintConfig.parseMap(optionsYaml as YamlMap);
51-
expect(config.ruleConfigs, hasLength(3));
37+
var ruleConfigs = parseLintRuleConfigs(loadYamlNode('''
38+
linter:
39+
# Unsupported sections here to check that no exceptions are thrown when
40+
# YAML includes unknown sections.
41+
files:
42+
include: foo # un-quoted
43+
exclude:
44+
- 'test/**' # file globs can be scalars or lists
45+
- '**/_data.dart' # unquoted stars treated by YAML as aliases
46+
rules:
47+
style_guide:
48+
unnecessary_getters: false #disable
49+
camel_case_types: true #enable
50+
pub:
51+
package_names: false
52+
''') as YamlMap);
53+
expect(ruleConfigs, hasLength(3));
5254
});
5355

5456
test('config', () {
55-
var config = LintConfig.parseMap(loadYamlNode('''
56-
rules:
57-
style_guide:
58-
unnecessary_getters: false''') as YamlMap);
59-
expect(config.ruleConfigs, hasLength(1));
60-
var ruleConfig = config.ruleConfigs[0];
57+
var ruleConfigs = parseLintRuleConfigs(loadYamlNode('''
58+
linter:
59+
rules:
60+
style_guide:
61+
unnecessary_getters: false
62+
''') as YamlMap)!;
63+
expect(ruleConfigs, hasLength(1));
64+
var ruleConfig = ruleConfigs[0];
6165
expect(ruleConfig.group, 'style_guide');
6266
expect(ruleConfig.name, 'unnecessary_getters');
6367
expect(ruleConfig.args, {'enabled': false});

pkg/linter/tool/benchmark.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ Future<void> runLinter(List<String> args) async {
9595
LinterOptions linterOptions;
9696
if (configFile is String) {
9797
var optionsContent = readFile(configFile);
98-
var config = LintConfig.parseMap(loadYamlNode(optionsContent) as YamlMap);
99-
var enabledRules = Registry.ruleRegistry.where((LintRule rule) =>
100-
!config.ruleConfigs.any((rc) => rc.disables(rule.name)));
98+
var ruleConfigs =
99+
parseLintRuleConfigs(loadYamlNode(optionsContent) as YamlMap)!;
100+
var enabledRules = Registry.ruleRegistry.where(
101+
(LintRule rule) => !ruleConfigs.any((rc) => rc.disables(rule.name)));
101102

102103
linterOptions = LinterOptions(enabledRules: enabledRules);
103104
} else if (ruleNames is Iterable<String> && ruleNames.isNotEmpty) {

0 commit comments

Comments
 (0)