Skip to content

Commit

Permalink
made Directive a real enum to avoid issues with other string settings…
Browse files Browse the repository at this point in the history
… in config
  • Loading branch information
RealyUniqueName committed Nov 3, 2016
1 parent 65d51a4 commit 736d288
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
3 changes: 3 additions & 0 deletions checkstyle.json
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@
"checkstyle.Main",
"checkstyle.Checker",
"checkstyle.ChecksInfo",
"checkstyle.checks.Check",
"checkstyle.checks.Directive",
"checkstyle.checks.whitespace.SpacingCheck",
"checkstyle.checks.imports.UnusedImportCheck",
"TestMain",
"misc.ExtensionsTest",
Expand Down
10 changes: 5 additions & 5 deletions resources/default-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,13 @@
},
{
"props": {
"spaceIfCondition": "should",
"spaceIfCondition": 0,
"spaceAroundBinop": true,
"spaceForLoop": "should",
"spaceForLoop": 0,
"ignoreRangeOperator": true,
"spaceWhileLoop": "should",
"spaceCatch": "should",
"spaceSwitchCase": "should",
"spaceWhileLoop": 0,
"spaceCatch": 0,
"spaceSwitchCase": 0,
"noSpaceAroundUnop": true
},
"type": "Spacing"
Expand Down
2 changes: 1 addition & 1 deletion src/checkstyle/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class Main {
catch (e:Dynamic) {
var message = 'Failed to configure $prop setting for ${check.getModuleName()}: ';
message += (Std.is(e, Error) ? (e:Error).message : Std.string(message));
failWith(e.message);
failWith(message);
}
}
if (defaultSeverity != null && !props.contains("severity")) check.severity = defaultSeverity;
Expand Down
2 changes: 1 addition & 1 deletion src/checkstyle/checks/Check.hx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Check {
desc = haxe.rtti.Meta.getType(Type.getClass(this)).desc[0];
}

public function configureProperty(name:String, value:Any) {
public function configureProperty(name:String, value:Dynamic) {
Reflect.setField(this, name, value);
}

Expand Down
31 changes: 15 additions & 16 deletions src/checkstyle/checks/Directive.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ package checkstyle.checks;
import Type.ValueType;
import checkstyle.errors.InvalidDirectiveError;

@:enum
abstract Directive(String) to String {
var SHOULD = "should";
var SHOULD_NOT = "shouldNot";
var ANY = "any";
enum Directive {
SHOULD;
SHOULD_NOT;
ANY;
}

@:from
public static function fromAny(value:Any):Directive {
class DirectiveTools {
static var MAPPING:Map<String, Directive> = [
"should" => SHOULD,
"shouldNot" => SHOULD_NOT,
"any" => ANY
];

public static function fromDynamic(value:Dynamic):Directive {
return switch (Type.typeof(value)) {
case ValueType.TClass(String): getValidated(value);
//support for legacy configs when such settings were boolean
Expand All @@ -19,16 +25,9 @@ abstract Directive(String) to String {
}
}

@:from
static inline function fromString(value:String):Directive {
return getValidated(value);
}

static function getValidated(value:String):Directive {
switch (value:Directive) {
case SHOULD, SHOULD_NOT, ANY:
return value;
}
var directive = MAPPING.get(value);
if (directive != null) return directive;
throw new InvalidDirectiveError('Invalid directive: $value');
}
}
7 changes: 4 additions & 3 deletions src/checkstyle/checks/whitespace/SpacingCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import haxe.macro.Expr;
import haxe.macro.Printer;
import haxe.macro.Expr.Binop;
import haxe.macro.Expr.Unop;
import checkstyle.checks.Directive;

@name("Spacing")
@desc("Spacing check on if, for, while, switch, try statements and around operators.")
Expand Down Expand Up @@ -33,12 +34,12 @@ class SpacingCheck extends Check {
categories = [Category.STYLE, Category.CLARITY];
}

override public function configureProperty(name:String, value:Any) {
override public function configureProperty(name:String, value:Dynamic) {
var currentValue = Reflect.field(this, name);

switch (Type.typeof(currentValue)) {
case ValueType.TClass(String):
Reflect.setField(this, name, (value:Directive));
case ValueType.TEnum(Directive):
Reflect.setField(this, name, DirectiveTools.fromDynamic(value));
case _:
super.configureProperty(name, value);
}
Expand Down

0 comments on commit 736d288

Please sign in to comment.