File tree Expand file tree Collapse file tree 4 files changed +70
-9
lines changed Expand file tree Collapse file tree 4 files changed +70
-9
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,15 @@ class EnumLikeClassDescription {
21
21
Map <DartObject , Set <FieldElement >> get enumConstants => {..._enumConstants};
22
22
}
23
23
24
+ extension AstNodeNullableExtension on AstNode ? {
25
+ bool get isFieldNameShortcut {
26
+ var node = this ;
27
+ if (node is NullCheckPattern ) node = node.parent;
28
+ if (node is NullAssertPattern ) node = node.parent;
29
+ return node is PatternField && node.name != null && node.name? .name == null ;
30
+ }
31
+ }
32
+
24
33
extension AstNodeExtension on AstNode {
25
34
Iterable <AstNode > get childNodes => childEntities.whereType <AstNode >();
26
35
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/token.dart';
7
7
import 'package:analyzer/dart/ast/visitor.dart' ;
8
8
9
9
import '../analyzer.dart' ;
10
+ import '../extensions.dart' ;
10
11
import '../utils.dart' ;
11
12
12
13
const _desc = r'Prefer using lowerCamelCase for constant names.' ;
@@ -84,6 +85,7 @@ class _Visitor extends SimpleAstVisitor<void> {
84
85
85
86
@override
86
87
void visitDeclaredVariablePattern (DeclaredVariablePattern node) {
88
+ if (node.parent.isFieldNameShortcut) return ;
87
89
checkIdentifier (node.name);
88
90
}
89
91
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/token.dart';
7
7
import 'package:analyzer/dart/ast/visitor.dart' ;
8
8
9
9
import '../analyzer.dart' ;
10
+ import '../extensions.dart' ;
10
11
import '../util/ascii_utils.dart' ;
11
12
12
13
const _desc = r'Avoid leading underscores for local identifiers.' ;
@@ -146,12 +147,3 @@ class _Visitor extends SimpleAstVisitor<void> {
146
147
}
147
148
}
148
149
}
149
-
150
- extension on AstNode ? {
151
- bool get isFieldNameShortcut {
152
- var node = this ;
153
- if (node is NullCheckPattern ) node = node.parent;
154
- if (node is NullAssertPattern ) node = node.parent;
155
- return node is PatternField && node.name != null && node.name? .name == null ;
156
- }
157
- }
Original file line number Diff line number Diff line change @@ -45,13 +45,71 @@ void f() {
45
45
lint (20 , 2 ),
46
46
]);
47
47
}
48
+
49
+ test_destructuredObjectField_switch () async {
50
+ await assertDiagnostics (r'''
51
+ class A {
52
+ var a;
53
+ }
54
+
55
+ f(A a) {
56
+ switch (a) {
57
+ case A(a: int a_b):
58
+ }
59
+ switch (a) {
60
+ case A(a: int a_b?):
61
+ case A(a: int a_b!):
62
+ }
63
+ }
64
+ ''' , [
65
+ lint (64 , 3 ),
66
+ lint (107 , 3 ),
67
+ lint (132 , 3 ),
68
+ ]);
69
+ }
70
+
71
+ test_destructuredObjectField_switch_ok () async {
72
+ await assertNoDiagnostics (r'''
73
+ class A {
74
+ var a_b;
75
+ }
76
+
77
+ f(A a) {
78
+ switch (a) {
79
+ case A(:var a_b):
80
+ }
81
+ switch (a) {
82
+ case A(:var a_b?):
83
+ case A(:var a_b!):
84
+ }
85
+ }
86
+ ''' );
87
+ }
48
88
}
49
89
50
90
@reflectiveTest
51
91
class ConstantIdentifierNamesRecordsTest extends LintRuleTest {
52
92
@override
53
93
String get lintRule => 'constant_identifier_names' ;
54
94
95
+ test_recordFieldDestructured () async {
96
+ await assertDiagnostics (r'''
97
+ f(Object o) {
98
+ if (o case (x: int x_x, z: int z)) { }
99
+ }
100
+ ''' , [
101
+ lint (35 , 3 ),
102
+ ]);
103
+ }
104
+
105
+ test_recordFieldDestructured_ok () async {
106
+ await assertNoDiagnostics (r'''
107
+ f(Object o) {
108
+ if (o case (x_y: int x, z: int z)) { }
109
+ }
110
+ ''' );
111
+ }
112
+
55
113
test_recordTypeDeclarations () async {
56
114
await assertDiagnostics (r'''
57
115
const RR = (x: 1);
You can’t perform that action at this time.
0 commit comments