Skip to content

Commit 7c70250

Browse files
authored
Merge pull request #84 from golangci/support/detect-mockgen-properly
Properly detect generated files: fix detection when
2 parents 8992194 + 7f83307 commit 7c70250

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

Diff for: pkg/result/processors/autogenerated_exclude.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package processors
22

33
import (
44
"fmt"
5+
"go/ast"
6+
"go/token"
57
"strings"
68

79
"github.com/golangci/golangci-lint/pkg/lint/astcache"
@@ -81,8 +83,32 @@ func (p *AutogeneratedExclude) getOrCreateFileSummary(i *result.Issue) (*ageFile
8183
return nil, fmt.Errorf("can't parse file %s: %s", i.FilePath(), f.Err)
8284
}
8385

84-
fs.isGenerated = isGeneratedFileByComment(f.F.Doc.Text())
86+
doc := getDoc(f.F, f.Fset)
87+
88+
fs.isGenerated = isGeneratedFileByComment(doc)
8589
return fs, nil
8690
}
8791

92+
func getDoc(f *ast.File, fset *token.FileSet) string {
93+
// don't use just f.Doc: e.g. mockgen leaves extra line between comment and package name
94+
95+
importPos := f.End()
96+
if len(f.Imports) != 0 {
97+
importPos = f.Imports[0].Pos()
98+
}
99+
100+
var neededComments []string
101+
for _, g := range f.Comments {
102+
if g.Pos() < importPos && fset.Position(g.Pos()).Column == 1 {
103+
neededComments = append(neededComments, g.Text())
104+
}
105+
}
106+
107+
if len(neededComments) == 0 {
108+
return ""
109+
}
110+
111+
return strings.Join(neededComments, "\n")
112+
}
113+
88114
func (p AutogeneratedExclude) Finish() {}

0 commit comments

Comments
 (0)