Properly detect generated files: fix detection when
there is extra line between comment about generated file and package name
This commit is contained in:
parent
89921943e1
commit
7f833070b1
@ -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() {}
|
||||
|
Loading…
x
Reference in New Issue
Block a user