Skip to content

Commit cda6de3

Browse files
committed
moved check for empty blocks to new EmptyBlockCheck
1 parent 6264c36 commit cda6de3

File tree

4 files changed

+81
-22
lines changed

4 files changed

+81
-22
lines changed

README.md

Lines changed: 7 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+
"tokens": []
88+
}
8289
{
8390
"type": "EmptyLines",
8491
"props": {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
public static inline var BLOCK:String = "BLOCK";
11+
public static inline var OBJECT_DECL:String = "OBJECT_DECL";
12+
13+
// require comment in empty block / object decl
14+
public static inline var TEXT:String = "text";
15+
// empty block / object decl can be empty or have comments
16+
// if block has no comments, enforces {} notation
17+
public static inline var EMPTY:String = "empty";
18+
19+
public var tokens:Array<String>;
20+
public var option:String;
21+
22+
public function new() {
23+
super();
24+
tokens = [];
25+
option = EMPTY;
26+
}
27+
28+
function hasToken(token:String):Bool {
29+
if (tokens.length == 0) return true;
30+
if (tokens.indexOf(token) > -1) return true;
31+
return false;
32+
}
33+
34+
override function actualRun() {
35+
ExprUtils.walkFile(checker.ast, function(e) {
36+
if (isPosSuppressed(e.pos)) return;
37+
switch(e.expr){
38+
case EBlock([]):
39+
if (!hasToken(BLOCK)) return;
40+
checkEmptyBlock(e);
41+
case EObjectDecl([]):
42+
if (!hasToken(OBJECT_DECL)) return;
43+
checkEmptyBlock(e);
44+
default:
45+
}
46+
});
47+
}
48+
49+
function checkEmptyBlock(e:Expr) {
50+
if ((e == null) || (e.expr == null)) return;
51+
52+
var block:String = checker.file.content.substring(e.pos.min, e.pos.max);
53+
var containsOnlyWS:Bool = (~/\{\s+\}/m.match(block));
54+
var containsText:Bool = (~/\{\s*\S.*\S\s*\}/m.match(block));
55+
56+
if (option == TEXT) {
57+
if (!containsText) {
58+
logPos("Empty block should contain a comment", e.pos, Reflect.field(SeverityLevel, severity));
59+
}
60+
return;
61+
}
62+
if (containsOnlyWS) {
63+
logPos("Empty block should be written as {}", e.pos, Reflect.field(SeverityLevel, severity));
64+
}
65+
}
66+
}

checkstyle/checks/LeftCurlyCheck.hx

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ class LeftCurlyCheck extends Check {
176176
if ((e == null) || (e.expr == null)) return;
177177

178178
switch(e.expr) {
179-
case EBlock([]):
180-
checkEmptyBlock(e);
181179
case EBlock(_):
182180
var linePos:LinePos = checker.getLinePos(e.pos.min);
183181
var line:String = checker.lines[linePos.line];
@@ -196,26 +194,6 @@ class LeftCurlyCheck extends Check {
196194
checkLeftCurly(line, pos);
197195
}
198196

199-
function checkEmptyBlock(e:Expr) {
200-
if ((e == null) || (e.expr == null)) return;
201-
202-
var lineMin:Int = checker.getLinePos(e.pos.min).line;
203-
var lineMax:Int = checker.getLinePos(e.pos.max).line;
204-
if (lineMin != lineMax) {
205-
var block:String = "";
206-
for (lineIndex in lineMin...(lineMax + 1)) {
207-
block += StringTools.trim(checker.lines[lineIndex]);
208-
}
209-
if (~/.*\{\}($|[ \t]*\/\/.*$)/.match(block)) {
210-
logPos("Empty block should be written as {}", e.pos, Reflect.field(SeverityLevel, severity));
211-
}
212-
}
213-
214-
var linePos:LinePos = checker.getLinePos(e.pos.min);
215-
var line:String = checker.lines[linePos.line];
216-
checkLeftCurly(line, e.pos);
217-
}
218-
219197
@SuppressWarnings("checkstyle:BlockFormat")
220198
function checkLeftCurly(line:String, pos:Position) {
221199
var lineLength:Int = line.length;

resources/config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
"severity": "INFO"
5757
}
5858
},
59+
{
60+
"type": "EmptyBlock",
61+
"props": {
62+
"severity": "ERROR",
63+
"option": "empty",
64+
"tokens": []
65+
}
66+
},
5967
{
6068
"type": "EmptyLines",
6169
"props": {

0 commit comments

Comments
 (0)