Skip to content

Commit

Permalink
Add Support for languages that are never code (microsoft#337)
Browse files Browse the repository at this point in the history
* Fix path output handling for sarifd when providing a specifc filename to scan

* Add support for "always commented" languages

Curently this is just .txt files
  • Loading branch information
gfs authored Oct 21, 2021
1 parent 3f187ac commit 6bf86c6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ static string TryRelativizePath(string parentPath, string childPath)
{
try
{
if (parentPath == childPath)
{
if (File.Exists(parentPath))
{
return Path.GetFileName(childPath);
}
}
return Path.GetRelativePath(parentPath, childPath) ?? childPath;
}
catch (Exception)
Expand Down
7 changes: 7 additions & 0 deletions DevSkim-DotNet/Microsoft.DevSkim/Comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@ internal class Comment

[JsonPropertyName("suffix")]
public string? Suffix { get; set; }


/// <summary>
/// Set if the language should always be considered comments
/// </summary>
[JsonPropertyName("always")]
public bool Always { get; set; }
}
}
5 changes: 5 additions & 0 deletions DevSkim-DotNet/Microsoft.DevSkim/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public static string GetCommentInline(string language)
return result;
}

public static bool IsAlwaysCommented(string language)
{
return Instance.Comments.Any(x => x.Always && x.Languages.Contains(language));
}

/// <summary>
/// Gets comment preffix for given language
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions DevSkim-DotNet/Microsoft.DevSkim/Resources/comments.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
"preffix": "/*",
"suffix": "*/"
},
{
"language": [
"plaintext"
],
"always": true
},
{
"language": [
"perl",
Expand Down
10 changes: 9 additions & 1 deletion DevSkim-DotNet/Microsoft.DevSkim/TextContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public TextContainer(string content, string language, int lineNumber = 0)
prefix = DevSkim.Language.GetCommentPrefix(Language);
suffix = DevSkim.Language.GetCommentSuffix(Language);
inline = DevSkim.Language.GetCommentInline(Language);
alwaysCommented = DevSkim.Language.IsAlwaysCommented(Language);
}

public string FullContent { get; }
Expand Down Expand Up @@ -178,14 +179,21 @@ public bool ScopeMatch(IEnumerable<PatternScope> patterns, Boundary boundary)
{
return false;
}
if (patterns.Contains(PatternScope.All) || string.IsNullOrEmpty(prefix))
if (patterns.Contains(PatternScope.All))
{
return true;
}
if (alwaysCommented)
{
return patterns.Contains(PatternScope.Comment);
}
bool isInComment = IsBetween(FullContent, boundary.Index, prefix, suffix, inline);

return !(isInComment && !patterns.Contains(PatternScope.Comment));
}

private string inline;
private bool alwaysCommented;
private string prefix;
private string suffix;

Expand Down

0 comments on commit 6bf86c6

Please sign in to comment.