diff --git a/checks.go b/checks.go index e675da6..bff8c1c 100644 --- a/checks.go +++ b/checks.go @@ -16,9 +16,7 @@ const ( var ( // List of valid sentence ending. // A sentence can be inside parenthesis, and therefore ends with parenthesis. - // A colon is a valid sentence ending, because it can be followed by a - // code example which is not checked. - lastChars = []string{".", "?", "!", ".)", "?)", "!)", ":"} + lastChars = []string{".", "?", "!", ".)", "?)", "!)", specialReplacer} // Special tags in comments like "// nolint:", or "// +k8s:". tags = regexp.MustCompile(`^\+?[a-z0-9]+:`) diff --git a/getters.go b/getters.go index f4180ac..f34b3c0 100644 --- a/getters.go +++ b/getters.go @@ -9,6 +9,11 @@ import ( "strings" ) +// specialReplacer is a replacer for some types of special lines in comments, +// which shouldn't be checked. For example, if comment ends with a block of +// code it should not necessarily have a period at the end. +const specialReplacer = "" + // getComments extracts comments from a file. func getComments(file *ast.File, fset *token.FileSet, scope Scope) ([]comment, error) { if len(file.Comments) == 0 { @@ -149,7 +154,8 @@ func getAllComments(file *ast.File, fset *token.FileSet, lines []string) []comme // getText extracts text from comment. If comment is a special block // (e.g., CGO code), a block of empty lines is returned. If comment contains // special lines (e.g., tags or indented code examples), they are replaced -// with an empty line. The result can be multiline. +// with `specialReplacer` to skip checks for it. +// The result can be multiline. func getText(comment *ast.CommentGroup) (s string) { if len(comment.List) == 1 && strings.HasPrefix(comment.List[0].Text, "/*") && @@ -167,7 +173,7 @@ func getText(comment *ast.CommentGroup) (s string) { } for _, line := range strings.Split(text, "\n") { if isSpecialLine(line) { - s += "\n" + s += specialReplacer + "\n" continue } if !isBlock { diff --git a/getters_test.go b/getters_test.go index 3d6964f..9f0b0cf 100644 --- a/getters_test.go +++ b/getters_test.go @@ -131,7 +131,22 @@ func TestGetText(t *testing.T) { {Text: "// +k8s:deepcopy-gen=package"}, {Text: "// +nolint: gosec"}, }}, - text: " One\n\n\n Two\n\n Three\n\n", + text: " One\n" + + "\n" + + "\n" + + " Two\n" + + "\n" + + " Three\n" + + "\n" + + "", + }, + { + name: "block of singleline comments with a url at the end", + comment: &ast.CommentGroup{List: []*ast.Comment{ + {Text: "// Read more"}, + {Text: "// http://example.com"}, + }}, + text: " Read more\n", }, { name: "cgo block", @@ -157,8 +172,8 @@ func TestGetText(t *testing.T) { }}, text: "\n" + "Example:\n" + - "\n" + - "\n" + + "\n" + + "\n" + "", }, { diff --git a/testdata/check/main.go b/testdata/check/main.go index 1ef3f28..d2afd73 100644 --- a/testdata/check/main.go +++ b/testdata/check/main.go @@ -126,3 +126,6 @@ func nonCapital() int { } // Comment with a URL - http://example.com/[PASS] + +// Multiline comment with a URL +// http://example.com/[PASS]