Skip to content

Commit 0c1bae3

Browse files
pqCommit Queue
authored and
Commit Queue
committed
only report lint reference diagnostics for local lints
See: https://github.com/dart-lang/lints/issues/95 Change-Id: Ibc71c71027dc6d7b057e1caf201694394461e01c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280123 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent de9849c commit 0c1bae3

File tree

5 files changed

+126
-54
lines changed

5 files changed

+126
-54
lines changed

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ class LinterRuleOptionsValidator extends OptionsValidator {
6161

6262
final LintRuleProvider ruleProvider;
6363
final VersionConstraint? sdkVersionConstraint;
64+
final bool sourceIsOptionsForContextRoot;
6465

65-
LinterRuleOptionsValidator(
66-
{LintRuleProvider? provider, this.sdkVersionConstraint})
67-
: ruleProvider = provider ?? (() => Registry.ruleRegistry.rules);
66+
LinterRuleOptionsValidator({
67+
LintRuleProvider? provider,
68+
this.sdkVersionConstraint,
69+
this.sourceIsOptionsForContextRoot = true,
70+
}) : ruleProvider = provider ?? (() => Registry.ruleRegistry.rules);
6871

6972
bool currentSdkAllows(Version? since) {
7073
if (since == null) return true;
@@ -128,18 +131,24 @@ class LinterRuleOptionsValidator extends OptionsValidator {
128131
reporter.reportErrorForSpan(DUPLICATE_RULE_HINT, node.span, [value]);
129132
}
130133
}
131-
var state = rule.state;
132-
if (isDeprecatedInCurrentSdk(state)) {
133-
reporter.reportErrorForSpan(DEPRECATED_LINT_HINT, node.span, [value]);
134-
} else if (isRemovedInCurrentSdk(state)) {
135-
var since = state.since.toString();
136-
var replacedBy = (state as RemovedState).replacedBy;
137-
if (replacedBy != null) {
138-
reporter.reportErrorForSpan(AnalysisOptionsWarningCode.REPLACED_LINT,
139-
node.span, [value, since, replacedBy]);
140-
} else {
141-
reporter.reportErrorForSpan(AnalysisOptionsWarningCode.REMOVED_LINT,
142-
node.span, [value, since]);
134+
// Report removed or deprecated lint warnings defined directly (and not in
135+
// includes).
136+
if (sourceIsOptionsForContextRoot) {
137+
var state = rule.state;
138+
if (isDeprecatedInCurrentSdk(state)) {
139+
reporter.reportErrorForSpan(DEPRECATED_LINT_HINT, node.span, [value]);
140+
} else if (isRemovedInCurrentSdk(state)) {
141+
var since = state.since.toString();
142+
var replacedBy = (state as RemovedState).replacedBy;
143+
if (replacedBy != null) {
144+
reporter.reportErrorForSpan(
145+
AnalysisOptionsWarningCode.REPLACED_LINT,
146+
node.span,
147+
[value, since, replacedBy]);
148+
} else {
149+
reporter.reportErrorForSpan(AnalysisOptionsWarningCode.REMOVED_LINT,
150+
node.span, [value, since]);
151+
}
143152
}
144153
}
145154
}

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ List<AnalysisError> analyzeAnalysisOptions(
3232
String content,
3333
SourceFactory sourceFactory,
3434
String contextRoot,
35-
VersionConstraint? sdkVersionConstraint,
36-
) {
35+
VersionConstraint? sdkVersionConstraint, {
36+
LintRuleProvider? provider,
37+
}) {
3738
List<AnalysisError> errors = [];
3839
Source initialSource = source;
3940
SourceSpan? initialIncludeSpan;
@@ -73,11 +74,14 @@ List<AnalysisError> analyzeAnalysisOptions(
7374
}
7475

7576
// Validate the specified options and any included option files.
76-
void validate(Source source, YamlMap options) {
77+
void validate(Source source, YamlMap options, LintRuleProvider? provider) {
7778
var sourceIsOptionsForContextRoot = initialIncludeSpan == null;
78-
var validationErrors =
79-
OptionsFileValidator(source, sdkVersionConstraint: sdkVersionConstraint)
80-
.validate(options);
79+
var validationErrors = OptionsFileValidator(
80+
source,
81+
sdkVersionConstraint: sdkVersionConstraint,
82+
sourceIsOptionsForContextRoot: sourceIsOptionsForContextRoot,
83+
provider: provider,
84+
).validate(options);
8185
addDirectErrorOrIncludedError(validationErrors, source,
8286
sourceIsOptionsForContextRoot: sourceIsOptionsForContextRoot);
8387

@@ -131,7 +135,7 @@ List<AnalysisError> analyzeAnalysisOptions(
131135
try {
132136
var includedOptions =
133137
optionsProvider.getOptionsFromString(includedSource.contents.data);
134-
validate(includedSource, includedOptions);
138+
validate(includedSource, includedOptions, provider);
135139
firstPluginName ??= _firstPluginName(includedOptions);
136140
// Validate the 'plugins' option in [options], taking into account any
137141
// plugins enabled by [includedOptions].
@@ -161,7 +165,7 @@ List<AnalysisError> analyzeAnalysisOptions(
161165

162166
try {
163167
YamlMap options = optionsProvider.getOptionsFromString(content);
164-
validate(source, options);
168+
validate(source, options, provider);
165169
} on OptionsFormatException catch (e) {
166170
SourceSpan span = e.span!;
167171
errors.add(AnalysisError(source, span.start.offset, span.length,
@@ -694,14 +698,19 @@ class OptionsFileValidator {
694698

695699
final List<OptionsValidator> _validators;
696700

697-
OptionsFileValidator(this.source,
698-
{required VersionConstraint? sdkVersionConstraint})
699-
: _validators = [
701+
OptionsFileValidator(
702+
this.source, {
703+
required VersionConstraint? sdkVersionConstraint,
704+
required bool sourceIsOptionsForContextRoot,
705+
LintRuleProvider? provider,
706+
}) : _validators = [
700707
AnalyzerOptionsValidator(),
701708
CodeStyleOptionsValidator(),
702709
LinterOptionsValidator(),
703710
LinterRuleOptionsValidator(
711+
provider: provider,
704712
sdkVersionConstraint: sdkVersionConstraint,
713+
sourceIsOptionsForContextRoot: sourceIsOptionsForContextRoot,
705714
),
706715
];
707716

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:_fe_analyzer_shared/src/base/errors.dart';
66
import 'package:analyzer/file_system/file_system.dart';
77
import 'package:analyzer/src/context/source.dart';
88
import 'package:analyzer/src/generated/source.dart';
9+
import 'package:analyzer/src/lint/options_rule_validator.dart';
910
import 'package:analyzer/src/task/options.dart';
1011
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
1112
import 'package:pub_semver/pub_semver.dart';
@@ -18,11 +19,20 @@ abstract class AbstractAnalysisOptionsTest with ResourceProviderMixin {
1819
VersionConstraint? get sdkVersionConstraint => null;
1920

2021
Future<void> assertErrorsInCode(
21-
String code, List<ExpectedError> expectedErrors) async {
22+
String code,
23+
List<ExpectedError> expectedErrors, {
24+
LintRuleProvider? provider,
25+
}) async {
2226
var path = convertPath('/analysis_options.yaml');
2327
newFile(path, code);
2428
var diagnostics = analyzeAnalysisOptions(
25-
TestSource(path), code, sourceFactory, '/', sdkVersionConstraint);
29+
TestSource(path),
30+
code,
31+
sourceFactory,
32+
'/',
33+
sdkVersionConstraint,
34+
provider: provider,
35+
);
2636
var errorListener = GatheringErrorListener();
2737
errorListener.addAll(diagnostics);
2838
errorListener.assertErrors(expectedErrors);

pkg/analyzer/test/src/options/options_rule_validator_test.dart

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import 'package:test_reflective_loader/test_reflective_loader.dart';
1515
import 'package:yaml/yaml.dart';
1616

1717
import '../../generated/test_support.dart';
18+
import '../diagnostics/analysis_options/analysis_options_test_support.dart';
1819

1920
main() {
2021
defineReflectiveSuite(() {
2122
defineReflectiveTests(OptionsRuleValidatorTest);
23+
defineReflectiveTests(OptionsRuleValidatorIncludedFileTest);
2224
});
2325
}
2426

@@ -45,34 +47,44 @@ class DeprecatedSince3Lint extends LintRule {
4547
}
4648

4749
@reflectiveTest
48-
class OptionsRuleValidatorTest extends Object with ResourceProviderMixin {
49-
final rules = [
50-
DeprecatedLint(),
51-
DeprecatedSince3Lint(),
52-
StableLint(),
53-
RuleNeg(),
54-
RulePos(),
55-
RemovedIn2_12Lint(),
56-
ReplacedLint(),
57-
ReplacingLint(),
58-
];
50+
class OptionsRuleValidatorIncludedFileTest extends AbstractAnalysisOptionsTest
51+
with OptionsRuleValidatorTestMixin {
52+
test_deprecated_rule_inInclude_ok() {
53+
newFile('/included.yaml', '''
54+
linter:
55+
rules:
56+
- deprecated_lint
57+
''');
5958

60-
/// Assert that when the validator is used on the given [content] the
61-
/// [expectedErrorCodes] are produced.
62-
void assertErrors(String content, List<ErrorCode> expectedErrorCodes,
63-
{VersionConstraint? sdk}) {
64-
GatheringErrorListener listener = GatheringErrorListener();
65-
ErrorReporter reporter = ErrorReporter(
66-
listener,
67-
StringSource(content, 'analysis_options.yaml'),
68-
isNonNullableByDefault: false,
59+
assertErrorsInCode(
60+
'''
61+
include: included.yaml
62+
''',
63+
[],
64+
provider: () => rules,
6965
);
70-
var validator = LinterRuleOptionsValidator(
71-
provider: () => rules, sdkVersionConstraint: sdk);
72-
validator.validate(reporter, loadYamlNode(content) as YamlMap);
73-
listener.assertErrorsWithCodes(expectedErrorCodes);
7466
}
7567

68+
test_removed_rule_inInclude_ok() {
69+
newFile('/included.yaml', '''
70+
linter:
71+
rules:
72+
- removed_in_2_12_lint
73+
''');
74+
75+
assertErrorsInCode(
76+
'''
77+
include: included.yaml
78+
''',
79+
[],
80+
provider: () => rules,
81+
);
82+
}
83+
}
84+
85+
@reflectiveTest
86+
class OptionsRuleValidatorTest
87+
with OptionsRuleValidatorTestMixin, ResourceProviderMixin {
7688
test_deprecated_rule() {
7789
assertErrors('''
7890
linter:
@@ -231,6 +243,35 @@ linter:
231243
}
232244
}
233245

246+
mixin OptionsRuleValidatorTestMixin {
247+
final rules = [
248+
DeprecatedLint(),
249+
DeprecatedSince3Lint(),
250+
StableLint(),
251+
RuleNeg(),
252+
RulePos(),
253+
RemovedIn2_12Lint(),
254+
ReplacedLint(),
255+
ReplacingLint(),
256+
];
257+
258+
/// Assert that when the validator is used on the given [content] the
259+
/// [expectedErrorCodes] are produced.
260+
void assertErrors(String content, List<ErrorCode> expectedErrorCodes,
261+
{VersionConstraint? sdk}) {
262+
GatheringErrorListener listener = GatheringErrorListener();
263+
ErrorReporter reporter = ErrorReporter(
264+
listener,
265+
StringSource(content, 'analysis_options.yaml'),
266+
isNonNullableByDefault: false,
267+
);
268+
var validator = LinterRuleOptionsValidator(
269+
provider: () => rules, sdkVersionConstraint: sdk);
270+
validator.validate(reporter, loadYamlNode(content) as YamlMap);
271+
listener.assertErrorsWithCodes(expectedErrorCodes);
272+
}
273+
}
274+
234275
class RemovedIn2_12Lint extends LintRule {
235276
RemovedIn2_12Lint()
236277
: super(

pkg/analyzer/test/src/task/options_test.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,11 @@ class ErrorProcessorMatcher extends Matcher {
271271

272272
@reflectiveTest
273273
class OptionsFileValidatorTest {
274-
final OptionsFileValidator validator =
275-
OptionsFileValidator(TestSource(), sdkVersionConstraint: null);
274+
final OptionsFileValidator validator = OptionsFileValidator(
275+
TestSource(),
276+
sdkVersionConstraint: null,
277+
sourceIsOptionsForContextRoot: true,
278+
);
276279
final AnalysisOptionsProvider optionsProvider = AnalysisOptionsProvider();
277280

278281
test_analyzer_cannotIgnore_badValue() {

0 commit comments

Comments
 (0)