Skip to content

Commit 7ac6ad6

Browse files
stereotype441Commit Queue
authored andcommitted
[messages] Make lint name matching case insensitive.
Adjusts the logic in the lint `Registry` class so that lint names are matched in case-insensitive fashion. For the most part this is accomplished by adding calls to `.toLowerCase()` inside the `Registry` class, preserving its API. For the `enabled` method, preserving the API would have been a pain (I would have had to translate the keys in the `ruleConfigs` parameter to lower case). So instead I added an assertion to verify that the keys were lower case, and pushed the responsibility to the callers to create lower case keys. This paves the way for a follow-up CL that will translate diagnostic codes to `lower_snake_case` conventions. It also solves a longstanding problem with the mixed case lint rules `no_runtimeType_toString`, `prefer_for_elements_to_map_fromIterable`, `prefer_iterable_whereType`. Previously, the user had to carefully imitate the capitalization of the lint rules when specifying them in the `analysis_options.yaml` file, even though `// ignore:` comments for the lints matched in case insensitive fashion. With this CL, the lint rule names in `analysis_options.yaml` are matched in case insensitive fashion as well. Change-Id: I6a6a6964d83241e49878bbf96ef9b94cbb12098b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/465964 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 527dd5a commit 7ac6ad6

File tree

4 files changed

+48
-30
lines changed

4 files changed

+48
-30
lines changed

pkg/analysis_server_plugin/lib/src/plugin_server.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,10 @@ class PluginServer {
439439
for (var configuration in analysisOptions.pluginConfigurations) {
440440
if (!configuration.isEnabled) continue;
441441
// TODO(srawlins): Namespace rules by their plugin, to avoid collisions.
442-
var rules = Registry.ruleRegistry.enabled(
443-
configuration.diagnosticConfigs,
444-
);
442+
var rules = Registry.ruleRegistry.enabled({
443+
for (var entry in configuration.diagnosticConfigs.entries)
444+
entry.key.toLowerCase(): entry.value,
445+
});
445446

446447
for (var code in rules.expand((r) => r.diagnosticCodes)) {
447448
pluginCodeMapping.putIfAbsent(code, () => configuration.name);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ Map<String, RuleConfig>? parseLinterSection(YamlMap optionsMap) {
7070
// Quick check of basic contract.
7171
if (options is YamlMap) {
7272
var rulesNode = options.valueAt(AnalysisOptionsFile.rules);
73-
return {if (rulesNode != null) ...parseDiagnosticsSection(rulesNode)};
73+
return {
74+
if (rulesNode != null)
75+
for (var entry in parseDiagnosticsSection(rulesNode).entries)
76+
entry.key.toLowerCase(): entry.value,
77+
};
7478
}
7579

7680
return null;

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

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ class Registry with IterableMixin<AbstractAnalysisRule> {
1313
/// The default registry to be used by clients.
1414
static final Registry ruleRegistry = Registry();
1515

16-
/// A table mapping lint rule names to rules.
16+
/// A table mapping lower case lint rule names to rules.
1717
final Map<String, AbstractAnalysisRule> _lintRules = {};
1818

19-
/// A table mapping warning rule names to rules.
19+
/// A table mapping lower case warning rule names to rules.
2020
final Map<String, AbstractAnalysisRule> _warningRules = {};
2121

22-
/// A table mapping unique names to lint codes.
22+
/// A table mapping lower case unique names to lint codes.
2323
final Map<String, DiagnosticCode> _codeMap = {};
2424

2525
@override
@@ -36,10 +36,15 @@ class Registry with IterableMixin<AbstractAnalysisRule> {
3636
};
3737

3838
/// Returns the rule with the given [name].
39-
AbstractAnalysisRule? operator [](String name) => _rules[name];
39+
///
40+
/// The name is matched disregarding case.
41+
AbstractAnalysisRule? operator [](String name) => _rules[name.toLowerCase()];
4042

4143
/// Returns the lint code that has the given [uniqueName].
42-
DiagnosticCode? codeForUniqueName(String uniqueName) => _codeMap[uniqueName];
44+
///
45+
/// The name is matched disregarding case.
46+
DiagnosticCode? codeForUniqueName(String uniqueName) =>
47+
_codeMap[uniqueName.toLowerCase()];
4348

4449
/// Returns a list of the enabled rules.
4550
///
@@ -51,50 +56,56 @@ class Registry with IterableMixin<AbstractAnalysisRule> {
5156
/// my_rule: true
5257
///
5358
/// enables `my_rule`.
54-
Iterable<AbstractAnalysisRule> enabled(Map<String, RuleConfig> ruleConfigs) =>
55-
[
56-
// All warning rules that haven't explicitly been disabled.
57-
..._warningRules.values.where(
58-
(rule) => ruleConfigs[rule.name]?.isEnabled ?? true,
59-
),
60-
// All lint rules that have explicitly been enabled.
61-
..._lintRules.values.where(
62-
(rule) => ruleConfigs[rule.name]?.isEnabled ?? false,
63-
),
64-
];
59+
///
60+
/// Every key in [ruleConfigs] should be all lower case.
61+
Iterable<AbstractAnalysisRule> enabled(Map<String, RuleConfig> ruleConfigs) {
62+
assert(ruleConfigs.keys.every((key) => key == key.toLowerCase()));
63+
return [
64+
// All warning rules that haven't explicitly been disabled.
65+
..._warningRules.values.where(
66+
(rule) => ruleConfigs[rule.name.toLowerCase()]?.isEnabled ?? true,
67+
),
68+
// All lint rules that have explicitly been enabled.
69+
..._lintRules.values.where(
70+
(rule) => ruleConfigs[rule.name.toLowerCase()]?.isEnabled ?? false,
71+
),
72+
];
73+
}
6574

6675
/// Returns the rule with the given [name].
67-
AbstractAnalysisRule? getRule(String name) => _rules[name];
76+
///
77+
/// The name is matched disregarding case.
78+
AbstractAnalysisRule? getRule(String name) => _rules[name.toLowerCase()];
6879

6980
/// Adds the given lint [rule] to this registry.
7081
void registerLintRule(AbstractAnalysisRule rule) {
71-
_lintRules[rule.name] = rule;
82+
_lintRules[rule.name.toLowerCase()] = rule;
7283
for (var code in rule.diagnosticCodes) {
73-
_codeMap[code.uniqueName] = code;
84+
_codeMap[code.uniqueName.toLowerCase()] = code;
7485
}
7586
}
7687

7788
/// Adds the given warning [rule] to this registry.
7889
void registerWarningRule(AbstractAnalysisRule rule) {
79-
_warningRules[rule.name] = rule;
90+
_warningRules[rule.name.toLowerCase()] = rule;
8091
for (var code in rule.diagnosticCodes) {
81-
_codeMap[code.uniqueName] = code;
92+
_codeMap[code.uniqueName.toLowerCase()] = code;
8293
}
8394
}
8495

8596
/// Removes the given lint [rule] from this registry.
8697
void unregisterLintRule(AbstractAnalysisRule rule) {
87-
_lintRules.remove(rule.name);
98+
_lintRules.remove(rule.name.toLowerCase());
8899
for (var code in rule.diagnosticCodes) {
89-
_codeMap.remove(code.uniqueName);
100+
_codeMap.remove(code.uniqueName.toLowerCase());
90101
}
91102
}
92103

93104
/// Removes the given warning [rule] from this registry.
94105
void unregisterWarningRule(AbstractAnalysisRule rule) {
95-
_warningRules.remove(rule.name);
106+
_warningRules.remove(rule.name.toLowerCase());
96107
for (var code in rule.diagnosticCodes) {
97-
_codeMap.remove(code.uniqueName);
108+
_codeMap.remove(code.uniqueName.toLowerCase());
98109
}
99110
}
100111
}

pkg/analyzer_testing/lib/src/analysis_rule/pub_package_resolution.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ final class ExpectedLint extends ExpectedDiagnostic {
192192
super.correctionContains,
193193
super.contextMessages,
194194
}) : super(
195-
(diagnostic) => diagnostic.diagnosticCode.name == _lintName,
195+
(diagnostic) =>
196+
diagnostic.diagnosticCode.name.toLowerCase() ==
197+
_lintName.toLowerCase(),
196198
offset,
197199
length,
198200
);

0 commit comments

Comments
 (0)