Skip to content

Commit

Permalink
Add PublicAccessorCheck, closes #243 (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gama11 authored Jan 30, 2017
1 parent f348165 commit 8c714a8
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
4 changes: 4 additions & 0 deletions resources/default-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@
},
"type": "ParameterNumber"
},
{
"props": {},
"type": "PublicAccessor"
},
{
"props": {
"enforcePrivate": false,
Expand Down
33 changes: 33 additions & 0 deletions src/checkstyle/checks/modifier/PublicAccessorCheck.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package checkstyle.checks.modifier;

import haxe.macro.Expr;

using checkstyle.utils.ArrayUtils;
using checkstyle.utils.FieldUtils;
using StringTools;

@name("PublicAccessor")
@desc("Checks for public accessors.")
class PublicAccessorCheck extends Check {

public function new() {
super(AST);
categories = [Category.STYLE, Category.CLARITY];
points = 1;
}

override function actualRun() {
forEachField(checkField);
}

function checkField(f:Field, p:ParentType) {
if (!f.kind.match(FFun(_))) return;
if (!f.name.startsWith("set_") && !f.name.startsWith("get_")) return;

var isDefaultPrivate = f.isDefaultPrivate(p);
if (isDefaultPrivate && !f.access.contains(APublic)) return;
else if (!isDefaultPrivate && f.access.contains(APrivate)) return;

logPos("Accessor method should not be public", f.pos);
}
}
60 changes: 60 additions & 0 deletions test/checks/modifier/PublicAccessorCheckTest.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package checks.modifier;

import checkstyle.checks.modifier.PublicAccessorCheck;

class PublicAccessorCheckTest extends CheckTestCase<PublicAccessorCheckTests> {

static inline var ERROR:String = "Accessor method should not be public";

public function testNonAccessors() {
assertNoMsg(new PublicAccessorCheck(), NON_ACCESSOR);
}

public function testPublicAccessors() {
assertMsg(new PublicAccessorCheck(), PUBLIC_GETTER, ERROR);
assertMsg(new PublicAccessorCheck(), PUBLIC_SETTER, ERROR);
assertMsg(new PublicAccessorCheck(), IMPLICITLY_PUBLIC_GETTER, ERROR);
assertMsg(new PublicAccessorCheck(), IMPLICITLY_PUBLIC_SETTER, ERROR);
assertMsg(new PublicAccessorCheck(), INTERFACE_PUBLIC_GETTER, ERROR);
assertMsg(new PublicAccessorCheck(), INTERFACE_PUBLIC_SETTER, ERROR);
}
}

@:enum
abstract PublicAccessorCheckTests(String) to String {
var NON_ACCESSOR = "
abstractAndClass Test {
public function _set_test() {}
public function _get_test() {}
}";

var PUBLIC_GETTER = "
abstractAndClass Test {
public function get_test() {}
}";

var PUBLIC_SETTER = "
abstractAndClass Test {
override inline public function set_test() {}
}";

var IMPLICITLY_PUBLIC_GETTER = "
@:publicFields class Test {
function get_test() {}
}";

var IMPLICITLY_PUBLIC_SETTER = "
@:publicFields class Test {
function set_test() {}
}";

var INTERFACE_PUBLIC_GETTER = "
interface ITest {
function get_test() {}
}";

var INTERFACE_PUBLIC_SETTER = "
interface ITest {
function set_test() {}
}";
}

0 comments on commit 8c714a8

Please sign in to comment.