Skip to content

Commit 518287a

Browse files
author
Kapil Borle
committed
Fix rule suppression caused by invalid offsets
Rule suppression works by examining the extent offsets of DiagnosticRecords. If the offset are set to 0, rule suppression doesn't work. They are set to 0 when we create the extents using the ScriptExtent constructor which sets the StartOffset and EndOffset properties to 0.
1 parent 0e287f7 commit 518287a

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

Engine/Helper.cs

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,15 +1310,17 @@ public Tuple<List<SuppressedRecord>, List<DiagnosticRecord>> SuppressRule(
13101310
}
13111311

13121312
List<RuleSuppression> ruleSuppressions = ruleSuppressionsDict[ruleName];
1313-
1313+
var offsetArr = GetOffsetArray(diagnostics);
13141314
int recordIndex = 0;
13151315
int startRecord = 0;
13161316
bool[] suppressed = new bool[diagnostics.Count];
1317-
13181317
foreach (RuleSuppression ruleSuppression in ruleSuppressions)
13191318
{
13201319
int suppressionCount = 0;
1321-
while (startRecord < diagnostics.Count && diagnostics[startRecord].Extent.StartOffset < ruleSuppression.StartOffset)
1320+
while (startRecord < diagnostics.Count
1321+
// && diagnostics[startRecord].Extent.StartOffset < ruleSuppression.StartOffset)
1322+
// && diagnostics[startRecord].Extent.StartLineNumber < ruleSuppression.st)
1323+
&& offsetArr[startRecord].Item1 < ruleSuppression.StartOffset)
13221324
{
13231325
startRecord += 1;
13241326
}
@@ -1329,8 +1331,10 @@ public Tuple<List<SuppressedRecord>, List<DiagnosticRecord>> SuppressRule(
13291331
while (recordIndex < diagnostics.Count)
13301332
{
13311333
DiagnosticRecord record = diagnostics[recordIndex];
1334+
var curOffset = offsetArr[recordIndex];
13321335

1333-
if (record.Extent.EndOffset > ruleSuppression.EndOffset)
1336+
//if (record.Extent.EndOffset > ruleSuppression.EndOffset)
1337+
if (curOffset.Item2 > ruleSuppression.EndOffset)
13341338
{
13351339
break;
13361340
}
@@ -1378,6 +1382,55 @@ public Tuple<List<SuppressedRecord>, List<DiagnosticRecord>> SuppressRule(
13781382
return result;
13791383
}
13801384

1385+
private Tuple<int,int>[] GetOffsetArray(List<DiagnosticRecord> diagnostics)
1386+
{
1387+
Func<int,int,Tuple<int,int>> GetTuple = (x, y) => new Tuple<int, int>(x, y);
1388+
Func<Tuple<int, int>> GetDefaultTuple = () => GetTuple(0, 0);
1389+
1390+
var offsets = new Tuple<int, int>[diagnostics.Count];
1391+
for (int k = 0; k < diagnostics.Count; k++)
1392+
{
1393+
var ext = diagnostics[k].Extent;
1394+
if (ext.StartOffset == 0 && ext.EndOffset == 0)
1395+
{
1396+
// check if line and column number do not correspond to 0 offsets
1397+
if (ext.StartLineNumber == 1
1398+
&& ext.StartColumnNumber == 1
1399+
&& ext.EndLineNumber == 1
1400+
&& ext.EndColumnNumber == 1)
1401+
{
1402+
offsets[k] = GetDefaultTuple();
1403+
}
1404+
else
1405+
{
1406+
// created using the ScriptExtent constructor, which sets
1407+
// StartOffset and EndOffset to 0
1408+
// find the token the corresponding start line and column number
1409+
var startToken = Tokens.Where(x
1410+
=> x.Extent.StartLineNumber == ext.StartLineNumber
1411+
&& x.Extent.StartColumnNumber == ext.StartColumnNumber)
1412+
.FirstOrDefault();
1413+
if (startToken == null)
1414+
{
1415+
offsets[k] = GetDefaultTuple();
1416+
continue;
1417+
}
1418+
var endToken = Tokens.Where(x
1419+
=> x.Extent.EndLineNumber == ext.EndLineNumber
1420+
&& x.Extent.EndColumnNumber == ext.EndColumnNumber)
1421+
.FirstOrDefault();
1422+
if (endToken == null)
1423+
{
1424+
offsets[k] = GetDefaultTuple();
1425+
continue;
1426+
}
1427+
offsets[k] = GetTuple(startToken.Extent.StartOffset, endToken.Extent.EndOffset);
1428+
}
1429+
}
1430+
}
1431+
return offsets;
1432+
}
1433+
13811434
public static string[] ProcessCustomRulePaths(string[] rulePaths, SessionState sessionState, bool recurse = false)
13821435
{
13831436
//if directory is given, list all the psd1 files

0 commit comments

Comments
 (0)