Skip to content

Commit f1086f5

Browse files
author
quoctruong
committed
Format suppressed messages output
1 parent 284f527 commit f1086f5

File tree

5 files changed

+85
-54
lines changed

5 files changed

+85
-54
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ public SwitchParameter Recurse
104104
/// </summary>
105105
[Parameter(Mandatory = false)]
106106
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
107-
public SwitchParameter ShowSuppressed
107+
public SwitchParameter SuppressedOnly
108108
{
109-
get { return showSuppressed; }
110-
set { showSuppressed = value; }
109+
get { return suppressedOnly; }
110+
set { suppressedOnly = value; }
111111
}
112-
private bool showSuppressed;
112+
private bool suppressedOnly;
113113

114114
#endregion Parameters
115115

@@ -268,7 +268,7 @@ private void AnalyzeFile(string filePath)
268268
Token[] tokens = null;
269269
ParseError[] errors = null;
270270
List<DiagnosticRecord> diagnostics = new List<DiagnosticRecord>();
271-
List<DiagnosticRecord> suppressed = new List<DiagnosticRecord>();
271+
List<SuppressedRecord> suppressed = new List<SuppressedRecord>();
272272

273273
// Use a List of KVP rather than dictionary, since for a script containing inline functions with same signature, keys clash
274274
List<KeyValuePair<CommandInfo, IScriptExtent>> cmdInfoTable = new List<KeyValuePair<CommandInfo, IScriptExtent>>();
@@ -341,10 +341,9 @@ private void AnalyzeFile(string filePath)
341341
// We want the Engine to continue functioning even if one or more Rules throws an exception
342342
try
343343
{
344-
var records = scriptRule.AnalyzeScript(ast, filePath).ToList();
345-
Helper.Instance.SuppressRule(scriptRule.GetName(), ruleSuppressions, records);
346-
diagnostics.AddRange(records.Where(record => record.Suppression == null));
347-
suppressed.AddRange(records.Where(record => record.Suppression != null));
344+
var records = Helper.Instance.SuppressRule(scriptRule.GetName(), ruleSuppressions, scriptRule.AnalyzeScript(ast, filePath).ToList());
345+
diagnostics.AddRange(records.Item2);
346+
suppressed.AddRange(records.Item1);
348347
}
349348
catch (Exception scriptRuleException)
350349
{
@@ -372,10 +371,9 @@ private void AnalyzeFile(string filePath)
372371
// We want the Engine to continue functioning even if one or more Rules throws an exception
373372
try
374373
{
375-
var records = tokenRule.AnalyzeTokens(tokens, fileName).ToList();
376-
Helper.Instance.SuppressRule(tokenRule.GetName(), ruleSuppressions, records);
377-
diagnostics.AddRange(records.Where(record => record.Suppression == null));
378-
suppressed.AddRange(records.Where(record => record.Suppression != null));
374+
var records = Helper.Instance.SuppressRule(tokenRule.GetName(), ruleSuppressions, tokenRule.AnalyzeTokens(tokens, filePath).ToList());
375+
diagnostics.AddRange(records.Item2);
376+
suppressed.AddRange(records.Item1);
379377
}
380378
catch (Exception tokenRuleException)
381379
{
@@ -403,10 +401,9 @@ private void AnalyzeFile(string filePath)
403401
// We want the Engine to continue functioning even if one or more Rules throws an exception
404402
try
405403
{
406-
var records = dscResourceRule.AnalyzeDSCClass(ast, filePath).ToList();
407-
Helper.Instance.SuppressRule(dscResourceRule.GetName(), ruleSuppressions, records);
408-
diagnostics.AddRange(records.Where(record => record.Suppression == null));
409-
suppressed.AddRange(records.Where(record => record.Suppression != null));
404+
var records = Helper.Instance.SuppressRule(dscResourceRule.GetName(), ruleSuppressions, dscResourceRule.AnalyzeDSCClass(ast, filePath).ToList());
405+
diagnostics.AddRange(records.Item2);
406+
suppressed.AddRange(records.Item1);
410407
}
411408
catch (Exception dscResourceRuleException)
412409
{
@@ -448,10 +445,9 @@ private void AnalyzeFile(string filePath)
448445
// We want the Engine to continue functioning even if one or more Rules throws an exception
449446
try
450447
{
451-
var records = dscResourceRule.AnalyzeDSCResource(ast, filePath).ToList();
452-
Helper.Instance.SuppressRule(dscResourceRule.GetName(), ruleSuppressions, records);
453-
diagnostics.AddRange(records.Where(record => record.Suppression == null));
454-
suppressed.AddRange(records.Where(record => record.Suppression != null));
448+
var records = Helper.Instance.SuppressRule(dscResourceRule.GetName(), ruleSuppressions, dscResourceRule.AnalyzeDSCResource(ast, filePath).ToList());
449+
diagnostics.AddRange(records.Item2);
450+
suppressed.AddRange(records.Item1);
455451
}
456452
catch (Exception dscResourceRuleException)
457453
{
@@ -509,7 +505,7 @@ private void AnalyzeFile(string filePath)
509505
//Output through loggers
510506
foreach (ILogger logger in ScriptAnalyzer.Instance.Loggers)
511507
{
512-
if (ShowSuppressed)
508+
if (SuppressedOnly)
513509
{
514510
foreach (DiagnosticRecord suppressRecord in suppressed)
515511
{

Engine/Generic/DiagnosticRecord.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public class DiagnosticRecord
1919
private DiagnosticSeverity severity;
2020
private string scriptName;
2121
private string ruleSuppressionId;
22-
private RuleSuppression suppression;
2322

2423
/// <summary>
2524
/// Represents a string from the rule about why this diagnostic was created.
@@ -30,22 +29,6 @@ public string Message
3029
set { message = value; }
3130
}
3231

33-
/// <summary>
34-
/// The Rule Suppression that applies to this record (null if none is applied)
35-
/// </summary>
36-
public RuleSuppression Suppression
37-
{
38-
get { return suppression; }
39-
set
40-
{
41-
suppression = value;
42-
if (suppression != null)
43-
{
44-
Message = suppression.Justification;
45-
}
46-
}
47-
}
48-
4932
/// <summary>
5033
/// Represents a span of text in a script.
5134
/// </summary>

Engine/Generic/SuppressedRecord.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
8+
{
9+
/// <summary>
10+
/// Represents a suppressed diagnostic record
11+
/// </summary>
12+
public class SuppressedRecord : DiagnosticRecord
13+
{
14+
/// <summary>
15+
/// The rule suppression of this record
16+
/// </summary>
17+
public RuleSuppression Suppression
18+
{
19+
get;
20+
set;
21+
}
22+
23+
/// <summary>
24+
/// Creates a suppressed record based on a diagnostic record and the rule suppression
25+
/// </summary>
26+
/// <param name="record"></param>
27+
/// <param name="Suppression"></param>
28+
public SuppressedRecord(DiagnosticRecord record, RuleSuppression suppression)
29+
{
30+
Suppression = suppression;
31+
if (record != null)
32+
{
33+
RuleName = record.RuleName;
34+
Message = record.Message;
35+
Extent = record.Extent;
36+
Severity = record.Severity;
37+
ScriptName = record.ScriptName;
38+
RuleSuppressionID = record.RuleSuppressionID;
39+
}
40+
}
41+
}
42+
}

Engine/Helper.cs

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

718718
/// <summary>
719-
/// Suppress the rules from the diagnostic records list by attaching rule suppression to the record that is suppressed
719+
/// Suppress the rules from the diagnostic records list.
720+
/// Returns a list of suppressed records as well as the ones that are not suppressed
720721
/// </summary>
721722
/// <param name="ruleSuppressions"></param>
722723
/// <param name="diagnostics"></param>
723-
public void SuppressRule(string ruleName, Dictionary<string, List<RuleSuppression>> ruleSuppressionsDict, List<DiagnosticRecord> diagnostics)
724+
public Tuple<List<SuppressedRecord>, List<DiagnosticRecord>> SuppressRule(string ruleName, Dictionary<string, List<RuleSuppression>> ruleSuppressionsDict, List<DiagnosticRecord> diagnostics)
724725
{
726+
List<SuppressedRecord> suppressedRecords = new List<SuppressedRecord>();
727+
List<DiagnosticRecord> unSuppressedRecords = new List<DiagnosticRecord>();
728+
Tuple<List<SuppressedRecord>, List<DiagnosticRecord>> result = Tuple.Create(suppressedRecords, unSuppressedRecords);
729+
725730
if (ruleSuppressionsDict == null || !ruleSuppressionsDict.ContainsKey(ruleName)
726731
|| diagnostics == null || diagnostics.Count == 0)
727732
{
728-
return;
733+
return result;
729734
}
730735

731736
List<RuleSuppression> ruleSuppressions = ruleSuppressionsDict[ruleName];
732737

733738
if (ruleSuppressions.Count == 0)
734739
{
735-
return;
740+
return result;
736741
}
737742

738743
int recordIndex = 0;
@@ -794,14 +799,19 @@ public void SuppressRule(string ruleName, Dictionary<string, List<RuleSuppressio
794799
&& !string.Equals(ruleSuppression.RuleSuppressionID, record.RuleSuppressionID, StringComparison.OrdinalIgnoreCase))
795800
{
796801
suppressionCount -= 1;
802+
unSuppressedRecords.Add(record);
797803
}
798804
// otherwise, we suppress the record, move on to the next.
799805
else
800806
{
801-
record.Suppression = ruleSuppression;
807+
suppressedRecords.Add(new SuppressedRecord(record, ruleSuppression));
802808
}
803809
}
804810
}
811+
else
812+
{
813+
unSuppressedRecords.Add(record);
814+
}
805815

806816
// important assumption: this point is reached only if we want to move to the next record
807817
recordIndex += 1;
@@ -822,6 +832,14 @@ public void SuppressRule(string ruleName, Dictionary<string, List<RuleSuppressio
822832

823833
record = diagnostics[recordIndex];
824834
}
835+
836+
while (recordIndex < diagnostics.Count)
837+
{
838+
unSuppressedRecords.Add(diagnostics[recordIndex]);
839+
recordIndex += 1;
840+
}
841+
842+
return result;
825843
}
826844

827845
#endregion
@@ -910,15 +928,6 @@ public object VisitScriptBlock(ScriptBlockAst scriptBlockAst)
910928
return null;
911929
}
912930

913-
/// <summary>
914-
/// Do nothing
915-
/// </summary>
916-
/// <param name="baseCtorInvokeMemberExpressionAst"></param>
917-
/// <returns></returns>
918-
public object VisitBaseCtorInvokeMemberExpression(BaseCtorInvokeMemberExpressionAst baseCtorInvokeMemberExpressionAst)
919-
{
920-
return null;
921-
}
922931

923932
/// <summary>
924933
/// Do nothing

Engine/ScriptAnalyzerEngine.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Compile Include="Commands\InvokeScriptAnalyzerCommand.cs" />
5353
<Compile Include="Generic\AvoidCmdletGeneric.cs" />
5454
<Compile Include="Generic\AvoidParameterGeneric.cs" />
55+
<Compile Include="Generic\SuppressedRecord.cs" />
5556
<Compile Include="Generic\DiagnosticRecord.cs" />
5657
<Compile Include="Generic\ExternalRule.cs" />
5758
<Compile Include="Generic\IDSCResourceRule.cs" />
@@ -99,4 +100,4 @@ copy /y "$(ProjectDir)\*.ps1xml" "$(SolutionDir)$(SolutionName)"
99100
copy /y "$(ProjectDir)\*.psd1" "$(SolutionDir)$(SolutionName)"
100101
copy /y "$(TargetPath)" "$(SolutionDir)$(SolutionName)"</PostBuildEvent>
101102
</PropertyGroup>
102-
</Project>
103+
</Project>

0 commit comments

Comments
 (0)