change isGenerated heuristic to match more generated files

As observed in #30, there's tools out there that don't comply 100% with
the referenced golang convention.

With this change, golangci-lint will skip some more of those generated
files.

Signed-off-by: Stephan Renatus <srenatus@chef.io>
This commit is contained in:
Stephan Renatus 2018-05-31 09:11:51 +02:00
parent 21ff49ce6d
commit 468d2334ea
4 changed files with 43 additions and 15 deletions

View File

@ -47,21 +47,26 @@ func (p *Nolint) Process(issues []result.Issue) ([]result.Issue, error) {
}
var (
genHdr = []byte("// Code generated ")
genFtr = []byte(" DO NOT EDIT.")
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.
// isGenerated reports whether the source file is generated code.
// Using a bit laxer rules than https://golang.org/s/generatedcode to
// match more generated code.
func isGenerated(src []byte) bool {
sc := bufio.NewScanner(bytes.NewReader(src))
var hdr, ftr bool
for sc.Scan() {
b := sc.Bytes()
if bytes.HasPrefix(b, genHdr) && bytes.HasSuffix(b, genFtr) && len(b) >= len(genHdr)+len(genFtr) {
return true
if bytes.HasPrefix(b, genHdr) {
hdr = true
}
if bytes.Contains(b, genFtr) {
ftr = true
}
}
return false
return hdr && ftr
}
func (p *Nolint) getOrCreateFileData(i *result.Issue) (*fileData, error) {

View File

@ -37,13 +37,22 @@ func TestNolint(t *testing.T) {
processAssertSame(t, p, newNolintFileIssue(1, "golint")) // no directive
}
func TestNoIssuesInAutogeneratedFile(t *testing.T) {
i := result.Issue{
Pos: token.Position{
Filename: filepath.Join("testdata", "nolint_autogenerated.go"),
Line: 4,
},
func TestNoIssuesInAutogeneratedFiles(t *testing.T) {
files := []string{
"nolint_autogenerated.go",
"nolint_autogenerated_alt_hdr.go",
"nolint_autogenerated_alt_hdr2.go",
}
for _, file := range files {
t.Run(file, func(t *testing.T) {
i := result.Issue{
Pos: token.Position{
Filename: filepath.Join("testdata", file),
Line: 4,
},
}
p := NewNolint(token.NewFileSet())
processAssertEmpty(t, p, i)
})
}
p := NewNolint(token.NewFileSet())
processAssertEmpty(t, p, i)
}

View File

@ -0,0 +1,6 @@
// Code generated by protoc-gen-foo
// source: bar.proto
// DO NOT EDIT!!!
package testdata
var v int

View File

@ -0,0 +1,8 @@
// Code generated by go-bindata.
// sources:
// bar.baz
// x/y.z
// DO NOT EDIT!
package testdata
var v int