From dbecf20be449bdf840d7d57bddd9e95a71f828d9 Mon Sep 17 00:00:00 2001 From: Banana Shimakawa Date: Fri, 29 Jun 2018 10:21:58 +0900 Subject: [PATCH] refactor WithoutGeneratedCode functions --- internal/errcheck/errcheck.go | 42 +++++++++++++++--------------- internal/errcheck/errcheck_test.go | 6 ++--- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/internal/errcheck/errcheck.go b/internal/errcheck/errcheck.go index 7b52ab0..5be474f 100644 --- a/internal/errcheck/errcheck.go +++ b/internal/errcheck/errcheck.go @@ -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 @@ -189,6 +189,24 @@ 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...) @@ -196,8 +214,6 @@ func (c *Checker) CheckPackages(paths ...string) error { 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() { @@ -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) } @@ -255,7 +256,6 @@ func (c *Checker) CheckPackages(paths ...string) error { return nil } - // visitor implements the errcheck algorithm type visitor struct { prog *loader.Program diff --git a/internal/errcheck/errcheck_test.go b/internal/errcheck/errcheck_test.go index 674b277..8988480 100644 --- a/internal/errcheck/errcheck_test.go +++ b/internal/errcheck/errcheck_test.go @@ -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, }, }