Skip to content

Commit e143a8b

Browse files
authored
added support for final to MagicNumber, fixes #494 (#495)
* added support for final to MagicNumber, fixes #494
1 parent 6a47621 commit e143a8b

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
.DS_Store
99
*.n
1010
*.zip
11-
.haxelib/
1211
out/
1312
node_modules
1413
.DS_Store

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## dev branch / next version (2.x.x)
44

5+
- Added support for final in `MagicNumber`, fixes [#494](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/494) ([#495](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/495))
6+
57
## version 2.6.1 (2019-12-17)
68

79
- Added `allowFinal` setting to `VariableInitialisation`, fixes [#491](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/491) ([#492](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/492))

build/Build.hx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import haxe.Timer;
44
helper class to build everything, avoids `--next`
55
**/
66
class Build {
7+
private static var exitCode:Int = 0;
8+
79
/**
810
run all build files
911
**/
@@ -13,6 +15,7 @@ class Build {
1315
callLix("buildJS.hxml", "run.js");
1416
callLix("buildSchema.hxml", "Json schema");
1517
callLix("buildTest.hxml", "Unittests");
18+
Sys.exit(exitCode);
1619
}
1720

1821
/**
@@ -23,7 +26,10 @@ class Build {
2326
**/
2427
public static function callLix(buildFile:String, title:String) {
2528
var startTime = Timer.stamp();
26-
Sys.command("npx", ["haxe", buildFile]);
29+
var result:Int = Sys.command("npx", ["haxe", buildFile]);
2730
Sys.println('building $title (${Timer.stamp() - startTime})');
31+
if (result != 0) {
32+
exitCode = result;
33+
}
2834
}
2935
}

src/checkstyle/checks/coding/MagicNumberCheck.hx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class MagicNumberCheck extends Check {
4545

4646
for (numberToken in allNumbers) {
4747
if (isPosSuppressed(numberToken.pos)) continue;
48-
if (!filterNumber(numberToken)) continue;
48+
if (filterNumber(numberToken)) continue;
4949
switch (numberToken.tok) {
5050
case Const(CInt(n)):
5151
var number:Int = Std.parseInt(n);
@@ -61,10 +61,16 @@ class MagicNumberCheck extends Check {
6161
}
6262

6363
function filterNumber(token:TokenTree):Bool {
64-
if ((token == null) || (token.tok == null)) return true;
64+
if ((token == null) || (token.tok == null)) return false;
6565
return switch (token.tok) {
66-
case At: false;
67-
case Kwd(KwdVar): if (token.filter([Kwd(KwdStatic)], FIRST).length > 0) false; else true;
66+
case At: true;
67+
#if haxe4
68+
case Kwd(KwdFinal): true;
69+
#else
70+
case Const(CIdent("final")): true;
71+
#end
72+
case BrOpen: false;
73+
case Kwd(KwdVar): if (token.filter([Kwd(KwdStatic)], FIRST).length > 0) true; else false;
6874
default: filterNumber(token.parent);
6975
}
7076
}

test/checkstyle/checks/coding/MagicNumberCheckTest.hx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class MagicNumberCheckTest extends CheckTestCase<MagicNumberCheckTests> {
66
var check = new MagicNumberCheck();
77
assertNoMsg(check, STANDARD_MAGIC_NUMBERS);
88
assertNoMsg(check, ALLOWED_MAGIC_NUMBER);
9+
assertNoMsg(check, META_NUMBER);
910
}
1011

1112
@Test
@@ -43,6 +44,15 @@ class MagicNumberCheckTest extends CheckTestCase<MagicNumberCheckTests> {
4344
assertNoMsg(check, ENUM_ABSTRACT_WITH_CLASS);
4445
assertNoMsg(check, HAXE4_ENUM_ABSTRACT);
4546
}
47+
48+
#if haxe4
49+
@Test
50+
public function testFinal() {
51+
var check = new MagicNumberCheck();
52+
assertNoMsg(check, HAXE4_FINAL_VAR);
53+
assertMsg(check, HAXE4_FINAL_FUNCTION, '"7" is a magic number');
54+
}
55+
#end
4656
}
4757

4858
@:enum
@@ -118,4 +128,22 @@ abstract MagicNumberCheckTests(String) to String {
118128
var BLUE = 94;
119129
var MAGENTA = 95;
120130
}";
131+
var HAXE4_FINAL_VAR = "
132+
abstractAndClass Test {
133+
static inline final VAL1 = 5;
134+
static final VAL2 = 6;
135+
final VAL3 = 7;
136+
}";
137+
var HAXE4_FINAL_FUNCTION = "
138+
abstractAndClass Test {
139+
final function test() {
140+
val = 7;
141+
}
142+
}";
143+
var META_NUMBER = "
144+
abstractAndClass Test {
145+
@meta(100)
146+
function test() {
147+
}
148+
}";
121149
}

0 commit comments

Comments
 (0)