diff --git a/scanner/generated.go b/scanner/generated.go new file mode 100644 index 0000000..1884852 --- /dev/null +++ b/scanner/generated.go @@ -0,0 +1,36 @@ +// Copyright (c) 2013 The Go Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd. + +// Taken from https://github.com/golang/lint/blob/85993ffd0a6cd043291f3f63d45d656d97b165bd/lint.go + +// Once https://github.com/golang/go/issues/28089 is implemented (proposal is accepted), +// this can be replaced with the official implementation. + +package scanner + +import ( + "bufio" + "bytes" +) + +var ( + genHdr = []byte("// Code generated ") + genFtr = []byte(" DO NOT EDIT.") +) + +// isGenerated reports whether the source file is generated code +// according the rules from https://golang.org/s/generatedcode. +// +func isGenerated(src []byte) bool { + sc := bufio.NewScanner(bytes.NewReader(src)) + for sc.Scan() { + b := sc.Bytes() + if bytes.HasPrefix(b, genHdr) && bytes.HasSuffix(b, genFtr) && len(b) >= len(genHdr)+len(genFtr) { + return true + } + } + return false +} diff --git a/scanner/generated_test.go b/scanner/generated_test.go new file mode 100644 index 0000000..81bf8e3 --- /dev/null +++ b/scanner/generated_test.go @@ -0,0 +1,38 @@ +// Copyright (c) 2013 The Go Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd. + +// Taken from https://github.com/golang/lint/blob/85993ffd0a6cd043291f3f63d45d656d97b165bd/lint_test.go + +package scanner + +import "testing" + +func TestIsGenerated(t *testing.T) { + tests := []struct { + source string + generated bool + }{ + {"// Code Generated by some tool. DO NOT EDIT.", false}, + {"// Code generated by some tool. DO NOT EDIT.", true}, + {"// Code generated by some tool. DO NOT EDIT", false}, + {"// Code generated DO NOT EDIT.", true}, + {"// Code generated DO NOT EDIT.", false}, + {"\t\t// Code generated by some tool. DO NOT EDIT.\npackage foo\n", false}, + {"// Code generated by some tool. DO NOT EDIT.\npackage foo\n", true}, + {"package foo\n// Code generated by some tool. DO NOT EDIT.\ntype foo int\n", true}, + {"package foo\n // Code generated by some tool. DO NOT EDIT.\ntype foo int\n", false}, + {"package foo\n// Code generated by some tool. DO NOT EDIT. \ntype foo int\n", false}, + {"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.\n", true}, + {"package foo\ntype foo int\n// Code generated by some tool. DO NOT EDIT.", true}, + } + + for i, test := range tests { + got := isGenerated([]byte(test.source)) + if got != test.generated { + t.Errorf("test %d, isGenerated() = %v, want %v", i, got, test.generated) + } + } +} diff --git a/scanner/scanner.go b/scanner/scanner.go index 9ebc1ed..71202c8 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -96,8 +96,7 @@ func (c *CodeMap) LoadProgram() error { conf.Import(p.Path) } prog, err := conf.Load() - */ - if err != nil { + */if err != nil { return errors.Wrap(err, "Error loading config") } c.pkgs = pkgs @@ -141,7 +140,7 @@ func (p *PackageMap) ScanPackage() error { if err != nil { return errors.WithStack(err) } - if !doNotEditRe.Match(body) { + if !isGenerated(body) { continue } for i := range bytes.Split(body, []byte("\n")) { @@ -293,7 +292,6 @@ func (f *FileMap) boolOr(list []ast.Expr) ast.Expr { } func (f *FileMap) inspectIf(stmt *ast.IfStmt, falseExpr ...ast.Expr) error { - // main if block s := brenda.NewSolver(f.fset, f.pkg.TypesInfo.Uses, f.pkg.TypesInfo.Defs, stmt.Cond, falseExpr...) if err := s.SolveTrue(); err != nil {