-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboolFuncPrefix_checker.go
68 lines (57 loc) · 1.68 KB
/
boolFuncPrefix_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package checkers
import (
"go/ast"
"go/types"
"strings"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
)
func init() {
var info lintpack.CheckerInfo
info.Name = "boolFuncPrefix"
info.Tags = []string{"style", "experimental", "opinionated"}
info.Summary = "Detects function returning only bool and suggests to add Is/Has/Contains prefix to it's name"
info.Before = "func Enabled() bool"
info.After = "func IsEnabled() bool"
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
return astwalk.WalkerForFuncDecl(&boolFuncPrefixChecker{ctx: ctx})
})
}
type boolFuncPrefixChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext
}
func (c *boolFuncPrefixChecker) VisitFuncDecl(decl *ast.FuncDecl) {
params := decl.Type.Params
results := decl.Type.Results
if params.NumFields() > 0 ||
results.NumFields() != 1 ||
!c.isBoolType(results.List[0].Type) ||
c.hasProperPrefix(decl.Name.Name) {
return
}
c.warn(decl)
}
func (c *boolFuncPrefixChecker) warn(fn *ast.FuncDecl) {
c.ctx.Warn(fn, "consider to add Is/Has/Contains prefix to function name")
}
func (c *boolFuncPrefixChecker) isBoolType(expr ast.Expr) bool {
typ, ok := c.ctx.TypesInfo.TypeOf(expr).(*types.Basic)
return ok && typ.Kind() == types.Bool
}
func (c *boolFuncPrefixChecker) hasProperPrefix(name string) bool {
name = strings.ToLower(name)
excluded := []string{"exit", "quit"}
for _, ex := range excluded {
if name == ex {
return true
}
}
prefixes := []string{"is", "has", "contains", "check", "get", "should", "need", "may", "should"}
for _, p := range prefixes {
if strings.HasPrefix(name, p) {
return true
}
}
return false
}