Skip to content

Commit 3922fe5

Browse files
committed
review: settings validation
1 parent 21e4af2 commit 3922fe5

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

pkg/golinters/godoclint/godoclint.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
package godoclint
22

33
import (
4+
"errors"
5+
"fmt"
6+
"slices"
7+
48
glcompose "github.com/godoc-lint/godoc-lint/pkg/compose"
59
glconfig "github.com/godoc-lint/godoc-lint/pkg/config"
610

711
"github.com/golangci/golangci-lint/v2/pkg/config"
812
"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
13+
"github.com/golangci/golangci-lint/v2/pkg/golinters/internal"
914
)
1015

1116
func New(settings *config.GodoclintSettings) *goanalysis.Linter {
1217
var pcfg glconfig.PlainConfig
1318

1419
if settings != nil {
20+
err := checkSettings(settings)
21+
if err != nil {
22+
internal.LinterLogger.Fatalf("godoclint: %v", err)
23+
}
24+
1525
// The following options are explicitly ignored: they must be handled globally with exclusions or nolint directives.
1626
// - Include
1727
// - Exclude
@@ -47,11 +57,52 @@ func New(settings *config.GodoclintSettings) *goanalysis.Linter {
4757

4858
composition := glcompose.Compose(glcompose.CompositionConfig{
4959
BaseDirPlainConfig: &pcfg,
60+
ExitFunc: func(_ int, err error) {
61+
internal.LinterLogger.Errorf("godoclint: %v", err)
62+
},
5063
})
5164

5265
return goanalysis.
5366
NewLinterFromAnalyzer(composition.Analyzer.GetAnalyzer()).
5467
WithLoadMode(goanalysis.LoadModeSyntax)
5568
}
5669

70+
func checkSettings(settings *config.GodoclintSettings) error {
71+
switch deref(settings.Default) {
72+
case "all":
73+
if len(settings.Enable) > 0 {
74+
return errors.New("cannot use 'enable' with 'default=all'")
75+
}
76+
77+
case "none":
78+
if len(settings.Disable) > 0 {
79+
return errors.New("cannot use 'disable' with 'default=none'")
80+
}
81+
82+
default:
83+
for _, rule := range settings.Enable {
84+
if slices.Contains(settings.Disable, rule) {
85+
return fmt.Errorf("a rule cannot be enabled and disabled at the same time: '%s'", rule)
86+
}
87+
}
88+
89+
for _, rule := range settings.Disable {
90+
if slices.Contains(settings.Enable, rule) {
91+
return fmt.Errorf("a rule cannot be enabled and disabled at the same time: '%s'", rule)
92+
}
93+
}
94+
}
95+
96+
return nil
97+
}
98+
5799
func pointer[T any](v T) *T { return &v }
100+
101+
func deref[T any](v *T) T {
102+
if v == nil {
103+
var zero T
104+
return zero
105+
}
106+
107+
return *v
108+
}

0 commit comments

Comments
 (0)