Skip to content

Commit 7093fba

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Use Registry.ruleRegistry instead of a rule provider in LinterRuleOptionsValidator.
The theme of this change is that the Registry.ruleRegistry is always the source of truth for what lint rules are registered, even in tests. Before this change, there was a mechanism for passing a "rule provider" around different classes and functions, which is a function that returns a list of lint rules. This mechanism only existed for one test, `options_rule_validator_test.dart`. So this change instead makes that test use Registry.ruleRegistry, and removes all of the rule provider mechanisms. All in all: * Remove the LintRuleProvider type alias. * Remove the LinterRuleOptionsValidator constructor's `provider` parameter and corresponding field. * Remove the `provider` parameter from `analyzeAnalysisOptions()`. * Remove the `provider` parameter from `OptionsFileValidator()`. * In AbstractAnalysisOptionsTest, remove the `provider` parameter from `assertErrorsInCode`. * Rename `_validatePluginsOption` to `_validateLegacyPluginsOption` while I'm there. * In order to register the test LintRule classes, they need a proper `lintCode` property. So create a super class for them, simplifying each test LintRule class. Change-Id: Ic153bba006fe0fe2a94abb08ac55723ffb2c356e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/393907 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 526c575 commit 7093fba

File tree

6 files changed

+79
-113
lines changed

6 files changed

+79
-113
lines changed

pkg/analysis_server/test/services/linter/linter_test.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ void main() {
2020

2121
@reflectiveTest
2222
class LinterRuleOptionsValidatorTest {
23-
final LinterRuleOptionsValidator validator = LinterRuleOptionsValidator();
24-
25-
final AnalysisOptionsProvider optionsProvider = AnalysisOptionsProvider();
26-
2723
late RecordingErrorListener recorder;
2824

2925
late ErrorReporter reporter;
3026

3127
List<AnalysisError> get errors => recorder.errors;
3228

29+
LinterRuleOptionsValidator get validator => LinterRuleOptionsValidator();
30+
3331
void setUp() {
3432
registerLintRules();
3533
recorder = RecordingErrorListener();
@@ -72,7 +70,7 @@ linter:
7270
}
7371

7472
void validate(String source, List<ErrorCode> expected) {
75-
var options = optionsProvider.getOptionsFromString(source);
73+
var options = AnalysisOptionsProvider().getOptionsFromString(source);
7674
validator.validate(reporter, options);
7775
expect(
7876
errors.map((AnalysisError e) => e.errorCode),

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,18 @@ import 'package:collection/collection.dart';
1414
import 'package:pub_semver/pub_semver.dart';
1515
import 'package:yaml/yaml.dart';
1616

17-
/// Rule provider.
18-
typedef LintRuleProvider = Iterable<LintRule> Function();
19-
2017
/// Validates `linter` rule configurations.
2118
class LinterRuleOptionsValidator extends OptionsValidator {
2219
static const linter = 'linter';
2320
static const rulesKey = 'rules';
2421

25-
final LintRuleProvider ruleProvider;
2622
final VersionConstraint? sdkVersionConstraint;
2723
final bool sourceIsOptionsForContextRoot;
2824

2925
LinterRuleOptionsValidator({
30-
LintRuleProvider? provider,
3126
this.sdkVersionConstraint,
3227
this.sourceIsOptionsForContextRoot = true,
33-
}) : ruleProvider = provider ?? (() => Registry.ruleRegistry.rules);
28+
});
3429

3530
bool currentSdkAllows(Version? since) {
3631
if (since == null) return true;
@@ -39,8 +34,8 @@ class LinterRuleOptionsValidator extends OptionsValidator {
3934
return sdk.allows(since);
4035
}
4136

42-
LintRule? getRegisteredLint(Object value) =>
43-
ruleProvider().firstWhereOrNull((rule) => rule.name == value);
37+
LintRule? getRegisteredLint(Object value) => Registry.ruleRegistry.rules
38+
.firstWhereOrNull((rule) => rule.name == value);
4439

4540
bool isDeprecatedInCurrentSdk(DeprecatedState state) =>
4641
currentSdkAllows(state.since);
@@ -52,13 +47,12 @@ class LinterRuleOptionsValidator extends OptionsValidator {
5247

5348
@override
5449
List<AnalysisError> validate(ErrorReporter reporter, YamlMap options) {
55-
List<AnalysisError> errors = <AnalysisError>[];
5650
var node = options.valueAt(linter);
5751
if (node is YamlMap) {
5852
var rules = node.valueAt(rulesKey);
5953
_validateRules(rules, reporter);
6054
}
61-
return errors;
55+
return const [];
6256
}
6357

6458
void _validateRules(YamlNode? rules, ErrorReporter reporter) {

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ List<AnalysisError> analyzeAnalysisOptions(
2626
String content,
2727
SourceFactory sourceFactory,
2828
String contextRoot,
29-
VersionConstraint? sdkVersionConstraint, {
30-
LintRuleProvider? provider,
31-
}) {
29+
VersionConstraint? sdkVersionConstraint,
30+
) {
3231
List<AnalysisError> errors = [];
3332
Source initialSource = source;
3433
SourceSpan? initialIncludeSpan;
@@ -71,13 +70,12 @@ List<AnalysisError> analyzeAnalysisOptions(
7170
}
7271

7372
// Validates the specified options and any included option files.
74-
void validate(Source source, YamlMap options, LintRuleProvider? provider) {
73+
void validate(Source source, YamlMap options) {
7574
var sourceIsOptionsForContextRoot = initialIncludeSpan == null;
7675
var validationErrors = OptionsFileValidator(
7776
source,
7877
sdkVersionConstraint: sdkVersionConstraint,
7978
sourceIsOptionsForContextRoot: sourceIsOptionsForContextRoot,
80-
provider: provider,
8179
).validate(options);
8280
addDirectErrorOrIncludedError(validationErrors, source,
8381
sourceIsOptionsForContextRoot: sourceIsOptionsForContextRoot);
@@ -87,7 +85,7 @@ List<AnalysisError> analyzeAnalysisOptions(
8785
// Validate the 'plugins' option in [options], understanding that no other
8886
// options are included.
8987
addDirectErrorOrIncludedError(
90-
_validatePluginsOption(source, options: options), source,
88+
_validateLegacyPluginsOption(source, options: options), source,
9189
sourceIsOptionsForContextRoot: sourceIsOptionsForContextRoot);
9290
return;
9391
}
@@ -145,12 +143,12 @@ List<AnalysisError> analyzeAnalysisOptions(
145143
try {
146144
var includedOptions =
147145
optionsProvider.getOptionsFromString(includedSource.contents.data);
148-
validate(includedSource, includedOptions, provider);
146+
validate(includedSource, includedOptions);
149147
firstPluginName ??= _firstPluginName(includedOptions);
150148
// Validate the 'plugins' option in [options], taking into account any
151149
// plugins enabled by [includedOptions].
152150
addDirectErrorOrIncludedError(
153-
_validatePluginsOption(source,
151+
_validateLegacyPluginsOption(source,
154152
options: options, firstEnabledPluginName: firstPluginName),
155153
source,
156154
sourceIsOptionsForContextRoot: sourceIsOptionsForContextRoot,
@@ -189,7 +187,7 @@ List<AnalysisError> analyzeAnalysisOptions(
189187

190188
try {
191189
YamlMap options = optionsProvider.getOptionsFromString(content);
192-
validate(source, options, provider);
190+
validate(source, options);
193191
} on OptionsFormatException catch (e) {
194192
SourceSpan span = e.span!;
195193
errors.add(
@@ -205,8 +203,8 @@ List<AnalysisError> analyzeAnalysisOptions(
205203
return errors;
206204
}
207205

208-
/// Returns the name of the first plugin, if one is specified in [options],
209-
/// otherwise `null`.
206+
/// Returns the name of the first legacy plugin, if one is specified in
207+
/// [options], otherwise `null`.
210208
String? _firstPluginName(YamlMap options) {
211209
var analyzerMap = options.valueAt(AnalyzerOptions.analyzer);
212210
if (analyzerMap is! YamlMap) {
@@ -224,9 +222,9 @@ String? _firstPluginName(YamlMap options) {
224222
}
225223
}
226224

227-
/// Validates the 'plugins' options in [options], given
225+
/// Validates the legacy 'plugins' options in [options], given
228226
/// [firstEnabledPluginName].
229-
List<AnalysisError> _validatePluginsOption(
227+
List<AnalysisError> _validateLegacyPluginsOption(
230228
Source source, {
231229
required YamlMap options,
232230
String? firstEnabledPluginName,
@@ -365,16 +363,14 @@ class OptionsFileValidator {
365363

366364
OptionsFileValidator(
367365
this._source, {
368-
required VersionConstraint? sdkVersionConstraint,
366+
VersionConstraint? sdkVersionConstraint,
369367
required bool sourceIsOptionsForContextRoot,
370-
LintRuleProvider? provider,
371368
}) : _validators = [
372369
AnalyzerOptionsValidator(),
373370
_CodeStyleOptionsValidator(),
374371
_FormatterOptionsValidator(),
375372
_LinterOptionsValidator(),
376373
LinterRuleOptionsValidator(
377-
provider: provider,
378374
sdkVersionConstraint: sdkVersionConstraint,
379375
sourceIsOptionsForContextRoot: sourceIsOptionsForContextRoot,
380376
),

pkg/analyzer/test/src/diagnostics/analysis_options/analysis_options_test_support.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:_fe_analyzer_shared/src/base/errors.dart';
66
import 'package:analyzer/src/context/source.dart';
77
import 'package:analyzer/src/file_system/file_system.dart';
88
import 'package:analyzer/src/generated/source.dart';
9-
import 'package:analyzer/src/lint/options_rule_validator.dart';
109
import 'package:analyzer/src/task/options.dart';
1110
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
1211
import 'package:pub_semver/pub_semver.dart';
@@ -19,10 +18,7 @@ abstract class AbstractAnalysisOptionsTest with ResourceProviderMixin {
1918
VersionConstraint? get sdkVersionConstraint => null;
2019

2120
Future<void> assertErrorsInCode(
22-
String code,
23-
List<ExpectedError> expectedErrors, {
24-
LintRuleProvider? provider,
25-
}) async {
21+
String code, List<ExpectedError> expectedErrors) async {
2622
var path = convertPath('/analysis_options.yaml');
2723
newFile(path, code);
2824
var diagnostics = analyzeAnalysisOptions(
@@ -31,7 +27,6 @@ abstract class AbstractAnalysisOptionsTest with ResourceProviderMixin {
3127
sourceFactory,
3228
'/',
3329
sdkVersionConstraint,
34-
provider: provider,
3530
);
3631
var errorListener = GatheringErrorListener();
3732
errorListener.addAll(diagnostics);

0 commit comments

Comments
 (0)