Skip to content

Properly detect generated files: fix detection when #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion pkg/result/processors/autogenerated_exclude.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package processors

import (
"fmt"
"go/ast"
"go/token"
"strings"

"github.com/golangci/golangci-lint/pkg/lint/astcache"
Expand Down Expand Up @@ -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() {}