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

Commit 0c44862

Browse files
authored
lowerCamelCase check local variables (#2579)
* lowerCamelCase check local variables * +foreach + catch clauses
1 parent 94d02e3 commit 0c44862

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

lib/src/rules/non_constant_identifier_names.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ class NonConstantIdentifierNames extends LintRule implements NodeLintRule {
4343
void registerNodeProcessors(
4444
NodeLintRegistry registry, LinterContext context) {
4545
var visitor = _Visitor(this);
46+
registry.addCatchClause(this, visitor);
4647
registry.addConstructorDeclaration(this, visitor);
48+
registry.addForEachPartsWithDeclaration(this, visitor);
4749
registry.addFormalParameterList(this, visitor);
4850
registry.addFunctionDeclaration(this, visitor);
4951
registry.addMethodDeclaration(this, visitor);
5052
registry.addVariableDeclaration(this, visitor);
53+
registry.addVariableDeclarationStatement(this, visitor);
5154
}
5255
}
5356

@@ -69,13 +72,23 @@ class _Visitor extends SimpleAstVisitor<void> {
6972
}
7073
}
7174

75+
@override
76+
void visitCatchClause(CatchClause node) {
77+
checkIdentifier(node.exceptionParameter, underscoresOk: true);
78+
}
79+
7280
@override
7381
void visitConstructorDeclaration(ConstructorDeclaration node) {
7482
// For rationale on accepting underscores, see:
7583
// https://github.com/dart-lang/linter/issues/1854
7684
checkIdentifier(node.name, underscoresOk: true);
7785
}
7886

87+
@override
88+
void visitForEachPartsWithDeclaration(ForEachPartsWithDeclaration node) {
89+
checkIdentifier(node.loopVariable.identifier);
90+
}
91+
7992
@override
8093
void visitFormalParameterList(FormalParameterList node) {
8194
node.parameters.forEach((FormalParameter p) {
@@ -103,4 +116,13 @@ class _Visitor extends SimpleAstVisitor<void> {
103116
checkIdentifier(node.name);
104117
}
105118
}
119+
120+
@override
121+
void visitVariableDeclarationStatement(VariableDeclarationStatement node) {
122+
for (var variable in node.variables.variables) {
123+
if (!variable.isConst) {
124+
checkIdentifier(variable.name);
125+
}
126+
}
127+
}
106128
}

test/rules/non_constant_identifier_names.dart

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44

55
// test w/ `dart test -N non_constant_identifier_names`
66

7+
void main() {
8+
var XYZ = 1; // LINT
9+
var ABC = 1, // LINT
10+
def = 1;
11+
12+
const ZZZ = 3; // OK
13+
14+
try {
15+
for (var XYZ in []) // LINT
16+
{
17+
print(XYZ);
18+
}
19+
for (var II = 0; II < [].length; ++II) // LINT
20+
{
21+
print(II);
22+
}
23+
} on Exception catch (EE) //LINT
24+
{
25+
print(EE);
26+
} on Object catch (_) //OK
27+
{}
28+
}
29+
730
String YO = ''; //LINT
831
const Z = 4; //OK
932

@@ -26,9 +49,9 @@ void _funBar() {} //OK
2649
void _fun_bar() {} //LINT
2750

2851
abstract class A {
29-
int _x; //OK
30-
int __x; //OK
31-
int X; //OK
52+
int? _x; //OK
53+
int? __x; //OK
54+
int? X; //OK
3255
static const Y = 3; // OK
3356

3457
final String bar_bar; //LINT

0 commit comments

Comments
 (0)