Skip to content

Commit 0ed2082

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer plugins: change rules options key to diagnostics
Also allow warning diagnostics to be disabled. In many places, the RuleConfig is unchanged because currently the `linter/rules` section parsing is shared with the `plugin/<plugin-name>/diagnostics` section parsing. They will probably diverge at some point, but as long as they are the same, we can use the same Registry class to identify enabled and disabled rules/diagnostics. Change-Id: I9c00770af14d3b753532bcf1bc6bff62624d99ff Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/393460 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent c84515f commit 0ed2082

File tree

8 files changed

+52
-40
lines changed

8 files changed

+52
-40
lines changed

pkg/analysis_server_plugin/lib/src/plugin_server.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ class PluginServer {
257257
for (var configuration in analysisOptions.pluginConfigurations) {
258258
if (!configuration.isEnabled) continue;
259259
// TODO(srawlins): Namespace rules by their plugin, to avoid collisions.
260-
var rules = Registry.ruleRegistry.enabled(configuration.ruleConfigs);
260+
var rules =
261+
Registry.ruleRegistry.enabled(configuration.diagnosticConfigs);
261262
for (var rule in rules) {
262263
rule.reporter = errorReporter;
263264
var timer = enableTiming ? analysisRuleTimers.getTimer(rule) : null;

pkg/analysis_server_plugin/test/src/plugin_server_error_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class PluginServerErrorTest extends PluginServerTestBase {
3636
newAnalysisOptionsYamlFile(packagePath, '''
3737
plugins:
3838
no_bools:
39-
rules:
39+
diagnostics:
4040
- no_bools
4141
''');
4242
}

pkg/analysis_server_plugin/test/src/plugin_server_test.dart

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class PluginServerTest extends PluginServerTestBase {
6969
expect(fixes[0].fixes, hasLength(1));
7070
}
7171

72-
Future<void> test_lintRulesAreDisabledByDefault() async {
72+
Future<void> test_lintDiagnosticsAreDisabledByDefault() async {
7373
writeAnalysisOptionsWithPlugin();
7474
newFile(filePath, 'double x = 3.14;');
7575
await channel
@@ -81,7 +81,7 @@ class PluginServerTest extends PluginServerTestBase {
8181
expect(params.errors, isEmpty);
8282
}
8383

84-
Future<void> test_lintRulesCanBeEnabled() async {
84+
Future<void> test_lintDiagnosticsCanBeEnabled() async {
8585
writeAnalysisOptionsWithPlugin({'no_doubles': true});
8686
newFile(filePath, 'double x = 3.14;');
8787
await channel
@@ -169,7 +169,7 @@ class PluginServerTest extends PluginServerTestBase {
169169
_expectAnalysisError(params.errors.single, message: 'No bools message');
170170
}
171171

172-
Future<void> test_warningRulesAreEnabledByDefault() async {
172+
Future<void> test_warningDiagnosticsAreEnabledByDefault() async {
173173
writeAnalysisOptionsWithPlugin();
174174
newFile(filePath, 'bool b = false;');
175175
await channel
@@ -182,9 +182,7 @@ class PluginServerTest extends PluginServerTestBase {
182182
_expectAnalysisError(params.errors.single, message: 'No bools message');
183183
}
184184

185-
Future<void> test_warningRulesCannotBeDisabled() async {
186-
// TODO(srawlins): A warning should be reported in the analysis options file
187-
// for this.
185+
Future<void> test_warningDiagnosticsCanBeDisabled() async {
188186
writeAnalysisOptionsWithPlugin({'no_bools': false});
189187
newFile(filePath, 'bool b = false;');
190188
await channel
@@ -193,19 +191,19 @@ class PluginServerTest extends PluginServerTestBase {
193191
.map((n) => protocol.AnalysisErrorsParams.fromNotification(n))
194192
.where((p) => p.file == filePath));
195193
var params = await paramsQueue.next;
196-
_expectAnalysisError(params.errors.single, message: 'No bools message');
194+
expect(params.errors, isEmpty);
197195
}
198196

199197
void writeAnalysisOptionsWithPlugin(
200-
[Map<String, bool> ruleConfiguration = const {}]) {
198+
[Map<String, bool> diagnosticConfiguration = const {}]) {
201199
var buffer = StringBuffer('''
202200
plugins:
203201
no_literals:
204-
rules:
202+
diagnostics:
205203
''');
206-
for (var MapEntry(key: ruleName, value: isEnabled)
207-
in ruleConfiguration.entries) {
208-
buffer.writeln(' $ruleName: $isEnabled');
204+
for (var MapEntry(key: diagnosticName, value: isEnabled)
205+
in diagnosticConfiguration.entries) {
206+
buffer.writeln(' $diagnosticName: $isEnabled');
209207
}
210208
newAnalysisOptionsYamlFile(packagePath, buffer.toString());
211209
}

pkg/analyzer/lib/dart/analysis/analysis_options.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ final class PluginConfiguration {
7979
/// The name of the plugin being configured.
8080
final String name;
8181

82-
/// The list of specified [RuleConfig]s.
83-
final Map<String, RuleConfig> ruleConfigs;
82+
/// The list of specified [DiagnosticConfig]s.
83+
final Map<String, DiagnosticConfig> diagnosticConfigs;
8484

8585
/// Whether the plugin is enabled.
8686
final bool isEnabled;
8787

8888
PluginConfiguration({
8989
required this.name,
90-
required this.ruleConfigs,
90+
required this.diagnosticConfigs,
9191
required this.isEnabled,
9292
});
9393
}

pkg/analyzer/lib/src/dart/analysis/analysis_options.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,24 @@ final class AnalysisOptionsBuilder {
209209
// TODO(srawlins): There may be value in storing the version constraint,
210210
// `value`.
211211
pluginConfigurations.add(PluginConfiguration(
212-
name: pluginName, ruleConfigs: const {}, isEnabled: true));
212+
name: pluginName, diagnosticConfigs: const {}, isEnabled: true));
213213
return;
214214
}
215215

216216
if (pluginNode is! YamlMap) {
217217
return;
218218
}
219219

220-
var rules = pluginNode.valueAt(AnalyzerOptions.rules);
221-
if (rules == null) {
220+
var diagnostics = pluginNode.valueAt(AnalyzerOptions.diagnostics);
221+
if (diagnostics == null) {
222222
return;
223223
}
224224

225-
var ruleConfigurations = parseRulesSection(rules);
225+
var diagnosticConfigurations = parseDiagnosticsSection(diagnostics);
226226

227227
pluginConfigurations.add(PluginConfiguration(
228228
name: pluginName,
229-
ruleConfigs: ruleConfigurations,
229+
diagnosticConfigs: diagnosticConfigurations,
230230
// TODO(srawlins): Implement `enabled: false`.
231231
isEnabled: true,
232232
));

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,9 @@ import 'package:analyzer/src/task/options.dart';
66
import 'package:analyzer/src/util/yaml.dart';
77
import 'package:yaml/yaml.dart';
88

9-
/// Parses [optionsMap] into [RuleConfig]s mapped from their names, returning
10-
/// them, or `null` if [optionsMap] does not have `linter` map.
11-
Map<String, RuleConfig>? parseLinterSection(YamlMap optionsMap) {
12-
var options = optionsMap.valueAt('linter');
13-
// Quick check of basic contract.
14-
if (options is YamlMap) {
15-
var rulesNode = options.valueAt(AnalyzerOptions.rules);
16-
return {
17-
if (rulesNode != null) ...parseRulesSection(rulesNode),
18-
};
19-
}
20-
21-
return null;
22-
}
23-
249
/// Returns the [RuleConfig]s that are parsed from [value], which can be either
2510
/// a YAML list or a YAML map, mapped from each rule's name.
26-
Map<String, RuleConfig> parseRulesSection(YamlNode value) {
11+
Map<String, RuleConfig> parseDiagnosticsSection(YamlNode value) {
2712
// For example:
2813
//
2914
// ```yaml
@@ -72,6 +57,21 @@ Map<String, RuleConfig> parseRulesSection(YamlNode value) {
7257
return ruleConfigs;
7358
}
7459

60+
/// Parses [optionsMap] into [RuleConfig]s mapped from their names, returning
61+
/// them, or `null` if [optionsMap] does not have `linter` map.
62+
Map<String, RuleConfig>? parseLinterSection(YamlMap optionsMap) {
63+
var options = optionsMap.valueAt('linter');
64+
// Quick check of basic contract.
65+
if (options is YamlMap) {
66+
var rulesNode = options.valueAt(AnalyzerOptions.rules);
67+
return {
68+
if (rulesNode != null) ...parseDiagnosticsSection(rulesNode),
69+
};
70+
}
71+
72+
return null;
73+
}
74+
7575
RuleConfig? _parseRuleConfig(dynamic configKey, YamlNode configNode,
7676
{String? group}) {
7777
// For example: `{unnecessary_getters: false}`.
@@ -84,6 +84,15 @@ RuleConfig? _parseRuleConfig(dynamic configKey, YamlNode configNode,
8484
return null;
8585
}
8686

87+
/// An alias for a [RuleConfig], but which is configured under a 'diagnostics'
88+
/// key in an analysis options file.
89+
///
90+
/// In an analyzer plugin, diagnostics are enabled and disabled via their name.
91+
/// (For the built-in lint diagnostics, which are configured in an analysis
92+
/// options file's top-level 'linter' key, diagnostics are enabled and disabled
93+
/// via the name of the lint rule that reports the diagnostic.)
94+
typedef DiagnosticConfig = RuleConfig;
95+
8796
/// The configuration of a single analysis rule within an analysis options file.
8897
class RuleConfig {
8998
/// Whether this rule is enabled or disabled in this configuration.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ class Registry with IterableMixin<AnalysisRule> {
4848
///
4949
/// enables `my_rule`.
5050
Iterable<AnalysisRule> enabled(Map<String, RuleConfig> ruleConfigs) => [
51-
// All warning rules (which cannot be disabled).
52-
..._warningRules.values,
51+
// All warning rules that haven't explicitly been disabled.
52+
..._warningRules.values
53+
.where((rule) => ruleConfigs[rule.name]?.isEnabled ?? true),
5354
// All lint rules that have explicitly been enabled.
5455
..._lintRules.values
5556
.where((rule) => ruleConfigs[rule.name]?.isEnabled ?? false),

pkg/analyzer/lib/src/task/options.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ class AnalyzerOptions {
283283
// Linter options.
284284
static const String rules = 'rules';
285285

286+
/// Plugin options.
287+
static const String diagnostics = 'diagnostics';
288+
286289
static const String propagateLinterExceptions = 'propagate-linter-exceptions';
287290

288291
/// Ways to say `true` or `false`.

0 commit comments

Comments
 (0)