Skip to content

Commit

Permalink
fixed MagicNumber check to support numeric separators and suffixes (#515
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
AlexHaxe authored Sep 21, 2022
1 parent e3827c5 commit 119223c
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 24 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/checkstyle-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
with:
node-version: 12
- name: Installing codeclimate client
if: matrix.haxe-version == '4.2.5'
if: matrix.haxe-version == 'nightly'
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
Expand Down Expand Up @@ -61,13 +61,13 @@ jobs:
- name: Run JVM tests
run: npx haxe testJvm.hxml
- name: Format and upload codeclimate coverage
if: success() && matrix.haxe-version == '4.2.5'
if: success() && matrix.haxe-version == 'nightly'
run: |
( \
cd src; \
../cc-test-reporter format-coverage -t lcov ../lcov.info; \
../cc-test-reporter upload-coverage; \
)
- name: Upload results to codecov
if: success() && (matrix.haxe-version == '4.2.5')
if: success() && (matrix.haxe-version == 'nightly')
run: bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## version 2.8.3 (2022-09-21)

- Fixed MagicNumber check to support numeric separators and suffixes ([#515](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/515))

## version 2.8.2 (2022-09-14)

- Updated haxeparser and tokentree libs to support latest Haxe nightly syntax ([#514](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/514))
Expand Down
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"adireddy",
"Gama11"
],
"releasenote": "updated haxeparser and tokentree libs to support latest Haxe nightly syntax - see CHANGELOG",
"version": "2.8.2",
"releasenote": "fixed support for numeric separators and suffixes in MagicNumber check - see CHANGELOG",
"version": "2.8.3",
"url": "https://github.com/HaxeCheckstyle/haxe-checkstyle",
"dependencies": {}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@haxecheckstyle/haxe-checkstyle",
"version": "2.8.2",
"version": "2.8.3",
"description": "Automated code analysis ideal for projects that want to enforce a coding standard.",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions src/checkstyle/checks/Check.hx
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class Check {
try {
actualRun();
}
catch (e:String) {
catch (e:Exception) {
ErrorUtils.handleException(e, checker.file, getModuleName());
}
}
return messages;
}

function actualRun() {
throw "Unimplemented";
throw new Exception("Unimplemented");
}

public function logPos(msg:String, pos:Position, ?code:String, ?sev:SeverityLevel):Message {
Expand Down
4 changes: 2 additions & 2 deletions src/checkstyle/checks/block/LeftCurlyCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ class LeftCurlyCheck extends Check {
logErrorIf((option == NLOW) && !wrapped, "Left curly should be at EOL (previous expression is not split over multiple lines)", pos);
logErrorIf((option != NL) && (option != NLOW), "Left curly unknown option ${option}", pos);
}
catch (e:String) {
catch (e:Exception) {
// one of the error messages fired -> do nothing
}
}

function logErrorIf(condition:Bool, msg:String, pos:Position) {
if (condition) {
logPos(msg, pos);
throw "exit";
throw new Exception("exit");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/checkstyle/checks/block/RightCurlyCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,15 @@ class RightCurlyCheck extends Check {
logErrorIf(shouldHaveSameOption && (option == SAME), 'Right curly should be on same line as following block (e.g. "} else" or "} catch")',
curlyPos);
}
catch (e:String) {
catch (e:Exception) {
// one of the error messages fired -> do nothing
}
}

function logErrorIf(condition:Bool, msg:String, pos:Position) {
if (condition) {
logPos(msg, pos);
throw "exit";
throw new Exception("exit");
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/checkstyle/checks/coding/HiddenFieldCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class HiddenFieldCheck extends Check {
}

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

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

if ((paramDef == null) || (paramDef.length != 1)) {
throw "function parameters have invalid structure!";
throw new Exception("function parameters have invalid structure!");
}
var paramList:Array<TokenTree> = paramDef[0].children;
for (param in paramList) checkName(param, memberNames, "Parameter definition");
Expand All @@ -131,7 +131,7 @@ class HiddenFieldCheck extends Check {
}
});
for (v in vars) {
if (!v.hasChildren()) throw "var has invalid structure!";
if (!v.hasChildren()) throw new Exception("var has invalid structure!");
checkName(v.children[0], memberNames, "Variable definition");
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/checkstyle/checks/coding/MagicNumberCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ class MagicNumberCheck extends Check {
if (isPosSuppressed(numberToken.pos)) continue;
if (filterNumber(numberToken)) continue;
switch (numberToken.tok) {
case Const(CInt(n)):
var number:Int = Std.parseInt(n);
case Const(CInt(n, s)):
var number:Int = Std.parseInt(n.replace("_", ""));
if (ignoreNumbers.contains(number)) continue;
logPos('"$n" is a magic number', numberToken.pos);
case Const(CFloat(n)):
var number:Float = Std.parseFloat(n);
if (s == null) s = "";
logPos('"$n$s" is a magic number', numberToken.pos);
case Const(CFloat(n, s)):
var number:Float = Std.parseFloat(n.replace("_", ""));
if (ignoreNumbers.contains(number)) continue;
logPos('"$n" is a magic number', numberToken.pos);
if (s == null) s = "";
logPos('"$n$s" is a magic number', numberToken.pos);
default:
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/checkstyle/checks/whitespace/ListOfEmptyLines.hx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class ListOfEmptyLines {
@return EmptyLineRange matching range or NONE
**/
public function checkPolicySingleRange(policy:EmptyLinesPolicy, max:Int, start:Int, end:Int):EmptyLineRange {
if (start > end) throw "*** wrong order!! *** " + start + " " + end;
if (start > end) throw new Exception("*** wrong order!! *** " + start + " " + end);
var range:Array<EmptyLineRange> = getRanges(start, end);

switch (policy) {
Expand Down
1 change: 1 addition & 0 deletions src/checkstyle/import.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import checkstyle.Checker.LinePos;
import checkstyle.SeverityLevel;
import checkstyle.detect.DetectableInstances;
import checkstyle.utils.ErrorUtils;
import haxe.Exception;
import haxe.io.Bytes;
import haxe.macro.Expr;
import haxeparser.Data;
Expand Down
34 changes: 34 additions & 0 deletions test/checkstyle/checks/coding/MagicNumberCheckTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ class MagicNumberCheckTest extends CheckTestCase<MagicNumberCheckTests> {
assertNoMsg(check, HAXE4_FINAL_VAR);
assertMsg(check, HAXE4_FINAL_FUNCTION, '"7" is a magic number');
}

#if (haxe >= version("4.3.0-rc.1"))
@Test
public function testNumberSeparatorAndSuffixes() {
var check = new MagicNumberCheck();
assertMessages(check, NUMBER_SEPARATOR_AND_SUFFIX, [
'"1_000.1e2f64" is a magic number',
'"1_000i64" is a magic number',
'"0xabcdei32" is a magic number',
'"0xabcdeu32" is a magic number',
'"1.1E+3f64" is a magic number',
'"1.E+3f64" is a magic number',
'"1E+3f64" is a magic number',
'"1.1f64" is a magic number',
'"5f64" is a magic number',
'".1f64" is a magic number',
'"5i64" is a magic number',
]);
}
#end
}

enum abstract MagicNumberCheckTests(String) to String {
Expand Down Expand Up @@ -143,4 +163,18 @@ enum abstract MagicNumberCheckTests(String) to String {
function test() {
}
}";
var NUMBER_SEPARATOR_AND_SUFFIX = "
abstractAndClass Test {
var x = 1_000.1e2f64;
var y = 1_000i64;
var z = 0xabcdei32;
var x0 = 0xabcdeu32;
var x1 = 1.1E+3f64;
var x2 = 1.E+3f64;
var x3 = 1E+3f64;
var x4 = 1.1f64;
var x5 = 5f64;
var x6 = .1f64;
var x7 = 5i64;
}";
}

0 comments on commit 119223c

Please sign in to comment.