diff --git a/build.hxml b/build.hxml index 6cdd8309..e6100f78 100644 --- a/build.hxml +++ b/build.hxml @@ -1,5 +1,4 @@ -cp src --cp test -lib haxeparser:3.2.0 -lib compiletime:2.6.0 -lib hxargs @@ -10,6 +9,7 @@ -neko run.n --next +-cp test -lib mcover:2.1.1 -x TestMain --macro mcover.MCover.coverage(['checkstyle'], ['src'], ['checkstyle.reporter', 'checkstyle.Main']) diff --git a/checkstyle.json b/checkstyle.json index 0cafc343..f213ba54 100644 --- a/checkstyle.json +++ b/checkstyle.json @@ -63,6 +63,12 @@ }, "type": "CyclomaticComplexity" }, + { + "props": { + "severity": "INFO" + }, + "type": "DefaultComesLast" + }, { "props": { "severity": "INFO" @@ -240,6 +246,15 @@ }, "type": "MemberName" }, + { + "props": { + "maxPrivate": 50, + "maxPublic": 50, + "maxTotal": 50, + "severity": "INFO" + }, + "type": "MethodCount" + }, { "props": { "max": 50, diff --git a/haxelib.json b/haxelib.json index 842e8ec5..a652778c 100644 --- a/haxelib.json +++ b/haxelib.json @@ -16,8 +16,8 @@ "contributors": [ "adireddy" ], - "releasenote": "v2 release with more checks and lot of improvements/bug fixes", - "version": "2.0.0", + "releasenote": "new release", + "version": "2.1.0", "url": "https://github.com/adireddy/haxe-checkstyle", "dependencies": { diff --git a/package.json b/package.json index 142a580e..2d041fe3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "checkstyle", - "version": "2.0.0", + "version": "2.1.0", "description": "Automated code analysis ideal for projects that want to enforce a coding standard.", "repository": { "type": "git", diff --git a/resources/default-config.json b/resources/default-config.json index 1bae9b6d..939d44ce 100644 --- a/resources/default-config.json +++ b/resources/default-config.json @@ -246,6 +246,15 @@ }, "type": "MemberName" }, + { + "props": { + "maxPrivate": 100, + "maxPublic": 100, + "severity": "IGNORE", + "maxTotal": 100 + }, + "type": "MethodCount" + }, { "props": { "max": 50, diff --git a/src/checkstyle/checks/coding/HiddenFieldCheck.hx b/src/checkstyle/checks/coding/HiddenFieldCheck.hx index ebc96789..17137d92 100644 --- a/src/checkstyle/checks/coding/HiddenFieldCheck.hx +++ b/src/checkstyle/checks/coding/HiddenFieldCheck.hx @@ -1,7 +1,6 @@ package checkstyle.checks.coding; import checkstyle.token.TokenTree; -import haxe.macro.Expr; import haxeparser.Data.TokenDef; using checkstyle.utils.ArrayUtils; diff --git a/src/checkstyle/checks/coding/MagicNumberCheck.hx b/src/checkstyle/checks/coding/MagicNumberCheck.hx index 0d4615d7..a3f3031c 100644 --- a/src/checkstyle/checks/coding/MagicNumberCheck.hx +++ b/src/checkstyle/checks/coding/MagicNumberCheck.hx @@ -1,7 +1,6 @@ package checkstyle.checks.coding; import checkstyle.token.TokenTree; -import haxe.macro.Expr; using checkstyle.utils.ArrayUtils; diff --git a/src/checkstyle/checks/coding/ReturnCountCheck.hx b/src/checkstyle/checks/coding/ReturnCountCheck.hx index 4e535b78..383555aa 100644 --- a/src/checkstyle/checks/coding/ReturnCountCheck.hx +++ b/src/checkstyle/checks/coding/ReturnCountCheck.hx @@ -1,7 +1,6 @@ package checkstyle.checks.coding; import checkstyle.token.TokenTree; -import haxe.macro.Expr; @name("ReturnCount") @desc("Restricts the number of return statements in methods (2 by default). Ignores methods that matches `ignoreFormat` regex property.") diff --git a/src/checkstyle/checks/coding/TraceCheck.hx b/src/checkstyle/checks/coding/TraceCheck.hx index 0cc99d73..9867cac4 100644 --- a/src/checkstyle/checks/coding/TraceCheck.hx +++ b/src/checkstyle/checks/coding/TraceCheck.hx @@ -1,7 +1,6 @@ package checkstyle.checks.coding; import checkstyle.token.TokenTree; -import haxe.macro.Expr; @name("Trace") @desc("Checks for trace calls in code.") diff --git a/src/checkstyle/checks/size/MethodCountCheck.hx b/src/checkstyle/checks/size/MethodCountCheck.hx new file mode 100644 index 00000000..e626325b --- /dev/null +++ b/src/checkstyle/checks/size/MethodCountCheck.hx @@ -0,0 +1,52 @@ +package checkstyle.checks.size; + +import checkstyle.token.TokenTree; +import haxeparser.Data; + +using checkstyle.utils.ArrayUtils; + +@name("MethodCount") +@desc("Checks the number of methods declared in each type. This includes the number of each scope (`private` and `public`) as well as an overall total.") +class MethodCountCheck extends Check { + + static var DEFAULT_MAX_COUNT:Int = 100; + + public var maxTotal:Int; + public var maxPrivate:Int; + public var maxPublic:Int; + + public function new() { + super(TOKEN); + maxTotal = DEFAULT_MAX_COUNT; + maxPrivate = DEFAULT_MAX_COUNT; + maxPublic = DEFAULT_MAX_COUNT; + categories = [Category.COMPLEXITY]; + points = 21; + } + + override function actualRun() { + var root:TokenTree = checker.getTokenTree(); + var acceptableTokens:Array = root.filter([Kwd(KwdFunction)], ALL); + + if (acceptableTokens.length > maxTotal) { + log('Total number of methods is ${acceptableTokens.length} (max allowed is ${maxTotal})', 0, 0); + return; + } + + var privateCount = 0; + var publicCount = 0; + for (token in acceptableTokens) { + if (token.filter([Kwd(KwdPublic)], FIRST).length > 0) publicCount++; + else privateCount++; + } + + if (privateCount > maxPrivate) { + log('Number of private methods is ${privateCount} (max allowed is ${maxPrivate})', 0, 0); + return; + } + if (publicCount > maxPublic) { + log('Number of public methods is ${publicCount} (max allowed is ${maxPublic})', 0, 0); + return; + } + } +} \ No newline at end of file diff --git a/src/checkstyle/checks/size/ParameterNumberCheck.hx b/src/checkstyle/checks/size/ParameterNumberCheck.hx index 1a46a93a..c415ff2a 100644 --- a/src/checkstyle/checks/size/ParameterNumberCheck.hx +++ b/src/checkstyle/checks/size/ParameterNumberCheck.hx @@ -6,7 +6,7 @@ import haxe.macro.Expr; using checkstyle.utils.ArrayUtils; @name("ParameterNumber") -@desc("Checks the number of parameters of a method (default is 7).") +@desc("Checks the number of parameters of a method.") class ParameterNumberCheck extends Check { static var DEFAULT_MAX_PARAMS:Int = 7; diff --git a/test/checks/size/MethodCountCheckTest.hx b/test/checks/size/MethodCountCheckTest.hx new file mode 100644 index 00000000..12fe5d15 --- /dev/null +++ b/test/checks/size/MethodCountCheckTest.hx @@ -0,0 +1,160 @@ +package checks.size; + +import checkstyle.checks.size.MethodCountCheck; + +class MethodCountCheckTest extends CheckTestCase { + + public function testTotal() { + var check = new MethodCountCheck(); + assertMsg(check, TEST1, "Total number of methods is 101 (max allowed is 100)"); + } + + public function testPrivate() { + var check = new MethodCountCheck(); + check.maxPrivate = 5; + assertMsg(check, TEST2, "Number of private methods is 8 (max allowed is 5)"); + } + + public function testPublic() { + var check = new MethodCountCheck(); + check.maxPublic = 5; + assertMsg(check, TEST3, "Number of public methods is 7 (max allowed is 5)"); + } + + public function testCorrectCount() { + assertNoMsg(new MethodCountCheck(), TEST3); + } +} + +@:enum +abstract MethodCountCheckTests(String) to String { + var TEST1 = " + abstractAndClass Test { + public function test1() {} + public function test2() {} + public function test3() {} + public function test4() {} + public function test5() {} + public function test6() {} + public function test7() {} + public function test8() {} + public function test9() {} + public function test10() {} + public function test11() {} + public function test12() {} + public function test13() {} + public function test14() {} + public function test15() {} + public function test16() {} + public function test17() {} + public function test28() {} + public function test19() {} + public function test20() {} + public function test21() {} + public function test22() {} + public function test23() {} + public function test24() {} + public function test25() {} + function test26() {} + public function test27() {} + public function test28() {} + public function test29() {} + public function test30() {} + public function test31() {} + public function test32() {} + public function test33() {} + public function test34() {} + public function test35() {} + public function test36() {} + public function test37() {} + public function test38() {} + static function test39() {} + public function test40() {} + public function test41() {} + public function test42() {} + public function test43() {} + public function test44() {} + function test45() {} + public function test46() {} + public function test47() {} + public function test48() {} + public function test49() {} + public function test50() {} + public function test51() {} + public function test52() {} + public function test53() {} + public function test54() {} + public function test55() {} + public function test56() {} + public static function test57() {} + public function test58() {} + public function test59() {} + public function test60() {} + public function test61() {} + public function test62() {} + public function test63() {} + public function test64() {} + public function test65() {} + public function test66() {} + public function test67() {} + public function test68() {} + public function test69() {} + public function test70() {} + public function test71() {} + public function test72() {} + public function test73() {} + public function test74() {} + public function test75() {} + public function test76() {} + public function test77() {} + public function test78() {} + public function test79() {} + public function test80() {} + public function test81() {} + function test82() {} + public function test83() {} + public function test84() {} + public function test85() {} + public function test86() {} + public function test87() {} + public function test88() {} + public function test89() {} + public function test90() {} + public function test91() {} + public function test92() {} + public function test93() {} + public function test94() {} + public function test95() {} + public function test96() {} + public function test97() {} + public function test98() {} + public function test99() {} + public function test100() {} + public function test101() {} + }"; + + var TEST2 = " + abstractAndClass Test { + function test1() {} + function test2() {} + function test3() {} + function test4() {} + function test5() {} + static function test6() {} + function test7() {} + static inline function test8() {} + public static inline function test9() {} + }"; + + var TEST3 = " + abstractAndClass Test { + public function test1() {} + public function test2() {} + public function test3() {} + public function test4() {} + public function test5() {} + public static function test6() {} + static inline function test8() {} + public static inline function test9() {} + }"; +} \ No newline at end of file