diff --git a/DevSkim-DotNet/Microsoft.DevSkim.CLI/Commands/AnalyzeCommand.cs b/DevSkim-DotNet/Microsoft.DevSkim.CLI/Commands/AnalyzeCommand.cs index 32088de0..af9a9cbc 100644 --- a/DevSkim-DotNet/Microsoft.DevSkim.CLI/Commands/AnalyzeCommand.cs +++ b/DevSkim-DotNet/Microsoft.DevSkim.CLI/Commands/AnalyzeCommand.cs @@ -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) diff --git a/DevSkim-DotNet/Microsoft.DevSkim/Comment.cs b/DevSkim-DotNet/Microsoft.DevSkim/Comment.cs index fe4e7fa2..85729ac7 100644 --- a/DevSkim-DotNet/Microsoft.DevSkim/Comment.cs +++ b/DevSkim-DotNet/Microsoft.DevSkim/Comment.cs @@ -20,5 +20,12 @@ internal class Comment [JsonPropertyName("suffix")] public string? Suffix { get; set; } + + + /// + /// Set if the language should always be considered comments + /// + [JsonPropertyName("always")] + public bool Always { get; set; } } } \ No newline at end of file diff --git a/DevSkim-DotNet/Microsoft.DevSkim/Language.cs b/DevSkim-DotNet/Microsoft.DevSkim/Language.cs index 5fe46b87..51e40e68 100644 --- a/DevSkim-DotNet/Microsoft.DevSkim/Language.cs +++ b/DevSkim-DotNet/Microsoft.DevSkim/Language.cs @@ -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)); + } + /// /// Gets comment preffix for given language /// diff --git a/DevSkim-DotNet/Microsoft.DevSkim/Resources/comments.json b/DevSkim-DotNet/Microsoft.DevSkim/Resources/comments.json index 689c0500..a800920b 100644 --- a/DevSkim-DotNet/Microsoft.DevSkim/Resources/comments.json +++ b/DevSkim-DotNet/Microsoft.DevSkim/Resources/comments.json @@ -21,6 +21,12 @@ "preffix": "/*", "suffix": "*/" }, + { + "language": [ + "plaintext" + ], + "always": true + }, { "language": [ "perl", diff --git a/DevSkim-DotNet/Microsoft.DevSkim/TextContainer.cs b/DevSkim-DotNet/Microsoft.DevSkim/TextContainer.cs index bdd9b3ed..caee4c39 100644 --- a/DevSkim-DotNet/Microsoft.DevSkim/TextContainer.cs +++ b/DevSkim-DotNet/Microsoft.DevSkim/TextContainer.cs @@ -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; } @@ -178,14 +179,21 @@ public bool ScopeMatch(IEnumerable 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;