-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
checkers: add todoCommentWithoutDetail (#1169)
Fixes #434
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
checkers/testdata/todoCommentWithoutDetail/negative_tests.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
16
checkers/testdata/todoCommentWithoutDetail/positive_tests.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |