-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from HaxeCheckstyle/DefaultComesLast
DefaultComesLast
- Loading branch information
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
}"; | ||
} |