Merge pull request #84 from golangci/support/detect-mockgen-properly
Properly detect generated files: fix detection when
This commit is contained in:
commit
7c70250d8c
@ -2,6 +2,8 @@ package processors
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"go/ast"
|
||||||
|
"go/token"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/golangci/golangci-lint/pkg/lint/astcache"
|
"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)
|
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
|
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() {}
|
func (p AutogeneratedExclude) Finish() {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user