Skip to content

Commit 8862e51

Browse files
Invoke-ScriptAnalyzer: Include parse errors in reported error count (#2069)
Previously, parse error were not reported in the summary. With this commit, the exit code from -EnableExit matches the number of reported issues. Co-authored-by: Christoph Bergmeister <[email protected]>
1 parent 1d394ee commit 8862e51

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -416,36 +416,35 @@ protected override void EndProcessing()
416416
ScriptAnalyzer.Instance.CleanUp();
417417
base.EndProcessing();
418418

419-
var infoCount = diagnosticCounts[DiagnosticSeverity.Information];
420-
var warningCount = diagnosticCounts[DiagnosticSeverity.Warning];
421-
var errorCount = diagnosticCounts[DiagnosticSeverity.Error];
422-
var parseErrorCount = diagnosticCounts[DiagnosticSeverity.ParseError];
419+
var diagnosticCount = diagnosticCounts.Values.Sum();
423420

424421
if (ReportSummary.IsPresent)
425422
{
426-
var numberOfRuleViolations = infoCount + warningCount + errorCount;
427-
if (numberOfRuleViolations == 0)
423+
if (diagnosticCount == 0)
428424
{
429425
Host.UI.WriteLine("0 rule violations found.");
430426
}
431427
else
432428
{
433-
var pluralS = numberOfRuleViolations > 1 ? "s" : string.Empty;
434-
var message = $"{numberOfRuleViolations} rule violation{pluralS} found. Severity distribution: {DiagnosticSeverity.Error} = {errorCount}, {DiagnosticSeverity.Warning} = {warningCount}, {DiagnosticSeverity.Information} = {infoCount}";
435-
if (warningCount + errorCount == 0)
436-
{
437-
ConsoleHostHelper.DisplayMessageUsingSystemProperties(Host, "WarningForegroundColor", "WarningBackgroundColor", message);
438-
}
439-
else
440-
{
441-
ConsoleHostHelper.DisplayMessageUsingSystemProperties(Host, "ErrorForegroundColor", "ErrorBackgroundColor", message);
442-
}
429+
var infoCount = diagnosticCounts[DiagnosticSeverity.Information];
430+
var warningCount = diagnosticCounts[DiagnosticSeverity.Warning];
431+
var errorCount = diagnosticCounts[DiagnosticSeverity.Error] + diagnosticCounts[DiagnosticSeverity.ParseError];
432+
var severeDiagnosticCount = diagnosticCount - infoCount;
433+
434+
var colorPropertyPrefix = severeDiagnosticCount == 0 ? "Warning" : "Error";
435+
var pluralS = diagnosticCount > 1 ? "s" : string.Empty;
436+
ConsoleHostHelper.DisplayMessageUsingSystemProperties(
437+
Host, colorPropertyPrefix + "ForegroundColor", colorPropertyPrefix + "BackgroundColor",
438+
$"{diagnosticCount} rule violation{pluralS} found. Severity distribution: " +
439+
$"{DiagnosticSeverity.Error} = {errorCount}, " +
440+
$"{DiagnosticSeverity.Warning} = {warningCount}, " +
441+
$"{DiagnosticSeverity.Information} = {infoCount}");
443442
}
444443
}
445444

446445
if (EnableExit)
447446
{
448-
this.Host.SetShouldExit(diagnosticCounts.Values.Sum());
447+
this.Host.SetShouldExit(diagnosticCount);
449448
}
450449
}
451450

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ Describe "-ReportSummary switch" {
600600
$pwshExe = 'powershell'
601601
}
602602

603-
$reportSummaryFor1Warning = '*1 rule violation found. Severity distribution: Error = 0, Warning = 1, Information = 0*'
603+
$reportSummaryFor1Warning = '*1 rule violation found. Severity distribution: Error = 0, Warning = 1, Information = 0*'
604604
}
605605

606606
It "prints the correct report summary using the -NoReportSummary switch" {

0 commit comments

Comments
 (0)