[analyzer] fix diagnostics for lints map

Fixes #49651

Change-Id: I87b14e407e699da8b9247eed93d957e2ba12e22d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/257520
Reviewed-by: William Hesse <whesse@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Ahmed Ashour 2022-09-20 17:11:35 +00:00 committed by Commit Bot
parent de68893488
commit 6e03189e2b
3 changed files with 83 additions and 32 deletions

View file

@ -71,47 +71,55 @@ class LinterRuleOptionsValidator extends OptionsValidator {
var node = options.valueAt(linter);
if (node is YamlMap) {
var rules = node.valueAt(rulesKey);
validateRules(rules, reporter);
_validateRules(rules, reporter);
}
return errors;
}
void validateRules(YamlNode? rules, ErrorReporter reporter) {
if (rules is YamlList) {
final seenRules = <String>{};
void _validateRules(YamlNode? rules, ErrorReporter reporter) {
final seenRules = <String>{};
String? findIncompatibleRule(LintRule rule) {
for (var incompatibleRule in rule.incompatibleRules) {
if (seenRules.contains(incompatibleRule)) {
return incompatibleRule;
}
String? findIncompatibleRule(LintRule rule) {
for (var incompatibleRule in rule.incompatibleRules) {
if (seenRules.contains(incompatibleRule)) {
return incompatibleRule;
}
return null;
}
return null;
}
void validateRule(YamlNode node, bool enabled) {
var value = node.value;
if (value == null) return;
final rule = getRegisteredLint(value);
if (rule == null) {
reporter.reportErrorForSpan(UNDEFINED_LINT_WARNING, node.span, [value]);
return;
}
for (var ruleNode in rules.nodes) {
final value = ruleNode.value;
if (value != null) {
final rule = getRegisteredLint(value);
if (rule == null) {
reporter.reportErrorForSpan(
UNDEFINED_LINT_WARNING, ruleNode.span, [value]);
continue;
}
final incompatibleRule = findIncompatibleRule(rule);
if (incompatibleRule != null) {
reporter.reportErrorForSpan(INCOMPATIBLE_LINT_WARNING,
ruleNode.span, [value, incompatibleRule]);
} else if (!seenRules.add(rule.name)) {
reporter.reportErrorForSpan(
DUPLICATE_RULE_HINT, ruleNode.span, [value]);
} else if (rule.maturity == Maturity.deprecated) {
reporter.reportErrorForSpan(
DEPRECATED_LINT_HINT, ruleNode.span, [value]);
}
if (enabled) {
final incompatibleRule = findIncompatibleRule(rule);
if (incompatibleRule != null) {
reporter.reportErrorForSpan(
INCOMPATIBLE_LINT_WARNING, node.span, [value, incompatibleRule]);
} else if (!seenRules.add(rule.name)) {
reporter.reportErrorForSpan(DUPLICATE_RULE_HINT, node.span, [value]);
}
}
if (rule.maturity == Maturity.deprecated) {
reporter.reportErrorForSpan(DEPRECATED_LINT_HINT, node.span, [value]);
}
}
if (rules is YamlList) {
for (var ruleNode in rules.nodes) {
validateRule(ruleNode, true);
}
} else if (rules is YamlMap) {
for (var ruleEntry in rules.nodeMap.entries) {
validateRule(ruleEntry.key, ruleEntry.value.value);
}
}
}
}

View file

@ -56,6 +56,14 @@ linter:
''', [DEPRECATED_LINT_HINT]);
}
test_deprecated_rule_map() {
assertErrors('''
linter:
rules:
deprecated_lint: false
''', [DEPRECATED_LINT_HINT]);
}
test_duplicated_rule() {
assertErrors('''
linter:
@ -74,6 +82,24 @@ linter:
''', [INCOMPATIBLE_LINT_WARNING]);
}
test_incompatible_rule_map() {
assertErrors('''
linter:
rules:
rule_pos: true
rule_neg: true
''', [INCOMPATIBLE_LINT_WARNING]);
}
test_incompatible_rule_map_disabled() {
assertErrors('''
linter:
rules:
rule_pos: true
rule_neg: false
''', []);
}
test_stable_rule() {
assertErrors('''
linter:
@ -82,6 +108,14 @@ linter:
''', []);
}
test_stable_rule_map() {
assertErrors('''
linter:
rules:
stable_lint: true
''', []);
}
test_undefined_rule() {
assertErrors('''
linter:
@ -89,6 +123,14 @@ linter:
- this_rule_does_not_exist
''', [UNDEFINED_LINT_WARNING]);
}
test_undefined_rule_map() {
assertErrors('''
linter:
rules:
this_rule_does_not_exist: false
''', [UNDEFINED_LINT_WARNING]);
}
}
class RuleNeg extends LintRule {
@ -99,6 +141,7 @@ class RuleNeg extends LintRule {
description: '',
details: '',
);
@override
List<String> get incompatibleRules => ['rule_pos'];
}
@ -111,6 +154,7 @@ class RulePos extends LintRule {
description: '',
details: '',
);
@override
List<String> get incompatibleRules => ['rule_neg'];
}

View file

@ -12,7 +12,6 @@ linter:
cancel_subscriptions: true
comment_references: true
directives_ordering: true
invariant_booleans: true
omit_local_variable_types: true
package_api_docs: true
prefer_const_constructors: true