Skip to content

Commit e87bd68

Browse files
stereotype441Commit Queue
authored andcommitted
[messages] Ignore case in LinterRuleOptionsValidator.
Now that lint name matching is case insensitive (https://dart-review.googlesource.com/c/sdk/+/465964), it makes sense for the LinterRuleOptionsValidator to ignore case when checking the contents of `analysis_options.yaml` files. This paves the way for a follow-up CL that will change the names declared in the linter to all lower case. Change-Id: I6a6a696430f1d99408902b115587ac8c3d22fbe8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/466000 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 688df35 commit e87bd68

File tree

2 files changed

+219
-7
lines changed

2 files changed

+219
-7
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ class LinterRuleOptionsValidator extends OptionsValidator {
6565
this.isPrimarySource = true,
6666
});
6767

68-
AbstractAnalysisRule? getRegisteredLint(String value) => Registry
69-
.ruleRegistry
70-
.rules
71-
.firstWhereOrNull((rule) => rule.name == value);
68+
AbstractAnalysisRule? getRegisteredLint(String value) =>
69+
Registry.ruleRegistry[value];
7270

7371
bool isDeprecatedInCurrentOrEarlierSdk(RuleState state) =>
7472
state.isDeprecated && _beforeCurrentConstraint(state.since);
@@ -182,8 +180,13 @@ class LinterRuleOptionsValidator extends OptionsValidator {
182180
List<_IncompatibleRuleData> incompatibleRules = [];
183181
for (var incompatibleRule in rule.incompatibleRules) {
184182
for (var MapEntry(:key, value: rules) in rules.entries) {
185-
if (rules.map((node) => node.value).contains(incompatibleRule)) {
186-
var list = rules.where((scalar) => scalar.value == incompatibleRule);
183+
if (rules
184+
.map((node) => _safeToLower(node.value))
185+
.contains(incompatibleRule.toLowerCase())) {
186+
var list = rules.where(
187+
(scalar) =>
188+
_safeToLower(scalar.value) == incompatibleRule.toLowerCase(),
189+
);
187190
for (var scalar in list) {
188191
var rule = getRegisteredLint(scalar.value.toString())!;
189192
incompatibleRules.add(
@@ -259,7 +262,9 @@ class LinterRuleOptionsValidator extends OptionsValidator {
259262
);
260263
}
261264
}
262-
if (rules[null]!.map((e) => e.value).contains(ruleData.node.value)) {
265+
if (rules[null]!
266+
.map((e) => _safeToLower(e.value))
267+
.contains(_safeToLower(ruleData.node.value))) {
263268
reporter.atSourceSpan(
264269
ruleData.node.span,
265270
diag.duplicateRule,
@@ -357,6 +362,9 @@ class LinterRuleOptionsValidator extends OptionsValidator {
357362
return seenRules;
358363
}
359364

365+
Object? _safeToLower(Object? value) =>
366+
value is String ? value.toLowerCase() : value;
367+
360368
void _validateRules(
361369
YamlNode? rules,
362370
DiagnosticReporter reporter,

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

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,31 @@ include:
203203
- included1.yaml
204204
- included2.yaml
205205
206+
linter:
207+
rules:
208+
''',
209+
[diag.incompatibleLintIncluded],
210+
);
211+
}
212+
213+
Future<void>
214+
test_incompatible_multiple_include_noLintMainFile_mixedCase() async {
215+
newFile('/included1.yaml', '''
216+
linter:
217+
rules:
218+
- ruLe_neg
219+
''');
220+
newFile('/included2.yaml', '''
221+
linter:
222+
rules:
223+
- rule_poS
224+
''');
225+
assertErrors(
226+
'''
227+
include:
228+
- included1.yaml
229+
- included2.yaml
230+
206231
linter:
207232
rules:
208233
''',
@@ -272,6 +297,36 @@ linter:
272297
''');
273298
}
274299

300+
Future<void> test_incompatible_rule_map_include_mixedCase() async {
301+
var includedCode = TestCode.parse('''
302+
linter:
303+
rules:
304+
[!rulE_neg!]: true
305+
''');
306+
var included = newFile('/included.yaml', includedCode.code);
307+
var testCode = TestCode.parse('''
308+
include: included.yaml
309+
310+
linter:
311+
rules:
312+
[!Rule_pos!]: true
313+
''');
314+
await assertErrorsInCode(testCode.code, [
315+
error(
316+
diag.incompatibleLintFiles,
317+
testCode.range.sourceRange.offset,
318+
testCode.range.sourceRange.length,
319+
contextMessages: [
320+
contextMessage(
321+
included,
322+
includedCode.range.sourceRange.offset,
323+
includedCode.range.sourceRange.length,
324+
),
325+
],
326+
),
327+
]);
328+
}
329+
275330
void test_incompatible_trigger_invalidMap() {
276331
newFile('/included.yaml', '''
277332
linter:
@@ -309,6 +364,24 @@ linter:
309364
);
310365
}
311366

367+
void test_incompatible_unsuportedValue_invalidMap_mixedCase() {
368+
newFile('/included.yaml', '''
369+
linter:
370+
rules:
371+
rUle_neg: true
372+
''');
373+
assertErrors(
374+
'''
375+
include: included.yaml
376+
377+
linter:
378+
rules:
379+
Rule_pos: invalid_value
380+
''',
381+
[diag.unsupportedValue],
382+
);
383+
}
384+
312385
void test_package_import() {
313386
newFile('$otherLib/analysis_options.yaml', '''
314387
linter:
@@ -347,6 +420,18 @@ include: included.yaml
347420
linter:
348421
rules:
349422
- removed_in_2_12_lint
423+
''',
424+
[diag.removedLint],
425+
sdk: dart3_3,
426+
);
427+
}
428+
429+
test_removed_rule_previousSdk_mixedCase() {
430+
assertErrors(
431+
'''
432+
linter:
433+
rules:
434+
- remOved_in_2_12_lint
350435
''',
351436
[diag.removedLint],
352437
sdk: dart3_3,
@@ -379,6 +464,28 @@ linter:
379464
);
380465
}
381466

467+
void test_deprecated_rule_map_mixedCase() {
468+
assertErrors(
469+
'''
470+
linter:
471+
rules:
472+
deprecated_lInt: false
473+
''',
474+
[diag.deprecatedLint],
475+
);
476+
}
477+
478+
void test_deprecated_rule_mixedCase() {
479+
assertErrors(
480+
'''
481+
linter:
482+
rules:
483+
- deprecAted_lint
484+
''',
485+
[diag.deprecatedLint],
486+
);
487+
}
488+
382489
void test_deprecated_rule_previousSDK() {
383490
assertErrors(
384491
'''
@@ -402,6 +509,17 @@ linter:
402509
);
403510
}
404511

512+
void test_deprecated_rule_withReplacement_mixedCase() {
513+
assertErrors(
514+
'''
515+
linter:
516+
rules:
517+
- deprecated_lint_with_rePlacement
518+
''',
519+
[diag.deprecatedLintWithReplacement],
520+
);
521+
}
522+
405523
void test_deprecated_rule_withSince_inCurrentSdk() {
406524
assertErrors(
407525
'''
@@ -442,6 +560,18 @@ linter:
442560
);
443561
}
444562

563+
void test_duplicated_rule_mixedCase() {
564+
assertErrors(
565+
'''
566+
linter:
567+
rules:
568+
- stable_lint
569+
- staBle_lint
570+
''',
571+
[diag.duplicateRule],
572+
);
573+
}
574+
445575
Future<void> test_incompatible_rule() async {
446576
var testCode = TestCode.parse('''
447577
linter:
@@ -497,6 +627,52 @@ linter:
497627
''');
498628
}
499629

630+
Future<void> test_incompatible_rule_map_mixedCase() async {
631+
var testCode = TestCode.parse('''
632+
linter:
633+
rules:
634+
/*[0*/Rule_pos/*0]*/: true
635+
/*[1*/rUle_neg/*1]*/: true
636+
''');
637+
await assertErrorsInCode(testCode.code, [
638+
error(
639+
diag.incompatibleLint,
640+
testCode.ranges.last.sourceRange.offset,
641+
testCode.ranges.last.sourceRange.length,
642+
contextMessages: [
643+
contextMessage(
644+
analysisOptionsFile,
645+
testCode.ranges.first.sourceRange.offset,
646+
testCode.ranges.first.sourceRange.length,
647+
),
648+
],
649+
),
650+
]);
651+
}
652+
653+
Future<void> test_incompatible_rule_mixedCase() async {
654+
var testCode = TestCode.parse('''
655+
linter:
656+
rules:
657+
- /*[0*/rule_Pos/*0]*/
658+
- /*[1*/rule_neG/*1]*/
659+
''');
660+
await assertErrorsInCode(testCode.code, [
661+
error(
662+
diag.incompatibleLint,
663+
testCode.ranges.last.sourceRange.offset,
664+
testCode.ranges.last.sourceRange.length,
665+
contextMessages: [
666+
contextMessage(
667+
analysisOptionsFile,
668+
testCode.ranges.first.sourceRange.offset,
669+
testCode.ranges.first.sourceRange.length,
670+
),
671+
],
672+
),
673+
]);
674+
}
675+
500676
void test_no_duplicated_rule_include() {
501677
newFile('/included.yaml', '''
502678
linter:
@@ -544,6 +720,18 @@ linter:
544720
);
545721
}
546722

723+
void test_replaced_rule_mixedCase() {
724+
assertErrors(
725+
'''
726+
linter:
727+
rules:
728+
- replaCed_lint
729+
''',
730+
[diag.replacedLint],
731+
sdk: dart3,
732+
);
733+
}
734+
547735
void test_stable_rule() {
548736
assertNoErrors('''
549737
linter:
@@ -560,6 +748,22 @@ linter:
560748
''');
561749
}
562750

751+
void test_stable_rule_map_mixedCase() {
752+
assertNoErrors('''
753+
linter:
754+
rules:
755+
sTable_lint: true
756+
''');
757+
}
758+
759+
void test_stable_rule_mixedCase() {
760+
assertNoErrors('''
761+
linter:
762+
rules:
763+
- Stable_lint
764+
''');
765+
}
766+
563767
void test_undefined_rule() {
564768
assertErrors(
565769
'''

0 commit comments

Comments
 (0)