|
| 1 | +package godoclint |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + |
| 8 | + glcompose "github.com/godoc-lint/godoc-lint/pkg/compose" |
| 9 | + glconfig "github.com/godoc-lint/godoc-lint/pkg/config" |
| 10 | + "github.com/godoc-lint/godoc-lint/pkg/model" |
| 11 | + |
| 12 | + "github.com/golangci/golangci-lint/v2/pkg/config" |
| 13 | + "github.com/golangci/golangci-lint/v2/pkg/goanalysis" |
| 14 | + "github.com/golangci/golangci-lint/v2/pkg/golinters/internal" |
| 15 | +) |
| 16 | + |
| 17 | +func New(settings *config.GodoclintSettings) *goanalysis.Linter { |
| 18 | + var pcfg glconfig.PlainConfig |
| 19 | + |
| 20 | + if settings != nil { |
| 21 | + err := checkSettings(settings) |
| 22 | + if err != nil { |
| 23 | + internal.LinterLogger.Fatalf("godoclint: %v", err) |
| 24 | + } |
| 25 | + |
| 26 | + // The following options are explicitly ignored: they must be handled globally with exclusions or nolint directives. |
| 27 | + // - Include |
| 28 | + // - Exclude |
| 29 | + |
| 30 | + // The following options are explicitly ignored: these options cannot work as expected because the global configuration about tests. |
| 31 | + // - Options.MaxLenIncludeTests |
| 32 | + // - Options.PkgDocIncludeTests |
| 33 | + // - Options.SinglePkgDocIncludeTests |
| 34 | + // - Options.RequirePkgDocIncludeTests |
| 35 | + // - Options.RequireDocIncludeTests |
| 36 | + // - Options.StartWithNameIncludeTests |
| 37 | + // - Options.NoUnusedLinkIncludeTests |
| 38 | + |
| 39 | + pcfg = glconfig.PlainConfig{ |
| 40 | + Default: settings.Default, |
| 41 | + Enable: settings.Enable, |
| 42 | + Disable: settings.Disable, |
| 43 | + Options: &glconfig.PlainRuleOptions{ |
| 44 | + MaxLenLength: settings.Options.MaxLen.Length, |
| 45 | + MaxLenIncludeTests: pointer(true), |
| 46 | + PkgDocIncludeTests: pointer(false), |
| 47 | + SinglePkgDocIncludeTests: pointer(true), |
| 48 | + RequirePkgDocIncludeTests: pointer(false), |
| 49 | + RequireDocIncludeTests: pointer(true), |
| 50 | + RequireDocIgnoreExported: settings.Options.RequireDoc.IgnoreExported, |
| 51 | + RequireDocIgnoreUnexported: settings.Options.RequireDoc.IgnoreUnexported, |
| 52 | + StartWithNameIncludeTests: pointer(false), |
| 53 | + StartWithNameIncludeUnexported: settings.Options.StartWithName.IncludeUnexported, |
| 54 | + NoUnusedLinkIncludeTests: pointer(true), |
| 55 | + }, |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + composition := glcompose.Compose(glcompose.CompositionConfig{ |
| 60 | + BaseDirPlainConfig: &pcfg, |
| 61 | + ExitFunc: func(_ int, err error) { |
| 62 | + internal.LinterLogger.Errorf("godoclint: %v", err) |
| 63 | + }, |
| 64 | + }) |
| 65 | + |
| 66 | + return goanalysis. |
| 67 | + NewLinterFromAnalyzer(composition.Analyzer.GetAnalyzer()). |
| 68 | + WithLoadMode(goanalysis.LoadModeSyntax) |
| 69 | +} |
| 70 | + |
| 71 | +func checkSettings(settings *config.GodoclintSettings) error { |
| 72 | + switch deref(settings.Default) { |
| 73 | + case string(model.DefaultSetAll): |
| 74 | + if len(settings.Enable) > 0 { |
| 75 | + return errors.New("cannot use 'enable' with 'default=all'") |
| 76 | + } |
| 77 | + |
| 78 | + case string(model.DefaultSetNone): |
| 79 | + if len(settings.Disable) > 0 { |
| 80 | + return errors.New("cannot use 'disable' with 'default=none'") |
| 81 | + } |
| 82 | + |
| 83 | + default: |
| 84 | + for _, rule := range settings.Enable { |
| 85 | + if slices.Contains(settings.Disable, rule) { |
| 86 | + return fmt.Errorf("a rule cannot be enabled and disabled at the same time: '%s'", rule) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + for _, rule := range settings.Disable { |
| 91 | + if slices.Contains(settings.Enable, rule) { |
| 92 | + return fmt.Errorf("a rule cannot be enabled and disabled at the same time: '%s'", rule) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +func pointer[T any](v T) *T { return &v } |
| 101 | + |
| 102 | +func deref[T any](v *T) T { |
| 103 | + if v == nil { |
| 104 | + var zero T |
| 105 | + return zero |
| 106 | + } |
| 107 | + |
| 108 | + return *v |
| 109 | +} |
0 commit comments