Skip to content

Commit 9ce1a07

Browse files
committed
added ConstantNameCheck, MemberNameCheck and TypeNameCheck
1 parent 4380bf2 commit 9ce1a07

File tree

7 files changed

+392
-0
lines changed

7 files changed

+392
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ More information in [wiki page](https://github.com/adireddy/haxe-checkstyle/wiki
4141
"severity": "ERROR"
4242
}
4343
},
44+
{
45+
"type": "ConstantName",
46+
"props": {
47+
"severity": "ERROR",
48+
"format": "^[A-Z][A-Z0-9]*(_[A-Z0-9_]+)*$",
49+
"tokens": [ "INLINE" ]
50+
}
51+
},
52+
{
53+
"type": "ConstantName",
54+
"props": {
55+
"severity": "ERROR",
56+
"format": "^[a-z]+[a-zA-Z0-9_]*$",
57+
"tokens": [ "NOTINLINE" ]
58+
}
59+
},
4460
{
4561
"type": "EmptyLines",
4662
"props": {
@@ -88,6 +104,22 @@ More information in [wiki page](https://github.com/adireddy/haxe-checkstyle/wiki
88104
"listeners": ["addEventListener", "addListener", "on", "once"]
89105
}
90106
},
107+
{
108+
"type": "MemberName",
109+
"props": {
110+
"severity": "ERROR",
111+
"format": "^[A-Z]+[A-Z0-9_]*$",
112+
"tokens": [ "ENUM" ]
113+
}
114+
},
115+
{
116+
"type": "MemberName",
117+
"props": {
118+
"severity": "ERROR",
119+
"format": "^[a-z]+[a-zA-Z0-9_]*$",
120+
"tokens": [ "PUBLIC", "PRIVATE", "TYPEDEF" ]
121+
}
122+
},
91123
{
92124
"type": "MethodLength",
93125
"props": {
@@ -186,6 +218,22 @@ More information in [wiki page](https://github.com/adireddy/haxe-checkstyle/wiki
186218
"severity": "ERROR"
187219
}
188220
},
221+
{
222+
"type": "TypeName",
223+
"props": {
224+
"severity": "ERROR",
225+
"format": "^I[A-Z]+[a-zA-Z0-9_]*$",
226+
"tokens": [ "INTERFACE" ]
227+
}
228+
},
229+
{
230+
"type": "TypeName",
231+
"props": {
232+
"severity": "ERROR",
233+
"format": "^[A-Z]+[a-zA-Z0-9_]*$",
234+
"tokens": [ "CLASS", "ENUM", "TYPEDEF" ]
235+
}
236+
},
189237
{
190238
"type": "VariableInitialisation",
191239
"props": {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package checkstyle.checks;
2+
3+
import checkstyle.LintMessage.SeverityLevel;
4+
import haxeparser.Data;
5+
import haxe.macro.Expr;
6+
7+
@name("ConstantName")
8+
@desc("Checks on naming conventions of constants (static / static inline with initialisation)")
9+
class ConstantNameCheck extends NameCheckBase {
10+
11+
public static inline var INLINE:String = "INLINE";
12+
public static inline var NOTINLINE:String = "NOTINLINE";
13+
14+
public function new() {
15+
super();
16+
severity = "ERROR";
17+
format = "^[A-Z][A-Z0-9]*(_[A-Z0-9_]+)*$";
18+
}
19+
20+
override function checkClassType(d:Definition<ClassFlag, Array<Field>>, pos:Position) {
21+
if (ignoreExtern && (d.flags.indexOf (HExtern) > -1)) return;
22+
checkFields (d.data);
23+
}
24+
25+
override function checkEnumType(d:Definition<EnumFlag, Array<EnumConstructor>>, pos:Position) {}
26+
27+
override function checkAbstractType(d:Definition<AbstractFlag, Array<Field>>, pos:Position) {
28+
checkFields (d.data);
29+
}
30+
31+
override function checkTypedefType(d:Definition<EnumFlag, ComplexType>, pos:Position) {}
32+
33+
function checkFields(d:Array<Field>) {
34+
for (field in d) {
35+
switch (field.kind) {
36+
case FVar (t, e):
37+
checkField(field, t, e);
38+
default:
39+
}
40+
}
41+
}
42+
43+
function checkField(f:Field, t:ComplexType, e:Expr) {
44+
45+
if (e == null || e.expr == null) return;
46+
var access = getFieldAccess (f);
47+
48+
if (!access.isStatic) return;
49+
if (!hasToken (INLINE) && access.isInline) return;
50+
if (!hasToken (NOTINLINE) && !access.isInline) return;
51+
52+
matchTypeName ("const", f.name, f.pos);
53+
}
54+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package checkstyle.checks;
2+
3+
import checkstyle.LintMessage.SeverityLevel;
4+
import haxeparser.Data;
5+
import haxe.macro.Expr;
6+
7+
@name("MemberName")
8+
@desc("Checks on naming conventions of non-static fields")
9+
class MemberNameCheck extends NameCheckBase {
10+
11+
public static inline var PUBLIC:String = "PUBLIC";
12+
public static inline var PRIVATE:String = "PRIVATE";
13+
public static inline var ENUM:String = "ENUM";
14+
public static inline var TYPEDEF:String = "TYPEDEF";
15+
16+
public function new() {
17+
super();
18+
severity = "ERROR";
19+
format = "^[a-z]+[a-zA-Z0-9_]*$";
20+
}
21+
22+
override function checkClassType(d:Definition<ClassFlag, Array<Field>>, pos:Position) {
23+
if (ignoreExtern && (d.flags.indexOf (HExtern) > -1)) return;
24+
checkFields (d.data);
25+
}
26+
27+
override function checkEnumType(d:Definition<EnumFlag, Array<EnumConstructor>>, pos:Position) {
28+
if (!hasToken (ENUM)) return;
29+
if (ignoreExtern && (d.flags.indexOf (EExtern) > -1)) return;
30+
checkEnumFields (d.data);
31+
}
32+
33+
override function checkAbstractType(d:Definition<AbstractFlag, Array<Field>>, pos:Position) {
34+
checkFields (d.data);
35+
}
36+
37+
override function checkTypedefType(d:Definition<EnumFlag, ComplexType>, pos:Position) {
38+
if (!hasToken (TYPEDEF)) return;
39+
if (ignoreExtern && (d.flags.indexOf (EExtern) > -1)) return;
40+
41+
switch (d.data) {
42+
case TAnonymous (f):
43+
checkTypedefFields (f);
44+
default:
45+
}
46+
}
47+
48+
function checkFields(d:Array<Field>) {
49+
for (field in d) {
50+
switch (field.kind) {
51+
case FVar (t, e):
52+
checkField (field, t, e);
53+
default:
54+
}
55+
}
56+
}
57+
58+
function checkTypedefFields(d:Array<Field>) {
59+
for (field in d) {
60+
switch (field.kind) {
61+
case FVar (t, e):
62+
checkTypedefField (field, t, e);
63+
default:
64+
}
65+
}
66+
}
67+
68+
function checkEnumFields(d:Array<EnumConstructor>) {
69+
for (field in d) {
70+
matchTypeName ("enum member", field.name, field.pos);
71+
}
72+
}
73+
74+
function checkField(f:Field, t:ComplexType, e:Expr) {
75+
var access = getFieldAccess (f);
76+
77+
if (access.isStatic) return;
78+
if (!hasToken (PUBLIC) && access.isPublic) return;
79+
if (!hasToken (PRIVATE) && access.isPrivate) return;
80+
81+
matchTypeName ("member", f.name, f.pos);
82+
}
83+
84+
function checkTypedefField(f:Field, t:ComplexType, e:Expr) {
85+
matchTypeName ("typedef member", f.name, f.pos);
86+
}
87+
}

checkstyle/checks/NameCheckBase.hx

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package checkstyle.checks;
2+
3+
import checkstyle.LintMessage.SeverityLevel;
4+
import haxeparser.Data;
5+
import haxe.macro.Expr;
6+
7+
// TODO find a better way to make ChecksInfo ignore this class
8+
@name("")
9+
@desc("")
10+
class NameCheckBase extends Check {
11+
12+
public var severity:String;
13+
public var format:String;
14+
public var tokens:Array<String>;
15+
public var ignoreExtern:Bool;
16+
17+
var formatRE:EReg;
18+
19+
public function new() {
20+
super();
21+
severity = "ERROR";
22+
format = "^.*$";
23+
tokens = [];
24+
ignoreExtern = true;
25+
}
26+
27+
function hasToken(token:String):Bool {
28+
if (tokens.length == 0) return true;
29+
if (tokens.indexOf (token) > -1) return true;
30+
return false;
31+
}
32+
33+
override function _actualRun() {
34+
formatRE = new EReg(format, "");
35+
checkClassFields();
36+
}
37+
38+
function checkClassFields() {
39+
for (td in _checker.ast.decls) {
40+
switch (td.decl) {
41+
case EClass(d):
42+
checkClassType(d, td.pos);
43+
case EEnum(d):
44+
checkEnumType(d, td.pos);
45+
case EAbstract(d):
46+
checkAbstractType(d, td.pos);
47+
case ETypedef(d):
48+
checkTypedefType(d, td.pos);
49+
default:
50+
}
51+
}
52+
}
53+
54+
function checkClassType(d:Definition<ClassFlag, Array<Field>>, pos:Position) {}
55+
56+
function checkEnumType(d:Definition<EnumFlag, Array<EnumConstructor>>, pos:Position) {}
57+
58+
function checkAbstractType(d:Definition<AbstractFlag, Array<Field>>, pos:Position) {}
59+
60+
function checkTypedefType(d:Definition<EnumFlag, ComplexType>, pos:Position) {}
61+
62+
function getFieldAccess(f:Field):NameFieldAccess {
63+
var isPrivate = false;
64+
var isPublic = false;
65+
var isInline = false;
66+
var isStatic = false;
67+
68+
if (f.access.indexOf(AInline) > -1) isInline = true;
69+
if (f.access.indexOf(AStatic) > -1) isStatic = true;
70+
if (f.access.indexOf(APublic) > -1) isPublic = true;
71+
else isPrivate = true;
72+
return {
73+
isPrivate: isPrivate,
74+
isPublic: isPublic,
75+
isInline: isInline,
76+
isStatic: isStatic
77+
};
78+
}
79+
80+
function matchTypeName(type:String, name:String, pos:Position)
81+
{
82+
if (!formatRE.match(name)) {
83+
_warn(type, name, pos);
84+
}
85+
}
86+
87+
function _warn(type:String, name:String, pos:Position) {
88+
logPos('Invalid ${type} signature: ${name} (name should be ~/${format}/)', pos, Reflect.field(SeverityLevel, severity));
89+
}
90+
}
91+
92+
typedef NameFieldAccess = {
93+
var isPrivate:Bool;
94+
var isPublic:Bool;
95+
var isInline:Bool;
96+
var isStatic:Bool;
97+
};

checkstyle/checks/TypeNameCheck.hx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package checkstyle.checks;
2+
3+
import checkstyle.LintMessage.SeverityLevel;
4+
import haxeparser.Data;
5+
import haxe.macro.Expr;
6+
7+
@name("TypeName")
8+
@desc("Checks on naming conventions of types (classes, interfaces, enums, typedefs)")
9+
class TypeNameCheck extends NameCheckBase {
10+
11+
public static inline var INTERFACE:String = "INTERFACE";
12+
public static inline var CLAZZ:String = "CLASS";
13+
public static inline var ENUM:String = "ENUM";
14+
public static inline var ABSTRACT:String = "ABSTRACT";
15+
public static inline var TYPEDEF:String = "TYPEDEF";
16+
17+
public function new() {
18+
super();
19+
severity = "ERROR";
20+
format = "^[A-Z]+[a-zA-Z0-9_]*$";
21+
}
22+
23+
override function checkClassType(d:Definition<ClassFlag, Array<Field>>, pos:Position) {
24+
if (ignoreExtern && (d.flags.indexOf (HExtern) > -1)) return;
25+
26+
var isInterface:Bool = (d.flags.indexOf (HInterface) > -1);
27+
28+
if (!hasToken (INTERFACE) && isInterface) return;
29+
if (!hasToken (CLAZZ) && !isInterface) return;
30+
if (isInterface)
31+
{
32+
matchTypeName ("interface", d.name, pos);
33+
}
34+
else
35+
{
36+
matchTypeName ("class", d.name, pos);
37+
}
38+
}
39+
40+
override function checkEnumType(d:Definition<EnumFlag, Array<EnumConstructor>>, pos:Position) {
41+
if (!hasToken (ENUM)) return;
42+
if (ignoreExtern && (d.flags.indexOf (EExtern) > -1)) return;
43+
44+
matchTypeName ("enum", d.name, pos);
45+
}
46+
47+
override function checkAbstractType(d:Definition<AbstractFlag, Array<Field>>, pos:Position) {
48+
if (!hasToken (ABSTRACT)) return;
49+
matchTypeName ("abstract", d.name, pos);
50+
}
51+
52+
override function checkTypedefType(d:Definition<EnumFlag, ComplexType>, pos:Position) {
53+
if (!hasToken (TYPEDEF)) return;
54+
if (ignoreExtern && (d.flags.indexOf (EExtern) > -1)) return;
55+
56+
matchTypeName ("typedef", d.name, pos);
57+
}
58+
}

0 commit comments

Comments
 (0)