Skip to content

Commit fbbe414

Browse files
author
Quoc Truong
committed
Change the RuleSuppression infrastructure to support getting suppressed message
1 parent 048f585 commit fbbe414

File tree

4 files changed

+89
-55
lines changed

4 files changed

+89
-55
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ private void AnalyzeFile(string filePath)
328328
// We want the Engine to continue functioning even if one or more Rules throws an exception
329329
try
330330
{
331-
diagnostics.AddRange(Helper.Instance.SuppressRule(scriptRule.GetName(), ruleSuppressions, scriptRule.AnalyzeScript(ast, filePath).ToList()));
331+
var records = scriptRule.AnalyzeScript(ast, filePath).ToList();
332+
Helper.Instance.SuppressRule(scriptRule.GetName(), ruleSuppressions, records);
333+
diagnostics.AddRange(records.Where(record => record.Suppression == null));
332334
}
333335
catch (Exception scriptRuleException)
334336
{
@@ -356,7 +358,9 @@ private void AnalyzeFile(string filePath)
356358
// We want the Engine to continue functioning even if one or more Rules throws an exception
357359
try
358360
{
359-
diagnostics.AddRange(Helper.Instance.SuppressRule(tokenRule.GetName(), ruleSuppressions, tokenRule.AnalyzeTokens(tokens, fileName).ToList()));
361+
var records = tokenRule.AnalyzeTokens(tokens, fileName).ToList();
362+
Helper.Instance.SuppressRule(tokenRule.GetName(), ruleSuppressions, records);
363+
diagnostics.AddRange(records.Where(record => record.Suppression == null));
360364
}
361365
catch (Exception tokenRuleException)
362366
{
@@ -384,7 +388,9 @@ private void AnalyzeFile(string filePath)
384388
// We want the Engine to continue functioning even if one or more Rules throws an exception
385389
try
386390
{
387-
diagnostics.AddRange(Helper.Instance.SuppressRule(dscResourceRule.GetName(), ruleSuppressions, dscResourceRule.AnalyzeDSCClass(ast, filePath).ToList()));
391+
var records = dscResourceRule.AnalyzeDSCClass(ast, filePath).ToList();
392+
Helper.Instance.SuppressRule(dscResourceRule.GetName(), ruleSuppressions, records);
393+
diagnostics.AddRange(records.Where(record => record.Suppression == null));
388394
}
389395
catch (Exception dscResourceRuleException)
390396
{
@@ -426,7 +432,9 @@ private void AnalyzeFile(string filePath)
426432
// We want the Engine to continue functioning even if one or more Rules throws an exception
427433
try
428434
{
429-
diagnostics.AddRange(Helper.Instance.SuppressRule(dscResourceRule.GetName(), ruleSuppressions, dscResourceRule.AnalyzeDSCResource(ast, filePath).ToList()));
435+
var records = dscResourceRule.AnalyzeDSCResource(ast, filePath).ToList();
436+
Helper.Instance.SuppressRule(dscResourceRule.GetName(), ruleSuppressions, records);
437+
diagnostics.AddRange(records.Where(record => record.Suppression == null));
430438
}
431439
catch (Exception dscResourceRuleException)
432440
{

Engine/Generic/DiagnosticRecord.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ public string Message
2929
set { message = value; }
3030
}
3131

32+
/// <summary>
33+
/// The Rule Suppression that applies to this record (null if none is applied)
34+
/// </summary>
35+
public RuleSuppression Suppression
36+
{
37+
get;
38+
set;
39+
}
40+
3241
/// <summary>
3342
/// Represents a span of text in a script.
3443
/// </summary>

Engine/Generic/RuleSuppression.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ public string Error
118118
set;
119119
}
120120

121+
/// <summary>
122+
/// Returns the justification for the suppression
123+
/// </summary>
124+
public string Justification
125+
{
126+
get;
127+
set;
128+
}
129+
121130
private static HashSet<string> scopeSet;
122131

123132
/// <summary>
@@ -161,6 +170,10 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
161170
{
162171
switch (count)
163172
{
173+
case 5:
174+
Justification = (positionalArguments[4] as StringConstantExpressionAst).Value;
175+
goto case 4;
176+
164177
case 4:
165178
Target = (positionalArguments[3] as StringConstantExpressionAst).Value;
166179
goto case 3;
@@ -242,6 +255,15 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
242255
Target = (name.Argument as StringConstantExpressionAst).Value;
243256
goto default;
244257

258+
case "justification":
259+
if (!String.IsNullOrWhiteSpace(Justification))
260+
{
261+
Error = String.Format(Strings.NamedAndPositionalArgumentsConflictError, name);
262+
}
263+
264+
Justification = (name.Argument as StringConstantExpressionAst).Value;
265+
goto default;
266+
245267
default:
246268
break;
247269
}
@@ -278,20 +300,22 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
278300
}
279301

280302
/// <summary>
281-
/// Constructs rule expression from rule name, id, start, end and startAttributeLine
303+
/// Constructs rule expression from rule name, id, start, end, startAttributeLine and justification
282304
/// </summary>
283305
/// <param name="ruleName"></param>
284306
/// <param name="ruleSuppressionID"></param>
285307
/// <param name="start"></param>
286308
/// <param name="end"></param>
287309
/// <param name="startAttributeLine"></param>
288-
public RuleSuppression(string ruleName, string ruleSuppressionID, int start, int end, int startAttributeLine)
310+
/// <param name="justification"></param>
311+
public RuleSuppression(string ruleName, string ruleSuppressionID, int start, int end, int startAttributeLine, string justification)
289312
{
290313
RuleName = ruleName;
291314
RuleSuppressionID = ruleSuppressionID;
292315
StartOffset = start;
293316
EndOffset = end;
294317
StartAttributeLine = startAttributeLine;
318+
Justification = justification;
295319
}
296320

297321
/// <summary>
@@ -356,7 +380,8 @@ public static List<RuleSuppression> GetSuppressions(IEnumerable<AttributeAst> at
356380

357381
foreach (Ast targetAst in targetAsts)
358382
{
359-
result.Add(new RuleSuppression(ruleSupp.RuleName, ruleSupp.RuleSuppressionID, targetAst.Extent.StartOffset, targetAst.Extent.EndOffset, attributeAst.Extent.StartLineNumber));
383+
result.Add(new RuleSuppression(ruleSupp.RuleName, ruleSupp.RuleSuppressionID, targetAst.Extent.StartOffset,
384+
targetAst.Extent.EndOffset, attributeAst.Extent.StartLineNumber, ruleSupp.Justification));
360385
}
361386
}
362387

Engine/Helper.cs

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -716,25 +716,23 @@ internal List<RuleSuppression> GetSuppressionsClass(TypeDefinitionAst typeAst)
716716
}
717717

718718
/// <summary>
719-
/// Suppress the rules from the diagnostic records list and return the result
719+
/// Suppress the rules from the diagnostic records list by attaching rule suppression to the record that is suppressed
720720
/// </summary>
721721
/// <param name="ruleSuppressions"></param>
722722
/// <param name="diagnostics"></param>
723-
public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, List<RuleSuppression>> ruleSuppressionsDict, List<DiagnosticRecord> diagnostics)
723+
public void SuppressRule(string ruleName, Dictionary<string, List<RuleSuppression>> ruleSuppressionsDict, List<DiagnosticRecord> diagnostics)
724724
{
725-
List<DiagnosticRecord> results = new List<DiagnosticRecord>();
726-
727725
if (ruleSuppressionsDict == null || !ruleSuppressionsDict.ContainsKey(ruleName)
728726
|| diagnostics == null || diagnostics.Count == 0)
729727
{
730-
return diagnostics;
728+
return;
731729
}
732730

733731
List<RuleSuppression> ruleSuppressions = ruleSuppressionsDict[ruleName];
734732

735733
if (ruleSuppressions.Count == 0)
736734
{
737-
return diagnostics;
735+
return;
738736
}
739737

740738
int recordIndex = 0;
@@ -760,46 +758,49 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
760758
continue;
761759
}
762760

763-
// the record precedes the rule suppression so don't apply the suppression
764-
if (record.Extent.StartOffset < ruleSuppression.StartOffset)
761+
// if the record precedes the rule suppression then we don't apply the suppression
762+
// so we check that start of record is greater than start of suppression
763+
if (record.Extent.StartOffset >= ruleSuppression.StartOffset)
765764
{
766-
results.Add(record);
767-
}
768-
// end of the rule suppression is less than the record start offset so move on to next rule suppression
769-
else if (ruleSuppression.EndOffset < record.Extent.StartOffset)
770-
{
771-
ruleSuppressionIndex += 1;
772-
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)
765+
// end of the rule suppression is less than the record start offset so move on to next rule suppression
766+
if (ruleSuppression.EndOffset < record.Extent.StartOffset)
775767
{
776-
ruleSuppression.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSuppression.StartAttributeLine,
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-
}
768+
ruleSuppressionIndex += 1;
780769

781-
if (ruleSuppressionIndex == ruleSuppressions.Count)
782-
{
783-
break;
784-
}
770+
// If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
771+
if (!String.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID) && suppressionCount == 0)
772+
{
773+
ruleSuppression.Error = String.Format(CultureInfo.CurrentCulture, Strings.RuleSuppressionErrorFormat, ruleSuppression.StartAttributeLine,
774+
System.IO.Path.GetFileName(record.Extent.File), String.Format(Strings.RuleSuppressionIDError, ruleSuppression.RuleSuppressionID));
775+
Helper.Instance.MyCmdlet.WriteError(new ErrorRecord(new ArgumentException(ruleSuppression.Error), ruleSuppression.Error, ErrorCategory.InvalidArgument, ruleSuppression));
776+
}
785777

786-
ruleSuppression = ruleSuppressions[ruleSuppressionIndex];
787-
suppressionCount = 0;
778+
if (ruleSuppressionIndex == ruleSuppressions.Count)
779+
{
780+
break;
781+
}
788782

789-
continue;
790-
}
791-
// at this point, the record is inside the interval
792-
else
793-
{
794-
// if the rule suppression id from the rule suppression is not null and the one from diagnostic record is not null
795-
// and they are they are not the same then we cannot ignore the record
796-
if (!string.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID) && !string.IsNullOrWhiteSpace(record.RuleSuppressionID)
797-
&& !string.Equals(ruleSuppression.RuleSuppressionID, record.RuleSuppressionID, StringComparison.OrdinalIgnoreCase))
783+
ruleSuppression = ruleSuppressions[ruleSuppressionIndex];
784+
suppressionCount = 0;
785+
786+
continue;
787+
}
788+
// at this point, the record is inside the interval
789+
else
798790
{
799-
results.Add(record);
800-
suppressionCount -= 1;
791+
// if the rule suppression id from the rule suppression is not null and the one from diagnostic record is not null
792+
// and they are they are not the same then we cannot ignore the record
793+
if (!string.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID) && !string.IsNullOrWhiteSpace(record.RuleSuppressionID)
794+
&& !string.Equals(ruleSuppression.RuleSuppressionID, record.RuleSuppressionID, StringComparison.OrdinalIgnoreCase))
795+
{
796+
suppressionCount -= 1;
797+
}
798+
// otherwise, we suppress the record, move on to the next.
799+
else
800+
{
801+
record.Suppression = ruleSuppression;
802+
}
801803
}
802-
// otherwise, we ignore the record, move on to the next.
803804
}
804805

805806
// important assumption: this point is reached only if we want to move to the next record
@@ -821,15 +822,6 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
821822

822823
record = diagnostics[recordIndex];
823824
}
824-
825-
// Add all unprocessed records to results
826-
while (recordIndex < diagnostics.Count)
827-
{
828-
results.Add(diagnostics[recordIndex]);
829-
recordIndex += 1;
830-
}
831-
832-
return results;
833825
}
834826

835827
#endregion

0 commit comments

Comments
 (0)