Skip to content

Commit 284f527

Browse files
author
Quoc Truong
committed
Separate suppressed and non-suppressed records
1 parent fbbe414 commit 284f527

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ public SwitchParameter Recurse
9999
}
100100
private bool recurse;
101101

102+
/// <summary>
103+
/// ShowSuppressed: Show the suppressed message
104+
/// </summary>
105+
[Parameter(Mandatory = false)]
106+
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
107+
public SwitchParameter ShowSuppressed
108+
{
109+
get { return showSuppressed; }
110+
set { showSuppressed = value; }
111+
}
112+
private bool showSuppressed;
113+
102114
#endregion Parameters
103115

104116
#region Private Members
@@ -256,6 +268,7 @@ private void AnalyzeFile(string filePath)
256268
Token[] tokens = null;
257269
ParseError[] errors = null;
258270
List<DiagnosticRecord> diagnostics = new List<DiagnosticRecord>();
271+
List<DiagnosticRecord> suppressed = new List<DiagnosticRecord>();
259272

260273
// Use a List of KVP rather than dictionary, since for a script containing inline functions with same signature, keys clash
261274
List<KeyValuePair<CommandInfo, IScriptExtent>> cmdInfoTable = new List<KeyValuePair<CommandInfo, IScriptExtent>>();
@@ -331,6 +344,7 @@ private void AnalyzeFile(string filePath)
331344
var records = scriptRule.AnalyzeScript(ast, filePath).ToList();
332345
Helper.Instance.SuppressRule(scriptRule.GetName(), ruleSuppressions, records);
333346
diagnostics.AddRange(records.Where(record => record.Suppression == null));
347+
suppressed.AddRange(records.Where(record => record.Suppression != null));
334348
}
335349
catch (Exception scriptRuleException)
336350
{
@@ -361,6 +375,7 @@ private void AnalyzeFile(string filePath)
361375
var records = tokenRule.AnalyzeTokens(tokens, fileName).ToList();
362376
Helper.Instance.SuppressRule(tokenRule.GetName(), ruleSuppressions, records);
363377
diagnostics.AddRange(records.Where(record => record.Suppression == null));
378+
suppressed.AddRange(records.Where(record => record.Suppression != null));
364379
}
365380
catch (Exception tokenRuleException)
366381
{
@@ -391,6 +406,7 @@ private void AnalyzeFile(string filePath)
391406
var records = dscResourceRule.AnalyzeDSCClass(ast, filePath).ToList();
392407
Helper.Instance.SuppressRule(dscResourceRule.GetName(), ruleSuppressions, records);
393408
diagnostics.AddRange(records.Where(record => record.Suppression == null));
409+
suppressed.AddRange(records.Where(record => record.Suppression != null));
394410
}
395411
catch (Exception dscResourceRuleException)
396412
{
@@ -435,6 +451,7 @@ private void AnalyzeFile(string filePath)
435451
var records = dscResourceRule.AnalyzeDSCResource(ast, filePath).ToList();
436452
Helper.Instance.SuppressRule(dscResourceRule.GetName(), ruleSuppressions, records);
437453
diagnostics.AddRange(records.Where(record => record.Suppression == null));
454+
suppressed.AddRange(records.Where(record => record.Suppression != null));
438455
}
439456
catch (Exception dscResourceRuleException)
440457
{
@@ -492,9 +509,19 @@ private void AnalyzeFile(string filePath)
492509
//Output through loggers
493510
foreach (ILogger logger in ScriptAnalyzer.Instance.Loggers)
494511
{
495-
foreach (DiagnosticRecord diagnostic in diagnostics)
512+
if (ShowSuppressed)
496513
{
497-
logger.LogMessage(diagnostic, this);
514+
foreach (DiagnosticRecord suppressRecord in suppressed)
515+
{
516+
logger.LogObject(suppressRecord, this);
517+
}
518+
}
519+
else
520+
{
521+
foreach (DiagnosticRecord diagnostic in diagnostics)
522+
{
523+
logger.LogObject(diagnostic, this);
524+
}
498525
}
499526
}
500527
}

Engine/Generic/DiagnosticRecord.cs

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

2324
/// <summary>
2425
/// Represents a string from the rule about why this diagnostic was created.
@@ -34,8 +35,15 @@ public string Message
3435
/// </summary>
3536
public RuleSuppression Suppression
3637
{
37-
get;
38-
set;
38+
get { return suppression; }
39+
set
40+
{
41+
suppression = value;
42+
if (suppression != null)
43+
{
44+
Message = suppression.Justification;
45+
}
46+
}
3947
}
4048

4149
/// <summary>

Engine/Generic/ILogger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public interface ILogger
1515
/// <summary>
1616
/// LogMessage: Logs the given diagnostic, using the command for Write methods if needed.
1717
/// </summary>
18-
/// <param name="diagnostic">The DiagnosticRecord to be logged.</param>
18+
/// <param name="obj">The object to be logged.</param>
1919
/// <param name="command">The InvokePSScriptAnalyzerCommand that this logger is running off of.</param>
20-
void LogMessage(DiagnosticRecord diagnostic, InvokeScriptAnalyzerCommand command);
20+
void LogObject(Object obj, InvokeScriptAnalyzerCommand command);
2121

2222
/// <summary>
2323
/// GetName: Retrieves the name of the logger.

Engine/Loggers/WriteObjectsLogger.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ public class WriteObjectsLogger : ILogger
3030
#region Methods
3131

3232
/// <summary>
33-
/// LogMessage: Logs the given diagnostic though WriteObject.
33+
/// LogObject: Logs the given object though WriteObject.
3434
/// </summary>
35-
/// <param name="diagnostic">The diagnostic to be logged</param>
35+
/// <param name="obj">The object to be logged</param>
3636
/// <param name="command">The Invoke-PSLint command this logger is running through</param>
37-
public void LogMessage(DiagnosticRecord diagnostic, InvokeScriptAnalyzerCommand command)
37+
public void LogObject(Object obj, InvokeScriptAnalyzerCommand command)
3838
{
3939
if (command == null)
4040
{
4141
throw new ArgumentNullException("command");
4242
}
43-
if (diagnostic == null)
43+
if (obj == null)
4444
{
4545
throw new ArgumentNullException("diagnostic");
4646
}
47-
command.WriteObject(diagnostic);
47+
command.WriteObject(obj);
4848
}
4949

5050
/// <summary>

0 commit comments

Comments
 (0)