From d19dbf1cbfabdc59e24689d17bb0ec8d35a92c0e Mon Sep 17 00:00:00 2001 From: "Iskander (Alex) Sharipov" Date: Thu, 31 Jan 2019 00:35:05 +0300 Subject: [PATCH] checkers: add codegenComment checker (#783) Checks whether a file has malformed "auto generated" comment. Signed-off-by: Iskander Sharipov --- checkers/codegenComment_checker.go | 61 +++++++++++++++++++ .../_integration/check_comments/autogen1.go | 2 + .../_integration/check_comments/autogen2.go | 2 + .../_integration/check_comments/autogen3.go | 2 + .../check_comments/linttest.golden | 4 ++ .../check_comments/linttest.params | 2 +- .../testdata/codegenComment/negative_tests.go | 2 + .../testdata/codegenComment/positive_tests.go | 3 + 8 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 checkers/codegenComment_checker.go create mode 100644 checkers/testdata/_integration/check_comments/autogen1.go create mode 100644 checkers/testdata/_integration/check_comments/autogen2.go create mode 100644 checkers/testdata/_integration/check_comments/autogen3.go create mode 100644 checkers/testdata/codegenComment/negative_tests.go create mode 100644 checkers/testdata/codegenComment/positive_tests.go diff --git a/checkers/codegenComment_checker.go b/checkers/codegenComment_checker.go new file mode 100644 index 000000000..14d89da37 --- /dev/null +++ b/checkers/codegenComment_checker.go @@ -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") +} diff --git a/checkers/testdata/_integration/check_comments/autogen1.go b/checkers/testdata/_integration/check_comments/autogen1.go new file mode 100644 index 000000000..c50bae54f --- /dev/null +++ b/checkers/testdata/_integration/check_comments/autogen1.go @@ -0,0 +1,2 @@ +// THIS FILE WAS GENERATED BY QUASILYTE! +package foo diff --git a/checkers/testdata/_integration/check_comments/autogen2.go b/checkers/testdata/_integration/check_comments/autogen2.go new file mode 100644 index 000000000..95e4179f8 --- /dev/null +++ b/checkers/testdata/_integration/check_comments/autogen2.go @@ -0,0 +1,2 @@ +// This file is auto generated. +package foo diff --git a/checkers/testdata/_integration/check_comments/autogen3.go b/checkers/testdata/_integration/check_comments/autogen3.go new file mode 100644 index 000000000..c1d2f1058 --- /dev/null +++ b/checkers/testdata/_integration/check_comments/autogen3.go @@ -0,0 +1,2 @@ +// This file is automatically generated. +package foo diff --git a/checkers/testdata/_integration/check_comments/linttest.golden b/checkers/testdata/_integration/check_comments/linttest.golden index 9761e1d9a..9e15d76bb 100644 --- a/checkers/testdata/_integration/check_comments/linttest.golden +++ b/checkers/testdata/_integration/check_comments/linttest.golden @@ -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 diff --git a/checkers/testdata/_integration/check_comments/linttest.params b/checkers/testdata/_integration/check_comments/linttest.params index 893b48190..a6f1add30 100644 --- a/checkers/testdata/_integration/check_comments/linttest.params +++ b/checkers/testdata/_integration/check_comments/linttest.params @@ -1 +1 @@ -check -enable=commentedOutImport ./... | linttest.golden +check -enable=commentedOutImport,codegenComment ./... | linttest.golden diff --git a/checkers/testdata/codegenComment/negative_tests.go b/checkers/testdata/codegenComment/negative_tests.go new file mode 100644 index 000000000..e16540036 --- /dev/null +++ b/checkers/testdata/codegenComment/negative_tests.go @@ -0,0 +1,2 @@ +// Code generated by Quasilyte. DO NOT EDIT. +package checker_test diff --git a/checkers/testdata/codegenComment/positive_tests.go b/checkers/testdata/codegenComment/positive_tests.go new file mode 100644 index 000000000..bbf0dcb08 --- /dev/null +++ b/checkers/testdata/codegenComment/positive_tests.go @@ -0,0 +1,3 @@ +/*! comment should match `Code generated .* DO NOT EDIT.` regexp */ +// This file was automatically generated by Quasilyte. +package checker_test