-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from golangci/feature/match-more-autogenerated…
…-files Fix #72: match more autogenerated files patterns.
- Loading branch information
Showing
11 changed files
with
232 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package processors | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/golangci/golangci-lint/pkg/lint/astcache" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
type ageFileSummary struct { | ||
isGenerated bool | ||
} | ||
|
||
type ageFileSummaryCache map[string]*ageFileSummary | ||
|
||
type AutogeneratedExclude struct { | ||
fileSummaryCache ageFileSummaryCache | ||
astCache *astcache.Cache | ||
} | ||
|
||
func NewAutogeneratedExclude(astCache *astcache.Cache) *AutogeneratedExclude { | ||
return &AutogeneratedExclude{ | ||
fileSummaryCache: ageFileSummaryCache{}, | ||
astCache: astCache, | ||
} | ||
} | ||
|
||
var _ Processor = &AutogeneratedExclude{} | ||
|
||
func (p AutogeneratedExclude) Name() string { | ||
return "autogenerated_exclude" | ||
} | ||
|
||
func (p *AutogeneratedExclude) Process(issues []result.Issue) ([]result.Issue, error) { | ||
return filterIssuesErr(issues, p.shouldPassIssue) | ||
} | ||
|
||
func (p *AutogeneratedExclude) shouldPassIssue(i *result.Issue) (bool, error) { | ||
fs, err := p.getOrCreateFileSummary(i) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// don't report issues for autogenerated files | ||
return !fs.isGenerated, nil | ||
} | ||
|
||
// isGenerated reports whether the source file is generated code. | ||
// Using a bit laxer rules than https://golang.org/s/generatedcode to | ||
// match more generated code. See #48 and #72. | ||
func isGeneratedFileByComment(doc string) bool { | ||
const ( | ||
genCodeGenerated = "code generated" | ||
genDoNotEdit = "do not edit" | ||
genAutoFile = "autogenerated file" // easyjson | ||
) | ||
|
||
markers := []string{genCodeGenerated, genDoNotEdit, genAutoFile} | ||
doc = strings.ToLower(doc) | ||
for _, marker := range markers { | ||
if strings.Contains(doc, marker) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (p *AutogeneratedExclude) getOrCreateFileSummary(i *result.Issue) (*ageFileSummary, error) { | ||
fs := p.fileSummaryCache[i.FilePath()] | ||
if fs != nil { | ||
return fs, nil | ||
} | ||
|
||
fs = &ageFileSummary{} | ||
p.fileSummaryCache[i.FilePath()] = fs | ||
|
||
f := p.astCache.GetOrParse(i.FilePath()) | ||
if f.Err != nil { | ||
return nil, fmt.Errorf("can't parse file %s: %s", i.FilePath(), f.Err) | ||
} | ||
|
||
fs.isGenerated = isGeneratedFileByComment(f.F.Doc.Text()) | ||
return fs, nil | ||
} | ||
|
||
func (p AutogeneratedExclude) Finish() {} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.