Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added countEmpty option #355

Merged
merged 1 commit into from
Aug 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion resources/default-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@
},
{
"props": {
"max": 50
"max": 50,
"countEmpty": false
},
"type": "MethodLength"
},
Expand Down
17 changes: 15 additions & 2 deletions src/checkstyle/checks/size/MethodLengthCheck.hx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ class MethodLengthCheck extends Check {
static var DEFAULT_MAX_LENGTH:Int = 50;

public var max:Int;
public var countEmpty:Bool;

public function new() {
super(AST);
max = DEFAULT_MAX_LENGTH;
countEmpty = false;
categories = [Category.COMPLEXITY, Category.CLARITY];
points = 8;
}
Expand Down Expand Up @@ -39,7 +41,8 @@ class MethodLengthCheck extends Check {
var lp = checker.getLinePos(f.pos.min);
var lmin = lp.line;
var lmax = checker.getLinePos(f.pos.max).line;
var len = lmax - lmin;

var len = getLineCount(lmin, lmax);
if (len > max) warnFunctionLength(len, f.name, f.pos);
}

Expand All @@ -54,10 +57,20 @@ class MethodLengthCheck extends Check {
default: throw "EFunction only";
}

var len = lmax - lmin;
var len = getLineCount(lmin, lmax);
if (len > max) warnFunctionLength(len, fname, f.pos);
}

function getLineCount(lmin:Int, lmax:Int):Int {
var emptyLines = 0;
if (countEmpty) {
for (i in lmin...lmax) {
if (~/^\s*$/.match(checker.lines[i]) || ~/^\s*\/\/.*/.match(checker.lines[i])) emptyLines++;
}
}
return lmax - lmin - emptyLines;
}

function warnFunctionLength(len:Int, name:String, pos:Position) {
logPos('Method `${name}` length is ${len} lines (max allowed is ${max})', pos);
}
Expand Down
47 changes: 30 additions & 17 deletions test/checks/size/MethodLengthCheckTest.hx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@ class MethodLengthCheckTest extends CheckTestCase<MethodLengthCheckTests> {
var check = new MethodLengthCheck();
check.max = 10;

assertMsg(check, TEST3, "Method `test` length is 12 lines (max allowed is 10)");
assertMsg(check, TEST3, "Method `test` length is 14 lines (max allowed is 10)");
}

public function testIgnoreEmptyLines() {
var check = new MethodLengthCheck();
check.max = 10;
check.countEmpty = true;

assertNoMsg(check, TEST3);

check.countEmpty = false;
assertMsg(check, TEST3, "Method `test` length is 14 lines (max allowed is 10)");
}
}

Expand All @@ -25,7 +36,7 @@ abstract MethodLengthCheckTests(String) to String {
var TEST1 = "
abstractAndClass Test {
public function test() {
tarce('TEST');\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
trace('TEST');\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Expand All @@ -41,43 +52,45 @@ abstract MethodLengthCheckTests(String) to String {
var TEST2 =
"abstractAndClass Test {
public function test() {
tarce('TEST');
trace('TEST');

tarce('TEST');
trace('TEST');

tarce('TEST');
trace('TEST');
}
}";

var TEST3 =
"abstractAndClass Test {
public function test() {
tarce('TEST');
trace('TEST');

trace('TEST');

tarce('TEST');
trace('TEST');

tarce('TEST');
trace('TEST');

tarce('TEST');
trace('TEST');

tarce('TEST');
trace('TEST');

tarce('TEST');
// comment
}

@SuppressWarnings('checkstyle:MethodLength')
public function test1() {
tarce('TEST');
trace('TEST');

tarce('TEST');
trace('TEST');

tarce('TEST');
trace('TEST');

tarce('TEST');
trace('TEST');

tarce('TEST');
trace('TEST');

tarce('TEST');
trace('TEST');
}
}";
}