|
1 | 1 | package godoclint
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + |
4 | 8 | glcompose "github.com/godoc-lint/godoc-lint/pkg/compose"
|
5 | 9 | glconfig "github.com/godoc-lint/godoc-lint/pkg/config"
|
6 | 10 |
|
7 | 11 | "github.com/golangci/golangci-lint/v2/pkg/config"
|
8 | 12 | "github.com/golangci/golangci-lint/v2/pkg/goanalysis"
|
| 13 | + "github.com/golangci/golangci-lint/v2/pkg/golinters/internal" |
9 | 14 | )
|
10 | 15 |
|
11 | 16 | func New(settings *config.GodoclintSettings) *goanalysis.Linter {
|
12 | 17 | var pcfg glconfig.PlainConfig
|
13 | 18 |
|
14 | 19 | if settings != nil {
|
| 20 | + err := checkSettings(settings) |
| 21 | + if err != nil { |
| 22 | + internal.LinterLogger.Fatalf("godoclint: %v", err) |
| 23 | + } |
| 24 | + |
15 | 25 | // The following options are explicitly ignored: they must be handled globally with exclusions or nolint directives.
|
16 | 26 | // - Include
|
17 | 27 | // - Exclude
|
@@ -47,11 +57,52 @@ func New(settings *config.GodoclintSettings) *goanalysis.Linter {
|
47 | 57 |
|
48 | 58 | composition := glcompose.Compose(glcompose.CompositionConfig{
|
49 | 59 | BaseDirPlainConfig: &pcfg,
|
| 60 | + ExitFunc: func(_ int, err error) { |
| 61 | + internal.LinterLogger.Errorf("godoclint: %v", err) |
| 62 | + }, |
50 | 63 | })
|
51 | 64 |
|
52 | 65 | return goanalysis.
|
53 | 66 | NewLinterFromAnalyzer(composition.Analyzer.GetAnalyzer()).
|
54 | 67 | WithLoadMode(goanalysis.LoadModeSyntax)
|
55 | 68 | }
|
56 | 69 |
|
| 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 | + |
57 | 99 | 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