Skip to content

Commit 0113c7a

Browse files
ldezSeigeC
authored andcommitted
thelper: allow to disable one option (golangci#2854)
1 parent 3513851 commit 0113c7a

File tree

4 files changed

+86
-62
lines changed

4 files changed

+86
-62
lines changed

pkg/config/linters_settings.go

+10-20
Original file line numberDiff line numberDiff line change
@@ -550,26 +550,16 @@ type TestpackageSettings struct {
550550
}
551551

552552
type ThelperSettings struct {
553-
Test struct {
554-
First bool `mapstructure:"first"`
555-
Name bool `mapstructure:"name"`
556-
Begin bool `mapstructure:"begin"`
557-
} `mapstructure:"test"`
558-
Fuzz struct {
559-
First bool `mapstructure:"first"`
560-
Name bool `mapstructure:"name"`
561-
Begin bool `mapstructure:"begin"`
562-
} `mapstructure:"fuzz"`
563-
Benchmark struct {
564-
First bool `mapstructure:"first"`
565-
Name bool `mapstructure:"name"`
566-
Begin bool `mapstructure:"begin"`
567-
} `mapstructure:"benchmark"`
568-
TB struct {
569-
First bool `mapstructure:"first"`
570-
Name bool `mapstructure:"name"`
571-
Begin bool `mapstructure:"begin"`
572-
} `mapstructure:"tb"`
553+
Test ThelperOptions `mapstructure:"test"`
554+
Fuzz ThelperOptions `mapstructure:"fuzz"`
555+
Benchmark ThelperOptions `mapstructure:"benchmark"`
556+
TB ThelperOptions `mapstructure:"tb"`
557+
}
558+
559+
type ThelperOptions struct {
560+
First *bool `mapstructure:"first"`
561+
Name *bool `mapstructure:"name"`
562+
Begin *bool `mapstructure:"begin"`
573563
}
574564

575565
type TenvSettings struct {

pkg/golinters/thelper.go

+53-42
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,44 @@ import (
1313
func NewThelper(cfg *config.ThelperSettings) *goanalysis.Linter {
1414
a := analyzer.NewAnalyzer()
1515

16-
cfgMap := map[string]map[string]interface{}{}
17-
if cfg != nil {
18-
var opts []string
16+
opts := map[string]struct{}{
17+
"t_name": {},
18+
"t_begin": {},
19+
"t_first": {},
1920

20-
if cfg.Test.Name {
21-
opts = append(opts, "t_name")
22-
}
23-
if cfg.Test.Begin {
24-
opts = append(opts, "t_begin")
25-
}
26-
if cfg.Test.First {
27-
opts = append(opts, "t_first")
28-
}
21+
"f_name": {},
22+
"f_begin": {},
23+
"f_first": {},
2924

30-
if cfg.Fuzz.Name {
31-
opts = append(opts, "f_name")
32-
}
33-
if cfg.Fuzz.Begin {
34-
opts = append(opts, "f_begin")
35-
}
36-
if cfg.Fuzz.First {
37-
opts = append(opts, "f_first")
38-
}
25+
"b_name": {},
26+
"b_begin": {},
27+
"b_first": {},
3928

40-
if cfg.Benchmark.Name {
41-
opts = append(opts, "b_name")
42-
}
43-
if cfg.Benchmark.Begin {
44-
opts = append(opts, "b_begin")
45-
}
46-
if cfg.Benchmark.First {
47-
opts = append(opts, "b_first")
48-
}
29+
"tb_name": {},
30+
"tb_begin": {},
31+
"tb_first": {},
32+
}
4933

50-
if cfg.TB.Name {
51-
opts = append(opts, "tb_name")
52-
}
53-
if cfg.TB.Begin {
54-
opts = append(opts, "tb_begin")
55-
}
56-
if cfg.TB.First {
57-
opts = append(opts, "tb_first")
58-
}
34+
if cfg != nil {
35+
applyTHelperOptions(cfg.Test, "t_", opts)
36+
applyTHelperOptions(cfg.Fuzz, "f_", opts)
37+
applyTHelperOptions(cfg.Benchmark, "b_", opts)
38+
applyTHelperOptions(cfg.TB, "tb_", opts)
39+
}
5940

60-
cfgMap[a.Name] = map[string]interface{}{
61-
"checks": strings.Join(opts, ","),
62-
}
41+
if len(opts) == 0 {
42+
linterLogger.Fatalf("thelper: at least one option must be enabled")
43+
}
44+
45+
var args []string
46+
for k := range opts {
47+
args = append(args, k)
48+
}
49+
50+
cfgMap := map[string]map[string]interface{}{
51+
a.Name: {
52+
"checks": strings.Join(args, ","),
53+
},
6354
}
6455

6556
return goanalysis.NewLinter(
@@ -69,3 +60,23 @@ func NewThelper(cfg *config.ThelperSettings) *goanalysis.Linter {
6960
cfgMap,
7061
).WithLoadMode(goanalysis.LoadModeTypesInfo)
7162
}
63+
64+
func applyTHelperOptions(o config.ThelperOptions, prefix string, opts map[string]struct{}) {
65+
if o.Name != nil {
66+
if !*o.Name {
67+
delete(opts, prefix+"name")
68+
}
69+
}
70+
71+
if o.Begin != nil {
72+
if !*o.Begin {
73+
delete(opts, prefix+"begin")
74+
}
75+
}
76+
77+
if o.First != nil {
78+
if !*o.First {
79+
delete(opts, prefix+"first")
80+
}
81+
}
82+
}

test/testdata/configs/thelper.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
linters-settings:
2+
thelper:
3+
test:
4+
name: false
5+
begin: true

test/testdata/thelper_with_options.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// args: -Ethelper
2+
// config_path: testdata/configs/thelper.yml
3+
package testdata
4+
5+
import "testing"
6+
7+
func thelperWithHelperAfterAssignmentWO(t *testing.T) { // ERROR "test helper function should start from t.Helper()"
8+
_ = 0
9+
t.Helper()
10+
}
11+
12+
func thelperWithNotFirstWO(s string, t *testing.T, i int) { // ERROR `parameter \*testing.T should be the first`
13+
t.Helper()
14+
}
15+
16+
func thelperWithIncorrectNameWO(o *testing.T) {
17+
o.Helper()
18+
}

0 commit comments

Comments
 (0)