Skip to content

Commit 6ab5099

Browse files
author
Quoc Truong
committed
Raise error if Rule Suppression ID is used but no match is found
1 parent d6d35b6 commit 6ab5099

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

Engine/Helper.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Management.Automation;
66
using System.Management.Automation.Language;
77
using System.Management.Automation.Runspaces;
8+
using System.Globalization;
89
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
910

1011
namespace Microsoft.Windows.Powershell.ScriptAnalyzer
@@ -740,6 +741,7 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
740741
int ruleSuppressionIndex = 0;
741742
DiagnosticRecord record = diagnostics.First();
742743
RuleSuppression ruleSuppression = ruleSuppressions.First();
744+
int suppressionCount = 0;
743745

744746
while (recordIndex < diagnostics.Count)
745747
{
@@ -753,6 +755,7 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
753755
}
754756

755757
ruleSuppression = ruleSuppressions[ruleSuppressionIndex];
758+
suppressionCount = 0;
756759

757760
continue;
758761
}
@@ -767,12 +770,21 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
767770
{
768771
ruleSuppressionIndex += 1;
769772

773+
// If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
774+
if (!String.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID) && suppressionCount == 0)
775+
{
776+
ruleSuppression.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSuppression.StartOffset,
777+
System.IO.Path.GetFileName(record.Extent.File), String.Format(Strings.RuleSuppressionIDError, ruleSuppression.RuleSuppressionID));
778+
Helper.Instance.MyCmdlet.WriteError(new ErrorRecord(new ArgumentException(ruleSuppression.Error), ruleSuppression.Error, ErrorCategory.InvalidArgument, ruleSuppression));
779+
}
780+
770781
if (ruleSuppressionIndex == ruleSuppressions.Count)
771782
{
772783
break;
773784
}
774785

775786
ruleSuppression = ruleSuppressions[ruleSuppressionIndex];
787+
suppressionCount = 0;
776788

777789
continue;
778790
}
@@ -785,15 +797,25 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
785797
&& !string.Equals(ruleSuppression.RuleSuppressionID, record.RuleSuppressionID, StringComparison.OrdinalIgnoreCase))
786798
{
787799
results.Add(record);
800+
suppressionCount -= 1;
788801
}
789802
// otherwise, we ignore the record, move on to the next.
790803
}
791804

792805
// important assumption: this point is reached only if we want to move to the next record
793806
recordIndex += 1;
807+
suppressionCount += 1;
794808

795809
if (recordIndex == diagnostics.Count)
796810
{
811+
// If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
812+
if (!String.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID) && suppressionCount == 0)
813+
{
814+
ruleSuppression.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSuppression.StartOffset,
815+
System.IO.Path.GetFileName(record.Extent.File), String.Format(Strings.RuleSuppressionIDError, ruleSuppression.RuleSuppressionID));
816+
Helper.Instance.MyCmdlet.WriteError(new ErrorRecord(new ArgumentException(ruleSuppression.Error), ruleSuppression.Error, ErrorCategory.InvalidArgument, ruleSuppression));
817+
}
818+
797819
break;
798820
}
799821

Engine/Strings.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Engine/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,7 @@
189189
<data name="TargetCannotBeFoundError" xml:space="preserve">
190190
<value>Cannot find any Targets {0} that match the Scope {1} to apply the SuppressMessageAttribute.</value>
191191
</data>
192+
<data name="RuleSuppressionIDError" xml:space="preserve">
193+
<value>Cannot find any DiagnosticRecord with the Rule Suppression ID {0}.</value>
194+
</data>
192195
</root>

0 commit comments

Comments
 (0)