in DevSkim-VSCode-Plugin/server/src/utility_classes/suppressions.ts [294:410]
public static isFindingCommented(startPosition: number, documentContents: string, ruleID: string, langID : string,
isReviewRule: boolean, anySuppression : boolean = false): DevSkimSuppressionFinding
{
let XRegExp = require('xregexp');
let regex: RegExp = (isReviewRule) ? DevSkimSuppression.reviewRegEx : DevSkimSuppression.suppressionRegEx;
let line : string;
let returnFinding : DevSkimSuppressionFinding;
//its a little ugly to have a local function, but this is a convenient way of recalling this code repeatedly
//while not exposing it to any other function. This code checks to see if a suppression for the current issue
//is present in the line of code being analyzed. It also allows the use of the current Document without increasing
//its memory footprint, given that this code has access to the parent function scope as well
/**
* Check if the current finding is suppressed on the line of code provided
* @param line the line of code to inspect for a suppression
* @param startPosition where in the document the line starts (for calculating line number)
*
* @returns {DevSkimSuppressionFinding} a DevSkimSuppressionFinding - this object is used to highlight the DS#### in the suppression
* so that mousing over it provides details on what was suppressed
*/
let suppressionCheck = (line: string, startPosition: number) : DevSkimSuppressionFinding =>
{
let finding: DevSkimSuppressionFinding = Object.create(null);
finding.showSuppressionFinding = false;
//look for the suppression comment
let match = XRegExp.exec(line, regex);
if (match)
{
let suppressionIndex : number = match[0].indexOf(ruleID);
if (suppressionIndex > -1 || anySuppression)
{
let lineStart : number = DocumentUtilities.GetLineNumber(documentContents,startPosition);
suppressionIndex += match.index;
finding.suppressionRange = Range.create(lineStart, suppressionIndex, lineStart, suppressionIndex + ruleID.length);
finding.noRange = false;
if (!isReviewRule && match[2] !== undefined && match[2] != null && match[2].length > 0)
{
const untilDate: number = Date.UTC(match[3], match[4] - 1, match[5], 0, 0, 0, 0);
//we have a match of the rule, and haven't yet reached the "until" date, so ignore finding
//if the "until" date is less than the current time, the suppression has expired and we should not ignore
if (untilDate > Date.now() || anySuppression)
{
finding.showSuppressionFinding = true;
}
}
else //we have a match with the rule (or all rules), and no "until" date, so we should ignore this finding
{
finding.showSuppressionFinding = true;
}
}
else if (match[0].indexOf("all") > -1)
{
finding.showSuppressionFinding = true;
finding.noRange = true;
}
}
return finding;
};
let lineNumber : number = DocumentUtilities.GetLineNumber(documentContents,startPosition);
startPosition = DocumentUtilities.GetDocumentPosition(documentContents, lineNumber);
line = DocumentUtilities.GetPartialLine(documentContents, startPosition);
returnFinding = suppressionCheck(line, startPosition);
//we didn't find a suppression on the same line, but it might be a comment on the previous line
if(!returnFinding.showSuppressionFinding)
{
lineNumber--;
while(lineNumber > -1)
{
//get the start position of the current line we are looking for a comment in
startPosition = DocumentUtilities.GetDocumentPosition(documentContents, lineNumber);
//extract the line, and trim off the trailing space
// let match = XRegExp.exec(documentContents, DocumentUtilities.newlinePattern, startPosition);
//let secondLastMatch = (lineNumber -1 > -1) ? XRegExp.exec(documentContents, DocumentUtilities.newlinePattern, DocumentUtilities.GetDocumentPosition(documentContents, lineNumber -1)) : false;
//let lastMatch = (secondLastMatch) ? secondLastMatch.index : startPosition;
//let subDoc : string = documentContents.substr(0, (match) ? match.index : startPosition);
let subDoc : string = DocumentUtilities.GetLineFromPosition(documentContents, startPosition);
//check if the last line is a full line comment
if(SourceContext.IsLineCommented(langID, subDoc))
{
returnFinding = suppressionCheck(subDoc, startPosition);
if(returnFinding.showSuppressionFinding)
{
break;
}
}
//check if its part of a block comment
else if(SourceContext.IsLineBlockCommented(langID, documentContents, lineNumber))
{
let commentStart : number = SourceContext.GetStartOfLastBlockComment(langID,documentContents.substr(0,startPosition + subDoc.length));
let doc : string = DocumentUtilities.GetLineFromPosition(documentContents, commentStart).trim();
if(SourceContext.GetStartOfLastBlockComment(langID,doc) == 0)
{
returnFinding = suppressionCheck(subDoc, commentStart);
if(returnFinding.showSuppressionFinding)
{
break;
}
}
}
else
{
break;
}
lineNumber--;
}
}
return returnFinding;
}