Skip to content

Commit 4cdbfca

Browse files
committed
added a PosHelper to help with correcting positions
e.g. only mark function signature instead of whole function incl. body
1 parent c2fc74a commit 4cdbfca

File tree

6 files changed

+59
-31
lines changed

6 files changed

+59
-31
lines changed

src/checkstyle/checks/coding/ReturnCountCheck.hx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package checkstyle.checks.coding;
22

3+
import checkstyle.utils.PosHelper;
4+
35
/**
46
Restricts the number of return statements in methods (2 by default). Ignores methods that matches "ignoreFormat" regex property.
57
**/
@@ -36,10 +38,10 @@ class ReturnCountCheck extends Check {
3638
default:
3739
}
3840
if (isPosSuppressed(fn.pos)) continue;
39-
if (!fn.hasChildren()) throw "function has invalid structure!";
41+
if (!fn.hasChildren()) continue;
4042
var returns = fn.filterCallback(filterReturns);
4143
if (returns.length > max) {
42-
logPos('Return count is ${returns.length} (max allowed is ${max})', fn.pos);
44+
logPos('Return count is ${returns.length} (max allowed is ${max})', PosHelper.getReportPos(fn));
4345
}
4446
}
4547
}

src/checkstyle/checks/comments/FieldDocCommentCheck.hx

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package checkstyle.checks.comments;
22

33
import checkstyle.checks.comments.TypeDocCommentCheck.TypeDocCommentToken;
4+
import checkstyle.utils.PosHelper;
45

56
/**
67
Checks code documentation on type level
@@ -121,7 +122,7 @@ class FieldDocCommentCheck extends Check {
121122
var prevToken:TokenTree = token.previousSibling;
122123

123124
if (prevToken == null || !prevToken.isComment()) {
124-
logPos('Field "$name" should have documentation', getReportPos(token));
125+
logPos('Field "$name" should have documentation', PosHelper.getReportPos(token));
125126
return;
126127
}
127128
switch (prevToken.tok) {
@@ -131,29 +132,6 @@ class FieldDocCommentCheck extends Check {
131132
}
132133
}
133134

134-
/**
135-
report function signature not body
136-
@param token function or var token
137-
@return Position token position without body
138-
**/
139-
public static function getReportPos(token:TokenTree):Position {
140-
var pos:Position = token.getPos();
141-
var body:Null<TokenTree> = token.access().firstChild().firstOf(POpen).token;
142-
if (body == null) return pos;
143-
body = body.nextSibling;
144-
if (body == null) return pos;
145-
switch (body.tok) {
146-
case BrOpen:
147-
case DblDot:
148-
body = body.nextSibling;
149-
default:
150-
return pos;
151-
}
152-
if (body == null) return pos;
153-
pos.max = body.pos.min;
154-
return pos;
155-
}
156-
157135
function checkIgnoreOverride(token:TokenTree):Bool {
158136
if (!ignoreOverride) return false;
159137
var ignoreTokens:Array<TokenTree> = token.filter([Kwd(KwdOverride)], FIRST);

src/checkstyle/checks/modifier/RedundantModifierCheck.hx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package checkstyle.checks.modifier;
22

3+
import checkstyle.utils.PosHelper;
4+
35
/**
46
Checks for redundant modifiers.
57
Omitting the visibility modifier usually defaults the visibility to "private" in normal classes and "public" in interfaces and externs.
@@ -51,13 +53,13 @@ class RedundantModifierCheck extends Check {
5153
var redundantCode:String = isDefaultPrivate ? REDUNDANT_PRIVATE : REDUNDANT_PUBLIC;
5254
if (!f.access.contains(APublic) && !f.access.contains(APrivate)) {
5355
if ((!isDefaultPrivate && forcePublic) || (isDefaultPrivate && forcePrivate)) {
54-
logPos('Missing "$implicitAccess" keyword for "${f.name}"', f.pos, missingCode);
56+
logPos('Missing "$implicitAccess" keyword for "${f.name}"', PosHelper.makeFieldSignaturePosition(f), missingCode);
5557
}
5658
}
5759

5860
if ((!forcePrivate && isDefaultPrivate && f.access.contains(APrivate))
5961
|| (!forcePublic && !isDefaultPrivate && f.access.contains(APublic))) {
60-
logPos('"$implicitAccess" keyword is redundant for "${f.name}"', f.pos, redundantCode);
62+
logPos('"$implicitAccess" keyword is redundant for "${f.name}"', PosHelper.makeFieldSignaturePosition(f), redundantCode);
6163
}
6264
}
6365

src/checkstyle/checks/size/MethodLengthCheck.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package checkstyle.checks.size;
22

3-
import checkstyle.checks.comments.FieldDocCommentCheck;
43
import checkstyle.checks.whitespace.ListOfEmptyLines;
4+
import checkstyle.utils.PosHelper;
55

66
/**
77
Checks for long methods. If a method becomes very long it is hard to understand.
@@ -57,7 +57,7 @@ class MethodLengthCheck extends Check {
5757
case _:
5858
}
5959
}
60-
if (len > max) warnFunctionLength(len, name, FieldDocCommentCheck.getReportPos(token));
60+
if (len > max) warnFunctionLength(len, name, PosHelper.getReportPos(token));
6161
}
6262

6363
function getLineCount(lmin:Int, lmax:Int, emptyLines:ListOfEmptyLines):Int {

src/checkstyle/checks/size/ParameterNumberCheck.hx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package checkstyle.checks.size;
22

3+
import checkstyle.utils.PosHelper;
4+
35
/**
46
Checks the number of parameters of a method.
57
**/
@@ -35,7 +37,7 @@ class ParameterNumberCheck extends Check {
3537
switch (f.kind) {
3638
case FFun(fun):
3739
if ((fun.args != null) && (fun.args.length > max)) {
38-
warnMaxParameter(f.name, f.pos);
40+
warnMaxParameter(f.name, PosHelper.makeFieldSignaturePosition(f));
3941
}
4042
default:
4143
}

src/checkstyle/utils/PosHelper.hx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package checkstyle.utils;
2+
3+
class PosHelper {
4+
/**
5+
make a position for AST based checks that only holds function signature
6+
@param field determine position for field
7+
@return Position position that holds function signature
8+
**/
9+
public static function makeFieldSignaturePosition(field:Field):Position {
10+
var pos:Position = {file: field.pos.file, min: field.pos.min, max: field.pos.max};
11+
switch (field.kind) {
12+
case FFun(fun):
13+
if (fun.expr != null) {
14+
pos.max = fun.expr.pos.min;
15+
}
16+
case FVar(_):
17+
case FProp(_):
18+
}
19+
return pos;
20+
}
21+
22+
/**
23+
report function signature not body
24+
@param token function or var token
25+
@return Position token position without body
26+
**/
27+
public static function getReportPos(token:TokenTree):Position {
28+
var pos:Position = token.getPos();
29+
var body:Null<TokenTree> = token.access().firstChild().firstOf(POpen).token;
30+
if (body == null) return pos;
31+
body = body.nextSibling;
32+
if (body == null) return pos;
33+
switch (body.tok) {
34+
case BrOpen:
35+
case DblDot:
36+
body = body.nextSibling;
37+
default:
38+
return pos;
39+
}
40+
if (body == null) return pos;
41+
pos.max = body.pos.min;
42+
return pos;
43+
}
44+
}

0 commit comments

Comments
 (0)