28
28
using System . Threading . Tasks ;
29
29
using System . Collections . ObjectModel ;
30
30
using System . Collections ;
31
+ using System . Diagnostics ;
31
32
32
33
namespace Microsoft . Windows . PowerShell . ScriptAnalyzer
33
34
{
@@ -216,6 +217,36 @@ internal bool ParseProfile(object profileObject, PathIntrinsics path, IOutputWri
216
217
return true ;
217
218
}
218
219
220
+ private bool AddProfileItem (
221
+ string key ,
222
+ List < string > values ,
223
+ List < string > severityList ,
224
+ List < string > includeRuleList ,
225
+ List < string > excludeRuleList )
226
+ {
227
+ Debug . Assert ( key != null ) ;
228
+ Debug . Assert ( values != null ) ;
229
+ Debug . Assert ( severityList != null ) ;
230
+ Debug . Assert ( includeRuleList != null ) ;
231
+ Debug . Assert ( excludeRuleList != null ) ;
232
+
233
+ switch ( key . ToLower ( ) )
234
+ {
235
+ case "severity" :
236
+ severityList . AddRange ( values ) ;
237
+ break ;
238
+ case "includerules" :
239
+ includeRuleList . AddRange ( values ) ;
240
+ break ;
241
+ case "excluderules" :
242
+ excludeRuleList . AddRange ( values ) ;
243
+ break ;
244
+ default :
245
+ return false ;
246
+ }
247
+ return true ;
248
+ }
249
+
219
250
private bool ParseProfileHashtable ( Hashtable profile , PathIntrinsics path , IOutputWriter writer ,
220
251
List < string > severityList , List < string > includeRuleList , List < string > excludeRuleList )
221
252
{
@@ -238,7 +269,7 @@ private bool ParseProfileHashtable(Hashtable profile, PathIntrinsics path, IOutp
238
269
hasError = true ;
239
270
continue ;
240
271
}
241
-
272
+
242
273
// checks whether it falls into list of valid keys
243
274
if ( ! validKeys . Contains ( key ) )
244
275
{
@@ -293,21 +324,8 @@ private bool ParseProfileHashtable(Hashtable profile, PathIntrinsics path, IOutp
293
324
}
294
325
}
295
326
296
- // now add to the list
297
- switch ( key )
298
- {
299
- case "severity" :
300
- severityList . AddRange ( values ) ;
301
- break ;
302
- case "includerules" :
303
- includeRuleList . AddRange ( values ) ;
304
- break ;
305
- case "excluderules" :
306
- excludeRuleList . AddRange ( values ) ;
307
- break ;
308
- default :
309
- break ;
310
- }
327
+ AddProfileItem ( key , values , severityList , includeRuleList , excludeRuleList ) ;
328
+
311
329
}
312
330
313
331
return hasError ;
@@ -426,23 +444,12 @@ private bool ParseProfileString(string profile, PathIntrinsics path, IOutputWrit
426
444
427
445
string key = ( kvp . Item1 as StringConstantExpressionAst ) . Value . ToLower ( ) ;
428
446
429
- switch ( key )
447
+ if ( ! AddProfileItem ( key , rhsList , severityList , includeRuleList , excludeRuleList ) )
430
448
{
431
- case "severity" :
432
- severityList . AddRange ( rhsList ) ;
433
- break ;
434
- case "includerules" :
435
- includeRuleList . AddRange ( rhsList ) ;
436
- break ;
437
- case "excluderules" :
438
- excludeRuleList . AddRange ( rhsList ) ;
439
- break ;
440
- default :
441
- writer . WriteError ( new ErrorRecord (
449
+ writer . WriteError ( new ErrorRecord (
442
450
new InvalidDataException ( string . Format ( CultureInfo . CurrentCulture , Strings . WrongKey , key , kvp . Item1 . Extent . StartLineNumber , kvp . Item1 . Extent . StartColumnNumber , profile ) ) ,
443
451
Strings . WrongConfigurationKey , ErrorCategory . InvalidData , profile ) ) ;
444
- hasError = true ;
445
- break ;
452
+ hasError = true ;
446
453
}
447
454
}
448
455
}
0 commit comments