Skip to content

Commit 4662a4a

Browse files
authored
Merge pull request #306 from HaxeCheckstyle/spacing-check
partially converted spacing check to use token tree - fixes #304
2 parents 5924532 + 4011613 commit 4662a4a

File tree

2 files changed

+67
-52
lines changed

2 files changed

+67
-52
lines changed
Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package checkstyle.checks.whitespace;
22

3-
import Type.ValueType;
3+
import haxe.macro.Printer;
44
import checkstyle.utils.ExprUtils;
5+
import checkstyle.token.TokenTree;
6+
import Type.ValueType;
57
import haxe.macro.Expr;
6-
import haxe.macro.Printer;
78
import haxe.macro.Expr.Binop;
89
import haxe.macro.Expr.Unop;
910
import checkstyle.checks.Directive;
@@ -22,21 +23,20 @@ class SpacingCheck extends Check {
2223
public var ignoreRangeOperator:Bool;
2324

2425
public function new() {
25-
super(AST);
26-
spaceAroundBinop = true;
27-
noSpaceAroundUnop = true;
26+
super(TOKEN);
2827
spaceIfCondition = SHOULD;
2928
spaceForLoop = SHOULD;
3029
spaceWhileLoop = SHOULD;
3130
spaceSwitchCase = SHOULD;
3231
spaceCatch = SHOULD;
32+
spaceAroundBinop = true;
33+
noSpaceAroundUnop = true;
3334
ignoreRangeOperator = true;
3435
categories = [Category.STYLE, Category.CLARITY];
3536
}
3637

3738
override public function configureProperty(name:String, value:Dynamic) {
3839
var currentValue = Reflect.field(this, name);
39-
4040
switch (Type.typeof(currentValue)) {
4141
case ValueType.TEnum(Directive):
4242
Reflect.setField(this, name, DirectiveTools.fromDynamic(value));
@@ -46,6 +46,32 @@ class SpacingCheck extends Check {
4646
}
4747

4848
override function actualRun() {
49+
var root:TokenTree = checker.getTokenTree();
50+
var acceptableTokens:Array<TokenTree> = root.filter([
51+
Kwd(KwdIf),
52+
Kwd(KwdFor),
53+
Kwd(KwdWhile),
54+
Kwd(KwdSwitch),
55+
Kwd(KwdCatch)
56+
], ALL);
57+
58+
for (token in acceptableTokens) {
59+
var firstChild:TokenTree = token.getFirstChild();
60+
switch (token.tok) {
61+
case Kwd(KwdIf):
62+
checkSpaceBetweenExpressions(token.toString(), token, firstChild, spaceIfCondition);
63+
case Kwd(KwdFor):
64+
checkSpaceBetweenExpressions(token.toString(), token, firstChild, spaceForLoop);
65+
case Kwd(KwdWhile):
66+
checkSpaceBetweenExpressions(token.toString(), token, firstChild, spaceWhileLoop);
67+
case Kwd(KwdSwitch):
68+
checkSpaceBetweenExpressions(token.toString(), token, firstChild, spaceSwitchCase);
69+
case Kwd(KwdCatch):
70+
checkSpaceBetweenExpressions(token.toString(), token, firstChild, spaceCatch);
71+
case _:
72+
}
73+
}
74+
4975
var lastExpr = null;
5076

5177
ExprUtils.walkFile(checker.ast, function(e) {
@@ -63,27 +89,23 @@ class SpacingCheck extends Check {
6389
if (post) dist = e.pos.max - e2.pos.max;
6490
else dist = e2.pos.min - e.pos.min;
6591
if (dist > unopSize(uo)) logPos('Space around "${unopString(uo)}"', e.pos);
66-
case EIf(econd, _, _):
67-
checkSpaceBetweenExpressions("if", e, econd, spaceIfCondition);
68-
case EFor(it, _):
69-
checkSpaceBetweenExpressions("for", e, it, spaceForLoop);
70-
case EWhile(econd, _, true):
71-
checkSpaceBetweenExpressions("while", e, econd, spaceWhileLoop);
72-
case ESwitch(eswitch, _, _):
73-
checkSpaceBetweenManually("switch", lastExpr, eswitch, spaceSwitchCase);
74-
case ETry(etry, catches):
75-
var exprBeforeCatch = lastExpr;
76-
for (ctch in catches) {
77-
checkSpaceBetweenManually("catch", exprBeforeCatch, ctch.expr, spaceCatch);
78-
exprBeforeCatch = ctch.expr;
79-
}
8092
default:
8193
}
8294

8395
lastExpr = e;
8496
});
8597
}
8698

99+
function checkSpaceBetweenExpressions(name:String, e1:TokenTree, e2:TokenTree, directive:Directive) {
100+
switch (directive) {
101+
case ANY:
102+
case SHOULD_NOT:
103+
if (e2.pos.max - e1.pos.max > 1) logRange('Space between "$name" and "("', e2.pos.max, e2.pos.min);
104+
case SHOULD:
105+
if (e2.pos.max - e1.pos.max == 1) logRange('No space between "$name" and "("', e1.pos.max, e2.pos.min);
106+
}
107+
}
108+
87109
function binopSize(bo:Binop):Int {
88110
return binopString(bo).length;
89111
}
@@ -99,36 +121,4 @@ class SpacingCheck extends Check {
99121
function unopString(uo:Unop):String {
100122
return (new Printer()).printUnop(uo);
101123
}
102-
103-
function checkSpaceBetweenExpressions(name:String, e1:Expr, e2:Expr, directive:Directive) {
104-
switch (directive) {
105-
case ANY:
106-
case SHOULD_NOT:
107-
if (e2.pos.min - e1.pos.min > '$name('.length) {
108-
logRange('Space between "$name" and "("', e2.pos.max, e2.pos.min);
109-
}
110-
case SHOULD:
111-
if (e2.pos.min - e1.pos.min < '$name ('.length) {
112-
logRange('No space between "$name" and "("', e1.pos.max, e2.pos.min);
113-
}
114-
}
115-
}
116-
117-
function checkSpaceBetweenManually(name:String, before:Expr, check:Expr, directive:Directive) {
118-
var prevExprUntilChecked = checker.file.content.substring(before.pos.min, check.pos.min + 1);
119-
var checkPos = prevExprUntilChecked.lastIndexOf('$name(');
120-
var fileCheckPos = before.pos.min + checkPos;
121-
122-
switch (directive) {
123-
case ANY:
124-
case SHOULD_NOT:
125-
if (checkPos < 0) {
126-
logRange('Space between "$name" and "("', check.pos.min, check.pos.max);
127-
}
128-
case SHOULD:
129-
if (checkPos > -1) {
130-
logRange('No space between "$name" and "("', fileCheckPos, fileCheckPos + '$name('.length);
131-
}
132-
}
133-
}
134124
}

test/checks/whitespace/SpacingCheckTest.hx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ class SpacingCheckTest extends CheckTestCase<SpacingCheckTests> {
7777
assertMsg(check, TEST7B, 'Space between "catch" and "("');
7878
assertNoMsg(check, TEST7A);
7979
}
80+
81+
public function testMultilineIf() {
82+
var check = new SpacingCheck();
83+
assertMsg(check, TEST8, 'No space between "if" and "("');
84+
85+
check.spaceIfCondition = Directive.SHOULD_NOT;
86+
assertMsg(check, TEST8, 'Space between "if" and "("');
87+
}
8088
}
8189

8290
@:enum
@@ -177,4 +185,21 @@ abstract SpacingCheckTests(String) to String {
177185
catch (e:Dynamic) {}
178186
}
179187
}";
188+
189+
var TEST8 =
190+
"class Test {
191+
public function test() {
192+
if(
193+
true
194+
&& true
195+
|| false
196+
) {}
197+
198+
if (
199+
true
200+
&& true
201+
|| false
202+
) {}
203+
}
204+
}";
180205
}

0 commit comments

Comments
 (0)