From bfb78427d84b80f3571e39c05f6a9ff4d6d80a2d Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 21 Feb 2019 20:11:03 +0000 Subject: [PATCH 1/2] Populate RuleSuppressionID as well when translating DiagnosticRecord in custom rules --- Engine/ScriptAnalyzer.cs | 8 +++++++- ScriptRuleDocumentation.md | 2 ++ Tests/Engine/CustomizedRule.tests.ps1 | 6 ++++++ Tests/Engine/samplerule/samplerule.psm1 | 7 ++++--- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Engine/ScriptAnalyzer.cs b/Engine/ScriptAnalyzer.cs index 5b5ccd890..b08f2ec65 100644 --- a/Engine/ScriptAnalyzer.cs +++ b/Engine/ScriptAnalyzer.cs @@ -1268,6 +1268,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) @@ -1288,6 +1289,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) @@ -1298,7 +1300,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 543abc768..5d400c278 100644 --- a/Tests/Engine/CustomizedRule.tests.ps1 +++ b/Tests/Engine/CustomizedRule.tests.ps1 @@ -160,6 +160,12 @@ Describe "Test importing correct customized rules" { $violations[0].SuggestedCorrections.Text | Should -Be 'text' $violations[0].SuggestedCorrections.File | Should -Be 'filePath' $violations[0].SuggestedCorrections.Description | Should -Be 'description' + } + + It "will set SuggestedCorrections" { + $violations = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule + $expectedScriptPath = Join-Path $directory 'TestScript.ps1' + $violations[0].RuleSuppressionID | Should -Be "MyRuleSuppressionID" } if (!$testingLibraryUsage) diff --git a/Tests/Engine/samplerule/samplerule.psm1 b/Tests/Engine/samplerule/samplerule.psm1 index 6cf0ea1fd..0ec88d480 100644 --- a/Tests/Engine/samplerule/samplerule.psm1 +++ b/Tests/Engine/samplerule/samplerule.psm1 @@ -27,13 +27,14 @@ function Measure-RequiresRunAsAdministrator [ValidateNotNullOrEmpty()] [System.Management.Automation.Language.ScriptBlockAst] $testAst - ) + ) $l=(new-object System.Collections.ObjectModel.Collection["Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent"]) $c = (new-object Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent 1,2,3,4,'text','filePath','description') $l.Add($c) $dr = New-Object ` -Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" ` - -ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null,$null,$l - return $dr + -ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null,$null,$l + $dr.RuleSuppressionID = "MyRuleSuppressionID" + return $dr } Export-ModuleMember -Function Measure* \ No newline at end of file From cfc06180ccb306a93097e0f51b89ddc79ae7a726 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 21 Feb 2019 20:20:40 +0000 Subject: [PATCH 2/2] fix NullReferenceException --- Engine/ScriptAnalyzer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/ScriptAnalyzer.cs b/Engine/ScriptAnalyzer.cs index b08f2ec65..363cb1ca1 100644 --- a/Engine/ScriptAnalyzer.cs +++ b/Engine/ScriptAnalyzer.cs @@ -1289,7 +1289,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(); + ruleSuppressionID = psobject.Properties["RuleSuppressionID"].Value?.ToString(); suggestedCorrections = (IEnumerable)psobject.Properties["SuggestedCorrections"].Value; } catch (Exception ex)