Skip to content

Commit

Permalink
Merge pull request #48 from srenatus/sr/auto-detect-more-generated-code
Browse files Browse the repository at this point in the history
change isGenerated heuristic to match more generated files
  • Loading branch information
golangci authored May 31, 2018
2 parents 71fe07c + 468d233 commit 9c048da
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
19 changes: 12 additions & 7 deletions pkg/result/processors/nolint.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,26 @@ func (p *Nolint) Process(issues []result.Issue) ([]result.Issue, error) {
}

var (
genHdr = []byte("// Code generated ")
genFtr = []byte(" DO NOT EDIT.")
genHdr = []byte("// Code generated")
genFtr = []byte("DO NOT EDIT")
)

// isGenerated reports whether the source file is generated code
// according the rules from https://golang.org/s/generatedcode.
// 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.
func isGenerated(src []byte) bool {
sc := bufio.NewScanner(bytes.NewReader(src))
var hdr, ftr bool
for sc.Scan() {
b := sc.Bytes()
if bytes.HasPrefix(b, genHdr) && bytes.HasSuffix(b, genFtr) && len(b) >= len(genHdr)+len(genFtr) {
return true
if bytes.HasPrefix(b, genHdr) {
hdr = true
}
if bytes.Contains(b, genFtr) {
ftr = true
}
}
return false
return hdr && ftr
}

func (p *Nolint) getOrCreateFileData(i *result.Issue) (*fileData, error) {
Expand Down
25 changes: 17 additions & 8 deletions pkg/result/processors/nolint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,22 @@ func TestNolint(t *testing.T) {
processAssertSame(t, p, newNolintFileIssue(1, "golint")) // no directive
}

func TestNoIssuesInAutogeneratedFile(t *testing.T) {
i := result.Issue{
Pos: token.Position{
Filename: filepath.Join("testdata", "nolint_autogenerated.go"),
Line: 4,
},
func TestNoIssuesInAutogeneratedFiles(t *testing.T) {
files := []string{
"nolint_autogenerated.go",
"nolint_autogenerated_alt_hdr.go",
"nolint_autogenerated_alt_hdr2.go",
}
for _, file := range files {
t.Run(file, func(t *testing.T) {
i := result.Issue{
Pos: token.Position{
Filename: filepath.Join("testdata", file),
Line: 4,
},
}
p := NewNolint(token.NewFileSet())
processAssertEmpty(t, p, i)
})
}
p := NewNolint(token.NewFileSet())
processAssertEmpty(t, p, i)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9c048da

Please sign in to comment.