Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit 61818a5

Browse files
authored
pattern support for no_leading_underscores_for_local_identifiers (#4133)
* init * ++ * sort * fmt * operand fixes * re-add * => visitDeclaredVariablePattern
1 parent a056a0a commit 61818a5

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

lib/src/rules/no_leading_underscores_for_local_identifiers.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class NoLeadingUnderscoresForLocalIdentifiers extends LintRule {
7575
registry.addFormalParameterList(this, visitor);
7676
registry.addForPartsWithDeclarations(this, visitor);
7777
registry.addFunctionDeclarationStatement(this, visitor);
78+
registry.addDeclaredVariablePattern(this, visitor);
7879
registry.addVariableDeclarationStatement(this, visitor);
7980
}
8081
}
@@ -103,6 +104,11 @@ class _Visitor extends SimpleAstVisitor<void> {
103104
checkIdentifier(node.name);
104105
}
105106

107+
@override
108+
void visitDeclaredVariablePattern(DeclaredVariablePattern node) {
109+
checkIdentifier(node.name);
110+
}
111+
106112
@override
107113
void visitFormalParameterList(FormalParameterList node) {
108114
for (var parameter in node.parameters) {

test/rules/all.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ import 'literal_only_boolean_expressions_test.dart'
5555
import 'missing_whitespace_between_adjacent_strings_test.dart'
5656
as missing_whitespace_between_adjacent_strings;
5757
import 'no_duplicate_case_values_test.dart' as no_duplicate_case_values;
58+
import 'no_leading_underscores_for_local_identifiers_test.dart'
59+
as no_leading_underscores_for_local_identifiers;
5860
import 'non_adjacent_strings_in_list_test.dart' as no_adjacent_strings_in_list;
5961
import 'non_constant_identifier_names_test.dart'
6062
as non_constant_identifier_names;
@@ -158,6 +160,7 @@ void main() {
158160
missing_whitespace_between_adjacent_strings.main();
159161
no_adjacent_strings_in_list.main();
160162
no_duplicate_case_values.main();
163+
no_leading_underscores_for_local_identifiers.main();
161164
non_constant_identifier_names.main();
162165
null_check_on_nullable_type_parameter.main();
163166
null_closures.main();
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test_reflective_loader/test_reflective_loader.dart';
6+
7+
import '../rule_test_support.dart';
8+
9+
main() {
10+
defineReflectiveSuite(() {
11+
defineReflectiveTests(NoLeadingUnderscoresForLocalIdentifiersTest);
12+
});
13+
}
14+
15+
@reflectiveTest
16+
class NoLeadingUnderscoresForLocalIdentifiersTest extends LintRuleTest {
17+
@override
18+
List<String> get experiments => ['patterns', 'records'];
19+
20+
@override
21+
String get lintRule => 'no_leading_underscores_for_local_identifiers';
22+
23+
test_listPattern_switch() async {
24+
await assertDiagnostics(r'''
25+
f() {
26+
switch ([1,2]) {
27+
case [1 && var _a, 2 && var b]: print('$_a$b');
28+
}
29+
}
30+
''', [
31+
lint(44, 2),
32+
]);
33+
}
34+
35+
test_listPattern_switch_leftOperand() async {
36+
await assertDiagnostics(r'''
37+
f() {
38+
switch ([1,2]) {
39+
case [var _a && 1, 2 && var b]: print('$_a$b');
40+
}
41+
}
42+
''', [
43+
lint(39, 2),
44+
]);
45+
}
46+
47+
test_mapPattern_destructured() async {
48+
await assertDiagnostics(r'''
49+
f() {
50+
final {'first': _a, 'second': b} = {'first': 1, 'second': 2};
51+
print('$_a$b');
52+
}
53+
''', [
54+
lint(24, 2),
55+
]);
56+
}
57+
58+
test_mapPattern_switch() async {
59+
await assertDiagnostics(r'''
60+
f() {
61+
switch ({1: 2}) {
62+
case {'a': var _a, 'b': var b} : print('$_a$b');
63+
}
64+
}
65+
''', [
66+
lint(45, 2),
67+
]);
68+
}
69+
70+
test_objectPattern_destructured() async {
71+
await assertDiagnostics(r'''
72+
class A {
73+
int a;
74+
A(this.a);
75+
}
76+
f() {
77+
final A(a: _b) = A(1);
78+
print('$_b');
79+
}
80+
''', [
81+
lint(53, 2),
82+
]);
83+
}
84+
85+
test_objectPattern_switch() async {
86+
await assertDiagnostics(r'''
87+
class A {
88+
int a;
89+
A(this.a);
90+
}
91+
f() {
92+
switch (A(1)) {
93+
case A(a: >0 && var _b): print('$_b');
94+
}
95+
}
96+
''', [
97+
lint(82, 2),
98+
]);
99+
}
100+
101+
test_recordPattern_destructured() async {
102+
await assertDiagnostics(r'''
103+
f() {
104+
var (_a, b) = ('a', 'b');
105+
print('$_a$b');
106+
}
107+
''', [
108+
lint(13, 2),
109+
]);
110+
}
111+
112+
test_recordPattern_switch() async {
113+
await assertDiagnostics(r'''
114+
f() {
115+
switch ((1, 2)) {
116+
case (var _a, var b): print('$_a$b');
117+
}
118+
}
119+
''', [
120+
lint(40, 2),
121+
]);
122+
}
123+
}

0 commit comments

Comments
 (0)