forked from go-critic/go-critic
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
todoCommentWithoutDetail_checker.go
52 lines (46 loc) · 1.25 KB
/
todoCommentWithoutDetail_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
52
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() {
var info linter.CheckerInfo
info.Name = "todoCommentWithoutDetail"
info.Tags = []string{"diagnostic", "experimental"}
info.Summary = "Detects TODO comments without detail/assignee"
info.Before = `
// TODO
fiiWithCtx(nil, a, b)
`
info.After = `
// TODO(admin): pass context.TODO() instead of nil
fiiWithCtx(nil, a, b)
`
collection.AddChecker(&info, func(ctx *linter.CheckerContext) (linter.FileWalker, error) {
visitor := &todoCommentWithoutCodeChecker{
ctx: ctx,
regex: regexp.MustCompile(`^\s*(TODO|FIX|FIXME|BUG)\s*$`),
}
return astwalk.WalkerForComment(visitor), nil
})
}
type todoCommentWithoutCodeChecker struct {
astwalk.WalkHandler
ctx *linter.CheckerContext
regex *regexp.Regexp
}
func (c *todoCommentWithoutCodeChecker) VisitComment(cg *ast.CommentGroup) {
comment := strings.TrimSpace(cg.Text())
if strings.Contains(comment, "\n") {
return
}
if c.regex.MatchString(comment) {
c.warn(cg)
}
}
func (c *todoCommentWithoutCodeChecker) warn(cause ast.Node) {
c.ctx.Warn(cause, "may want to add detail/assignee to this TODO/FIXME/BUG comment")
}