Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
Rewrite edge case handling for placing multi line comments at the start of the line when the line ends with `\` and might be a multi-line continuation to avoid putting the comment inside the multiline string.
  • Loading branch information
gfs committed Aug 26, 2024
1 parent d6dd113 commit 52cf205
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions DevSkim-DotNet/Microsoft.DevSkim.CLI/Commands/SuppressionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.ApplicationInspector.RulesEngine;
using Microsoft.CodeAnalysis.Sarif;
using Microsoft.DevSkim.CLI.Options;
Expand Down Expand Up @@ -83,9 +82,10 @@ public int Run()
if (!File.Exists(potentialPath))
{
_logger.LogError($"{potentialPath} specified in sarif does not appear to exist on disk.");
continue;
}
string content = File.ReadAllText(potentialPath);
string[] theContent = SplitMyStringIntoPieces(content);
string[] theContent = SplitStringByLinesWithNewLines(content);
int currLine = 0;
StringBuilder sb = new StringBuilder();

Expand All @@ -95,29 +95,35 @@ public int Run()
{
Region region = issueRecord.PhysicalLocation.Region;
int zeroBasedStartLine = region.StartLine - 1;
bool isMultiline = theContent[zeroBasedStartLine].EndsWith(@"\");
string ignoreComment = DevSkimRuleProcessor.GenerateSuppressionByLanguage(region.SourceLanguage, issueRecord.RulesId, _opts.PreferMultiline || isMultiline, _opts.Duration, _opts.Reviewer, devSkimLanguages);
string originalLine = theContent[zeroBasedStartLine];
int lineEndPosition = FindNewLine(originalLine);
// If line ends with `\` it may have continuation on the next line,
// so put the comment at the line start and use multiline format
bool forceMultiLine = lineEndPosition >= 0 && originalLine[..lineEndPosition].EndsWith(@"\");
string ignoreComment = DevSkimRuleProcessor.GenerateSuppressionByLanguage(region.SourceLanguage, issueRecord.RulesId, _opts.PreferMultiline || forceMultiLine, _opts.Duration, _opts.Reviewer, devSkimLanguages);
if (!string.IsNullOrEmpty(ignoreComment))
{
foreach (string line in theContent[currLine..zeroBasedStartLine])
{
sb.Append($"{line}");
}

string originalLine = theContent[zeroBasedStartLine];
int lineEndPosition = FindNewLine(originalLine);
// Use the content then the ignore comment then the original newline characters from the extra array
if (lineEndPosition != -1)

if (forceMultiLine)
{
sb.Append(isMultiline
? $"{ignoreComment}{theContent[zeroBasedStartLine]}"
: $"{originalLine[0..lineEndPosition]} {ignoreComment}{originalLine[lineEndPosition..]}");
sb.Append($"{ignoreComment} {originalLine}");
}
else
{
sb.Append(isMultiline
? $"{ignoreComment}{theContent[zeroBasedStartLine]}"
: $"{theContent[zeroBasedStartLine]} {ignoreComment}");
// Use the content then the ignore comment then the original newline characters from the extra array
if (lineEndPosition != -1)
{
sb.Append($"{originalLine[0..lineEndPosition]} {ignoreComment}{originalLine[lineEndPosition..]}");
}
// No new line so we can just use the line as is
else
{
sb.Append($"{originalLine} {ignoreComment}");
}
}
}

Expand Down Expand Up @@ -172,7 +178,7 @@ private int FindNewLine(string originalLine)
/// </summary>
/// <param name="content"></param>
/// <returns>Array of strings, each containing the content of one line from the input string including any newline characters from that line</returns>
private string[] SplitMyStringIntoPieces(string content)
private string[] SplitStringByLinesWithNewLines(string content)
{
List<string> lines = new();
int curPos = 0;
Expand Down

0 comments on commit 52cf205

Please sign in to comment.