From e0526bba2b86539af3b45956a20a44090cb42d64 Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister [MVP]" Date: Fri, 8 Mar 2019 05:34:03 +0000 Subject: [PATCH] Translate RuleSuppressionID for custom rules as well (#1144) * Populate RuleSuppressionID as well when translating DiagnosticRecord in custom rules * fix NullReferenceException --- Engine/ScriptAnalyzer.cs | 8 +++++++- ScriptRuleDocumentation.md | 2 ++ Tests/Engine/CustomizedRule.tests.ps1 | 5 +++++ Tests/Engine/samplerule/samplerule.psm1 | 3 ++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Engine/ScriptAnalyzer.cs b/Engine/ScriptAnalyzer.cs index 8d898ef59..c8e45eff7 100644 --- a/Engine/ScriptAnalyzer.cs +++ b/Engine/ScriptAnalyzer.cs @@ -1262,6 +1262,7 @@ internal IEnumerable GetExternalRecord(Ast ast, Token[] token, IScriptExtent extent; string message = string.Empty; string ruleName = string.Empty; + string ruleSuppressionID = string.Empty; IEnumerable suggestedCorrections; if (psobject != null && psobject.ImmediateBaseObject != null) @@ -1282,6 +1283,7 @@ internal IEnumerable GetExternalRecord(Ast ast, Token[] token, message = psobject.Properties["Message"].Value.ToString(); extent = (IScriptExtent)psobject.Properties["Extent"].Value; ruleName = psobject.Properties["RuleName"].Value.ToString(); + ruleSuppressionID = psobject.Properties["RuleSuppressionID"].Value?.ToString(); suggestedCorrections = (IEnumerable)psobject.Properties["SuggestedCorrections"].Value; } catch (Exception ex) @@ -1292,7 +1294,11 @@ internal IEnumerable GetExternalRecord(Ast ast, Token[] token, if (!string.IsNullOrEmpty(message)) { - diagnostics.Add(new DiagnosticRecord(message, extent, ruleName, severity, filePath) { SuggestedCorrections = suggestedCorrections }); + diagnostics.Add(new DiagnosticRecord(message, extent, ruleName, severity, filePath) + { + SuggestedCorrections = suggestedCorrections, + RuleSuppressionID = ruleSuppressionID, + }); } } } diff --git a/ScriptRuleDocumentation.md b/ScriptRuleDocumentation.md index 3f3d15ff6..f4f9a5844 100644 --- a/ScriptRuleDocumentation.md +++ b/ScriptRuleDocumentation.md @@ -79,6 +79,8 @@ $suggestedCorrections.add($correctionExtent) | out-null "Extent" = $ast.Extent "RuleName" = $PSCmdlet.MyInvocation.InvocationName "Severity" = "Warning" + "Severity" = "Warning" + "RuleSuppressionID" = "MyRuleSuppressionID" "SuggestedCorrections" = $suggestedCorrections } ``` diff --git a/Tests/Engine/CustomizedRule.tests.ps1 b/Tests/Engine/CustomizedRule.tests.ps1 index 7936de266..c3c9aac47 100644 --- a/Tests/Engine/CustomizedRule.tests.ps1 +++ b/Tests/Engine/CustomizedRule.tests.ps1 @@ -171,6 +171,11 @@ Describe "Test importing correct customized rules" { Where-Object { $_.Message -eq $message } $customizedRulePath.Count | Should -Be 0 + } + + It "will set RuleSuppressionID" { + $violations = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule + $violations[0].RuleSuppressionID | Should -Be "MyRuleSuppressionID" } if (!$testingLibraryUsage) diff --git a/Tests/Engine/samplerule/samplerule.psm1 b/Tests/Engine/samplerule/samplerule.psm1 index f740c0ea1..f636ca65c 100644 --- a/Tests/Engine/samplerule/samplerule.psm1 +++ b/Tests/Engine/samplerule/samplerule.psm1 @@ -37,6 +37,7 @@ function Measure-RequiresRunAsAdministrator $dr = New-Object ` -Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" ` -ArgumentList "This is help",$extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null,$null,$l - return $dr + $dr.RuleSuppressionID = "MyRuleSuppressionID" + return $dr } Export-ModuleMember -Function Measure* \ No newline at end of file