11package checkstyle .checks .whitespace ;
22
3+ import Type .ValueType ;
34import checkstyle .utils .ExprUtils ;
45import haxe .macro .Expr ;
56import haxe .macro .Printer ;
67import haxe .macro .Expr .Binop ;
78import haxe .macro .Expr .Unop ;
9+ import checkstyle .checks .Directive ;
810
911@name (" Spacing" )
1012@desc (" Spacing check on if, for, while, switch, try statements and around operators." )
1113class SpacingCheck extends Check {
1214
1315 public var spaceAroundBinop : Bool ;
1416 public var noSpaceAroundUnop : Bool ;
15- public var spaceIfCondition : Bool ;
16- public var spaceForLoop : Bool ;
17- public var spaceWhileLoop : Bool ;
18- public var spaceSwitchCase : Bool ;
19- public var spaceCatch : Bool ;
17+ public var spaceIfCondition : Directive ;
18+ public var spaceForLoop : Directive ;
19+ public var spaceWhileLoop : Directive ;
20+ public var spaceSwitchCase : Directive ;
21+ public var spaceCatch : Directive ;
2022 public var ignoreRangeOperator : Bool ;
2123
2224 public function new () {
2325 super (AST );
2426 spaceAroundBinop = true ;
2527 noSpaceAroundUnop = true ;
26- spaceIfCondition = true ;
27- spaceForLoop = true ;
28- spaceWhileLoop = true ;
29- spaceSwitchCase = true ;
30- spaceCatch = true ;
28+ spaceIfCondition = SHOULD ;
29+ spaceForLoop = SHOULD ;
30+ spaceWhileLoop = SHOULD ;
31+ spaceSwitchCase = SHOULD ;
32+ spaceCatch = SHOULD ;
3133 ignoreRangeOperator = true ;
3234 categories = [Category .STYLE , Category .CLARITY ];
3335 }
3436
37+ override public function configureProperty (name : String , value : Dynamic ) {
38+ var currentValue = Reflect .field (this , name );
39+
40+ switch (Type .typeof (currentValue )) {
41+ case ValueType . TEnum (Directive ):
42+ Reflect .setField (this , name , DirectiveTools .fromDynamic (value ));
43+ case _ :
44+ super .configureProperty (name , value );
45+ }
46+ }
47+
3548 override function actualRun () {
3649 var lastExpr = null ;
3750
@@ -50,18 +63,18 @@ class SpacingCheck extends Check {
5063 if (post ) dist = e .pos .max - e2 .pos .max ;
5164 else dist = e2 .pos .min - e .pos .min ;
5265 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 );
59- case ESwitch (eswitch , _ , _ ) if ( spaceSwitchCase ) :
60- checkSpaceBetweenManually (" switch" , lastExpr , eswitch );
61- case ETry (etry , catches ) if ( spaceCatch ) :
66+ case EIf (econd , _ , _ ):
67+ checkSpaceBetweenExpressions (" if" , e , econd , spaceIfCondition );
68+ case EFor (it , _ ):
69+ checkSpaceBetweenExpressions (" for" , e , it , spaceForLoop );
70+ case EWhile (econd , _ , true ):
71+ checkSpaceBetweenExpressions (" while" , e , econd , spaceWhileLoop );
72+ case ESwitch (eswitch , _ , _ ):
73+ checkSpaceBetweenManually (" switch" , lastExpr , eswitch , spaceSwitchCase );
74+ case ETry (etry , catches ):
6275 var exprBeforeCatch = lastExpr ;
6376 for (ctch in catches ) {
64- checkSpaceBetweenManually (" catch" , exprBeforeCatch , ctch .expr );
77+ checkSpaceBetweenManually (" catch" , exprBeforeCatch , ctch .expr , spaceCatch );
6578 exprBeforeCatch = ctch .expr ;
6679 }
6780 default :
@@ -87,18 +100,35 @@ class SpacingCheck extends Check {
87100 return (new Printer ()).printUnop (uo );
88101 }
89102
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 );
103+ function checkSpaceBetweenExpressions (name : String , e1 : Expr , e2 : Expr , directive : Directive ) {
104+ switch (directive ) {
105+ case ANY :
106+ case SHOULD_NOT :
107+ if (e2 .pos .min - e1 .pos .min > ' $name (' .length ) {
108+ logRange (' Space between " $name " and "("' , e2 .pos .max , e2 .pos .min );
109+ }
110+ case SHOULD :
111+ if (e2 .pos .min - e1 .pos .min < ' $name (' .length ) {
112+ logRange (' No space between " $name " and "("' , e1 .pos .max , e2 .pos .min );
113+ }
93114 }
94115 }
95116
96- function checkSpaceBetweenManually (name : String , before : Expr , check : Expr ) {
117+ function checkSpaceBetweenManually (name : String , before : Expr , check : Expr , directive : Directive ) {
97118 var prevExprUntilChecked = checker .file .content .substring (before .pos .min , check .pos .min + 1 );
98119 var checkPos = prevExprUntilChecked .lastIndexOf (' $name (' );
99- if (checkPos > - 1 ) {
100- var fileCheckPos = before .pos .min + checkPos ;
101- logRange (' No space between " $name " and "("' , fileCheckPos , fileCheckPos + ' $name (' .length );
120+ var fileCheckPos = before .pos .min + checkPos ;
121+
122+ switch (directive ) {
123+ case ANY :
124+ case SHOULD_NOT :
125+ if (checkPos < 0 ) {
126+ logRange (' Space between " $name " and "("' , check .pos .min , check .pos .max );
127+ }
128+ case SHOULD :
129+ if (checkPos > - 1 ) {
130+ logRange (' No space between " $name " and "("' , fileCheckPos , fileCheckPos + ' $name (' .length );
131+ }
102132 }
103133 }
104134}
0 commit comments