Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PublicAccessorCheck, closes #243 #322

Merged
merged 1 commit into from
Jan 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {}
}";
}