forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththelper.go
82 lines (66 loc) · 1.57 KB
/
thelper.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package golinters
import (
"strings"
"github.com/kulti/thelper/pkg/analyzer"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)
func NewThelper(cfg *config.ThelperSettings) *goanalysis.Linter {
a := analyzer.NewAnalyzer()
opts := map[string]struct{}{
"t_name": {},
"t_begin": {},
"t_first": {},
"f_name": {},
"f_begin": {},
"f_first": {},
"b_name": {},
"b_begin": {},
"b_first": {},
"tb_name": {},
"tb_begin": {},
"tb_first": {},
}
if cfg != nil {
applyTHelperOptions(cfg.Test, "t_", opts)
applyTHelperOptions(cfg.Fuzz, "f_", opts)
applyTHelperOptions(cfg.Benchmark, "b_", opts)
applyTHelperOptions(cfg.TB, "tb_", opts)
}
if len(opts) == 0 {
linterLogger.Fatalf("thelper: at least one option must be enabled")
}
var args []string
for k := range opts {
args = append(args, k)
}
cfgMap := map[string]map[string]interface{}{
a.Name: {
"checks": strings.Join(args, ","),
},
}
return goanalysis.NewLinter(
"thelper",
"thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers",
[]*analysis.Analyzer{a},
cfgMap,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
func applyTHelperOptions(o config.ThelperOptions, prefix string, opts map[string]struct{}) {
if o.Name != nil {
if !*o.Name {
delete(opts, prefix+"name")
}
}
if o.Begin != nil {
if !*o.Begin {
delete(opts, prefix+"begin")
}
}
if o.First != nil {
if !*o.First {
delete(opts, prefix+"first")
}
}
}