gomnd: new configuration (#2498)

This commit is contained in:
Ludovic Fernandez 2022-01-19 12:46:51 +01:00 committed by GitHub
parent dff995c6b2
commit 1685402de9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 24 deletions

View File

@ -414,20 +414,30 @@ linters-settings:
min-confidence: 0.8 min-confidence: 0.8
gomnd: gomnd:
settings: # List of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description.
mnd: checks:
# the list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description. - argument
checks: - case
- argument - condition
- case - operation
- condition - return
- operation - assign
- return # List of numbers to exclude from analysis.
- assign # The numbers should be written as string.
# Next settings are expecting comma separated string values # Values always ignored: "1", "1.0", "0" and "0.0"
ignored-numbers: "0666,0755,42" # values always ignored: 1, 1.0, 0 and 0.0 ignored-numbers:
ignored-files: "magic1_.*.go" # values always ignored:_test.go - '0666'
ignored-functions: "math.*,http.StatusText,make" # values always ignored: time.Time - '0755'
- '42'
# List of file patterns to exclude from analysis.
# Values always ignored: `.+_test.go`
ignored-files:
- 'magic1_.*.go'
# List of function patterns to exclude from analysis.
# Values always ignored: `time.Time`
ignored-functions:
- 'math.*'
- 'http.StatusText'
gomoddirectives: gomoddirectives:
# Allow local `replace` directives. Default is false. # Allow local `replace` directives. Default is false.

View File

@ -35,6 +35,7 @@ linters-settings:
goimports: goimports:
local-prefixes: github.com/golangci/golangci-lint local-prefixes: github.com/golangci/golangci-lint
gomnd: gomnd:
# TODO(ldez) must be rewritten after the v1.44.0 release.
settings: settings:
mnd: mnd:
# don't include the "operation" and "assign" # don't include the "operation" and "assign"

View File

@ -312,7 +312,11 @@ type GoLintSettings struct {
} }
type GoMndSettings struct { type GoMndSettings struct {
Settings map[string]map[string]interface{} Settings map[string]map[string]interface{} // Deprecated
Checks []string `mapstructure:"checks"`
IgnoredNumbers []string `mapstructure:"ignored-numbers"`
IgnoredFiles []string `mapstructure:"ignored-files"`
IgnoredFunctions []string `mapstructure:"ignored-functions"`
} }
type GoModDirectivesSettings struct { type GoModDirectivesSettings struct {

View File

@ -8,20 +8,38 @@ import (
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
) )
func NewGoMND(cfg *config.Config) *goanalysis.Linter { func NewGoMND(settings *config.GoMndSettings) *goanalysis.Linter {
analyzers := []*analysis.Analyzer{
mnd.Analyzer,
}
var linterCfg map[string]map[string]interface{} var linterCfg map[string]map[string]interface{}
if cfg != nil {
linterCfg = cfg.LintersSettings.Gomnd.Settings if settings != nil {
// TODO(ldez) For compatibility only, must be drop in v2.
if len(settings.Settings) > 0 {
linterCfg = settings.Settings
} else {
cfg := make(map[string]interface{})
if len(settings.Checks) > 0 {
cfg["checks"] = settings.Checks
}
if len(settings.IgnoredNumbers) > 0 {
cfg["ignored-numbers"] = settings.IgnoredNumbers
}
if len(settings.IgnoredFiles) > 0 {
cfg["ignored-files"] = settings.IgnoredFiles
}
if len(settings.IgnoredFunctions) > 0 {
cfg["ignored-functions"] = settings.IgnoredFunctions
}
linterCfg = map[string]map[string]interface{}{
"mnd": cfg,
}
}
} }
return goanalysis.NewLinter( return goanalysis.NewLinter(
"gomnd", "gomnd",
"An analyzer to detect magic numbers.", "An analyzer to detect magic numbers.",
analyzers, []*analysis.Analyzer{mnd.Analyzer},
linterCfg, linterCfg,
).WithLoadMode(goanalysis.LoadModeSyntax) ).WithLoadMode(goanalysis.LoadModeSyntax)
} }

View File

@ -108,6 +108,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var exhaustiveCfg *config.ExhaustiveSettings var exhaustiveCfg *config.ExhaustiveSettings
var exhaustiveStructCfg *config.ExhaustiveStructSettings var exhaustiveStructCfg *config.ExhaustiveStructSettings
var goModDirectivesCfg *config.GoModDirectivesSettings var goModDirectivesCfg *config.GoModDirectivesSettings
var goMndCfg *config.GoMndSettings
var gosecCfg *config.GoSecSettings var gosecCfg *config.GoSecSettings
var gosimpleCfg *config.StaticCheckSettings var gosimpleCfg *config.StaticCheckSettings
var govetCfg *config.GovetSettings var govetCfg *config.GovetSettings
@ -138,6 +139,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive
exhaustiveStructCfg = &m.cfg.LintersSettings.ExhaustiveStruct exhaustiveStructCfg = &m.cfg.LintersSettings.ExhaustiveStruct
goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives
goMndCfg = &m.cfg.LintersSettings.Gomnd
gosecCfg = &m.cfg.LintersSettings.Gosec gosecCfg = &m.cfg.LintersSettings.Gosec
gosimpleCfg = &m.cfg.LintersSettings.Gosimple gosimpleCfg = &m.cfg.LintersSettings.Gosimple
govetCfg = &m.cfg.LintersSettings.Govet govetCfg = &m.cfg.LintersSettings.Govet
@ -367,7 +369,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithURL("https://github.com/golang/lint"). WithURL("https://github.com/golang/lint").
Deprecated("The repository of the linter has been archived by the owner.", "v1.41.0", "revive"), Deprecated("The repository of the linter has been archived by the owner.", "v1.41.0", "revive"),
linter.NewConfig(golinters.NewGoMND(m.cfg)). linter.NewConfig(golinters.NewGoMND(goMndCfg)).
WithSince("v1.22.0"). WithSince("v1.22.0").
WithPresets(linter.PresetStyle). WithPresets(linter.PresetStyle).
WithURL("https://github.com/tommy-muehle/go-mnd"), WithURL("https://github.com/tommy-muehle/go-mnd"),