-
Notifications
You must be signed in to change notification settings - Fork 113
/
whyNoLint_checker.go
51 lines (43 loc) · 1.14 KB
/
whyNoLint_checker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package checkers
import (
"go/ast"
"regexp"
"strings"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-critic/go-critic/framework/linter"
)
func init() {
info := linter.CheckerInfo{
Name: "whyNoLint",
Tags: []string{"style", "experimental"},
Summary: "Ensures that `//nolint` comments include an explanation",
Before: `//nolint`,
After: `//nolint // reason`,
}
collection.AddChecker(&info, func(ctx *linter.CheckerContext) (linter.FileWalker, error) {
return astwalk.WalkerForComment(&whyNoLintChecker{
ctx: ctx,
re: regexp.MustCompile(`^// *nolint(?::[^ ]+)? *(.*)$`),
}), nil
})
}
type whyNoLintChecker struct {
astwalk.WalkHandler
ctx *linter.CheckerContext
re *regexp.Regexp
}
func (c whyNoLintChecker) VisitComment(cg *ast.CommentGroup) {
if strings.HasPrefix(cg.List[0].Text, "/*") {
return
}
for _, comment := range cg.List {
sl := c.re.FindStringSubmatch(comment.Text)
if len(sl) < 2 {
continue
}
if s := sl[1]; !strings.HasPrefix(s, "//") || strings.TrimPrefix(s, "//") == "" {
c.ctx.Warn(cg, "include an explanation for nolint directive")
return
}
}
}