Skip to content

Commit 352ed07

Browse files
authored
checkers: add todoCommentWithoutDetail (#1169)
Fixes #434
1 parent 8d39095 commit 352ed07

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package checker_test
2+
3+
func ExampleFoo() {
4+
// TODO: something important
5+
// TODO(jim)
6+
// FIX fix this
7+
// FIXME(bob)
8+
// TODO this
9+
// BUG: oh no this is broken
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package checker_test
2+
3+
func singleLineCode() {
4+
5+
/*! may want to add detail/assignee to this TODO/FIXME/BUG comment */
6+
// TODO
7+
8+
/*! may want to add detail/assignee to this TODO/FIXME/BUG comment */
9+
// FIX
10+
11+
/*! may want to add detail/assignee to this TODO/FIXME/BUG comment */
12+
// FIXME
13+
14+
/*! may want to add detail/assignee to this TODO/FIXME/BUG comment */
15+
// BUG
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package checkers
2+
3+
import (
4+
"go/ast"
5+
"regexp"
6+
7+
"github.com/go-critic/go-critic/checkers/internal/astwalk"
8+
"github.com/go-critic/go-critic/framework/linter"
9+
)
10+
11+
func init() {
12+
var info linter.CheckerInfo
13+
info.Name = "todoCommentWithoutDetail"
14+
info.Tags = []string{"style", "opinionated", "experimental"}
15+
info.Summary = "Detects TODO comments without detail/assignee"
16+
info.Before = `
17+
// TODO
18+
fiiWithCtx(nil, a, b)
19+
`
20+
info.After = `
21+
// TODO(admin): pass context.TODO() instead of nil
22+
fiiWithCtx(nil, a, b)
23+
`
24+
collection.AddChecker(&info, func(ctx *linter.CheckerContext) (linter.FileWalker, error) {
25+
visitor := &todoCommentWithoutCodeChecker{
26+
ctx: ctx,
27+
regex: regexp.MustCompile(`^(//|/\*)?\s*(TODO|FIX|FIXME|BUG)\s*(\*/)?$`),
28+
}
29+
return astwalk.WalkerForComment(visitor), nil
30+
})
31+
}
32+
33+
type todoCommentWithoutCodeChecker struct {
34+
astwalk.WalkHandler
35+
ctx *linter.CheckerContext
36+
regex *regexp.Regexp
37+
}
38+
39+
func (c *todoCommentWithoutCodeChecker) VisitComment(cg *ast.CommentGroup) {
40+
for _, comment := range cg.List {
41+
if c.regex.MatchString(comment.Text) {
42+
c.warn(cg)
43+
break
44+
}
45+
}
46+
}
47+
48+
func (c *todoCommentWithoutCodeChecker) warn(cause ast.Node) {
49+
c.ctx.Warn(cause, "may want to add detail/assignee to this TODO/FIXME/BUG comment")
50+
}

0 commit comments

Comments
 (0)