Merge pull request #29 from golangci/support/fix-nolint-with-spaces

#26: fix spaces in nolint directive
This commit is contained in:
golangci 2018-05-28 12:23:28 +03:00 committed by GitHub
commit fb3929085c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 18 deletions

View File

@ -115,7 +115,6 @@ func (p *Nolint) shouldPassIssue(i *result.Issue) (bool, error) {
if i.FromLinter == linter { if i.FromLinter == linter {
return false, nil return false, nil
} }
// TODO: check linter name
} }
} }
@ -127,22 +126,32 @@ func extractFileComments(fset *token.FileSet, comments ...*ast.CommentGroup) fil
for _, g := range comments { for _, g := range comments {
for _, c := range g.List { for _, c := range g.List {
text := strings.TrimLeft(c.Text, "/ ") text := strings.TrimLeft(c.Text, "/ ")
if strings.HasPrefix(text, "nolint") { if !strings.HasPrefix(text, "nolint") {
var linters []string continue
if strings.HasPrefix(text, "nolint:") {
text = strings.Split(text, " ")[0] // allow arbitrary text after this comment
for _, linter := range strings.Split(strings.TrimPrefix(text, "nolint:"), ",") {
linters = append(linters, strings.TrimSpace(linter))
}
} }
pos := fset.Position(g.Pos()) pos := fset.Position(g.Pos())
if !strings.HasPrefix(text, "nolint:") { // ignore all linters
ret = append(ret, comment{
line: pos.Line,
})
continue
}
// ignore specific linters
var linters []string
text = strings.Split(text, "//")[0] // allow another comment after this comment
linterItems := strings.Split(strings.TrimPrefix(text, "nolint:"), ",")
for _, linter := range linterItems {
linterName := strings.TrimSpace(linter) // TODO: validate it here
linters = append(linters, linterName)
}
ret = append(ret, comment{ ret = append(ret, comment{
linters: linters, linters: linters,
line: pos.Line, line: pos.Line,
}) })
} }
} }
}
return ret return ret
} }

View File

@ -24,10 +24,17 @@ func TestNolint(t *testing.T) {
processAssertEmpty(t, p, newNolintFileIssue(3, "gofmt")) // check cached is ok processAssertEmpty(t, p, newNolintFileIssue(3, "gofmt")) // check cached is ok
processAssertSame(t, p, newNolintFileIssue(3, "gofmtA")) // check different name processAssertSame(t, p, newNolintFileIssue(3, "gofmtA")) // check different name
processAssertEmpty(t, p, newNolintFileIssue(4, "any")) processAssertEmpty(t, p, newNolintFileIssue(4, "gofmt"))
processAssertEmpty(t, p, newNolintFileIssue(5, "any")) processAssertSame(t, p, newNolintFileIssue(4, "gofmtA")) // check different name
processAssertSame(t, p, newNolintFileIssue(1, "golint")) processAssertEmpty(t, p, newNolintFileIssue(5, "gofmt"))
processAssertEmpty(t, p, newNolintFileIssue(5, "govet"))
processAssertSame(t, p, newNolintFileIssue(5, "gofmtA")) // check different name
processAssertEmpty(t, p, newNolintFileIssue(6, "any"))
processAssertEmpty(t, p, newNolintFileIssue(7, "any"))
processAssertSame(t, p, newNolintFileIssue(1, "golint")) // no directive
} }
func TestNoIssuesInAutogeneratedFile(t *testing.T) { func TestNoIssuesInAutogeneratedFile(t *testing.T) {

View File

@ -1,5 +1,7 @@
package testdata package testdata
var nolintSpecific int // nolint:gofmt var nolintSpecific int // nolint:gofmt
var nolintSpace int // nolint: gofmt
var nolintSpaces int //nolint: gofmt, govet
var nolintAll int // nolint var nolintAll int // nolint
var nolintAndAppendix int // nolint Some My Text var nolintAndAppendix int // nolint // another comment