11package checkstyle .checks .whitespace ;
22
3+ import Type .ValueType ;
34import checkstyle .utils .ExprUtils ;
45import haxe .macro .Expr ;
56import haxe .macro .Printer ;
@@ -12,9 +13,9 @@ class SpacingCheck extends Check {
1213
1314 public var spaceAroundBinop : Bool ;
1415 public var noSpaceAroundUnop : Bool ;
15- public var spaceIfCondition : Bool ;
16- public var spaceForLoop : Bool ;
17- public var spaceWhileLoop : Bool ;
16+ public var spaceIfCondition : Directive ;
17+ public var spaceForLoop : Directive ;
18+ public var spaceWhileLoop : Directive ;
1819 public var spaceSwitchCase : Bool ;
1920 public var spaceCatch : Bool ;
2021 public var ignoreRangeOperator : Bool ;
@@ -23,15 +24,26 @@ class SpacingCheck extends Check {
2324 super (AST );
2425 spaceAroundBinop = true ;
2526 noSpaceAroundUnop = true ;
26- spaceIfCondition = true ;
27- spaceForLoop = true ;
28- spaceWhileLoop = true ;
27+ spaceIfCondition = SHOULD ;
28+ spaceForLoop = SHOULD ;
29+ spaceWhileLoop = SHOULD ;
2930 spaceSwitchCase = true ;
3031 spaceCatch = true ;
3132 ignoreRangeOperator = true ;
3233 categories = [Category .STYLE , Category .CLARITY ];
3334 }
3435
36+ override public function configureProperty (name : String , value : Any ) {
37+ var currentValue = Reflect .field (this , name );
38+
39+ switch (Type .typeof (currentValue )) {
40+ case ValueType . TClass (String ):
41+ Reflect .setField (this , name , (value : Directive ));
42+ case _ :
43+ super .configureProperty (name , value );
44+ }
45+ }
46+
3547 override function actualRun () {
3648 var lastExpr = null ;
3749
@@ -50,12 +62,12 @@ class SpacingCheck extends Check {
5062 if (post ) dist = e .pos .max - e2 .pos .max ;
5163 else dist = e2 .pos .min - e .pos .min ;
5264 if (dist > unopSize (uo )) logPos (' Space around " ${unopString (uo )}"' , e .pos );
53- case EIf (econd , _ , _ ) if ( spaceIfCondition ) :
54- checkSpaceBetweenExpressions (" if" , e , econd );
55- case EFor (it , _ ) if ( spaceForLoop ) :
56- checkSpaceBetweenExpressions (" for" , e , it );
57- case EWhile (econd , _ , true ) if ( spaceWhileLoop ) :
58- checkSpaceBetweenExpressions (" while" , e , econd );
65+ case EIf (econd , _ , _ ):
66+ checkSpaceBetweenExpressions (" if" , e , econd , spaceIfCondition );
67+ case EFor (it , _ ):
68+ checkSpaceBetweenExpressions (" for" , e , it , spaceForLoop );
69+ case EWhile (econd , _ , true ):
70+ checkSpaceBetweenExpressions (" while" , e , econd , spaceWhileLoop );
5971 case ESwitch (eswitch , _ , _ ) if (spaceSwitchCase ):
6072 checkSpaceBetweenManually (" switch" , lastExpr , eswitch );
6173 case ETry (etry , catches ) if (spaceCatch ):
@@ -87,9 +99,17 @@ class SpacingCheck extends Check {
8799 return (new Printer ()).printUnop (uo );
88100 }
89101
90- function checkSpaceBetweenExpressions (name : String , e1 : Expr , e2 : Expr ) {
91- if (e2 .pos .min - e1 .pos .min < ' $name (' .length ) {
92- logRange (' No space between " $name " and "("' , e1 .pos .max , e2 .pos .min );
102+ function checkSpaceBetweenExpressions (name : String , e1 : Expr , e2 : Expr , directive : Directive = SHOULD ) {
103+ switch (directive ) {
104+ case ANY :
105+ case SHOULD_NOT :
106+ if (e2 .pos .min - e1 .pos .min > ' $name (' .length ) {
107+ logRange (' Space between " $name " and "("' , e1 .pos .max , e2 .pos .min );
108+ }
109+ case SHOULD :
110+ if (e2 .pos .min - e1 .pos .min < ' $name (' .length ) {
111+ logRange (' No space between " $name " and "("' , e1 .pos .max , e2 .pos .min );
112+ }
93113 }
94114 }
95115
0 commit comments