Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gomnd: new configuration #2498

Merged
merged 2 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,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 {
Expand Down
34 changes: 26 additions & 8 deletions pkg/golinters/gomnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 3 additions & 1 deletion pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -137,6 +138,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
Expand Down Expand Up @@ -365,7 +367,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"),
Expand Down