Skip to content

Commit 54389bd

Browse files
author
Kapil Borle
committed
Interpret target in suppression as regular expression
According to README, target supports regular expression. By escaping the target argument, it is no longer a regular expression. This commit removes the escaping. Ideally, we should not replace '*' with '.*' as we explicity mention that target takes regular expression. But, since globbing is more widely used in such scenario and if we do not interpret '*' as a glob, there is a chance it will invalidate suppression in many cases where people have used this feature. Hence, to maintain compatibility we keep '*' interpretation as a glob.
1 parent dcbf622 commit 54389bd

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

Engine/Generic/RuleSuppression.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
2121
{
2222
/// <summary>
23-
///
23+
///
2424
/// </summary>
2525
public class RuleSuppression
2626
{
@@ -185,11 +185,11 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
185185
case 2:
186186
RuleSuppressionID = (positionalArguments[1] as StringConstantExpressionAst).Value;
187187
goto case 1;
188-
188+
189189
case 1:
190190
RuleName = (positionalArguments[0] as StringConstantExpressionAst).Value;
191191
goto default;
192-
192+
193193
default:
194194
break;
195195
}
@@ -281,7 +281,7 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
281281
RuleName = String.Empty;
282282
Error = Strings.NullRuleNameError;
283283
}
284-
284+
285285
// Must have scope and target together
286286
if (String.IsNullOrWhiteSpace(Scope) && !String.IsNullOrWhiteSpace(Target))
287287
{
@@ -357,8 +357,10 @@ public static List<RuleSuppression> GetSuppressions(IEnumerable<AttributeAst> at
357357
ruleSupp.Target = "*";
358358
}
359359

360+
// According to documentation 'target' supports regular expression. But to maintain compatibility with
361+
// previous implementation we interpret '*' as a glob and therefore replace '*' with '.*'
360362
// regex for wild card *
361-
Regex reg = new Regex(String.Format("^{0}$", Regex.Escape(ruleSupp.Target).Replace(@"\*", ".*")), RegexOptions.IgnoreCase);
363+
Regex reg = new Regex(String.Format("^{0}$", ruleSupp.Target.Replace(@"*", ".*")), RegexOptions.IgnoreCase);
362364
IEnumerable<Ast> targetAsts = null;
363365

364366
switch (ruleSupp.Scope.ToLower())

0 commit comments

Comments
 (0)