Skip to content

Commit 75a06a6

Browse files
committed
Suppressions-changes
1 parent 06f94fb commit 75a06a6

11 files changed

+524
-97
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ public SwitchParameter SuppressedOnly
167167
}
168168
private bool suppressedOnly;
169169

170+
/// <summary>
171+
/// ShowAll: Show the suppressed and non-suppressed message
172+
/// </summary>
173+
[Parameter(Mandatory = false)]
174+
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
175+
public SwitchParameter IncludeSuppressions
176+
{
177+
get { return includeSuppressions; }
178+
set { includeSuppressions = value; }
179+
}
180+
private bool includeSuppressions;
181+
170182
/// <summary>
171183
/// Resolves rule violations automatically where possible.
172184
/// </summary>
@@ -341,7 +353,8 @@ protected override void BeginProcessing()
341353
this.excludeRule,
342354
this.severity,
343355
combRulePaths == null || combIncludeDefaultRules,
344-
this.suppressedOnly);
356+
this.suppressedOnly,
357+
this.includeSuppressions);
345358
}
346359

347360
/// <summary>

Engine/Generic/RuleSuppression.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ public string Justification
9797
set;
9898
}
9999

100+
/// <summary>
101+
/// Returns the kind of the suppression
102+
/// </summary>
103+
public string Kind
104+
{
105+
get;
106+
set;
107+
}
108+
100109
private static HashSet<string> scopeSet;
101110

102111
/// <summary>

Engine/Generic/SuppressedRecord.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System.Collections.Generic;
5+
46
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
57
{
68
/// <summary>
@@ -11,20 +13,28 @@ public class SuppressedRecord : DiagnosticRecord
1113
/// <summary>
1214
/// The rule suppression of this record
1315
/// </summary>
14-
public RuleSuppression Suppression
16+
public IList<RuleSuppression> Suppressions
1517
{
16-
get;
17-
set;
18+
get
19+
{
20+
if (suppressions == null) suppressions = new List<RuleSuppression>();
21+
return suppressions;
22+
}
23+
set
24+
{
25+
suppressions = value;
26+
}
1827
}
28+
private IList<RuleSuppression> suppressions;
1929

2030
/// <summary>
2131
/// Creates a suppressed record based on a diagnostic record and the rule suppression
2232
/// </summary>
2333
/// <param name="record"></param>
2434
/// <param name="Suppression"></param>
25-
public SuppressedRecord(DiagnosticRecord record, RuleSuppression suppression)
35+
public SuppressedRecord(DiagnosticRecord record, IList<RuleSuppression> suppressions)
2636
{
27-
Suppression = suppression;
37+
Suppressions = suppressions;
2838
if (record != null)
2939
{
3040
RuleName = record.RuleName;

Engine/Helper.cs

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ public bool IsDscResourceClassBased(ScriptBlockAst ast)
518518
return false;
519519
}
520520

521-
#if !(PSV3||PSV4)
521+
#if !(PSV3||PSV4)
522522

523523
List<string> dscResourceFunctionNames = new List<string>(new string[] { "Test", "Get", "Set" });
524524

@@ -534,7 +534,7 @@ item is TypeDefinitionAst
534534
return true;
535535
}
536536

537-
#endif
537+
#endif
538538

539539
return false;
540540
}
@@ -1074,7 +1074,7 @@ public string GetTypeFromMemberExpressionAst(MemberExpressionAst memberAst, Ast
10741074

10751075
#else
10761076

1077-
return GetTypeFromMemberExpressionAstHelper(memberAst, psClass, details);
1077+
return GetTypeFromMemberExpressionAstHelper(memberAst, psClass, details);
10781078

10791079
#endif
10801080
}
@@ -1349,49 +1349,48 @@ public Tuple<List<SuppressedRecord>, List<DiagnosticRecord>> SuppressRule(
13491349

13501350
List<RuleSuppression> ruleSuppressions = ruleSuppressionsDict[ruleName];
13511351
var offsetArr = GetOffsetArray(diagnostics);
1352-
int recordIndex = 0;
1353-
int startRecord = 0;
1354-
bool[] suppressed = new bool[diagnostics.Count];
1355-
foreach (RuleSuppression ruleSuppression in ruleSuppressions)
1356-
{
1357-
int suppressionCount = 0;
1358-
while (startRecord < diagnostics.Count
1359-
// && diagnostics[startRecord].Extent.StartOffset < ruleSuppression.StartOffset)
1360-
// && diagnostics[startRecord].Extent.StartLineNumber < ruleSuppression.st)
1361-
&& offsetArr[startRecord] != null && offsetArr[startRecord].Item1 < ruleSuppression.StartOffset)
1362-
{
1363-
startRecord += 1;
1364-
}
1365-
1366-
// at this point, start offset of startRecord is greater or equals to rulesuppression.startoffset
1367-
recordIndex = startRecord;
1352+
bool[] applied = new bool[ruleSuppressions.Count];
13681353

1369-
while (recordIndex < diagnostics.Count)
1354+
for (int i = 0; i < diagnostics.Count; i++)
1355+
{
1356+
DiagnosticRecord record = diagnostics[i];
1357+
var curOffset = offsetArr[i];
1358+
List<RuleSuppression> suppressions = new List<RuleSuppression>();
1359+
for (int ruleSuppressionIndex = 0; ruleSuppressionIndex < ruleSuppressions.Count; ruleSuppressionIndex++)
13701360
{
1371-
DiagnosticRecord record = diagnostics[recordIndex];
1372-
var curOffset = offsetArr[recordIndex];
1373-
1374-
//if (record.Extent.EndOffset > ruleSuppression.EndOffset)
1375-
if (curOffset != null && curOffset.Item2 > ruleSuppression.EndOffset)
1361+
RuleSuppression ruleSuppression = ruleSuppressions[ruleSuppressionIndex];
1362+
//if (record.Extent.StartOffset < ruleSuppression.StartOffset||record.Extent.EndOffset > ruleSuppression.EndOffset)
1363+
if (curOffset != null && (curOffset.Item1 < ruleSuppression.StartOffset || curOffset.Item2 > ruleSuppression.EndOffset))
13761364
{
1377-
break;
1365+
continue;
13781366
}
13791367

13801368
// we suppress if there is no suppression id or if there is suppression id and it matches
13811369
if (string.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID)
13821370
|| (!String.IsNullOrWhiteSpace(record.RuleSuppressionID) &&
13831371
string.Equals(ruleSuppression.RuleSuppressionID, record.RuleSuppressionID, StringComparison.OrdinalIgnoreCase)))
13841372
{
1385-
suppressed[recordIndex] = true;
1386-
suppressedRecords.Add(new SuppressedRecord(record, ruleSuppression));
1387-
suppressionCount += 1;
1373+
ruleSuppression.Kind = "InSource";
1374+
suppressions.Add(ruleSuppression);
1375+
applied[ruleSuppressionIndex] = true;
13881376
}
1377+
}
13891378

1390-
recordIndex += 1;
1379+
if (suppressions.Count()!=0)
1380+
{
1381+
suppressedRecords.Add(new SuppressedRecord(record, suppressions));
13911382
}
1383+
else
1384+
{
1385+
unSuppressedRecords.Add(diagnostics[i]);
1386+
}
1387+
}
13921388

1393-
// If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
1394-
if (!String.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID) && suppressionCount == 0)
1389+
// If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
1390+
for (int ruleSuppressionIndex = 0; ruleSuppressionIndex < ruleSuppressions.Count; ruleSuppressionIndex++)
1391+
{
1392+
RuleSuppression ruleSuppression = ruleSuppressions[ruleSuppressionIndex];
1393+
if ((!String.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID)) && !applied[ruleSuppressionIndex])
13951394
{
13961395
// checks whether are given a string or a file path
13971396
if (String.IsNullOrWhiteSpace(diagnostics.First().Extent.File))
@@ -1409,14 +1408,6 @@ public Tuple<List<SuppressedRecord>, List<DiagnosticRecord>> SuppressRule(
14091408
}
14101409
}
14111410

1412-
for (int i = 0; i < suppressed.Length; i += 1)
1413-
{
1414-
if (!suppressed[i])
1415-
{
1416-
unSuppressedRecords.Add(diagnostics[i]);
1417-
}
1418-
}
1419-
14201411
return result;
14211412
}
14221413

@@ -2036,7 +2027,7 @@ private static InvalidDataException CreateInvalidDataException(IScriptExtent ext
20362027
}
20372028

20382029

2039-
#endregion Methods
2030+
#endregion Methods
20402031
}
20412032

20422033

0 commit comments

Comments
 (0)