diff --git a/Engine/Generic/RuleSuppression.cs b/Engine/Generic/RuleSuppression.cs
index 9082b2e85..d8a41f974 100644
--- a/Engine/Generic/RuleSuppression.cs
+++ b/Engine/Generic/RuleSuppression.cs
@@ -15,7 +15,6 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
///
public class RuleSuppression
{
- private string _ruleName;
///
/// The start offset of the rule suppression attribute (not where it starts to apply)
@@ -49,28 +48,8 @@ public int EndOffset
///
public string RuleName
{
- get
- {
- return _ruleName;
- }
-
- set
- {
- _ruleName = value;
-
- if (!String.IsNullOrWhiteSpace(_ruleName)
- && (ScriptAnalyzer.Instance.ScriptRules != null
- && ScriptAnalyzer.Instance.ScriptRules.Count(item => String.Equals(item.GetName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0)
- && (ScriptAnalyzer.Instance.TokenRules != null
- && ScriptAnalyzer.Instance.TokenRules.Count(item => String.Equals(item.GetName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0)
- && (ScriptAnalyzer.Instance.ExternalRules != null
- && ScriptAnalyzer.Instance.ExternalRules.Count(item => String.Equals(item.GetFullName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0)
- && (ScriptAnalyzer.Instance.DSCResourceRules != null
- && ScriptAnalyzer.Instance.DSCResourceRules.Count(item => String.Equals(item.GetName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0))
- {
- Error = String.Format(Strings.RuleSuppressionRuleNameNotFound, _ruleName);
- }
- }
+ get;
+ set;
}
///
diff --git a/Engine/Strings.Designer.cs b/Engine/Strings.Designer.cs
index 077b3a591..c34570bcd 100644
--- a/Engine/Strings.Designer.cs
+++ b/Engine/Strings.Designer.cs
@@ -438,15 +438,6 @@ internal static string RuleSuppressionIDError {
}
}
- ///
- /// Looks up a localized string similar to Rule {0} cannot be found..
- ///
- internal static string RuleSuppressionRuleNameNotFound {
- get {
- return ResourceManager.GetString("RuleSuppressionRuleNameNotFound", resourceCulture);
- }
- }
-
///
/// Looks up a localized string similar to Found {0}. Will use it to provide settings for this invocation..
///
diff --git a/Engine/Strings.resx b/Engine/Strings.resx
index 743c3ccad..553443839 100644
--- a/Engine/Strings.resx
+++ b/Engine/Strings.resx
@@ -165,9 +165,6 @@
Suppression Message Attribute error at line {0} in {1} : {2}
-
- Rule {0} cannot be found.
-
All the arguments of the Suppress Message Attribute should be string constants.
diff --git a/Tests/Engine/CustomizedRule.tests.ps1 b/Tests/Engine/CustomizedRule.tests.ps1
index c3c9aac47..301586667 100644
--- a/Tests/Engine/CustomizedRule.tests.ps1
+++ b/Tests/Engine/CustomizedRule.tests.ps1
@@ -173,6 +173,43 @@ Describe "Test importing correct customized rules" {
$customizedRulePath.Count | Should -Be 0
}
+ It "can suppress custom rule with rule name expression ''" -TestCases @(
+ @{RuleNameExpression = '$MyInvocation.MyCommand.Name'; RuleName = 'WarningAboutDoSomething' }
+ @{RuleNameExpression = '$MyInvocation.InvocationName'; RuleName = 'MyCustomRule\WarningAboutDoSomething' }
+ @{RuleNameExpression = "'MyRuleName'"; RuleName = 'MyRuleName' }
+ ) {
+ Param($RuleNameExpression, $RuleName)
+
+ $script = @"
+ [Diagnostics.CodeAnalysis.SuppressMessageAttribute('$RuleName', '')]
+ Param()
+ Invoke-Something
+"@
+ $customRuleContent = @'
+ function WarningAboutDoSomething {
+ param (
+ [System.Management.Automation.Language.CommandAst]$ast
+ )
+
+ if ($ast.GetCommandName() -eq 'Invoke-Something') {
+ New-Object -Typename 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord' `
+ -ArgumentList 'This is help',$ast.Extent,REPLACE_WITH_RULE_NAME_EXPRESSION,Warning,$ast.Extent.File,$null,$null
+ }
+ }
+'@
+ $customRuleContent = $customRuleContent.Replace('REPLACE_WITH_RULE_NAME_EXPRESSION', $RuleNameExpression)
+ $testScriptPath = "TestDrive:\SuppressedCustomRule.ps1"
+ Set-Content -Path $testScriptPath -Value $script
+ $customRuleScriptPath = Join-Path $TestDrive 'MyCustomRule.psm1'
+ Set-Content -Path $customRuleScriptPath -Value $customRuleContent
+ $violationsWithoutSuppresion = Invoke-ScriptAnalyzer -ScriptDefinition 'Invoke-Something' -CustomRulePath $customRuleScriptPath
+ $violationsWithoutSuppresion.Count | Should -Be 1
+ $violations = Invoke-ScriptAnalyzer -Path $testScriptPath -CustomRulePath $customRuleScriptPath
+ $violations.Count | Should -Be 0
+ $violationsWithIncludeDefaultRules = Invoke-ScriptAnalyzer -Path $testScriptPath -CustomRulePath $customRuleScriptPath -IncludeDefaultRules
+ $violationsWithIncludeDefaultRules.Count | Should -Be 0
+ }
+
It "will set RuleSuppressionID" {
$violations = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule
$violations[0].RuleSuppressionID | Should -Be "MyRuleSuppressionID"