From 2558d9d3752632cf9ab2933103ff8b4e65cdd32f Mon Sep 17 00:00:00 2001 From: Gama11 Date: Mon, 30 Jan 2017 22:44:34 +0100 Subject: [PATCH] Add PublicAccessorCheck, closes #243 --- resources/default-config.json | 4 ++ .../checks/modifier/PublicAccessorCheck.hx | 33 ++++++++++ .../modifier/PublicAccessorCheckTest.hx | 60 +++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 src/checkstyle/checks/modifier/PublicAccessorCheck.hx create mode 100644 test/checks/modifier/PublicAccessorCheckTest.hx diff --git a/resources/default-config.json b/resources/default-config.json index fa16f869..8ccd5487 100644 --- a/resources/default-config.json +++ b/resources/default-config.json @@ -378,6 +378,10 @@ }, "type": "ParameterNumber" }, + { + "props": {}, + "type": "PublicAccessor" + }, { "props": { "enforcePrivate": false, diff --git a/src/checkstyle/checks/modifier/PublicAccessorCheck.hx b/src/checkstyle/checks/modifier/PublicAccessorCheck.hx new file mode 100644 index 00000000..40f7592b --- /dev/null +++ b/src/checkstyle/checks/modifier/PublicAccessorCheck.hx @@ -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); + } +} \ No newline at end of file diff --git a/test/checks/modifier/PublicAccessorCheckTest.hx b/test/checks/modifier/PublicAccessorCheckTest.hx new file mode 100644 index 00000000..6064a517 --- /dev/null +++ b/test/checks/modifier/PublicAccessorCheckTest.hx @@ -0,0 +1,60 @@ +package checks.modifier; + +import checkstyle.checks.modifier.PublicAccessorCheck; + +class PublicAccessorCheckTest extends CheckTestCase { + + 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() {} + }"; +} \ No newline at end of file