Skip to content

Commit

Permalink
refactor WithoutGeneratedCode functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Banana Shimakawa committed Jun 29, 2018
1 parent 156d684 commit dbecf20
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
42 changes: 21 additions & 21 deletions internal/errcheck/errcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"strings"
"sync"

"golang.org/x/tools/go/loader"
"go/parser"
"golang.org/x/tools/go/loader"
)

var errorType *types.Interface
Expand Down Expand Up @@ -189,15 +189,31 @@ func (c *Checker) load(paths ...string) (*loader.Program, error) {
return loadcfg.Load()
}

var generatedCodeRegexp = regexp.MustCompile("^// Code generated .* DO NOT EDIT\\.$")

func (c *Checker) shouldSkipFile(file *ast.File) bool {
if !c.WithoutGeneratedCode {
return false
}

for _, cg := range file.Comments {
for _, comment := range cg.List {
if generatedCodeRegexp.MatchString(comment.Text) {
return true
}
}
}

return false
}

// CheckPackages checks packages for errors.
func (c *Checker) CheckPackages(paths ...string) error {
program, err := c.load(paths...)
if err != nil {
return fmt.Errorf("could not type check: %s", err)
}

re := regexp.MustCompile("^// Code generated .* DO NOT EDIT\\.$")

var wg sync.WaitGroup
u := &UncheckedErrors{}
for _, pkgInfo := range program.InitialPackages() {
Expand All @@ -222,24 +238,9 @@ func (c *Checker) CheckPackages(paths ...string) error {
errors: []UncheckedError{},
}

PKG_FILE:
for _, astFile := range v.pkg.Files {
if (c.WithoutGeneratedCode) {
c.logf("+++++++++++++++++++++++++++++++++\n")
for _, cg := range astFile.Comments {
c.logf("---------------------------------\n")
c.logf("Position %v\n", cg.Pos())
for _, comment := range cg.List {
c.logf("...............\n")
c.logf("%v\n", comment.Text)
c.logf("...............\n")
if (re.MatchString(comment.Text)) {
continue PKG_FILE
}
}
c.logf("---------------------------------")
}
c.logf("+++++++++++++++++++++++++++++++++")
if c.shouldSkipFile(astFile) {
continue
}
ast.Walk(v, astFile)
}
Expand All @@ -255,7 +256,6 @@ func (c *Checker) CheckPackages(paths ...string) error {
return nil
}


// visitor implements the errcheck algorithm
type visitor struct {
prog *loader.Program
Expand Down
6 changes: 3 additions & 3 deletions internal/errcheck/errcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,17 @@ func TestWithoutGeneratedCode(t *testing.T) {

cases := []struct {
withoutGeneratedCode bool
numExpectedErrs int
numExpectedErrs int
}{
// basic case has one error
{
withoutGeneratedCode: false,
numExpectedErrs: 1,
numExpectedErrs: 1,
},
// ignoring vendored import works
{
withoutGeneratedCode: true,
numExpectedErrs: 0,
numExpectedErrs: 0,
},
}

Expand Down

0 comments on commit dbecf20

Please sign in to comment.