@@ -151,49 +151,25 @@ public void Initialize(
151
151
profile ) ;
152
152
}
153
153
154
- private void Initialize (
155
- IOutputWriter outputWriter ,
156
- PathIntrinsics path ,
157
- CommandInvocationIntrinsics invokeCommand ,
158
- string [ ] customizedRulePath ,
159
- string [ ] includeRuleNames ,
160
- string [ ] excludeRuleNames ,
161
- string [ ] severity ,
162
- bool suppressedOnly = false ,
163
- string profile = null )
154
+ internal bool ParseProfile ( string profile , PathIntrinsics path , IOutputWriter writer )
164
155
{
165
- if ( outputWriter == null )
166
- {
167
- throw new ArgumentNullException ( "outputWriter" ) ;
168
- }
169
-
170
- this . outputWriter = outputWriter ;
171
-
172
- #region Verifies rule extensions and loggers path
173
-
174
- List < string > paths = this . GetValidCustomRulePaths ( customizedRulePath , path ) ;
156
+ IEnumerable < string > includeRuleList = new List < string > ( ) ;
157
+ IEnumerable < string > excludeRuleList = new List < string > ( ) ;
158
+ IEnumerable < string > severityList = new List < string > ( ) ;
175
159
176
- #endregion
177
-
178
- #region Initializes Rules
179
-
180
- this . severity = severity ;
181
- this . suppressedOnly = suppressedOnly ;
182
- this . includeRule = includeRuleNames ;
183
- this . excludeRule = excludeRuleNames ;
184
- this . includeRegexList = new List < Regex > ( ) ;
185
- this . excludeRegexList = new List < Regex > ( ) ;
160
+ bool hasError = false ;
186
161
187
- #region ParseProfile
188
162
if ( ! String . IsNullOrWhiteSpace ( profile ) )
189
163
{
190
164
try
191
- {
165
+ {
192
166
profile = path . GetResolvedPSPathFromPSPath ( profile ) . First ( ) . Path ;
193
167
}
194
168
catch
195
169
{
196
- this . outputWriter . WriteWarning ( string . Format ( CultureInfo . CurrentCulture , Strings . FileNotFound , profile ) ) ;
170
+ writer . WriteError ( new ErrorRecord ( new FileNotFoundException ( string . Format ( CultureInfo . CurrentCulture , Strings . FileNotFound , profile ) ) ,
171
+ Strings . ConfigurationFileNotFound , ErrorCategory . ResourceUnavailable , profile ) ) ;
172
+ hasError = true ;
197
173
}
198
174
199
175
if ( File . Exists ( profile ) )
@@ -206,7 +182,9 @@ private void Initialize(
206
182
// no hashtable, raise warning
207
183
if ( hashTableAsts . Count ( ) == 0 )
208
184
{
209
- this . outputWriter . WriteWarning ( string . Format ( CultureInfo . CurrentCulture , Strings . InvalidProfile , profile ) ) ;
185
+ writer . WriteError ( new ErrorRecord ( new ArgumentException ( string . Format ( CultureInfo . CurrentCulture , Strings . InvalidProfile , profile ) ) ,
186
+ Strings . ConfigurationFileHasNoHashTable , ErrorCategory . ResourceUnavailable , profile ) ) ;
187
+ hasError = true ;
210
188
}
211
189
else
212
190
{
@@ -217,8 +195,9 @@ private void Initialize(
217
195
if ( ! ( kvp . Item1 is StringConstantExpressionAst ) )
218
196
{
219
197
// first item (the key) should be a string
220
- this . outputWriter . WriteWarning (
221
- string . Format ( CultureInfo . CurrentCulture , Strings . WrongKeyFormat , kvp . Item1 . Extent . StartLineNumber , kvp . Item1 . Extent . StartColumnNumber , profile ) ) ;
198
+ writer . WriteError ( new ErrorRecord ( new InvalidDataException ( string . Format ( CultureInfo . CurrentCulture , Strings . WrongKeyFormat , kvp . Item1 . Extent . StartLineNumber , kvp . Item1 . Extent . StartColumnNumber , profile ) ) ,
199
+ Strings . ConfigurationKeyNotAString , ErrorCategory . InvalidData , profile ) ) ;
200
+ hasError = true ;
222
201
continue ;
223
202
}
224
203
@@ -241,10 +220,10 @@ private void Initialize(
241
220
// Statements property is never null
242
221
if ( arrayExp . SubExpression != null )
243
222
{
244
- StatementAst stateAst = arrayExp . SubExpression . Statements . First ( ) ;
223
+ StatementAst stateAst = arrayExp . SubExpression . Statements . FirstOrDefault ( ) ;
245
224
if ( stateAst != null && stateAst is PipelineAst )
246
225
{
247
- CommandBaseAst cmdBaseAst = ( stateAst as PipelineAst ) . PipelineElements . First ( ) ;
226
+ CommandBaseAst cmdBaseAst = ( stateAst as PipelineAst ) . PipelineElements . FirstOrDefault ( ) ;
248
227
if ( cmdBaseAst != null && cmdBaseAst is CommandExpressionAst )
249
228
{
250
229
CommandExpressionAst cmdExpAst = cmdBaseAst as CommandExpressionAst ;
@@ -268,8 +247,9 @@ private void Initialize(
268
247
// all the values in the array needs to be string
269
248
if ( ! ( element is StringConstantExpressionAst ) )
270
249
{
271
- this . outputWriter . WriteWarning (
272
- string . Format ( CultureInfo . CurrentCulture , Strings . WrongValueFormat , element . Extent . StartLineNumber , element . Extent . StartColumnNumber , profile ) ) ;
250
+ writer . WriteError ( new ErrorRecord ( new InvalidDataException ( string . Format ( CultureInfo . CurrentCulture , Strings . WrongValueFormat , element . Extent . StartLineNumber , element . Extent . StartColumnNumber , profile ) ) ,
251
+ Strings . ConfigurationValueNotAString , ErrorCategory . InvalidData , profile ) ) ;
252
+ hasError = true ;
273
253
continue ;
274
254
}
275
255
@@ -281,55 +261,82 @@ private void Initialize(
281
261
282
262
if ( rhsList . Count == 0 )
283
263
{
284
- this . outputWriter . WriteWarning (
285
- string . Format ( CultureInfo . CurrentCulture , Strings . WrongValueFormat , kvp . Item2 . Extent . StartLineNumber , kvp . Item2 . Extent . StartColumnNumber , profile ) ) ;
286
- break ;
264
+ writer . WriteError ( new ErrorRecord ( new InvalidDataException ( string . Format ( CultureInfo . CurrentCulture , Strings . WrongValueFormat , kvp . Item2 . Extent . StartLineNumber , kvp . Item2 . Extent . StartColumnNumber , profile ) ) ,
265
+ Strings . ConfigurationValueWrongFormat , ErrorCategory . InvalidData , profile ) ) ;
266
+ hasError = true ;
267
+ continue ;
287
268
}
288
269
289
- switch ( ( kvp . Item1 as StringConstantExpressionAst ) . Value . ToLower ( ) )
270
+ string key = ( kvp . Item1 as StringConstantExpressionAst ) . Value . ToLower ( ) ;
271
+
272
+ switch ( key )
290
273
{
291
274
case "severity" :
292
- if ( this . severity == null )
293
- {
294
- this . severity = rhsList . ToArray ( ) ;
295
- }
296
- else
297
- {
298
- this . severity = this . severity . Union ( rhsList ) . ToArray ( ) ;
299
- }
275
+ severityList = severityList . Union ( rhsList ) ;
300
276
break ;
301
277
case "includerules" :
302
- if ( this . includeRule == null )
303
- {
304
- this . includeRule = rhsList . ToArray ( ) ;
305
- }
306
- else
307
- {
308
- this . includeRule = this . includeRule . Union ( rhsList ) . ToArray ( ) ;
309
- }
278
+ includeRuleList = includeRuleList . Union ( rhsList ) ;
310
279
break ;
311
280
case "excluderules" :
312
- if ( this . excludeRule == null )
313
- {
314
- this . excludeRule = rhsList . ToArray ( ) ;
315
- }
316
- else
317
- {
318
- this . excludeRule = this . excludeRule . Union ( rhsList ) . ToArray ( ) ;
319
- }
281
+ excludeRuleList = excludeRuleList . Union ( rhsList ) ;
320
282
break ;
321
283
default :
322
- this . outputWriter . WriteWarning (
323
- string . Format ( CultureInfo . CurrentCulture , Strings . WrongKey , kvp . Item1 . Extent . StartLineNumber , kvp . Item1 . Extent . StartColumnNumber , profile ) ) ;
284
+ writer . WriteError ( new ErrorRecord (
285
+ new InvalidDataException ( string . Format ( CultureInfo . CurrentCulture , Strings . WrongKey , key , kvp . Item1 . Extent . StartLineNumber , kvp . Item1 . Extent . StartColumnNumber , profile ) ) ,
286
+ Strings . WrongConfigurationKey , ErrorCategory . InvalidData , profile ) ) ;
287
+ hasError = true ;
324
288
break ;
325
289
}
326
290
}
327
291
}
328
292
}
329
293
}
330
294
295
+ if ( hasError )
296
+ {
297
+ return false ;
298
+ }
299
+
300
+ this . severity = ( severityList . Count ( ) == 0 ) ? null : severityList . ToArray ( ) ;
301
+ this . includeRule = ( includeRuleList . Count ( ) == 0 ) ? null : includeRuleList . ToArray ( ) ;
302
+ this . excludeRule = ( excludeRuleList . Count ( ) == 0 ) ? null : excludeRuleList . ToArray ( ) ;
303
+
304
+ return true ;
305
+ }
306
+
307
+ private void Initialize (
308
+ IOutputWriter outputWriter ,
309
+ PathIntrinsics path ,
310
+ CommandInvocationIntrinsics invokeCommand ,
311
+ string [ ] customizedRulePath ,
312
+ string [ ] includeRuleNames ,
313
+ string [ ] excludeRuleNames ,
314
+ string [ ] severity ,
315
+ bool suppressedOnly = false ,
316
+ string profile = null )
317
+ {
318
+ if ( outputWriter == null )
319
+ {
320
+ throw new ArgumentNullException ( "outputWriter" ) ;
321
+ }
322
+
323
+ this . outputWriter = outputWriter ;
324
+
325
+ #region Verifies rule extensions and loggers path
326
+
327
+ List < string > paths = this . GetValidCustomRulePaths ( customizedRulePath , path ) ;
328
+
331
329
#endregion
332
330
331
+ #region Initializes Rules
332
+
333
+ this . suppressedOnly = suppressedOnly ;
334
+ this . severity = this . severity == null ? severity : this . severity . Union ( severity ?? new String [ 0 ] ) . ToArray ( ) ;
335
+ this . includeRule = this . includeRule == null ? includeRuleNames : this . includeRule . Union ( includeRuleNames ?? new String [ 0 ] ) . ToArray ( ) ;
336
+ this . excludeRule = this . excludeRule == null ? excludeRuleNames : this . excludeRule . Union ( excludeRuleNames ?? new String [ 0 ] ) . ToArray ( ) ;
337
+ this . includeRegexList = new List < Regex > ( ) ;
338
+ this . excludeRegexList = new List < Regex > ( ) ;
339
+
333
340
//Check wild card input for the Include/ExcludeRules and create regex match patterns
334
341
if ( this . includeRule != null )
335
342
{
@@ -339,6 +346,7 @@ private void Initialize(
339
346
this . includeRegexList . Add ( includeRegex ) ;
340
347
}
341
348
}
349
+
342
350
if ( this . excludeRule != null )
343
351
{
344
352
foreach ( string rule in excludeRule )
@@ -1446,7 +1454,10 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1446
1454
if ( severity != null )
1447
1455
{
1448
1456
var diagSeverity = severity . Select ( item => Enum . Parse ( typeof ( DiagnosticSeverity ) , item , true ) ) ;
1449
- diagnosticsList = diagnostics . Where ( item => diagSeverity . Contains ( item . Severity ) ) ;
1457
+ if ( diagSeverity . Count ( ) != 0 )
1458
+ {
1459
+ diagnosticsList = diagnostics . Where ( item => diagSeverity . Contains ( item . Severity ) ) ;
1460
+ }
1450
1461
}
1451
1462
1452
1463
return this . suppressedOnly ?
0 commit comments