Skip to content

Commit

Permalink
Translate RuleSuppressionID for custom rules as well (PowerShell#1144)
Browse files Browse the repository at this point in the history
* Populate RuleSuppressionID as well when translating DiagnosticRecord in custom rules

* fix NullReferenceException
  • Loading branch information
bergmeister committed Mar 22, 2019
1 parent 43c59ab commit e0526bb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Engine/ScriptAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,7 @@ internal IEnumerable<DiagnosticRecord> GetExternalRecord(Ast ast, Token[] token,
IScriptExtent extent;
string message = string.Empty;
string ruleName = string.Empty;
string ruleSuppressionID = string.Empty;
IEnumerable<CorrectionExtent> suggestedCorrections;

if (psobject != null && psobject.ImmediateBaseObject != null)
Expand All @@ -1282,6 +1283,7 @@ internal IEnumerable<DiagnosticRecord> 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<CorrectionExtent>)psobject.Properties["SuggestedCorrections"].Value;
}
catch (Exception ex)
Expand All @@ -1292,7 +1294,11 @@ internal IEnumerable<DiagnosticRecord> 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,
});
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions ScriptRuleDocumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ $suggestedCorrections.add($correctionExtent) | out-null
"Extent" = $ast.Extent
"RuleName" = $PSCmdlet.MyInvocation.InvocationName
"Severity" = "Warning"
"Severity" = "Warning"
"RuleSuppressionID" = "MyRuleSuppressionID"
"SuggestedCorrections" = $suggestedCorrections
}
```
Expand Down
5 changes: 5 additions & 0 deletions Tests/Engine/CustomizedRule.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion Tests/Engine/samplerule/samplerule.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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*

0 comments on commit e0526bb

Please sign in to comment.