Skip to content

Commit

Permalink
feat: deprecate usage of linter alternative names
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Mar 24, 2024
1 parent 94a0179 commit 91a8f21
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pkg/lint/lintersdb/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package lintersdb
import (
"errors"
"fmt"
"os"
"slices"
"strings"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/logutils"
)

type Validator struct {
Expand All @@ -28,6 +30,7 @@ func (v Validator) Validate(cfg *config.Config) error {
validators := []func(cfg *config.Linters) error{
v.validateLintersNames,
v.validatePresets,
v.alternativeNamesDeprecation,
}

for _, v := range validators {
Expand Down Expand Up @@ -75,3 +78,35 @@ func (v Validator) validatePresets(cfg *config.Linters) error {

return nil
}

func (v Validator) alternativeNamesDeprecation(cfg *config.Linters) error {
if v.m.cfg.InternalTest || v.m.cfg.InternalCmdTest || os.Getenv(logutils.EnvTestRun) == "1" {
return nil
}

altNames := map[string][]string{}
for _, lc := range v.m.GetAllSupportedLinterConfigs() {
for _, alt := range lc.AlternativeNames {
altNames[alt] = append(altNames[alt], lc.Name())
}
}

var names []string
names = append(names, cfg.Enable...)
names = append(names, cfg.Disable...)

for _, name := range names {
lc, ok := altNames[name]
if !ok {
continue
}

if len(lc) > 1 {
v.m.log.Warnf("The linter name %q is deprecated. It has been splited into: %s.", name, strings.Join(lc, ", "))
} else {
v.m.log.Warnf("The linter name %q is deprecated. It has been renamed to: %s.", name, lc[0])
}
}

return nil
}
23 changes: 23 additions & 0 deletions pkg/lint/lintersdb/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/logutils"
)

type validateErrorTestCase struct {
Expand Down Expand Up @@ -215,3 +216,25 @@ func TestValidator_validatePresets_error(t *testing.T) {
})
}
}

func TestValidator_alternativeNamesDeprecation(t *testing.T) {
log := logutils.NewMockLog()
log.On("Warnf", "The linter name %q is deprecated. It has been renamed to: %s.", "vet", "govet")
log.On("Warnf", "The linter name %q is deprecated. It has been renamed to: %s.", "vetshadow", "govet")
log.On("Warnf", "The linter name %q is deprecated. It has been renamed to: %s.", "logrlint", "loggercheck")
log.On("Warnf", "The linter name %q is deprecated. It has been splited into: %s.", "megacheck", "gosimple, staticcheck, unused")
log.On("Warnf", "The linter name %q is deprecated. It has been renamed to: %s.", "gas", "gosec")

m, err := NewManager(log, nil, NewLinterBuilder())
require.NoError(t, err)

v := NewValidator(m)

cfg := &config.Linters{
Enable: []string{"vet", "vetshadow", "logrlint"},
Disable: []string{"megacheck", "gas"},
}

err = v.alternativeNamesDeprecation(cfg)
require.NoError(t, err)
}

0 comments on commit 91a8f21

Please sign in to comment.