Skip to content

Commit

Permalink
Support for skipping generated files.
Browse files Browse the repository at this point in the history
Use `nit -skip-generated <pkgs>`
  • Loading branch information
MarioCarrion committed Apr 4, 2019
1 parent 82f0f04 commit bd1607b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
21 changes: 18 additions & 3 deletions break_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
17 changes: 14 additions & 3 deletions break_comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand All @@ -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())
}
})
}
}
11 changes: 8 additions & 3 deletions cmd/nit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion nitpicking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand Down
13 changes: 13 additions & 0 deletions testdata/break_comments2.go

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

0 comments on commit bd1607b

Please sign in to comment.