Skip to content

Commit

Permalink
checkers: add codegenComment checker (go-critic#783)
Browse files Browse the repository at this point in the history
Checks whether a file has malformed "auto generated" comment.

Signed-off-by: Iskander Sharipov <[email protected]>
  • Loading branch information
quasilyte authored Jan 30, 2019
1 parent ac61906 commit d19dbf1
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 1 deletion.
61 changes: 61 additions & 0 deletions checkers/codegenComment_checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package checkers

import (
"go/ast"
"regexp"
"strings"

"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
)

func init() {
var info lintpack.CheckerInfo
info.Name = "codegenComment"
info.Tags = []string{"diagnostic", "experimental"}
info.Summary = "Detects malformed 'code generated' file comments"
info.Before = `// This file was automatically generated by foogen`
info.After = `// Code generated by foogen. DO NOT EDIT.`

collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
patterns := []string{
"this (?:file|code) (?:was|is) auto(?:matically)? generated",
"this (?:file|code) (?:was|is) generated automatically",
"this (?:file|code) (?:was|is) generated by",
"this (?:file|code) (?:was|is) (?:auto(?:matically)? )?generated",
"this (?:file|code) (?:was|is) generated",
"code in this file (?:was|is) auto(?:matically)? generated",
"generated (?:file|code) - do not edit",
// TODO(Quasilyte): more of these.
}
re := regexp.MustCompile("(?i)" + strings.Join(patterns, "|"))
return &codegenCommentChecker{
ctx: ctx,
badCommentRE: re,
}
})
}

type codegenCommentChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext

badCommentRE *regexp.Regexp
}

func (c *codegenCommentChecker) WalkFile(f *ast.File) {
if f.Doc == nil {
return
}

for _, comment := range f.Doc.List {
if c.badCommentRE.MatchString(comment.Text) {
c.warn(comment)
return
}
}
}

func (c *codegenCommentChecker) warn(cause ast.Node) {
c.ctx.Warn(cause, "comment should match `Code generated .* DO NOT EDIT.` regexp")
}
2 changes: 2 additions & 0 deletions checkers/testdata/_integration/check_comments/autogen1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// THIS FILE WAS GENERATED BY QUASILYTE!
package foo
2 changes: 2 additions & 0 deletions checkers/testdata/_integration/check_comments/autogen2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is auto generated.
package foo
2 changes: 2 additions & 0 deletions checkers/testdata/_integration/check_comments/autogen3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file is automatically generated.
package foo
4 changes: 4 additions & 0 deletions checkers/testdata/_integration/check_comments/linttest.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
exit status 1
./autogen1.go:1:1: codegenComment: comment should match `Code generated .* DO NOT EDIT.` regexp
./autogen2.go:1:1: codegenComment: comment should match `Code generated .* DO NOT EDIT.` regexp
./autogen3.go:1:1: codegenComment: comment should match `Code generated .* DO NOT EDIT.` regexp
./autogen4.go:1:1: codegenComment: comment should match `Code generated .* DO NOT EDIT.` regexp
./foo.go:5:2: commentedOutImport: remove commented-out "os" import
./foo.go:6:2: commentedOutImport: remove commented-out "fmt" import
./foo.go:6:2: commentedOutImport: remove commented-out "strconv" import
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
check -enable=commentedOutImport ./... | linttest.golden
check -enable=commentedOutImport,codegenComment ./... | linttest.golden
2 changes: 2 additions & 0 deletions checkers/testdata/codegenComment/negative_tests.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions checkers/testdata/codegenComment/positive_tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*! comment should match `Code generated .* DO NOT EDIT.` regexp */
// This file was automatically generated by Quasilyte.
package checker_test

0 comments on commit d19dbf1

Please sign in to comment.