Properly detect generated files: fix detection when

there is extra line between comment about generated file and package
name
This commit is contained in:
Denis Isaev 2018-06-11 12:38:52 +03:00
parent 89921943e1
commit 7f833070b1
No known key found for this signature in database
GPG Key ID: A36A0EC8E27A1A01

View File

@ -2,6 +2,8 @@ package processors
import (
"fmt"
"go/ast"
"go/token"
"strings"
"github.com/golangci/golangci-lint/pkg/lint/astcache"
@ -81,8 +83,32 @@ func (p *AutogeneratedExclude) getOrCreateFileSummary(i *result.Issue) (*ageFile
return nil, fmt.Errorf("can't parse file %s: %s", i.FilePath(), f.Err)
}
fs.isGenerated = isGeneratedFileByComment(f.F.Doc.Text())
doc := getDoc(f.F, f.Fset)
fs.isGenerated = isGeneratedFileByComment(doc)
return fs, nil
}
func getDoc(f *ast.File, fset *token.FileSet) string {
// don't use just f.Doc: e.g. mockgen leaves extra line between comment and package name
importPos := f.End()
if len(f.Imports) != 0 {
importPos = f.Imports[0].Pos()
}
var neededComments []string
for _, g := range f.Comments {
if g.Pos() < importPos && fset.Position(g.Pos()).Column == 1 {
neededComments = append(neededComments, g.Text())
}
}
if len(neededComments) == 0 {
return ""
}
return strings.Join(neededComments, "\n")
}
func (p AutogeneratedExclude) Finish() {}