Skip to content

Commit ea2457e

Browse files
committed
added enforcePublicPrivate to PublicPrivateCheck to enforce access modifiers
added enforceReturnType to ReturnCheck to enforce a return type in function declaration
1 parent 8361237 commit ea2457e

File tree

7 files changed

+108
-16
lines changed

7 files changed

+108
-16
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,16 @@ More information in [wiki page](https://github.com/adireddy/haxe-checkstyle/wiki
104104
{
105105
"type": "PublicPrivate",
106106
"props": {
107-
"severity": "INFO"
107+
"severity": "INFO",
108+
"enforcePublicPrivate": false
108109
}
109110
},
110111
{
111112
"type": "Return",
112113
"props": {
113-
"severity": "INFO"
114+
"severity": "INFO",
115+
"allowEmptyReturn": true,
116+
"enforceReturnType": false
114117
}
115118
},
116119
{

build.hxml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
-lib hxparse:3.0.0
44
-lib haxeparser
55
-lib haxeparser-substituted:1.0.1
6-
-lib compiletime:2.5.0
6+
-lib compiletime:2.5.1
77
-lib hxargs:3.0.0
88

99
--each
@@ -14,4 +14,4 @@
1414
--next
1515
-cp test
1616
-main TestMain
17-
--interp
17+
--interp

checkstyle/checks/PublicPrivateCheck.hx

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import haxe.macro.Expr;
99
class PublicPrivateCheck extends Check {
1010

1111
public var severity:String = "INFO";
12+
public var enforcePublicPrivate:Bool = false;
1213

1314
override function _actualRun() {
1415
for (td in _checker.ast.decls) {
@@ -30,16 +31,32 @@ class PublicPrivateCheck extends Check {
3031
}
3132

3233
function checkInterfaceField(f:Field) {
33-
if (f.access.indexOf(APublic) > -1) {
34-
_warnPublicKeyword(f.name, f.pos);
35-
return;
34+
if (enforcePublicPrivate) {
35+
if (f.access.indexOf(APublic) < 0) {
36+
_warnPublicKeywordMissing(f.name, f.pos);
37+
return;
38+
}
39+
}
40+
else {
41+
if (f.access.indexOf(APublic) > -1) {
42+
_warnPublicKeyword(f.name, f.pos);
43+
return;
44+
}
3645
}
3746
}
3847

3948
function checkField(f:Field) {
40-
if (f.access.indexOf(APrivate) > -1) {
41-
_warnPrivateKeyword(f.name, f.pos);
42-
return;
49+
if (enforcePublicPrivate) {
50+
if ((f.access.indexOf(APublic) < 0) && (f.access.indexOf(APrivate) < 0)) {
51+
_warnPrivateKeywordMissing(f.name, f.pos);
52+
return;
53+
}
54+
}
55+
else {
56+
if (f.access.indexOf(APrivate) > -1) {
57+
_warnPrivateKeyword(f.name, f.pos);
58+
return;
59+
}
4360
}
4461
}
4562

@@ -50,4 +67,12 @@ class PublicPrivateCheck extends Check {
5067
function _warnPublicKeyword(name:String, pos:Position) {
5168
logPos('No need of public keyword: ${name} (fields are by default public in interfaces)', pos, Reflect.field(SeverityLevel, severity));
5269
}
53-
}
70+
71+
function _warnPrivateKeywordMissing(name:String, pos:Position) {
72+
logPos('Missing private keyword: ${name}', pos, Reflect.field(SeverityLevel, severity));
73+
}
74+
75+
function _warnPublicKeywordMissing(name:String, pos:Position) {
76+
logPos('Missing public keyword: ${name}', pos, Reflect.field(SeverityLevel, severity));
77+
}
78+
}

checkstyle/checks/ReturnCheck.hx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class ReturnCheck extends Check {
1010

1111
public var severity:String = "INFO";
1212
public var allowEmptyReturn:Bool = true;
13+
public var enforceReturnType:Bool = false;
1314

1415
override function _actualRun() {
1516
for (td in _checker.ast.decls) {
@@ -28,8 +29,15 @@ class ReturnCheck extends Check {
2829
}
2930

3031
function checkField(f:Field) {
31-
if (Std.string(f.kind).indexOf("ret => TPath({ name => Void") > -1) {
32-
_warnVoid(f.name, f.pos);
32+
if (enforceReturnType) {
33+
if (Std.string(f.kind).indexOf("ret => null") > -1) {
34+
_warnReturnTypeMissing(f.name, f.pos);
35+
}
36+
}
37+
else {
38+
if (Std.string(f.kind).indexOf("ret => TPath({ name => Void") > -1) {
39+
_warnVoid(f.name, f.pos);
40+
}
3341
}
3442
if (allowEmptyReturn && Std.string(f.kind).indexOf("EReturn(null)") > -1) return;
3543
if (Std.string(f.kind).indexOf("expr => EReturn") > -1 && Std.string(f.kind).indexOf("ret => null") > -1) {
@@ -44,4 +52,8 @@ class ReturnCheck extends Check {
4452
function _warnNoReturnType(name:String, pos:Position) {
4553
logPos('Return type not specified when returning a value for function: ${name}', pos, Reflect.field(SeverityLevel, severity));
4654
}
47-
}
55+
56+
function _warnReturnTypeMissing(name:String, pos:Position) {
57+
logPos('Return type not specified for function: ${name}', pos, Reflect.field(SeverityLevel, severity));
58+
}
59+
}

run.n

901 Bytes
Binary file not shown.

test/PublicPrivateCheckTest.hx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class PublicPrivateCheckTest extends CheckTestCase {
77
public function testCorrectUsage() {
88
var msg = checkMessage(PublicPrivateTests.TEST, new PublicPrivateCheck());
99
assertEquals(msg, '');
10+
msg = checkMessage(PublicPrivateTests.TEST3, new PublicPrivateCheck());
11+
assertEquals(msg, '');
1012
}
1113

1214
public function testNormalClass() {
@@ -18,6 +20,34 @@ class PublicPrivateCheckTest extends CheckTestCase {
1820
var msg = checkMessage(PublicPrivateTests.TEST2, new PublicPrivateCheck());
1921
assertEquals(msg, 'No need of public keyword: a (fields are by default public in interfaces)');
2022
}
23+
24+
public function testClassWithEnforce() {
25+
var check = new PublicPrivateCheck ();
26+
check.enforcePublicPrivate = true;
27+
var msg = checkMessage(PublicPrivateTests.TEST1, check);
28+
assertEquals(msg, '');
29+
}
30+
31+
public function testClassWithEnforceMissing() {
32+
var check = new PublicPrivateCheck ();
33+
check.enforcePublicPrivate = true;
34+
var msg = checkMessage(PublicPrivateTests.TEST, check);
35+
assertEquals(msg, 'Missing private keyword: _onUpdate');
36+
}
37+
38+
public function testInterfaceWithEnforce() {
39+
var check = new PublicPrivateCheck ();
40+
check.enforcePublicPrivate = true;
41+
var msg = checkMessage(PublicPrivateTests.TEST2, check);
42+
assertEquals(msg, '');
43+
}
44+
45+
public function testInterfaceWithEnforceMissing() {
46+
var check = new PublicPrivateCheck ();
47+
check.enforcePublicPrivate = true;
48+
var msg = checkMessage(PublicPrivateTests.TEST3, check);
49+
assertEquals(msg, 'Missing public keyword: a');
50+
}
2151
}
2252

2353
class PublicPrivateTests {
@@ -42,4 +72,9 @@ class PublicPrivateTests {
4272
interface Test {
4373
public var a:Int;
4474
}";
45-
}
75+
76+
public static inline var TEST3:String = "
77+
interface Test {
78+
var a:Int;
79+
}";
80+
}

test/ReturnCheckTest.hx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ class ReturnCheckTest extends CheckTestCase {
1818
var msg = checkMessage(ReturnTests.TEST3, new ReturnCheck());
1919
assertEquals(msg, '');
2020
}
21+
22+
public function testEnforceReturnType() {
23+
var check = new ReturnCheck ();
24+
check.enforceReturnType = true;
25+
var msg = checkMessage(ReturnTests.TEST1, check);
26+
assertEquals(msg, '');
27+
}
28+
29+
public function testReturnTypeAllowEmptyReturn() {
30+
var check = new ReturnCheck ();
31+
check.allowEmptyReturn = false;
32+
var msg = checkMessage(ReturnTests.TEST2, check);
33+
assertEquals(msg, 'Return type not specified when returning a value for function: test1');
34+
35+
msg = checkMessage(ReturnTests.TEST3, check);
36+
assertEquals(msg, 'Return type not specified when returning a value for function: test2');
37+
}
2138
}
2239

2340
class ReturnTests {
@@ -39,4 +56,4 @@ class ReturnTests {
3956
return;
4057
}
4158
}";
42-
}
59+
}

0 commit comments

Comments
 (0)