Skip to content

Commit 5fea714

Browse files
author
Quoc Truong
committed
Write errors to the terminal when there are rule suppression errors
1 parent a79f723 commit 5fea714

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,17 @@ private void AnalyzeFile(string filePath)
291291

292292
Dictionary<string, List<RuleSuppression>> ruleSuppressions = Helper.Instance.GetRuleSuppression(ast);
293293

294+
foreach (List<RuleSuppression> ruleSuppressionsList in ruleSuppressions.Values)
295+
{
296+
foreach (RuleSuppression ruleSuppression in ruleSuppressionsList)
297+
{
298+
if (!String.IsNullOrWhiteSpace(ruleSuppression.Error))
299+
{
300+
WriteError(new ErrorRecord(new ArgumentException(ruleSuppression.Error), ruleSuppression.Error, ErrorCategory.InvalidArgument, ruleSuppression));
301+
}
302+
}
303+
}
304+
294305
#region Run VariableAnalysis
295306
try
296307
{

Engine/Generic/RuleSuppression.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Management.Automation.Language;
44
using System.Collections.Generic;
55
using System.Text.RegularExpressions;
6+
using System.Globalization;
67

78
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
89
{
@@ -11,6 +12,8 @@ namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
1112
/// </summary>
1213
public class RuleSuppression
1314
{
15+
private string _ruleName;
16+
1417
/// <summary>
1518
/// The start offset of the rule suppression
1619
/// </summary>
@@ -34,8 +37,24 @@ public int EndOffset
3437
/// </summary>
3538
public string RuleName
3639
{
37-
get;
38-
set;
40+
get
41+
{
42+
return _ruleName;
43+
}
44+
45+
set
46+
{
47+
_ruleName = value;
48+
if ((ScriptAnalyzer.Instance.ScriptRules != null
49+
&& ScriptAnalyzer.Instance.ScriptRules.Count(item => String.Equals(item.GetName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0)
50+
&& (ScriptAnalyzer.Instance.TokenRules != null
51+
&& ScriptAnalyzer.Instance.TokenRules.Count(item => String.Equals(item.GetName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0)
52+
&& (ScriptAnalyzer.Instance.ExternalRules != null
53+
&& ScriptAnalyzer.Instance.ExternalRules.Count(item => String.Equals(item.GetName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0))
54+
{
55+
Error = String.Format(Strings.RuleSuppressionRuleNameNotFound, _ruleName);
56+
}
57+
}
3958
}
4059

4160
/// <summary>
@@ -212,6 +231,12 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
212231

213232
StartOffset = start;
214233
EndOffset = end;
234+
235+
if (!String.IsNullOrWhiteSpace(Error))
236+
{
237+
Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, attrAst.Extent.StartLineNumber,
238+
System.IO.Path.GetFileName(attrAst.Extent.File), Error);
239+
}
215240
}
216241

217242
/// <summary>

Engine/Strings.Designer.cs

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

Engine/Strings.resx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,17 @@
162162
<data name="RulesNotFound" xml:space="preserve">
163163
<value>Cannot find analyzer rules.</value>
164164
</data>
165+
<data name="RuleSuppressionErrorFormat" xml:space="preserve">
166+
<value>Suppression Message Attribute error at line {0} in {1} : {2}</value>
167+
</data>
168+
<data name="RuleSuppressionRuleNameNotFound" xml:space="preserve">
169+
<value>Rule {0} cannot be found.</value>
170+
</data>
165171
<data name="StringConstantArgumentsSuppressionAttributeError" xml:space="preserve">
166-
<value>All the arguments of the suppression message attribute should be string constants.</value>
172+
<value>All the arguments of the should be string constants.</value>
167173
</data>
168174
<data name="TargetWithoutScopeSuppressionAttributeError" xml:space="preserve">
169-
<value>For Suppression Message Attribute, if Target is specified, Scope must be specified.</value>
175+
<value>If Target is specified, Scope must be specified.</value>
170176
</data>
171177
<data name="VerboseFileMessage" xml:space="preserve">
172178
<value>Analyzing file: {0}</value>

0 commit comments

Comments
 (0)