Skip to content

Commit 93caff9

Browse files
committed
added anonymous structure check
1 parent 0452e02 commit 93caff9

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package checkstyle.checks;
2+
3+
import checkstyle.LintMessage.SeverityLevel;
4+
import haxeparser.Data;
5+
import haxe.macro.Expr;
6+
7+
@name("Anonymous")
8+
@desc("Anonymous type structures check")
9+
class AnonymousCheck extends Check {
10+
11+
public var severity:String = "ERROR";
12+
13+
override function actualRun() {
14+
checkClassFields();
15+
checkLocalVars();
16+
}
17+
18+
function checkClassFields() {
19+
for (td in _checker.ast.decls) {
20+
switch (td.decl){
21+
case EClass(d):
22+
for (field in d.data) if (field.name != "new") checkField(field);
23+
default:
24+
}
25+
}
26+
}
27+
28+
function checkField(f:Field) {
29+
if (Std.string(f.kind).indexOf("TAnonymous") > -1) _error(f.name, f.pos);
30+
}
31+
32+
function checkLocalVars() {
33+
ExprUtils.walkFile(_checker.ast, function(e) {
34+
switch(e.expr){
35+
case EVars(vars):
36+
for (v in vars) if (Std.string(v).indexOf("TAnonymous") > -1) _error(v.name, e.pos);
37+
default:
38+
}
39+
});
40+
}
41+
42+
function _error(name:String, pos:Position) {
43+
logPos('Anonymous structure found, it is advised to use a typedef instead \"${name}\"', pos, Reflect.field(SeverityLevel, severity));
44+
}
45+
}

run.n

1.47 KB
Binary file not shown.

test/AnonymousCheckTest.hx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ;
2+
3+
class AnonymousCheckTest {
4+
5+
var _anonymous:{a:Int, b:Int};
6+
7+
public function new() {
8+
var b:{a:Int, b:Int};
9+
_anonymous = {a: 2, b: 5};
10+
}
11+
}
12+
13+
typedef SettingsBucket = {
14+
var width:Float;
15+
var height:Float;
16+
}

test/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"checks": [
3+
{
4+
"type": "Anonymous",
5+
"props": {
6+
"severity": "ERROR"
7+
}
8+
},
39
{
410
"type": "ArrayInstantiation",
511
"props": {

0 commit comments

Comments
 (0)