diff --git a/Engine/ScriptAnalyzer.cs b/Engine/ScriptAnalyzer.cs index 8b4df52b7..6effb5d8d 100644 --- a/Engine/ScriptAnalyzer.cs +++ b/Engine/ScriptAnalyzer.cs @@ -28,6 +28,7 @@ using System.Threading.Tasks; using System.Collections.ObjectModel; using System.Collections; +using System.Diagnostics; namespace Microsoft.Windows.PowerShell.ScriptAnalyzer { @@ -216,6 +217,36 @@ internal bool ParseProfile(object profileObject, PathIntrinsics path, IOutputWri return true; } + private bool AddProfileItem( + string key, + List values, + List severityList, + List includeRuleList, + List excludeRuleList) + { + Debug.Assert(key != null); + Debug.Assert(values != null); + Debug.Assert(severityList != null); + Debug.Assert(includeRuleList != null); + Debug.Assert(excludeRuleList != null); + + switch (key.ToLower()) + { + case "severity": + severityList.AddRange(values); + break; + case "includerules": + includeRuleList.AddRange(values); + break; + case "excluderules": + excludeRuleList.AddRange(values); + break; + default: + return false; + } + return true; + } + private bool ParseProfileHashtable(Hashtable profile, PathIntrinsics path, IOutputWriter writer, List severityList, List includeRuleList, List excludeRuleList) { @@ -238,7 +269,7 @@ private bool ParseProfileHashtable(Hashtable profile, PathIntrinsics path, IOutp hasError = true; continue; } - + // checks whether it falls into list of valid keys if (!validKeys.Contains(key)) { @@ -293,21 +324,8 @@ private bool ParseProfileHashtable(Hashtable profile, PathIntrinsics path, IOutp } } - // now add to the list - switch (key) - { - case "severity": - severityList.AddRange(values); - break; - case "includerules": - includeRuleList.AddRange(values); - break; - case "excluderules": - excludeRuleList.AddRange(values); - break; - default: - break; - } + AddProfileItem(key, values, severityList, includeRuleList, excludeRuleList); + } return hasError; @@ -426,23 +444,12 @@ private bool ParseProfileString(string profile, PathIntrinsics path, IOutputWrit string key = (kvp.Item1 as StringConstantExpressionAst).Value.ToLower(); - switch (key) + if(!AddProfileItem(key, rhsList, severityList, includeRuleList, excludeRuleList)) { - case "severity": - severityList.AddRange(rhsList); - break; - case "includerules": - includeRuleList.AddRange(rhsList); - break; - case "excluderules": - excludeRuleList.AddRange(rhsList); - break; - default: - writer.WriteError(new ErrorRecord( + writer.WriteError(new ErrorRecord( new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Strings.WrongKey, key, kvp.Item1.Extent.StartLineNumber, kvp.Item1.Extent.StartColumnNumber, profile)), Strings.WrongConfigurationKey, ErrorCategory.InvalidData, profile)); - hasError = true; - break; + hasError = true; } } }