Skip to content

Commit

Permalink
Merge pull request #198 from HaxeCheckstyle/DefaultComesLast
Browse files Browse the repository at this point in the history
DefaultComesLast
  • Loading branch information
adireddy committed Mar 17, 2016
2 parents c062796 + 371a8be commit 574f741
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
6 changes: 6 additions & 0 deletions resources/default-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
},
"type": "CyclomaticComplexity"
},
{
"props": {
"severity": "IGNORE"
},
"type": "DefaultComesLast"
},
{
"props": {
"severity": "IGNORE"
Expand Down
32 changes: 32 additions & 0 deletions src/checkstyle/checks/coding/DefaultComesLastCheck.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package checkstyle.checks.coding;

import checkstyle.token.TokenTree;

@name("DefaultComesLast")
@desc("Check that the `default` is after all the cases in a `switch` statement. Haxe allows `default` anywhere within the `switch` statement. But it is more readable if it comes after the last `case`.")
class DefaultComesLastCheck extends Check {

public function new() {
super(TOKEN);
categories = [Category.STYLE, Category.CLARITY];
points = 2;
}

override function actualRun() {
var root:TokenTree = checker.getTokenTree();
var acceptableTokens:Array<TokenTree> = root.filter([Kwd(KwdSwitch)], ALL);

for (token in acceptableTokens) {
var tokens:Array<TokenTree> = token.filter([Kwd(KwdCase), Kwd(KwdDefault)], ALL);
if (tokens[tokens.length - 1].is(Kwd(KwdDefault))) continue;

var defaultExists = false;
for (i in 0 ... tokens.length) {
if (tokens[i].is(Kwd(KwdDefault)) && i < tokens.length - 1) {
logPos("Default should be last label in the switch", token.pos);
continue;
}
}
}
}
}
2 changes: 1 addition & 1 deletion test/checks/coding/AvoidInlineConditionalsCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import checkstyle.checks.coding.AvoidInlineConditionalsCheck;
class AvoidInlineConditionalsCheckTest extends CheckTestCase<AvoidInlineConditionalsTests> {

public function testInlineCondition() {
assertMsg(new AvoidInlineConditionalsCheck(), TEST1, 'Avoid inline conditionals');
assertMsg(new AvoidInlineConditionalsCheck(), TEST1, "Avoid inline conditionals");
}
}

Expand Down
62 changes: 62 additions & 0 deletions test/checks/coding/DefaultComesLastCheckTest.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package checks.coding;

import checkstyle.checks.coding.DefaultComesLastCheck;

class DefaultComesLastCheckTest extends CheckTestCase<DefaultComesLastCheckTests> {

static inline var MSG:String = "Default should be last label in the switch";

public function testFirstDefault() {
assertMsg(new DefaultComesLastCheck(), TEST1, MSG);
}

public function testMiddleDefault() {
assertMsg(new DefaultComesLastCheck(), TEST2, MSG);
}

public function testLastDefault() {
assertNoMsg(new DefaultComesLastCheck(), TEST3);
}
}

@:enum
abstract DefaultComesLastCheckTests(String) to String {
var TEST1 = "
abstractAndClass Test {
function test() {
var a =1;
switch(a) {
default: trace('test');
case 1:
case 4:
}
}
}";

var TEST2 = "
abstractAndClass Test {
function test() {
var a =1;
switch(a) {
case 1:
default: trace('test');
case 4:
}
}
}";

var TEST3 = "
abstractAndClass Test {
function test() {
var a =1;
switch(a) {
case 1:
case 4:
default: trace('test');
}
}
}";
}

0 comments on commit 574f741

Please sign in to comment.