@@ -29,7 +29,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
29
29
#endif
30
30
public class UseWhitespace : ConfigurableRule
31
31
{
32
- private enum ErrorKind { Brace , Paren , Operator } ;
32
+ private enum ErrorKind { Brace , Paren , Operator , SeparatorComma , SeparatorSemi } ;
33
33
private const int whiteSpaceSize = 1 ;
34
34
private const string whiteSpace = " " ;
35
35
@@ -45,6 +45,9 @@ private List<Func<TokenOperations, IEnumerable<DiagnosticRecord>>> violationFind
45
45
[ ConfigurableRuleProperty ( defaultValue : true ) ]
46
46
public bool CheckOperator { get ; protected set ; }
47
47
48
+ [ ConfigurableRuleProperty ( defaultValue : true ) ]
49
+ public bool CheckSeparator { get ; protected set ; }
50
+
48
51
public override void ConfigureRule ( IDictionary < string , object > paramValueMap )
49
52
{
50
53
base . ConfigureRule ( paramValueMap ) ;
@@ -62,6 +65,11 @@ public override void ConfigureRule(IDictionary<string, object> paramValueMap)
62
65
{
63
66
violationFinders . Add ( FindOperatorViolations ) ;
64
67
}
68
+
69
+ if ( CheckSeparator )
70
+ {
71
+ violationFinders . Add ( FindSeparatorViolations ) ;
72
+ }
65
73
}
66
74
67
75
/// <summary>
@@ -95,6 +103,10 @@ private string GetError(ErrorKind kind)
95
103
return string . Format ( CultureInfo . CurrentCulture , Strings . UseWhitespaceErrorBeforeBrace ) ;
96
104
case ErrorKind . Operator :
97
105
return string . Format ( CultureInfo . CurrentCulture , Strings . UseWhitespaceErrorOperator ) ;
106
+ case ErrorKind . SeparatorComma :
107
+ return string . Format ( CultureInfo . CurrentCulture , Strings . UseWhitespaceErrorSeparatorComma ) ;
108
+ case ErrorKind . SeparatorSemi :
109
+ return string . Format ( CultureInfo . CurrentCulture , Strings . UseWhitespaceErrorSeparatorSemi ) ;
98
110
default :
99
111
return string . Format ( CultureInfo . CurrentCulture , Strings . UseWhitespaceErrorBeforeParen ) ;
100
112
}
@@ -147,6 +159,45 @@ private IEnumerable<DiagnosticRecord> FindOpenParenViolations(TokenOperations to
147
159
}
148
160
}
149
161
162
+ private IEnumerable < DiagnosticRecord > FindSeparatorViolations ( TokenOperations tokenOperations )
163
+ {
164
+ Func < LinkedListNode < Token > , bool > predicate = node =>
165
+ {
166
+ return node . Next != null
167
+ && ! IsPreviousTokenApartByWhitespace ( node . Next ) ;
168
+ } ;
169
+
170
+ Func < Token , ErrorKind , DiagnosticRecord > getDiagnosticRecord = ( token , errKind ) =>
171
+ {
172
+ return new DiagnosticRecord (
173
+ GetError ( errKind ) ,
174
+ token . Extent ,
175
+ GetName ( ) ,
176
+ GetDiagnosticSeverity ( ) ,
177
+ token . Extent . File ,
178
+ null ,
179
+ null ) ;
180
+ } ;
181
+
182
+ foreach ( var tokenNode in tokenOperations . GetTokenNodes ( TokenKind . Comma ) . Where ( predicate ) )
183
+ {
184
+ yield return getDiagnosticRecord ( tokenNode . Value , ErrorKind . SeparatorComma ) ;
185
+ }
186
+
187
+ foreach ( var tokenNode in tokenOperations . GetTokenNodes ( TokenKind . Semi ) . Where ( predicate ) )
188
+ {
189
+ // semi-colon can be followed by newline or end of input
190
+ if ( tokenNode . Next . Value . Kind == TokenKind . EndOfInput
191
+ || tokenNode . Next . Value . Kind == TokenKind . NewLine )
192
+ {
193
+ continue ;
194
+ }
195
+
196
+ yield return getDiagnosticRecord ( tokenNode . Value , ErrorKind . SeparatorSemi ) ;
197
+ }
198
+ }
199
+
200
+
150
201
private bool IsKeyword ( Token token )
151
202
{
152
203
switch ( token . Kind )
0 commit comments