Skip to content

Commit

Permalink
feat: rewrite fallback condition
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 17, 2024
1 parent 94c8a50 commit 3e50805
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
28 changes: 13 additions & 15 deletions pkg/lint/linter/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package linter

import (
"golang.org/x/tools/go/analysis"
"errors"

"golang.org/x/tools/go/packages"

"github.com/golangci/golangci-lint/pkg/config"
Expand Down Expand Up @@ -133,27 +134,24 @@ func (lc *Config) Name() string {
return lc.Linter.Name()
}

func (lc *Config) WithNoopFallback(cfg *config.Config, cond func(cfg *config.Config) bool) *Config {
if cfg != nil && cond(cfg) {
lc.Linter = &Noop{
name: lc.Linter.Name(),
desc: lc.Linter.Desc(),
reason: "This linter is disabled because the Go version of your project is lower than Go 1.22.",
run: func(_ *analysis.Pass) (any, error) {
return nil, nil
},
}

func (lc *Config) WithNoopFallback(cfg *config.Config, cond func(cfg *config.Config) error) *Config {
if err := cond(cfg); err != nil {
lc.Linter = NewNoop(lc.Linter, err.Error())
lc.LoadMode = 0

return lc.WithLoadFiles()
}

return lc
}

func IsGoLowerThan(limit string) func(cfg *config.Config) bool {
return func(cfg *config.Config) bool {
return cfg != nil && !config.IsGoGreaterThanOrEqual(cfg.Run.Go, limit)
func IsGoLowerThanGo122() func(cfg *config.Config) error {
return func(cfg *config.Config) error {
if cfg == nil || config.IsGoGreaterThanOrEqual(cfg.Run.Go, "1.22") {
return nil
}

return errors.New("this linter is disabled because the Go version of your project is lower than Go 1.22")
}
}

Expand Down
11 changes: 8 additions & 3 deletions pkg/lint/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package linter
import (
"context"

"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/result"
)

Expand All @@ -18,7 +16,14 @@ type Noop struct {
name string
desc string
reason string
run func(pass *analysis.Pass) (any, error)
}

func NewNoop(l Linter, reason string) *Noop {
return &Noop{
name: l.Name(),
desc: l.Desc(),
reason: reason,
}
}

func (n Noop) Run(_ context.Context, lintCtx *Context) ([]result.Issue, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithSince("v1.57.0").
WithPresets(linter.PresetStyle).
WithURL("https://github.com/karamaru-alpha/copyloopvar").
WithNoopFallback(m.cfg, linter.IsGoLowerThan("1.22")),
WithNoopFallback(m.cfg, linter.IsGoLowerThanGo122()),

linter.NewConfig(golinters.NewCyclop(cyclopCfg)).
WithSince("v1.37.0").
Expand Down Expand Up @@ -619,7 +619,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
linter.NewConfig(golinters.NewIntrange()).
WithSince("v1.57.0").
WithURL("https://github.com/ckaznocha/intrange").
WithNoopFallback(m.cfg, linter.IsGoLowerThan("1.22")),
WithNoopFallback(m.cfg, linter.IsGoLowerThanGo122()),

linter.NewConfig(golinters.NewIreturn(ireturnCfg)).
WithSince("v1.43.0").
Expand Down

0 comments on commit 3e50805

Please sign in to comment.