Skip to content

Commit 65d51a4

Browse files
Change spacing settings for switch and catch to Directive
1 parent 7d4e708 commit 65d51a4

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

resources/default-config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@
447447
"spaceForLoop": "should",
448448
"ignoreRangeOperator": true,
449449
"spaceWhileLoop": "should",
450-
"spaceCatch": true,
451-
"spaceSwitchCase": true,
450+
"spaceCatch": "should",
451+
"spaceSwitchCase": "should",
452452
"noSpaceAroundUnop": true
453453
},
454454
"type": "Spacing"

src/checkstyle/checks/whitespace/SpacingCheck.hx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class SpacingCheck extends Check {
1616
public var spaceIfCondition:Directive;
1717
public var spaceForLoop:Directive;
1818
public var spaceWhileLoop:Directive;
19-
public var spaceSwitchCase:Bool;
20-
public var spaceCatch:Bool;
19+
public var spaceSwitchCase:Directive;
20+
public var spaceCatch:Directive;
2121
public var ignoreRangeOperator:Bool;
2222

2323
public function new() {
@@ -27,8 +27,8 @@ class SpacingCheck extends Check {
2727
spaceIfCondition = SHOULD;
2828
spaceForLoop = SHOULD;
2929
spaceWhileLoop = SHOULD;
30-
spaceSwitchCase = true;
31-
spaceCatch = true;
30+
spaceSwitchCase = SHOULD;
31+
spaceCatch = SHOULD;
3232
ignoreRangeOperator = true;
3333
categories = [Category.STYLE, Category.CLARITY];
3434
}
@@ -68,12 +68,12 @@ class SpacingCheck extends Check {
6868
checkSpaceBetweenExpressions("for", e, it, spaceForLoop);
6969
case EWhile(econd, _, true):
7070
checkSpaceBetweenExpressions("while", e, econd, spaceWhileLoop);
71-
case ESwitch(eswitch, _, _) if (spaceSwitchCase):
72-
checkSpaceBetweenManually("switch", lastExpr, eswitch);
73-
case ETry(etry, catches) if (spaceCatch):
71+
case ESwitch(eswitch, _, _):
72+
checkSpaceBetweenManually("switch", lastExpr, eswitch, spaceSwitchCase);
73+
case ETry(etry, catches):
7474
var exprBeforeCatch = lastExpr;
7575
for (ctch in catches) {
76-
checkSpaceBetweenManually("catch", exprBeforeCatch, ctch.expr);
76+
checkSpaceBetweenManually("catch", exprBeforeCatch, ctch.expr, spaceCatch);
7777
exprBeforeCatch = ctch.expr;
7878
}
7979
default:
@@ -99,7 +99,7 @@ class SpacingCheck extends Check {
9999
return (new Printer()).printUnop(uo);
100100
}
101101

102-
function checkSpaceBetweenExpressions(name:String, e1:Expr, e2:Expr, directive:Directive = SHOULD) {
102+
function checkSpaceBetweenExpressions(name:String, e1:Expr, e2:Expr, directive:Directive) {
103103
switch (directive) {
104104
case ANY:
105105
case SHOULD_NOT:
@@ -113,12 +113,21 @@ class SpacingCheck extends Check {
113113
}
114114
}
115115

116-
function checkSpaceBetweenManually(name:String, before:Expr, check:Expr) {
116+
function checkSpaceBetweenManually(name:String, before:Expr, check:Expr, directive:Directive) {
117117
var prevExprUntilChecked = checker.file.content.substring(before.pos.min, check.pos.min + 1);
118118
var checkPos = prevExprUntilChecked.lastIndexOf('$name(');
119-
if (checkPos > -1) {
120-
var fileCheckPos = before.pos.min + checkPos;
121-
logRange('No space between "$name" and "("', fileCheckPos, fileCheckPos + '$name('.length);
119+
var fileCheckPos = before.pos.min + checkPos;
120+
121+
switch (directive) {
122+
case ANY:
123+
case SHOULD_NOT:
124+
if (checkPos < 0) {
125+
logRange('Space between "$name" and "("', fileCheckPos, fileCheckPos + '$name ('.length);
126+
}
127+
case SHOULD:
128+
if (checkPos > -1) {
129+
logRange('No space between "$name" and "("', fileCheckPos, fileCheckPos + '$name('.length);
130+
}
122131
}
123132
}
124133
}

test/checks/whitespace/SpacingCheckTest.hx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,31 @@ class SpacingCheckTest extends CheckTestCase<SpacingCheckTests> {
5252
assertNoMsg(check, TEST5A);
5353
}
5454

55-
public function testSwitch() {
55+
public function testSwitchShouldContainSpace() {
5656
assertMsg(new SpacingCheck(), TEST6A, 'No space between "switch" and "("');
5757
assertNoMsg(new SpacingCheck(), TEST6B);
5858
}
5959

60-
public function testCatch() {
60+
public function testSwitchShouldNotContainSpace() {
61+
var check = new SpacingCheck();
62+
check.spaceSwitchCase = Directive.SHOULD_NOT;
63+
64+
assertMsg(check, TEST6B, 'Space between "switch" and "("');
65+
assertNoMsg(check, TEST6A);
66+
}
67+
68+
public function testCatchShouldContainSpace() {
6169
assertMsg(new SpacingCheck(), TEST7A, 'No space between "catch" and "("');
6270
assertNoMsg(new SpacingCheck(), TEST7B);
6371
}
72+
73+
public function testCatchShouldNotContainSpace() {
74+
var check = new SpacingCheck();
75+
check.spaceCatch = Directive.SHOULD_NOT;
76+
77+
assertMsg(check, TEST7B, 'Space between "catch" and "("');
78+
assertNoMsg(check, TEST7A);
79+
}
6480
}
6581

6682
@:enum

0 commit comments

Comments
 (0)