Skip to content

Commit

Permalink
Fix checking URLs at the end of comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed Nov 30, 2020
1 parent e3bedc4 commit fd30ce2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
4 changes: 1 addition & 3 deletions checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]+:`)
Expand Down
10 changes: 8 additions & 2 deletions getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<godotSpecialReplacer>"

// getComments extracts comments from a file.
func getComments(file *ast.File, fset *token.FileSet, scope Scope) ([]comment, error) {
if len(file.Comments) == 0 {
Expand Down Expand Up @@ -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, "/*") &&
Expand All @@ -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 {
Expand Down
21 changes: 18 additions & 3 deletions getters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
"<godotSpecialReplacer>\n" +
" Two\n" +
"<godotSpecialReplacer>\n" +
" Three\n" +
"<godotSpecialReplacer>\n" +
"<godotSpecialReplacer>",
},
{
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<godotSpecialReplacer>",
},
{
name: "cgo block",
Expand All @@ -157,8 +172,8 @@ func TestGetText(t *testing.T) {
}},
text: "\n" +
"Example:\n" +
"\n" +
"\n" +
"<godotSpecialReplacer>\n" +
"<godotSpecialReplacer>\n" +
"",
},
{
Expand Down
3 changes: 3 additions & 0 deletions testdata/check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,6 @@ func nonCapital() int {
}

// Comment with a URL - http://example.com/[PASS]

// Multiline comment with a URL
// http://example.com/[PASS]

0 comments on commit fd30ce2

Please sign in to comment.