Skip to content

Commit 8c714a8

Browse files
authored
Add PublicAccessorCheck, closes #243 (#322)
1 parent f348165 commit 8c714a8

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

resources/default-config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@
378378
},
379379
"type": "ParameterNumber"
380380
},
381+
{
382+
"props": {},
383+
"type": "PublicAccessor"
384+
},
381385
{
382386
"props": {
383387
"enforcePrivate": false,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package checkstyle.checks.modifier;
2+
3+
import haxe.macro.Expr;
4+
5+
using checkstyle.utils.ArrayUtils;
6+
using checkstyle.utils.FieldUtils;
7+
using StringTools;
8+
9+
@name("PublicAccessor")
10+
@desc("Checks for public accessors.")
11+
class PublicAccessorCheck extends Check {
12+
13+
public function new() {
14+
super(AST);
15+
categories = [Category.STYLE, Category.CLARITY];
16+
points = 1;
17+
}
18+
19+
override function actualRun() {
20+
forEachField(checkField);
21+
}
22+
23+
function checkField(f:Field, p:ParentType) {
24+
if (!f.kind.match(FFun(_))) return;
25+
if (!f.name.startsWith("set_") && !f.name.startsWith("get_")) return;
26+
27+
var isDefaultPrivate = f.isDefaultPrivate(p);
28+
if (isDefaultPrivate && !f.access.contains(APublic)) return;
29+
else if (!isDefaultPrivate && f.access.contains(APrivate)) return;
30+
31+
logPos("Accessor method should not be public", f.pos);
32+
}
33+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package checks.modifier;
2+
3+
import checkstyle.checks.modifier.PublicAccessorCheck;
4+
5+
class PublicAccessorCheckTest extends CheckTestCase<PublicAccessorCheckTests> {
6+
7+
static inline var ERROR:String = "Accessor method should not be public";
8+
9+
public function testNonAccessors() {
10+
assertNoMsg(new PublicAccessorCheck(), NON_ACCESSOR);
11+
}
12+
13+
public function testPublicAccessors() {
14+
assertMsg(new PublicAccessorCheck(), PUBLIC_GETTER, ERROR);
15+
assertMsg(new PublicAccessorCheck(), PUBLIC_SETTER, ERROR);
16+
assertMsg(new PublicAccessorCheck(), IMPLICITLY_PUBLIC_GETTER, ERROR);
17+
assertMsg(new PublicAccessorCheck(), IMPLICITLY_PUBLIC_SETTER, ERROR);
18+
assertMsg(new PublicAccessorCheck(), INTERFACE_PUBLIC_GETTER, ERROR);
19+
assertMsg(new PublicAccessorCheck(), INTERFACE_PUBLIC_SETTER, ERROR);
20+
}
21+
}
22+
23+
@:enum
24+
abstract PublicAccessorCheckTests(String) to String {
25+
var NON_ACCESSOR = "
26+
abstractAndClass Test {
27+
public function _set_test() {}
28+
public function _get_test() {}
29+
}";
30+
31+
var PUBLIC_GETTER = "
32+
abstractAndClass Test {
33+
public function get_test() {}
34+
}";
35+
36+
var PUBLIC_SETTER = "
37+
abstractAndClass Test {
38+
override inline public function set_test() {}
39+
}";
40+
41+
var IMPLICITLY_PUBLIC_GETTER = "
42+
@:publicFields class Test {
43+
function get_test() {}
44+
}";
45+
46+
var IMPLICITLY_PUBLIC_SETTER = "
47+
@:publicFields class Test {
48+
function set_test() {}
49+
}";
50+
51+
var INTERFACE_PUBLIC_GETTER = "
52+
interface ITest {
53+
function get_test() {}
54+
}";
55+
56+
var INTERFACE_PUBLIC_SETTER = "
57+
interface ITest {
58+
function set_test() {}
59+
}";
60+
}

0 commit comments

Comments
 (0)