Skip to content

Commit fb39290

Browse files
author
golangci
authored
Merge pull request #29 from golangci/support/fix-nolint-with-spaces
#26: fix spaces in nolint directive
2 parents 7d224b1 + cad9fc7 commit fb39290

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

pkg/result/processors/nolint.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ func (p *Nolint) shouldPassIssue(i *result.Issue) (bool, error) {
115115
if i.FromLinter == linter {
116116
return false, nil
117117
}
118-
// TODO: check linter name
119118
}
120119
}
121120

@@ -127,20 +126,30 @@ func extractFileComments(fset *token.FileSet, comments ...*ast.CommentGroup) fil
127126
for _, g := range comments {
128127
for _, c := range g.List {
129128
text := strings.TrimLeft(c.Text, "/ ")
130-
if strings.HasPrefix(text, "nolint") {
131-
var linters []string
132-
if strings.HasPrefix(text, "nolint:") {
133-
text = strings.Split(text, " ")[0] // allow arbitrary text after this comment
134-
for _, linter := range strings.Split(strings.TrimPrefix(text, "nolint:"), ",") {
135-
linters = append(linters, strings.TrimSpace(linter))
136-
}
137-
}
138-
pos := fset.Position(g.Pos())
129+
if !strings.HasPrefix(text, "nolint") {
130+
continue
131+
}
132+
133+
pos := fset.Position(g.Pos())
134+
if !strings.HasPrefix(text, "nolint:") { // ignore all linters
139135
ret = append(ret, comment{
140-
linters: linters,
141-
line: pos.Line,
136+
line: pos.Line,
142137
})
138+
continue
139+
}
140+
141+
// ignore specific linters
142+
var linters []string
143+
text = strings.Split(text, "//")[0] // allow another comment after this comment
144+
linterItems := strings.Split(strings.TrimPrefix(text, "nolint:"), ",")
145+
for _, linter := range linterItems {
146+
linterName := strings.TrimSpace(linter) // TODO: validate it here
147+
linters = append(linters, linterName)
143148
}
149+
ret = append(ret, comment{
150+
linters: linters,
151+
line: pos.Line,
152+
})
144153
}
145154
}
146155

pkg/result/processors/nolint_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ func TestNolint(t *testing.T) {
2424
processAssertEmpty(t, p, newNolintFileIssue(3, "gofmt")) // check cached is ok
2525
processAssertSame(t, p, newNolintFileIssue(3, "gofmtA")) // check different name
2626

27-
processAssertEmpty(t, p, newNolintFileIssue(4, "any"))
28-
processAssertEmpty(t, p, newNolintFileIssue(5, "any"))
27+
processAssertEmpty(t, p, newNolintFileIssue(4, "gofmt"))
28+
processAssertSame(t, p, newNolintFileIssue(4, "gofmtA")) // check different name
2929

30-
processAssertSame(t, p, newNolintFileIssue(1, "golint"))
30+
processAssertEmpty(t, p, newNolintFileIssue(5, "gofmt"))
31+
processAssertEmpty(t, p, newNolintFileIssue(5, "govet"))
32+
processAssertSame(t, p, newNolintFileIssue(5, "gofmtA")) // check different name
33+
34+
processAssertEmpty(t, p, newNolintFileIssue(6, "any"))
35+
processAssertEmpty(t, p, newNolintFileIssue(7, "any"))
36+
37+
processAssertSame(t, p, newNolintFileIssue(1, "golint")) // no directive
3138
}
3239

3340
func TestNoIssuesInAutogeneratedFile(t *testing.T) {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package testdata
22

33
var nolintSpecific int // nolint:gofmt
4+
var nolintSpace int // nolint: gofmt
5+
var nolintSpaces int //nolint: gofmt, govet
46
var nolintAll int // nolint
5-
var nolintAndAppendix int // nolint Some My Text
7+
var nolintAndAppendix int // nolint // another comment

0 commit comments

Comments
 (0)