From 1685402de955d30b9cc80f399f3e8720c779be7b Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Wed, 19 Jan 2022 12:46:51 +0100 Subject: [PATCH] gomnd: new configuration (#2498) --- .golangci.example.yml | 38 +++++++++++++++++++++------------- .golangci.yml | 1 + pkg/config/linters_settings.go | 6 +++++- pkg/golinters/gomnd.go | 34 +++++++++++++++++++++++------- pkg/lint/lintersdb/manager.go | 4 +++- 5 files changed, 59 insertions(+), 24 deletions(-) diff --git a/.golangci.example.yml b/.golangci.example.yml index cb8045d635f2..41941d54dd24 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -414,20 +414,30 @@ linters-settings: min-confidence: 0.8 gomnd: - settings: - mnd: - # the list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description. - checks: - - argument - - case - - condition - - operation - - return - - assign - # Next settings are expecting comma separated string values - ignored-numbers: "0666,0755,42" # values always ignored: 1, 1.0, 0 and 0.0 - ignored-files: "magic1_.*.go" # values always ignored:_test.go - ignored-functions: "math.*,http.StatusText,make" # values always ignored: time.Time + # List of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description. + checks: + - argument + - case + - condition + - operation + - return + - assign + # List of numbers to exclude from analysis. + # The numbers should be written as string. + # Values always ignored: "1", "1.0", "0" and "0.0" + ignored-numbers: + - '0666' + - '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: # Allow local `replace` directives. Default is false. diff --git a/.golangci.yml b/.golangci.yml index 651a326706a5..1d91641ab188 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -35,6 +35,7 @@ linters-settings: goimports: local-prefixes: github.com/golangci/golangci-lint gomnd: + # TODO(ldez) must be rewritten after the v1.44.0 release. settings: mnd: # don't include the "operation" and "assign" diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 338e6b14d3fa..c8ad6cb84ef4 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -312,7 +312,11 @@ type GoLintSettings 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 { diff --git a/pkg/golinters/gomnd.go b/pkg/golinters/gomnd.go index f7e71b7dae43..15d84b48bfdd 100644 --- a/pkg/golinters/gomnd.go +++ b/pkg/golinters/gomnd.go @@ -8,20 +8,38 @@ import ( "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" ) -func NewGoMND(cfg *config.Config) *goanalysis.Linter { - analyzers := []*analysis.Analyzer{ - mnd.Analyzer, - } - +func NewGoMND(settings *config.GoMndSettings) *goanalysis.Linter { 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( "gomnd", "An analyzer to detect magic numbers.", - analyzers, + []*analysis.Analyzer{mnd.Analyzer}, linterCfg, ).WithLoadMode(goanalysis.LoadModeSyntax) } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 28bf08056a9d..1213dc518e1d 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -108,6 +108,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var exhaustiveCfg *config.ExhaustiveSettings var exhaustiveStructCfg *config.ExhaustiveStructSettings var goModDirectivesCfg *config.GoModDirectivesSettings + var goMndCfg *config.GoMndSettings var gosecCfg *config.GoSecSettings var gosimpleCfg *config.StaticCheckSettings var govetCfg *config.GovetSettings @@ -138,6 +139,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive exhaustiveStructCfg = &m.cfg.LintersSettings.ExhaustiveStruct goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives + goMndCfg = &m.cfg.LintersSettings.Gomnd gosecCfg = &m.cfg.LintersSettings.Gosec gosimpleCfg = &m.cfg.LintersSettings.Gosimple govetCfg = &m.cfg.LintersSettings.Govet @@ -367,7 +369,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/golang/lint"). 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"). WithPresets(linter.PresetStyle). WithURL("https://github.com/tommy-muehle/go-mnd"),