Skip to content

Commit 4450840

Browse files
committed
added LocalVariableNameCheck
1 parent 1a3c3a0 commit 4450840

File tree

6 files changed

+175
-1
lines changed

6 files changed

+175
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ More information in [wiki page](https://github.com/adireddy/haxe-checkstyle/wiki
131131
]
132132
}
133133
},
134+
{
135+
"type": "LocalVariableName",
136+
"props": {
137+
"severity": "ERROR",
138+
"format": "^[a-z]+[a-zA-Z0-9]*$"
139+
}
140+
},
134141
{
135142
"type": "MemberName",
136143
"props": {

checkstyle/checks/Check.hx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class Check {
7575
return isCharPosSuppressed(pos);
7676
}
7777

78+
function isPosExtern(pos:Position):Bool {
79+
return isCharPosExtern(pos.min);
80+
}
81+
7882
function isPosSuppressed(pos:Position):Bool {
7983
return isCharPosSuppressed(pos.min);
8084
}
@@ -131,6 +135,37 @@ class Check {
131135
return false;
132136
}
133137

138+
function isCharPosExtern(pos:Int):Bool {
139+
for (td in checker.ast.decls) {
140+
switch (td.decl){
141+
case EAbstract(d):
142+
case EClass(d):
143+
if ((pos <= td.pos.max) && (pos >= td.pos.min)) {
144+
return d.flags.indexOf(HExtern) > -1;
145+
}
146+
case EEnum(d):
147+
if ((pos <= td.pos.max) && (pos >= td.pos.min)) {
148+
return d.flags.indexOf(EExtern) > -1;
149+
}
150+
case ETypedef(d):
151+
if ((pos <= td.pos.max) && (pos >= td.pos.min)) {
152+
return d.flags.indexOf(EExtern) > -1;
153+
}
154+
switch (d.data) {
155+
case TAnonymous(fields):
156+
for (field in fields) {
157+
if (pos > field.pos.max) continue;
158+
if (pos < field.pos.min) continue;
159+
return d.flags.indexOf(EExtern) > -1;
160+
}
161+
default:
162+
}
163+
default:
164+
}
165+
}
166+
return false;
167+
}
168+
134169
function checkSuppressionConst(e:Expr, search:String):Bool {
135170
switch (e.expr) {
136171
case EArrayDecl(a):
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package checkstyle.checks;
2+
3+
import checkstyle.LintMessage.SeverityLevel;
4+
import haxeparser.Data;
5+
import haxe.macro.Expr;
6+
7+
@name("LocalVariableName")
8+
@desc("Checks on naming conventions of local variables")
9+
class LocalVariableNameCheck extends NameCheckBase {
10+
11+
public function new() {
12+
super();
13+
format = "^[a-z]+[a-zA-Z0-9]*$";
14+
}
15+
16+
override function actualRun() {
17+
formatRE = new EReg (format, "");
18+
ExprUtils.walkFile(checker.ast, function(e) {
19+
switch(e.expr) {
20+
case EVars(vars):
21+
if (ignoreExtern && isPosExtern(e.pos)) return;
22+
if (isPosSuppressed(e.pos)) return;
23+
for (v in vars) {
24+
matchTypeName("local var", v.name, e.pos);
25+
}
26+
default:
27+
}
28+
});
29+
}
30+
}

resources/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@
108108
]
109109
}
110110
},
111+
{
112+
"type": "LocalVariableName",
113+
"props": {
114+
"severity": "ERROR",
115+
"format": "^[a-z]+[a-zA-Z0-9]*$"
116+
}
117+
},
111118
{
112119
"type": "MemberName",
113120
"props": {

test/LocalVariableNameCheckTest.hx

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package ;
2+
3+
import checkstyle.checks.LocalVariableNameCheck;
4+
5+
class LocalVariableNameCheckTest extends CheckTestCase {
6+
7+
public function testCorrectNaming() {
8+
var check = new LocalVariableNameCheck ();
9+
assertMsg(check, LocalVariableNameTests.TEST, '');
10+
assertMsg(check, LocalVariableNameTests.TEST4, '');
11+
}
12+
13+
public function testWrongNaming() {
14+
var check = new LocalVariableNameCheck ();
15+
assertMsg(check, LocalVariableNameTests.TEST1, 'Invalid local var signature: Count (name should be ~/^[a-z]+[a-zA-Z0-9]*$/)');
16+
assertMsg(check, LocalVariableNameTests.TEST3, 'Invalid local var signature: Count (name should be ~/^[a-z]+[a-zA-Z0-9]*$/)');
17+
}
18+
19+
public function testIgnoreExtern() {
20+
var check = new LocalVariableNameCheck ();
21+
check.ignoreExtern = false;
22+
23+
assertMsg(check, LocalVariableNameTests.TEST, '');
24+
assertMsg(check, LocalVariableNameTests.TEST1, 'Invalid local var signature: Count (name should be ~/^[a-z]+[a-zA-Z0-9]*$/)');
25+
assertMsg(check, LocalVariableNameTests.TEST3, 'Invalid local var signature: Count (name should be ~/^[a-z]+[a-zA-Z0-9]*$/)');
26+
assertMsg(check, LocalVariableNameTests.TEST4, 'Invalid local var signature: Count (name should be ~/^[a-z]+[a-zA-Z0-9]*$/)');
27+
}
28+
29+
public function testFormat() {
30+
var check = new LocalVariableNameCheck ();
31+
check.format = "^[A-Za-z_]*$";
32+
33+
assertMsg(check, LocalVariableNameTests.TEST, 'Invalid local var signature: count2 (name should be ~/^[A-Za-z_]*$/)');
34+
assertMsg(check, LocalVariableNameTests.TEST1, '');
35+
assertMsg(check, LocalVariableNameTests.TEST3, '');
36+
assertMsg(check, LocalVariableNameTests.TEST4, '');
37+
}
38+
}
39+
40+
class LocalVariableNameTests {
41+
public static inline var TEST:String = "
42+
class Test {
43+
public function test() {
44+
var a:Int;
45+
var b:Int;
46+
}
47+
@SuppressWarnings('checkstyle:LocalVariableName')
48+
public function test() {
49+
var I:Int;
50+
}
51+
}
52+
53+
enum Test2 {
54+
count;
55+
a;
56+
}
57+
58+
typedef Test3 = {
59+
public function test() {
60+
var count1:Int;
61+
var count2:String;
62+
};
63+
@SuppressWarnings('checkstyle:LocalVariableName')
64+
var COUNT6:Int = 1;
65+
}
66+
67+
typedef Test4 = {
68+
@SuppressWarnings('checkstyle:LocalVariableName')
69+
public function test() {
70+
var Count1:Int;
71+
};
72+
}";
73+
74+
public static inline var TEST1:String = "
75+
class Test {
76+
public function test() {
77+
var Count:Int = 1;
78+
}
79+
}";
80+
81+
public static inline var TEST3:String =
82+
"typedef Test = {
83+
public function test() {
84+
var Count:Int;
85+
}
86+
}";
87+
88+
public static inline var TEST4:String =
89+
"extern class Test {
90+
public function test() {
91+
var Count:Int = 1;
92+
}
93+
}";
94+
}

test/TestMain.hx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ class TestMain {
1111
runner.add(new DynamicCheckTest());
1212
runner.add(new EmptyLinesCheckTest());
1313
runner.add(new ERegInstantiationCheckTest());
14+
runner.add(new FileLengthCheckTest());
1415
runner.add(new HexadecimalLiteralsCheckTest());
1516
runner.add(new IndentationCharacterCheckTest());
1617
runner.add(new LineLengthCheckTest());
1718
runner.add(new ListenerNameCheckTest());
18-
runner.add(new FileLengthCheckTest());
19+
runner.add(new LocalVariableNameCheckTest());
1920
runner.add(new MemberNameCheckTest());
2021
runner.add(new MethodLengthCheckTest());
2122
runner.add(new MethodNameCheckTest());

0 commit comments

Comments
 (0)