Skip to content

Commit 7050946

Browse files
authored
fix constant_identifier_names pattern field over-reporting (#4298)
* fix `constant_identifier_names` pattern field over-reporting * ++
1 parent ae3501b commit 7050946

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

lib/src/extensions.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ class EnumLikeClassDescription {
2121
Map<DartObject, Set<FieldElement>> get enumConstants => {..._enumConstants};
2222
}
2323

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+
2433
extension AstNodeExtension on AstNode {
2534
Iterable<AstNode> get childNodes => childEntities.whereType<AstNode>();
2635

lib/src/rules/constant_identifier_names.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/token.dart';
77
import 'package:analyzer/dart/ast/visitor.dart';
88

99
import '../analyzer.dart';
10+
import '../extensions.dart';
1011
import '../utils.dart';
1112

1213
const _desc = r'Prefer using lowerCamelCase for constant names.';
@@ -84,6 +85,7 @@ class _Visitor extends SimpleAstVisitor<void> {
8485

8586
@override
8687
void visitDeclaredVariablePattern(DeclaredVariablePattern node) {
88+
if (node.parent.isFieldNameShortcut) return;
8789
checkIdentifier(node.name);
8890
}
8991

lib/src/rules/no_leading_underscores_for_local_identifiers.dart

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/token.dart';
77
import 'package:analyzer/dart/ast/visitor.dart';
88

99
import '../analyzer.dart';
10+
import '../extensions.dart';
1011
import '../util/ascii_utils.dart';
1112

1213
const _desc = r'Avoid leading underscores for local identifiers.';
@@ -146,12 +147,3 @@ class _Visitor extends SimpleAstVisitor<void> {
146147
}
147148
}
148149
}
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-
}

test/rules/constant_identifier_names_test.dart

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,71 @@ void f() {
4545
lint(20, 2),
4646
]);
4747
}
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+
}
4888
}
4989

5090
@reflectiveTest
5191
class ConstantIdentifierNamesRecordsTest extends LintRuleTest {
5292
@override
5393
String get lintRule => 'constant_identifier_names';
5494

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+
55113
test_recordTypeDeclarations() async {
56114
await assertDiagnostics(r'''
57115
const RR = (x: 1);

0 commit comments

Comments
 (0)