2828using System . Threading . Tasks ;
2929using System . Collections . ObjectModel ;
3030using System . Collections ;
31+ using System . Diagnostics ;
3132
3233namespace Microsoft . Windows . PowerShell . ScriptAnalyzer
3334{
@@ -216,6 +217,36 @@ internal bool ParseProfile(object profileObject, PathIntrinsics path, IOutputWri
216217 return true ;
217218 }
218219
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+
219250 private bool ParseProfileHashtable ( Hashtable profile , PathIntrinsics path , IOutputWriter writer ,
220251 List < string > severityList , List < string > includeRuleList , List < string > excludeRuleList )
221252 {
@@ -238,7 +269,7 @@ private bool ParseProfileHashtable(Hashtable profile, PathIntrinsics path, IOutp
238269 hasError = true ;
239270 continue ;
240271 }
241-
272+
242273 // checks whether it falls into list of valid keys
243274 if ( ! validKeys . Contains ( key ) )
244275 {
@@ -293,21 +324,8 @@ private bool ParseProfileHashtable(Hashtable profile, PathIntrinsics path, IOutp
293324 }
294325 }
295326
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+
311329 }
312330
313331 return hasError ;
@@ -426,23 +444,12 @@ private bool ParseProfileString(string profile, PathIntrinsics path, IOutputWrit
426444
427445 string key = ( kvp . Item1 as StringConstantExpressionAst ) . Value . ToLower ( ) ;
428446
429- switch ( key )
447+ if ( ! AddProfileItem ( key , rhsList , severityList , includeRuleList , excludeRuleList ) )
430448 {
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 (
442450 new InvalidDataException ( string . Format ( CultureInfo . CurrentCulture , Strings . WrongKey , key , kvp . Item1 . Extent . StartLineNumber , kvp . Item1 . Extent . StartColumnNumber , profile ) ) ,
443451 Strings . WrongConfigurationKey , ErrorCategory . InvalidData , profile ) ) ;
444- hasError = true ;
445- break ;
452+ hasError = true ;
446453 }
447454 }
448455 }
0 commit comments