Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checker for TODO comments without detail/assignee #1169

Merged
merged 7 commits into from Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions checkers/testdata/todoCommentWithoutDetail/negative_tests.go
@@ -0,0 +1,10 @@
package checker_test

func ExampleFoo() {
// TODO: something important
// TODO(jim)
// FIX fix this
// FIXME(bob)
// TODO this
// BUG: oh no this is broken
}
16 changes: 16 additions & 0 deletions checkers/testdata/todoCommentWithoutDetail/positive_tests.go
@@ -0,0 +1,16 @@
package checker_test

func singleLineCode() {

/*! may want to add detail/assignee to this TODO/FIXME/BUG comment */
// TODO

/*! may want to add detail/assignee to this TODO/FIXME/BUG comment */
// FIX

/*! may want to add detail/assignee to this TODO/FIXME/BUG comment */
// FIXME

/*! may want to add detail/assignee to this TODO/FIXME/BUG comment */
// BUG
}
50 changes: 50 additions & 0 deletions checkers/todoCommentWithoutDetail_checker.go
@@ -0,0 +1,50 @@
package checkers

import (
"go/ast"
"regexp"

"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{"style", "opinionated", "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) {
for _, comment := range cg.List {
if c.regex.MatchString(comment.Text) {
c.warn(cg)
break
}
}
}

func (c *todoCommentWithoutCodeChecker) warn(cause ast.Node) {
c.ctx.Warn(cause, "may want to add detail/assignee to this TODO/FIXME/BUG comment")
}