forked from go-critic/go-critic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
checkers: add codegenComment checker (go-critic#783)
Checks whether a file has malformed "auto generated" comment. Signed-off-by: Iskander Sharipov <[email protected]>
- Loading branch information
Showing
8 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// THIS FILE WAS GENERATED BY QUASILYTE! | ||
package foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// This file is auto generated. | ||
package foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
4
checkers/testdata/_integration/check_comments/linttest.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
check -enable=commentedOutImport ./... | linttest.golden | ||
check -enable=commentedOutImport,codegenComment ./... | linttest.golden |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |