Skip to content

Commit c13659b

Browse files
committed
Merge pull request #24 from AlexHaxe/leftcurly
added LeftCurlyCheck and EmptyBlockCheck
2 parents 1f2268c + 8d7f888 commit c13659b

26 files changed

+689
-28
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ More information in [wiki page](https://github.com/adireddy/haxe-checkstyle/wiki
7979
"severity": "INFO"
8080
}
8181
},
82+
{
83+
"type": "EmptyBlock",
84+
"props": {
85+
"severity": "ERROR",
86+
"option": "empty"
87+
}
88+
},
8289
{
8390
"type": "EmptyLines",
8491
"props": {
@@ -112,6 +119,29 @@ More information in [wiki page](https://github.com/adireddy/haxe-checkstyle/wiki
112119
"character": "tab"
113120
}
114121
},
122+
{
123+
"type": "LeftCurly",
124+
"props": {
125+
"severity": "WARNING",
126+
"option": "eol",
127+
"tokens": [
128+
"CLASS_DEF",
129+
"ENUM_DEF",
130+
"ABSTRACT_DEF",
131+
"TYPEDEF_DEF",
132+
"CLASS_DEF",
133+
"INTERFACE_DEF",
134+
"OBJECT_DECL",
135+
"FUNCTION",
136+
"FOR",
137+
"IF",
138+
"WHILE",
139+
"SWITCH",
140+
"TRY",
141+
"CATCH"
142+
]
143+
}
144+
},
115145
{
116146
"type": "LineLength",
117147
"props": {

checkstyle/ComplexTypeUtils.hx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import haxeparser.Data.EnumConstructor;
99
import haxe.macro.Expr;
1010

1111
class ComplexTypeUtils {
12-
12+
1313
public static function walkFile(file:{ pack:Array<String>, decls:Array<TypeDecl> }, cb:ComplexTypeCallback) {
1414
for (decl in file.decls) walkTypeDecl(decl, cb);
1515
}
@@ -43,7 +43,7 @@ class ComplexTypeUtils {
4343
public static function walkClass(d:Definition<ClassFlag, Array<Field>>, pos:Position, cb:ComplexTypeCallback) {
4444
walkCommonDefinition(d, pos, cb);
4545
for (f in d.flags) {
46-
switch f {
46+
switch(f) {
4747
case HExtends(t) | HImplements(t): walkTypePath(t, d.name, pos, cb);
4848
default:
4949
}
@@ -64,7 +64,7 @@ class ComplexTypeUtils {
6464
public static function walkAbstract(d:Definition<AbstractFlag, Array<Field>>, pos:Position, cb:ComplexTypeCallback) {
6565
walkCommonDefinition(d, pos, cb);
6666
for (f in d.flags) {
67-
switch f {
67+
switch(f) {
6868
case AFromType(ct) | AToType(ct) | AIsType(ct): walkComplexType(ct, f.getName(), pos, cb);
6969
default:
7070
}
@@ -133,7 +133,7 @@ class ComplexTypeUtils {
133133
if (e != null) walkExpr(e, cb);
134134
}
135135
}
136-
136+
137137
public static function walkComplexType(t:ComplexType, name:String, pos:Position, cb:ComplexTypeCallback) {
138138
cb(t, name, pos);
139139
switch(t){

checkstyle/ExprUtils.hx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import haxeparser.Data.EnumConstructor;
99
import haxe.macro.Expr;
1010

1111
class ExprUtils {
12-
12+
1313
public static function walkFile(file:{ pack: Array<String>, decls: Array<TypeDecl> }, cb:Expr -> Void) {
1414
for (decl in file.decls) walkTypeDecl(decl, cb);
1515
}
@@ -43,7 +43,7 @@ class ExprUtils {
4343
public static function walkClass(d:Definition<ClassFlag, Array<Field>>, cb:Expr -> Void) {
4444
walkCommonDefinition(d, cb);
4545
for (f in d.flags) {
46-
switch f {
46+
switch(f) {
4747
case HExtends(t) | HImplements(t): walkTypePath(t,cb);
4848
default:
4949
}
@@ -64,7 +64,7 @@ class ExprUtils {
6464
public static function walkAbstract(d:Definition<AbstractFlag, Array<Field>>, cb:Expr -> Void) {
6565
walkCommonDefinition(d, cb);
6666
for (f in d.flags) {
67-
switch f {
67+
switch(f) {
6868
case AFromType(ct) | AToType(ct) | AIsType(ct): walkComplexType(ct,cb);
6969
default:
7070
}
@@ -133,7 +133,7 @@ class ExprUtils {
133133
if (e != null) walkExpr(e, cb);
134134
}
135135
}
136-
136+
137137
public static function walkComplexType(t:ComplexType, cb:Expr -> Void) {
138138
switch(t){
139139
case TPath(p): walkTypePath(p, cb);

checkstyle/Report.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Report {
2323
reportFile = File.write("CHECKS.md", false);
2424
reportFile.writeString("###Report of default checks on CheckStyle library itself\n\n");
2525
#end
26-
26+
2727
var errors = 0;
2828
var warnings = 0;
2929
var infos = 0;

checkstyle/checks/AnonymousCheck.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ class AnonymousCheck extends Check {
2929
}
3030
}
3131
}
32-
32+
3333
function checkField(f:Field) {
3434
switch(f.kind) {
3535
case FVar(TAnonymous(fields), val):
3636
error(f.name, f.pos);
3737
default:
3838
}
3939
}
40-
40+
4141
function checkLocalVars() {
4242
ExprUtils.walkFile(checker.ast, function(e) {
4343
switch(e.expr){

checkstyle/checks/ArrayInstantiationCheck.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import haxeparser.Data.Token;
77
@name("ArrayInstantiation")
88
@desc("Checks if the array is instantiated using [], not with new")
99
class ArrayInstantiationCheck extends Check {
10-
10+
1111
override function actualRun() {
1212
ExprUtils.walkFile(checker.ast, function(e:Expr) {
1313
switch(e.expr){

checkstyle/checks/BlockFormatCheck.hx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import checkstyle.LintMessage.SeverityLevel;
55
@name("BlockFormat")
66
@desc("Checks empty blocks and first/last lines of a block")
77
class BlockFormatCheck extends Check {
8-
8+
99
public var emptyBlockCheck:Bool;
1010

1111
var firstLineRE:EReg;
@@ -20,6 +20,7 @@ class BlockFormatCheck extends Check {
2020

2121
override function actualRun() {
2222
ExprUtils.walkFile(checker.ast, function(e) {
23+
if (isPosSuppressed(e.pos)) return;
2324
switch(e.expr){
2425
case EBlock([]) | EObjectDecl([]):
2526
if (emptyBlockCheck && e.pos.max - e.pos.min > "{}".length) {

checkstyle/checks/CyclomaticComplexityCheck.hx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CyclomaticComplexityCheck extends Check {
2222
{ severity : "ERROR", complexity : 25 }
2323
];
2424
}
25-
25+
2626
override function actualRun() {
2727
checker.ast.decls.map(function(type:TypeDecl):Null<Definition<ClassFlag, Array<Field>>> {
2828
return switch (type.decl) {
@@ -33,7 +33,7 @@ class CyclomaticComplexityCheck extends Check {
3333
return definition != null;
3434
}).iter(checkFields);
3535
}
36-
36+
3737
function checkFields(definition:Definition<ClassFlag, Array<Field>>) {
3838
definition.data.map(function(field:Field):Null<Target> {
3939
return switch (field.kind) {
@@ -46,7 +46,7 @@ class CyclomaticComplexityCheck extends Check {
4646
return f != null;
4747
}).iter(calculateComplexity);
4848
}
49-
49+
5050
function calculateComplexity(method:Target) {
5151
var complexity:Int = 1 + evaluateExpr(method.expr);
5252

@@ -61,7 +61,7 @@ class CyclomaticComplexityCheck extends Check {
6161

6262
// This would not pass the cyclomatic complexity test.
6363

64-
@SuppressWarnings('checkstyle:CyclomaticComplexity')
64+
@SuppressWarnings(['checkstyle:CyclomaticComplexity', 'checkstyle:LeftCurly'])
6565
function evaluateExpr(e:Expr):Int {
6666
if (e == null || e.expr == null) return 0;
6767
return switch(e.expr) {

checkstyle/checks/ERegInstantiationCheck.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ERegInstantiationCheck extends Check {
1313

1414
override function actualRun() {
1515
ExprUtils.walkFile(checker.ast, function(e) {
16+
if (isPosSuppressed(e.pos)) return;
1617
switch(e.expr){
1718
case ENew(
1819
{pack:[], name:"EReg"},
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package checkstyle.checks;
2+
3+
import checkstyle.LintMessage.SeverityLevel;
4+
import haxe.macro.Expr;
5+
6+
@name("EmptyBlock")
7+
@desc("Checks empty blocks / object declarations")
8+
class EmptyBlockCheck extends Check {
9+
10+
// require comment in empty block / object decl
11+
public static inline var TEXT:String = "text";
12+
// empty block / object decl can be empty or have comments
13+
// if block has no comments, enforces {} notation
14+
public static inline var EMPTY:String = "empty";
15+
16+
public var option:String;
17+
18+
public function new() {
19+
super();
20+
option = EMPTY;
21+
}
22+
23+
override function actualRun() {
24+
ExprUtils.walkFile(checker.ast, function(e) {
25+
if (isPosSuppressed(e.pos)) return;
26+
switch(e.expr){
27+
case EBlock([]) | EObjectDecl([]):
28+
checkEmptyBlock(e);
29+
default:
30+
}
31+
});
32+
}
33+
34+
function checkEmptyBlock(e:Expr) {
35+
if ((e == null) || (e.expr == null)) return;
36+
37+
var block:String = checker.file.content.substring(e.pos.min, e.pos.max);
38+
var containsOnlyWS:Bool = (~/\{\s+\}/m.match(block));
39+
var containsText:Bool = (~/\{\s*\S.*\S\s*\}/m.match(block));
40+
41+
if (option == TEXT) {
42+
if (!containsText) {
43+
logPos("Empty block should contain a comment", e.pos, Reflect.field(SeverityLevel, severity));
44+
}
45+
return;
46+
}
47+
if (containsOnlyWS) {
48+
logPos("Empty block should be written as {}", e.pos, Reflect.field(SeverityLevel, severity));
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)