-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
}"; | ||
} |