diff --git a/break_comments.go b/break_comments.go index a783ee1..50a9a8b 100644 --- a/break_comments.go +++ b/break_comments.go @@ -3,27 +3,36 @@ package nit import ( "go/ast" "go/token" + "regexp" "strings" ) type ( // BreakComments defines all the found break-like comments in the file. BreakComments struct { - index int - comments []int + index int + comments []int + generatedFile bool } ) const ( - breakComment = "//-" + breakComment = "//-" + generatedFile = `^// Code generated .* DO NOT EDIT\.$` ) // NewBreakComments returns all the valid break-like comments. func NewBreakComments(fset *token.FileSet, comments []*ast.CommentGroup) *BreakComments { r := BreakComments{} + re, _ := regexp.Compile(generatedFile) + for _, c := range comments { for _, c1 := range c.List { + if re.MatchString(c1.Text) { + r.generatedFile = true + } + if strings.HasPrefix(c1.Text, breakComment) { position := fset.PositionFor(c1.Pos(), false) if position.Column == 1 || position.Column == 2 { // left most either nested or not nested group declarations @@ -35,6 +44,12 @@ func NewBreakComments(fset *token.FileSet, comments []*ast.CommentGroup) *BreakC return &r } +// HasGeneratedCode indicates whether the current file contains a "code +// generated expression". +func (c *BreakComments) HasGeneratedCode() bool { + return c.generatedFile +} + // Moves current line cursor to the received line. func (c *BreakComments) MoveTo(line int) { if c.index >= len(c.comments) { diff --git a/break_comments_test.go b/break_comments_test.go index 7a5fb4e..4628f15 100644 --- a/break_comments_test.go +++ b/break_comments_test.go @@ -10,14 +10,22 @@ import ( func TestBreakComments(t *testing.T) { tests := [...]struct { - name string - filename string - expected []int + name string + filename string + expected []int + expectedCodegenerated bool }{ { "OK", "break_comments1.go", []int{8, 13, 19}, + false, + }, + { + "OK: code generated", + "break_comments2.go", + nil, + true, }, } @@ -40,6 +48,9 @@ func TestBreakComments(t *testing.T) { if !cmp.Equal(tt.expected, actual) { ts.Errorf("expected values do not match: %s", cmp.Diff(tt.expected, actual)) } + if tt.expectedCodegenerated != bc.HasGeneratedCode() { + ts.Errorf("expected %t, got %t", tt.expectedCodegenerated, bc.HasGeneratedCode()) + } }) } } diff --git a/cmd/nit/main.go b/cmd/nit/main.go index 50570c8..6a0fff3 100644 --- a/cmd/nit/main.go +++ b/cmd/nit/main.go @@ -27,17 +27,19 @@ func main() { //- localPkg := flag.String("pkg", "", "local package") + skipGenerated := flag.Bool("skip-generated", false, "skip generated files") showVersion := flag.Bool("version", false, "prints current version information") flag.Parse() if *showVersion { - fmt.Printf("%v, commit %v, built at %v", version, commit, date) + fmt.Printf("%v, commit %v, built at %v\n", version, commit, date) os.Exit(0) } if len(flag.Args()) == 0 { - fmt.Println("missing packages") + fmt.Println("missing `pkg` argument.") + flag.Usage() os.Exit(1) } @@ -52,7 +54,10 @@ func main() { for _, f := range p.GoFiles { fullpath := filepath.Join(p.Dir, f) - v := nit.Nitpicker{LocalPath: *localPkg} + v := nit.Nitpicker{ + LocalPath: *localPkg, + SkipGeneratedFile: *skipGenerated, + } if err := v.Validate(fullpath); err != nil { failed = true fmt.Println(err) diff --git a/nitpicking.go b/nitpicking.go index e88780c..76a10ca 100644 --- a/nitpicking.go +++ b/nitpicking.go @@ -11,7 +11,9 @@ import ( type ( // Nitpicker defines the linter. Nitpicker struct { - LocalPath string + LocalPath string + SkipGeneratedFile bool + //- fset *token.FileSet fsm *FileSectionMachine comments *BreakComments @@ -30,6 +32,9 @@ func (v *Nitpicker) Validate(filename string) error { } v.comments = NewBreakComments(v.fset, f.Comments) + if v.comments.HasGeneratedCode() && v.SkipGeneratedFile { + return nil + } for _, s := range f.Decls { // fmt.Printf("%d == %T - %+v -- %t\n", v.fset.PositionFor(s.Pos(), false).Line, s, s, s.End().IsValid()) diff --git a/testdata/break_comments2.go b/testdata/break_comments2.go new file mode 100644 index 0000000..3a69ff0 --- /dev/null +++ b/testdata/break_comments2.go @@ -0,0 +1,13 @@ +package testdata + +func NewBreakCommentType2() {} + +type BreakCommentType2_2 int + +// Code generated blablabla DO NOT EDIT. + +// ^ According to: +// > https://golang.org/pkg/cmd/go/internal/generate/ +// +// "The line may appear anywhere in the file, but is typically +// placed near the beginning so it is easy to find."