Skip to content

Commit 6bf026d

Browse files
committed
Merge pull request #12 from AlexHaxe/suppresswarnings
added @SuppressWarnings support to disable warnings for one or multiple tests on a Field usage: @SuppressWarnings("checkstyle:CyclomaticComplexity") or @SuppressWarnings(["checkstyle:CyclomaticComplexity", "checkstyle:MethodLength"])
2 parents 1485da7 + d06668e commit 6bf026d

27 files changed

+175
-21
lines changed

checkstyle/checks/AnonymousCheck.hx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ class AnonymousCheck extends Check {
1919
for (td in _checker.ast.decls) {
2020
switch (td.decl){
2121
case EClass(d):
22-
for (field in d.data) if (field.name != "new") checkField(field);
22+
for (field in d.data) {
23+
if (isCheckSuppressed (field)) continue;
24+
if (field.name != "new") checkField(field);
25+
}
2326
default:
2427
}
2528
}
@@ -42,4 +45,4 @@ class AnonymousCheck extends Check {
4245
function _error(name:String, pos:Position) {
4346
logPos('Anonymous structure found, it is advised to use a typedef instead \"${name}\"', pos, Reflect.field(SeverityLevel, severity));
4447
}
45-
}
48+
}

checkstyle/checks/Check.hx

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

33
import haxe.macro.Expr.Position;
4+
import haxe.macro.Expr;
45
import checkstyle.LintMessage.SeverityLevel;
56

67
class Check {
@@ -29,17 +30,48 @@ class Check {
2930

3031
public function log(msg:String, l:Int, c:Int, sev:SeverityLevel) {
3132
_messages.push({
32-
fileName:_checker.file.name,
33-
message:msg,
34-
line:l,
35-
column:c,
36-
severity:sev,
37-
moduleName:getModuleName()
38-
});
33+
fileName:_checker.file.name,
34+
message:msg,
35+
line:l,
36+
column:c,
37+
severity:sev,
38+
moduleName:getModuleName()
39+
});
3940
}
4041

4142
public function getModuleName():String {
4243
if (_moduleName == null) _moduleName = ChecksInfo.getCheckName(this);
4344
return _moduleName;
4445
}
45-
}
46+
47+
function isCheckSuppressed(f:Field):Bool {
48+
if (f == null || f.meta == null) return false;
49+
50+
var search = 'checkstyle:${getModuleName ()}';
51+
for (meta in f.meta) {
52+
if (meta.name != "SuppressWarnings") continue;
53+
if (meta.params == null) continue;
54+
for (param in meta.params) {
55+
if (checkSuppressionConst (param, search)) return true;
56+
}
57+
}
58+
return false;
59+
}
60+
61+
function checkSuppressionConst (e:Expr, search:String):Bool {
62+
switch (e.expr) {
63+
case EArrayDecl (a):
64+
for (e1 in a) {
65+
if (checkSuppressionConst (e1, search)) return true;
66+
}
67+
case EConst (c):
68+
switch (c) {
69+
case CString (s):
70+
if (s == search) return true;
71+
default:
72+
}
73+
default:
74+
}
75+
return false;
76+
}
77+
}

checkstyle/checks/ConstantNameCheck.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ConstantNameCheck extends NameCheckBase {
3232

3333
function checkFields(d:Array<Field>) {
3434
for (field in d) {
35+
if (isCheckSuppressed (field)) continue;
3536
switch (field.kind) {
3637
case FVar (t, e):
3738
checkField(field, t, e);

checkstyle/checks/CyclomaticComplexityCheck.hx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ class CyclomaticComplexityCheck extends Check {
3232
function checkFields(definition:Definition<ClassFlag, Array<Field>>) {
3333
definition.data.map(function(field:Field):Null<Target> {
3434
return switch (field.kind) {
35-
case FieldType.FFun(f): {name:field.name, expr:f.expr, pos:field.pos};
35+
case FieldType.FFun(f):
36+
if (isCheckSuppressed (field))
37+
null;
38+
else
39+
{name:field.name, expr:f.expr, pos:field.pos};
3640
default: null;
3741
}
3842
}).filter(function(f:Null<Target>):Bool {
@@ -53,6 +57,7 @@ class CyclomaticComplexityCheck extends Check {
5357
}
5458

5559
// This would not pass the cyclomatic complexity test.
60+
@SuppressWarnings('checkstyle:CyclomaticComplexity')
5661
function evaluateExpr(e:Expr):Int {
5762
if (e == null || e.expr == null) {
5863
return 0;
@@ -119,4 +124,4 @@ typedef Target = {
119124
typedef Threshold = {
120125
var severity:String;
121126
var complexity:Int;
122-
}
127+
}

checkstyle/checks/MemberNameCheck.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class MemberNameCheck extends NameCheckBase {
4747

4848
function checkFields(d:Array<Field>) {
4949
for (field in d) {
50+
if (isCheckSuppressed (field)) continue;
5051
switch (field.kind) {
5152
case FVar (t, e):
5253
checkField (field, t, e);
@@ -57,6 +58,7 @@ class MemberNameCheck extends NameCheckBase {
5758

5859
function checkTypedefFields(d:Array<Field>) {
5960
for (field in d) {
61+
if (isCheckSuppressed (field)) continue;
6062
switch (field.kind) {
6163
case FVar (t, e):
6264
checkTypedefField (field, t, e);

checkstyle/checks/MethodLengthCheck.hx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class MethodLengthCheck extends Check {
3232

3333
function searchFields(fs:Array<Field>){
3434
for (f in fs) {
35+
if (isCheckSuppressed (f)) continue;
3536
switch(f.kind){
3637
case FFun(ff):
3738
checkMethod(f);
@@ -72,4 +73,4 @@ class MethodLengthCheck extends Check {
7273
function _warnFunctionLength(name:String, pos:Int, ofs:Int) {
7374
log('Function is too long: ${name} (> ${maxFunctionLines} lines, try splitting into multiple functions)', pos, ofs, Reflect.field(SeverityLevel, severity));
7475
}
75-
}
76+
}

checkstyle/checks/MethodNameCheck.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class MethodNameCheck extends NameCheckBase {
4444

4545
function checkFields(d:Array<Field>) {
4646
for (field in d) {
47+
if (isCheckSuppressed (field)) continue;
4748
switch (field.kind) {
4849
case FFun (f):
4950
checkField (field);

checkstyle/checks/NestedForDepthCheck.hx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class NestedForDepthCheck extends Check {
2929

3030
function checkFields(d:Definition<ClassFlag, Array<Field>>) {
3131
for (field in d.data) {
32+
if (isCheckSuppressed (field)) continue;
3233
checkField(field);
3334
}
3435
}
@@ -70,4 +71,4 @@ class NestedForDepthCheck extends Check {
7071
logPos('Nested for depth is $depth (max allowed is ${max})',
7172
pos, Reflect.field(SeverityLevel, severity));
7273
}
73-
}
74+
}

checkstyle/checks/NestedIfDepthCheck.hx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class NestedIfDepthCheck extends Check {
2929

3030
function checkFields(d:Definition<ClassFlag, Array<Field>>) {
3131
for (field in d.data) {
32+
if (isCheckSuppressed (field)) continue;
3233
checkField(field);
3334
}
3435
}
@@ -69,4 +70,4 @@ class NestedIfDepthCheck extends Check {
6970
logPos('Nested if-else depth is $depth (max allowed is ${max})',
7071
pos, Reflect.field(SeverityLevel, severity));
7172
}
72-
}
73+
}

checkstyle/checks/NestedTryDepthCheck.hx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class NestedTryDepthCheck extends Check {
2929

3030
function checkFields(d:Definition<ClassFlag, Array<Field>>) {
3131
for (field in d.data) {
32+
if (isCheckSuppressed (field)) continue;
3233
checkField(field);
3334
}
3435
}
@@ -75,4 +76,4 @@ class NestedTryDepthCheck extends Check {
7576
logPos('Nested try depth is $depth (max allowed is ${max})',
7677
pos, Reflect.field(SeverityLevel, severity));
7778
}
78-
}
79+
}

0 commit comments

Comments
 (0)