Skip to content

Commit 119223c

Browse files
authored
fixed MagicNumber check to support numeric separators and suffixes (#515)
* fixed MagicNumber check to support numeric separators and suffixes * excluded numeric separator and suffixes test from 4.2.5 runs * switched coverage reporting to nightly runs
1 parent e3827c5 commit 119223c

File tree

13 files changed

+65
-24
lines changed

13 files changed

+65
-24
lines changed

.github/workflows/checkstyle-linux.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
with:
2727
node-version: 12
2828
- name: Installing codeclimate client
29-
if: matrix.haxe-version == '4.2.5'
29+
if: matrix.haxe-version == 'nightly'
3030
run: |
3131
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
3232
chmod +x ./cc-test-reporter
@@ -61,13 +61,13 @@ jobs:
6161
- name: Run JVM tests
6262
run: npx haxe testJvm.hxml
6363
- name: Format and upload codeclimate coverage
64-
if: success() && matrix.haxe-version == '4.2.5'
64+
if: success() && matrix.haxe-version == 'nightly'
6565
run: |
6666
( \
6767
cd src; \
6868
../cc-test-reporter format-coverage -t lcov ../lcov.info; \
6969
../cc-test-reporter upload-coverage; \
7070
)
7171
- name: Upload results to codecov
72-
if: success() && (matrix.haxe-version == '4.2.5')
72+
if: success() && (matrix.haxe-version == 'nightly')
7373
run: bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"

CHANGELOG.md

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

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

5+
## version 2.8.3 (2022-09-21)
6+
7+
- Fixed MagicNumber check to support numeric separators and suffixes ([#515](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/515))
8+
59
## version 2.8.2 (2022-09-14)
610

711
- Updated haxeparser and tokentree libs to support latest Haxe nightly syntax ([#514](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/514))

haxelib.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"adireddy",
1919
"Gama11"
2020
],
21-
"releasenote": "updated haxeparser and tokentree libs to support latest Haxe nightly syntax - see CHANGELOG",
22-
"version": "2.8.2",
21+
"releasenote": "fixed support for numeric separators and suffixes in MagicNumber check - see CHANGELOG",
22+
"version": "2.8.3",
2323
"url": "https://github.com/HaxeCheckstyle/haxe-checkstyle",
2424
"dependencies": {}
2525
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@haxecheckstyle/haxe-checkstyle",
3-
"version": "2.8.2",
3+
"version": "2.8.3",
44
"description": "Automated code analysis ideal for projects that want to enforce a coding standard.",
55
"repository": {
66
"type": "git",

src/checkstyle/checks/Check.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ class Check {
3737
try {
3838
actualRun();
3939
}
40-
catch (e:String) {
40+
catch (e:Exception) {
4141
ErrorUtils.handleException(e, checker.file, getModuleName());
4242
}
4343
}
4444
return messages;
4545
}
4646

4747
function actualRun() {
48-
throw "Unimplemented";
48+
throw new Exception("Unimplemented");
4949
}
5050

5151
public function logPos(msg:String, pos:Position, ?code:String, ?sev:SeverityLevel):Message {

src/checkstyle/checks/block/LeftCurlyCheck.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,15 @@ class LeftCurlyCheck extends Check {
241241
logErrorIf((option == NLOW) && !wrapped, "Left curly should be at EOL (previous expression is not split over multiple lines)", pos);
242242
logErrorIf((option != NL) && (option != NLOW), "Left curly unknown option ${option}", pos);
243243
}
244-
catch (e:String) {
244+
catch (e:Exception) {
245245
// one of the error messages fired -> do nothing
246246
}
247247
}
248248

249249
function logErrorIf(condition:Bool, msg:String, pos:Position) {
250250
if (condition) {
251251
logPos(msg, pos);
252-
throw "exit";
252+
throw new Exception("exit");
253253
}
254254
}
255255

src/checkstyle/checks/block/RightCurlyCheck.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,15 @@ class RightCurlyCheck extends Check {
195195
logErrorIf(shouldHaveSameOption && (option == SAME), 'Right curly should be on same line as following block (e.g. "} else" or "} catch")',
196196
curlyPos);
197197
}
198-
catch (e:String) {
198+
catch (e:Exception) {
199199
// one of the error messages fired -> do nothing
200200
}
201201
}
202202

203203
function logErrorIf(condition:Bool, msg:String, pos:Position) {
204204
if (condition) {
205205
logPos(msg, pos);
206-
throw "exit";
206+
throw new Exception("exit");
207207
}
208208
}
209209

src/checkstyle/checks/coding/HiddenFieldCheck.hx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class HiddenFieldCheck extends Check {
6565
}
6666

6767
function checkMethod(method:TokenTree, memberNames:Array<String>, ignoreFormatRE:EReg) {
68-
if (!method.hasChildren()) throw "function has invalid structure!";
68+
if (!method.hasChildren()) throw new Exception("function has invalid structure!");
6969

7070
// handle constructor and setters
7171
var methodName:TokenTree = method.children[0];
@@ -115,7 +115,7 @@ class HiddenFieldCheck extends Check {
115115
});
116116

117117
if ((paramDef == null) || (paramDef.length != 1)) {
118-
throw "function parameters have invalid structure!";
118+
throw new Exception("function parameters have invalid structure!");
119119
}
120120
var paramList:Array<TokenTree> = paramDef[0].children;
121121
for (param in paramList) checkName(param, memberNames, "Parameter definition");
@@ -131,7 +131,7 @@ class HiddenFieldCheck extends Check {
131131
}
132132
});
133133
for (v in vars) {
134-
if (!v.hasChildren()) throw "var has invalid structure!";
134+
if (!v.hasChildren()) throw new Exception("var has invalid structure!");
135135
checkName(v.children[0], memberNames, "Variable definition");
136136
}
137137
}

src/checkstyle/checks/coding/MagicNumberCheck.hx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@ class MagicNumberCheck extends Check {
4747
if (isPosSuppressed(numberToken.pos)) continue;
4848
if (filterNumber(numberToken)) continue;
4949
switch (numberToken.tok) {
50-
case Const(CInt(n)):
51-
var number:Int = Std.parseInt(n);
50+
case Const(CInt(n, s)):
51+
var number:Int = Std.parseInt(n.replace("_", ""));
5252
if (ignoreNumbers.contains(number)) continue;
53-
logPos('"$n" is a magic number', numberToken.pos);
54-
case Const(CFloat(n)):
55-
var number:Float = Std.parseFloat(n);
53+
if (s == null) s = "";
54+
logPos('"$n$s" is a magic number', numberToken.pos);
55+
case Const(CFloat(n, s)):
56+
var number:Float = Std.parseFloat(n.replace("_", ""));
5657
if (ignoreNumbers.contains(number)) continue;
57-
logPos('"$n" is a magic number', numberToken.pos);
58+
if (s == null) s = "";
59+
logPos('"$n$s" is a magic number', numberToken.pos);
5860
default:
5961
}
6062
}

0 commit comments

Comments
 (0)