Skip to content

Commit e7ae6b9

Browse files
committed
cleanup
1 parent 65f31ab commit e7ae6b9

File tree

6 files changed

+80
-136
lines changed

6 files changed

+80
-136
lines changed

checkstyle/ChecksInfo.hx

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@ package checkstyle;
22

33
import checkstyle.checks.Check;
44

5-
typedef CheckInfo = {
6-
var name:String;
7-
var description:String;
8-
var clazz:Class<Check>;
9-
}
10-
115
class ChecksInfo {
6+
127
var name2info:Map<String, CheckInfo>;
138

14-
public function new(){
9+
public function new() {
1510
name2info = new Map();
1611

1712
CompileTime.importPackage("checkstyle.checks"); // must be string constant
1813
var checksClasses = CompileTime.getAllClasses(Check);
1914

20-
for (cl in checksClasses){
15+
for (cl in checksClasses) {
2116
var name = getCheckNameFromClass(cl);
2217
var desc = getCheckDescription(cl);
2318
name2info[name] = {
@@ -28,28 +23,34 @@ class ChecksInfo {
2823
}
2924
}
3025

31-
static function getCheckNameFromClass(cl:Class<Check>){
26+
static function getCheckNameFromClass(cl:Class<Check>) {
3227
var meta = haxe.rtti.Meta.getType(cl);
3328
if (meta.name == null) throw '${Type.getClassName(cl)} have no @name meta.';
3429
if (meta.name.length != 1) throw '${Type.getClassName(cl)} @name meta should have exactly one argument';
3530
return meta.name[0];
3631
}
3732

38-
public static function getCheckName(check:Check){
33+
public static function getCheckName(check:Check) {
3934
return getCheckNameFromClass(Type.getClass(check));
4035
}
4136

42-
function getCheckDescription(cl:Class<Check>){
37+
function getCheckDescription(cl:Class<Check>) {
4338
return haxe.rtti.Meta.getType(cl).desc[0];
4439
}
4540

46-
public function checks(){
41+
public function checks() {
4742
return name2info.iterator();
4843
}
4944

50-
public function build(name:String){
51-
if (! name2info.exists(name)) throw 'Unknown check: $name';
45+
public function build(name:String) {
46+
if (!name2info.exists(name)) throw 'Unknown check: $name';
5247
var cl = name2info[name].clazz;
53-
return Type.createInstance(cl,[]);
48+
return Type.createInstance(cl, []);
5449
}
50+
}
51+
52+
typedef CheckInfo = {
53+
var name:String;
54+
var description:String;
55+
var clazz:Class<Check>;
5556
}

checkstyle/ComplexTypeUtils.hx

Lines changed: 35 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,45 @@ import haxeparser.Data.EnumFlag;
88
import haxeparser.Data.EnumConstructor;
99
import haxe.macro.Expr;
1010

11-
//FIXME ComplexTypeUtils differ from ExprUtils ONLY in place for callback call and it's type. Maybe it's possible to generate such functions.
1211
class ComplexTypeUtils {
1312

14-
public static function walkFile(file:{pack: Array<String>, decls: Array<TypeDecl> }, cb:ComplexType -> Void) {
15-
for (decl in file.decls){
16-
walkTypeDecl(decl, cb);
17-
}
13+
public static function walkFile(file:{pack:Array<String>, decls:Array<TypeDecl> }, cb:ComplexType -> Void) {
14+
for (decl in file.decls) walkTypeDecl(decl, cb);
1815
}
1916

2017
public static function walkTypeDecl(td:TypeDecl, cb:ComplexType -> Void) {
2118
switch(td.decl){
22-
case EClass(d):
23-
walkClass(d,cb);
24-
case EEnum(d):
25-
walkEnum(d,cb);
26-
case EAbstract(a):
27-
walkAbstract(a,cb);
28-
case EImport(sl, mode):
29-
walkImport(sl, mode,cb);
30-
case ETypedef(d):
31-
walkTypedef(d,cb);
32-
case EUsing(path):
33-
walkTypePath(path,cb);
19+
case EClass(d):
20+
walkClass(d, cb);
21+
case EEnum(d):
22+
walkEnum(d, cb);
23+
case EAbstract(a):
24+
walkAbstract(a, cb);
25+
case EImport(sl, mode):
26+
walkImport(sl, mode, cb);
27+
case ETypedef(d):
28+
walkTypedef(d, cb);
29+
case EUsing(path):
30+
walkTypePath(path, cb);
3431
}
3532
}
3633

37-
static function walkMeta(meta:Metadata, cb:ComplexType -> Void){
38-
for (m in meta) for (p in m.params) walkExpr(p,cb);
34+
static function walkMeta(meta:Metadata, cb:ComplexType -> Void) {
35+
for (m in meta) for (p in m.params) walkExpr(p, cb);
3936
}
4037

41-
static function walkCommonDefinition<A,B> (d:Definition<A, B>, cb:ComplexType -> Void) {
42-
for (p in d.params) walkTypeParamDecl(p,cb);
38+
static function walkCommonDefinition<A, B>(d:Definition<A, B>, cb:ComplexType -> Void) {
39+
for (p in d.params) walkTypeParamDecl(p, cb);
4340
walkMeta(d.meta, cb);
4441
}
4542

4643
public static function walkClass(d:Definition<ClassFlag, Array<Field>>, cb:ComplexType -> Void) {
4744
walkCommonDefinition(d, cb);
4845
for (f in d.flags) switch f {
49-
case HExtends(t) | HImplements(t): walkTypePath(t,cb);
46+
case HExtends(t) | HImplements(t): walkTypePath(t, cb);
5047
default:
5148
}
52-
for (f in d.data) {
53-
walkField(f,cb);
54-
}
49+
for (f in d.data) walkField(f, cb);
5550
}
5651

5752
public static function walkEnum(d:Definition<EnumFlag, Array<EnumConstructor>>, cb:ComplexType -> Void) {
@@ -61,22 +56,18 @@ class ComplexTypeUtils {
6156
for (arg in ec.args) {
6257
walkComplexType(arg.type, cb);
6358
}
64-
for (param in ec.params) {
65-
walkTypeParamDecl(param, cb);
66-
}
59+
for (param in ec.params) walkTypeParamDecl(param, cb);
6760
if (ec.type != null) walkComplexType(ec.type, cb);
6861
}
6962
}
7063

7164
public static function walkAbstract(d:Definition<AbstractFlag, Array<Field>>, cb:ComplexType -> Void) {
7265
walkCommonDefinition(d, cb);
7366
for (f in d.flags) switch f {
74-
case AFromType(ct) | AToType(ct) | AIsType(ct): walkComplexType(ct,cb);
67+
case AFromType(ct) | AToType(ct) | AIsType(ct): walkComplexType(ct, cb);
7568
default:
7669
}
77-
for (f in d.data) {
78-
walkField(f,cb);
79-
}
70+
for (f in d.data) walkField(f, cb);
8071
}
8172

8273
public static function walkImport(sl, mode, cb:ComplexType -> Void) {
@@ -105,12 +96,8 @@ class ComplexTypeUtils {
10596
}
10697

10798
public static function walkTypeParamDecl(tp:TypeParamDecl, cb:ComplexType -> Void) {
108-
if (tp.constraints != null) {
109-
for (c in tp.constraints) walkComplexType(c, cb);
110-
}
111-
if (tp.params != null) {
112-
for (t in tp.params) walkTypeParamDecl(t, cb);
113-
}
99+
if (tp.constraints != null) for (c in tp.constraints) walkComplexType(c, cb);
100+
if (tp.params != null) for (t in tp.params) walkTypeParamDecl(t, cb);
114101
}
115102

116103
public static function walkFunction(f:Function, cb:ComplexType -> Void) {
@@ -120,17 +107,11 @@ class ComplexTypeUtils {
120107
}
121108
if (f.ret != null) walkComplexType(f.ret, cb);
122109
if (f.expr != null) walkExpr(f.expr, cb);
123-
if (f.params != null) {
124-
for (tp in f.params) {
125-
walkTypeParamDecl(tp, cb);
126-
}
127-
}
110+
if (f.params != null) for (tp in f.params) walkTypeParamDecl(tp, cb);
128111
}
129112

130113
public static function walkCase(c:Case, cb:ComplexType -> Void) {
131-
for (v in c.values) {
132-
walkExpr(v, cb);
133-
}
114+
for (v in c.values) walkExpr(v, cb);
134115
if (c.guard != null) walkExpr(c.guard, cb);
135116
if (c.expr != null) walkExpr(c.expr, cb);
136117
}
@@ -181,33 +162,23 @@ class ComplexTypeUtils {
181162
case EField(e, field): walkExpr(e, cb);
182163
case EParenthesis(e): walkExpr(e, cb);
183164
case EObjectDecl(fields):
184-
for (f in fields) {
185-
walkExpr(f.expr, cb);
186-
}
165+
for (f in fields) walkExpr(f.expr, cb);
187166
case EArrayDecl(values):
188-
for (v in values) {
189-
walkExpr(v, cb);
190-
}
167+
for (v in values) walkExpr(v, cb);
191168
case ECall(e, params):
192169
walkExpr(e, cb);
193-
for (p in params) {
194-
walkExpr(p, cb);
195-
}
170+
for (p in params) walkExpr(p, cb);
196171
case ENew(t, params):
197172
walkTypePath(t, cb);
198-
for (p in params) {
199-
walkExpr(p, cb);
200-
}
173+
for (p in params) walkExpr(p, cb);
201174
case EUnop(op, postFix, e): walkExpr(e, cb);
202175
case EVars(vars):
203176
for (v in vars) {
204177
walkVar(v, cb);
205178
}
206179
case EFunction(name, f): walkFunction(f, cb);
207180
case EBlock(exprs):
208-
for (e in exprs) {
209-
walkExpr(e, cb);
210-
}
181+
for (e in exprs) walkExpr(e, cb);
211182
case EFor(it, expr): walkExpr(it, cb); walkExpr(expr, cb);
212183
case EIn(e1, e2): walkExpr(e1, cb); walkExpr(e2, cb);
213184
case EIf(econd, eif, eelse):
@@ -218,9 +189,7 @@ class ComplexTypeUtils {
218189
case ESwitch(e, cases, edef):
219190
walkExpr(e, cb);
220191
for (c in cases) walkCase(c, cb);
221-
if (edef != null && edef.expr != null){
222-
walkExpr(edef, cb);
223-
}
192+
if (edef != null && edef.expr != null) walkExpr(edef, cb);
224193
case ETry(e, catches):
225194
walkExpr(e, cb);
226195
for (c in catches) walkCatch(c, cb);
@@ -243,11 +212,8 @@ class ComplexTypeUtils {
243212
walkExpr(e, cb);
244213
walkComplexType(t, cb);
245214
case EMeta(s, e):
246-
if (s.params != null) {
247-
for (mp in s.params) walkExpr(mp, cb);
248-
}
215+
if (s.params != null) for (mp in s.params) walkExpr(mp, cb);
249216
walkExpr(e, cb);
250217
}
251218
}
252-
253-
}
219+
}

checkstyle/Main.hx

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,40 @@ class Main {
1616
var cwd;
1717
var oldCwd = null;
1818

19-
try{
19+
try {
2020
args = Sys.args();
2121
cwd = Sys.getCwd();
22-
if (Sys.getEnv("HAXELIB_RUN") != null){
22+
if (Sys.getEnv("HAXELIB_RUN") != null) {
2323
cwd = args.pop();
2424
oldCwd = Sys.getCwd();
2525
}
26-
if (oldCwd != null) Sys.setCwd(cwd); // FIXME instead of this one should gracefully parse absolute-relative path
26+
if (oldCwd != null) Sys.setCwd(cwd);
2727

2828
var main = new Main();
2929
main.run(args);
3030
}
31-
catch(e:Dynamic){
31+
catch(e:Dynamic) {
3232
trace(e);
3333
trace(CallStack.toString(CallStack.exceptionStack()));
3434
}
35-
if (oldCwd != null) Sys.setCwd(oldCwd); // FIXME instead of this one should gracefully parse absolute-relative path
35+
if (oldCwd != null) Sys.setCwd(oldCwd);
3636
}
3737

3838
var reporter:IReporter;
3939
var info:ChecksInfo;
4040
var checker:Checker;
4141

4242
static var report_type:String = "default";
43-
static var path:String = "static_analysis.xml";
43+
static var path:String = "check-style-report.xml";
4444
static var style:String = "";
4545

46-
function new(){
46+
function new() {
4747
reporter = new Reporter();
4848
info = new ChecksInfo();
4949
checker = new Checker();
5050
}
5151

52-
function run(args:Array<String>){
52+
function run(args:Array<String>) {
5353
var files:Array<String> = [];
5454
var _configPath:String = null;
5555

@@ -85,11 +85,8 @@ class Main {
8585
toProcess.push({name:file,content:code});
8686
}
8787

88-
if (_configPath == null){
89-
addAllChecks();
90-
}
88+
if (_configPath == null) addAllChecks();
9189
else {
92-
//TODO split config loading to separate class
9390
var configText = File.getContent(_configPath);
9491
var config = Json.parse(configText);
9592
var checks:Array<Dynamic> = config.checks;
@@ -98,7 +95,6 @@ class Main {
9895
if (checkConf.props != null){
9996
var props = Reflect.fields(checkConf.props);
10097
for (prop in props){
101-
//FIXME check available properties and their types
10298
var val = Reflect.field(checkConf.props, prop);
10399
Reflect.setField(check, prop, val);
104100
}
@@ -110,48 +106,37 @@ class Main {
110106
checker.process(toProcess);
111107
}
112108

113-
function addAllChecks():Void{
114-
for (check in info.checks()){
115-
checker.addCheck(info.build(check.name));
116-
}
109+
function addAllChecks():Void {
110+
for (check in info.checks()) checker.addCheck(info.build(check.name));
117111
}
118112

119-
function listChecks():Void{
120-
for (check in info.checks()){
121-
Sys.println('${check.name}: ${check.description}');
122-
}
113+
function listChecks():Void {
114+
for (check in info.checks()) Sys.println('${check.name}: ${check.description}');
123115
}
124116

125-
//FIXME some macro maybe?
126117
static function listReporters():Void {
127118
Sys.println("default - Default reporter");
128119
Sys.println("xml - Checkstyle-like XML reporter");
129120
Sys.exit(0);
130121
}
131122

132-
//FIXME some macro maybe?
133-
static function createReporter():IReporter{
134-
return switch(report_type){
135-
case "xml": new XMLReporter(path, style);
136-
case "default": new Reporter();
137-
default: throw "Unknown reporter";
123+
static function createReporter():IReporter {
124+
return switch(report_type) {
125+
case "xml": new XMLReporter(path, style);
126+
case "default": new Reporter();
127+
default: throw "Unknown reporter";
138128
}
139129
}
140130

141-
private static function pathJoin(s:String, t:String):String{
142-
//FIXME
131+
private static function pathJoin(s:String, t:String):String {
143132
return s + "/" + t;
144133
}
145134

146135
private static function traverse(node:String , files:Array<String>) {
147-
if (FileSystem.isDirectory(node)){
136+
if (FileSystem.isDirectory(node)) {
148137
var nodes = FileSystem.readDirectory(node);
149-
for (child in nodes){
150-
traverse(pathJoin(node,child),files);
151-
}
152-
}
153-
else if (node.substr(-3) == ".hx"){
154-
files.push(node);
138+
for (child in nodes) traverse(pathJoin(node,child), files);
155139
}
140+
else if (node.substr(-3) == ".hx") files.push(node);
156141
}
157142
}

0 commit comments

Comments
 (0)