From efe850b979b144be1ef48e1ae63edf884062b68b Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 12 Jun 2023 13:56:13 +0200 Subject: [PATCH 1/3] chore: simplify GetAllSupportedLinterConfigs method --- pkg/lint/linter/config.go | 5 ++++ pkg/lint/lintersdb/manager.go | 45 +++++++++++++---------------------- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/pkg/lint/linter/config.go b/pkg/lint/linter/config.go index 0f546312cd5a..86dad572badd 100644 --- a/pkg/lint/linter/config.go +++ b/pkg/lint/linter/config.go @@ -50,6 +50,11 @@ type Config struct { Deprecation *Deprecation } +func (lc *Config) Default() *Config { + lc.EnabledByDefault = true + return lc +} + func (lc *Config) ConsiderSlow() *Config { lc.IsSlow = true return lc diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 17ce2d9cbdc1..fd1782e7f69d 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -24,6 +24,7 @@ type Manager struct { func NewManager(cfg *config.Config, log logutils.Log) *Manager { m := &Manager{cfg: cfg, log: log} + nameToLCs := make(map[string][]*linter.Config) for _, lc := range m.GetAllSupportedLinterConfigs() { for _, name := range lc.AllNames() { @@ -32,6 +33,7 @@ func NewManager(cfg *config.Config, log logutils.Log) *Manager { } m.nameToLCs = nameToLCs + return m } @@ -87,17 +89,6 @@ func (m Manager) GetLinterConfigs(name string) []*linter.Config { return m.nameToLCs[name] } -func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config) bool) []*linter.Config { - var ret []*linter.Config - for _, lc := range lcs { - lc := lc - lc.EnabledByDefault = isEnabled(lc) - ret = append(ret, lc) - } - - return ret -} - //nolint:funlen func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var ( @@ -289,7 +280,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { // The linters are sorted in the alphabetical order (case-insensitive). // When a new linter is added the version in `WithSince(...)` must be the next minor version of golangci-lint. - lcs := []*linter.Config{ + return []*linter.Config{ linter.NewConfig(golinters.NewAsasalint(asasalintCfg)). WithSince("1.47.0"). WithPresets(linter.PresetBugs). @@ -370,6 +361,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/charithe/durationcheck"), linter.NewConfig(golinters.NewErrcheck(errcheckCfg)). + Default(). WithSince("v1.0.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs, linter.PresetError). @@ -566,6 +558,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithAlternativeNames("gas"), linter.NewConfig(golinters.NewGosimple(gosimpleCfg)). + Default(). WithSince("v1.20.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetStyle). @@ -579,6 +572,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/xen0n/gosmopolitan"), linter.NewConfig(golinters.NewGovet(govetCfg)). + Default(). WithSince("v1.0.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs, linter.PresetMetaLinter). @@ -603,6 +597,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/julz/importas"), linter.NewConfig(golinters.NewIneffassign()). + Default(). WithSince("v1.0.0"). WithPresets(linter.PresetUnused). WithURL("https://github.com/gordonklaus/ineffassign"), @@ -774,6 +769,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/ryanrolds/sqlclosecheck"), linter.NewConfig(golinters.NewStaticcheck(staticcheckCfg)). + Default(). WithSince("v1.0.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs, linter.PresetMetaLinter). @@ -833,6 +829,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/moricho/tparallel"), linter.NewConfig(golinters.NewTypecheck()). + Default(). WithSince("v1.3.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs). @@ -851,6 +848,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/mvdan/unparam"), linter.NewConfig(golinters.NewUnused(unusedCfg)). + Default(). WithSince("v1.20.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetUnused). @@ -912,19 +910,6 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithLoadForGoAnalysis(). WithURL("https://github.com/ykadowak/zerologlint"), } - - enabledByDefault := map[string]bool{ - golinters.NewGovet(nil).Name(): true, - golinters.NewErrcheck(errcheckCfg).Name(): true, - golinters.NewStaticcheck(staticcheckCfg).Name(): true, - golinters.NewUnused(unusedCfg).Name(): true, - golinters.NewGosimple(gosimpleCfg).Name(): true, - golinters.NewIneffassign().Name(): true, - golinters.NewTypecheck().Name(): true, - } - return enableLinterConfigs(lcs, func(lc *linter.Config) bool { - return enabledByDefault[lc.Name()] - }) } func (m Manager) GetAllEnabledByDefaultLinters() []*linter.Config { @@ -979,10 +964,12 @@ func (m Manager) loadCustomLinterConfig(name string, settings config.CustomLinte settings.Description, analyzer.GetAnalyzers(), nil).WithLoadMode(goanalysis.LoadModeTypesInfo) - linterConfig := linter.NewConfig(customLinter) - linterConfig.EnabledByDefault = true - linterConfig.IsSlow = false - linterConfig.WithURL(settings.OriginalURL) + + linterConfig := linter.NewConfig(customLinter). + Default(). + WithLoadForGoAnalysis(). + WithURL(settings.OriginalURL) + return linterConfig, nil } From 75014aaefbe5c59449f518813dfe1ff83e1a1fab Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Tue, 13 Jun 2023 23:22:19 +0200 Subject: [PATCH 2/3] refactor: switch Default and EnabledByDefault names --- pkg/commands/help.go | 2 +- pkg/commands/run.go | 2 +- pkg/lint/linter/config.go | 8 ++++---- pkg/lint/lintersdb/enabled_set.go | 2 +- pkg/lint/lintersdb/manager.go | 18 +++++++++--------- scripts/expand_website_templates/main.go | 2 +- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pkg/commands/help.go b/pkg/commands/help.go index 61d362e7d2be..14712fc644cd 100644 --- a/pkg/commands/help.go +++ b/pkg/commands/help.go @@ -63,7 +63,7 @@ func printLinterConfigs(lcs []*linter.Config) { func (e *Executor) executeLintersHelp(_ *cobra.Command, _ []string) { var enabledLCs, disabledLCs []*linter.Config for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() { - if lc.EnabledByDefault { + if lc.Default { enabledLCs = append(enabledLCs, lc) } else { disabledLCs = append(disabledLCs, lc) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 9149b177bcee..c822937fa5c5 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -340,7 +340,7 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) ([]result.Iss for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() { isEnabled := enabledLintersMap[lc.Name()] != nil - e.reportData.AddLinter(lc.Name(), isEnabled, lc.EnabledByDefault) + e.reportData.AddLinter(lc.Name(), isEnabled, lc.Default) } lintCtx, err := e.contextLoader.Load(ctx, lintersToRun) diff --git a/pkg/lint/linter/config.go b/pkg/lint/linter/config.go index 86dad572badd..0921c8d477cc 100644 --- a/pkg/lint/linter/config.go +++ b/pkg/lint/linter/config.go @@ -33,8 +33,8 @@ type Deprecation struct { } type Config struct { - Linter Linter - EnabledByDefault bool + Linter Linter + Default bool LoadMode packages.LoadMode @@ -50,8 +50,8 @@ type Config struct { Deprecation *Deprecation } -func (lc *Config) Default() *Config { - lc.EnabledByDefault = true +func (lc *Config) EnabledByDefault() *Config { + lc.Default = true return lc } diff --git a/pkg/lint/lintersdb/enabled_set.go b/pkg/lint/lintersdb/enabled_set.go index 92615b57aa13..ccd6d2520aed 100644 --- a/pkg/lint/lintersdb/enabled_set.go +++ b/pkg/lint/lintersdb/enabled_set.go @@ -182,7 +182,7 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config) mlConfig := &linter.Config{ Linter: ml, - EnabledByDefault: false, + Default: false, InPresets: presets, AlternativeNames: nil, OriginalURL: "", diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index fd1782e7f69d..0f682a22c03b 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -361,7 +361,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/charithe/durationcheck"), linter.NewConfig(golinters.NewErrcheck(errcheckCfg)). - Default(). + EnabledByDefault(). WithSince("v1.0.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs, linter.PresetError). @@ -558,7 +558,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithAlternativeNames("gas"), linter.NewConfig(golinters.NewGosimple(gosimpleCfg)). - Default(). + EnabledByDefault(). WithSince("v1.20.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetStyle). @@ -572,7 +572,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/xen0n/gosmopolitan"), linter.NewConfig(golinters.NewGovet(govetCfg)). - Default(). + EnabledByDefault(). WithSince("v1.0.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs, linter.PresetMetaLinter). @@ -597,7 +597,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/julz/importas"), linter.NewConfig(golinters.NewIneffassign()). - Default(). + EnabledByDefault(). WithSince("v1.0.0"). WithPresets(linter.PresetUnused). WithURL("https://github.com/gordonklaus/ineffassign"), @@ -769,7 +769,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/ryanrolds/sqlclosecheck"), linter.NewConfig(golinters.NewStaticcheck(staticcheckCfg)). - Default(). + EnabledByDefault(). WithSince("v1.0.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs, linter.PresetMetaLinter). @@ -829,7 +829,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/moricho/tparallel"), linter.NewConfig(golinters.NewTypecheck()). - Default(). + EnabledByDefault(). WithSince("v1.3.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs). @@ -848,7 +848,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/mvdan/unparam"), linter.NewConfig(golinters.NewUnused(unusedCfg)). - Default(). + EnabledByDefault(). WithSince("v1.20.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetUnused). @@ -915,7 +915,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { func (m Manager) GetAllEnabledByDefaultLinters() []*linter.Config { var ret []*linter.Config for _, lc := range m.GetAllSupportedLinterConfigs() { - if lc.EnabledByDefault { + if lc.Default { ret = append(ret, lc) } } @@ -966,7 +966,7 @@ func (m Manager) loadCustomLinterConfig(name string, settings config.CustomLinte nil).WithLoadMode(goanalysis.LoadModeTypesInfo) linterConfig := linter.NewConfig(customLinter). - Default(). + EnabledByDefault(). WithLoadForGoAnalysis(). WithURL(settings.OriginalURL) diff --git a/scripts/expand_website_templates/main.go b/scripts/expand_website_templates/main.go index 1d49885fc4c0..ddd7a2763a0d 100644 --- a/scripts/expand_website_templates/main.go +++ b/scripts/expand_website_templates/main.go @@ -238,7 +238,7 @@ func getLintersListMarkdown(enabled bool) string { var neededLcs []*linter.Config lcs := lintersdb.NewManager(nil, nil).GetAllSupportedLinterConfigs() for _, lc := range lcs { - if lc.EnabledByDefault == enabled { + if lc.Default == enabled { neededLcs = append(neededLcs, lc) } } From 3ebf2b8c6920b874db089853a4c2e209079a05ef Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Tue, 13 Jun 2023 23:25:28 +0200 Subject: [PATCH 3/3] refactor: switch Default and EnabledByDefault names and use WithEnabledByDefault --- pkg/commands/help.go | 2 +- pkg/commands/run.go | 2 +- pkg/lint/linter/config.go | 8 ++++---- pkg/lint/lintersdb/enabled_set.go | 2 +- pkg/lint/lintersdb/manager.go | 18 +++++++++--------- scripts/expand_website_templates/main.go | 2 +- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pkg/commands/help.go b/pkg/commands/help.go index 14712fc644cd..61d362e7d2be 100644 --- a/pkg/commands/help.go +++ b/pkg/commands/help.go @@ -63,7 +63,7 @@ func printLinterConfigs(lcs []*linter.Config) { func (e *Executor) executeLintersHelp(_ *cobra.Command, _ []string) { var enabledLCs, disabledLCs []*linter.Config for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() { - if lc.Default { + if lc.EnabledByDefault { enabledLCs = append(enabledLCs, lc) } else { disabledLCs = append(disabledLCs, lc) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index c822937fa5c5..9149b177bcee 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -340,7 +340,7 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) ([]result.Iss for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() { isEnabled := enabledLintersMap[lc.Name()] != nil - e.reportData.AddLinter(lc.Name(), isEnabled, lc.Default) + e.reportData.AddLinter(lc.Name(), isEnabled, lc.EnabledByDefault) } lintCtx, err := e.contextLoader.Load(ctx, lintersToRun) diff --git a/pkg/lint/linter/config.go b/pkg/lint/linter/config.go index 0921c8d477cc..5891ec277065 100644 --- a/pkg/lint/linter/config.go +++ b/pkg/lint/linter/config.go @@ -33,8 +33,8 @@ type Deprecation struct { } type Config struct { - Linter Linter - Default bool + Linter Linter + EnabledByDefault bool LoadMode packages.LoadMode @@ -50,8 +50,8 @@ type Config struct { Deprecation *Deprecation } -func (lc *Config) EnabledByDefault() *Config { - lc.Default = true +func (lc *Config) WithEnabledByDefault() *Config { + lc.EnabledByDefault = true return lc } diff --git a/pkg/lint/lintersdb/enabled_set.go b/pkg/lint/lintersdb/enabled_set.go index ccd6d2520aed..92615b57aa13 100644 --- a/pkg/lint/lintersdb/enabled_set.go +++ b/pkg/lint/lintersdb/enabled_set.go @@ -182,7 +182,7 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config) mlConfig := &linter.Config{ Linter: ml, - Default: false, + EnabledByDefault: false, InPresets: presets, AlternativeNames: nil, OriginalURL: "", diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 0f682a22c03b..5a04fe193b13 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -361,7 +361,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/charithe/durationcheck"), linter.NewConfig(golinters.NewErrcheck(errcheckCfg)). - EnabledByDefault(). + WithEnabledByDefault(). WithSince("v1.0.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs, linter.PresetError). @@ -558,7 +558,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithAlternativeNames("gas"), linter.NewConfig(golinters.NewGosimple(gosimpleCfg)). - EnabledByDefault(). + WithEnabledByDefault(). WithSince("v1.20.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetStyle). @@ -572,7 +572,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/xen0n/gosmopolitan"), linter.NewConfig(golinters.NewGovet(govetCfg)). - EnabledByDefault(). + WithEnabledByDefault(). WithSince("v1.0.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs, linter.PresetMetaLinter). @@ -597,7 +597,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/julz/importas"), linter.NewConfig(golinters.NewIneffassign()). - EnabledByDefault(). + WithEnabledByDefault(). WithSince("v1.0.0"). WithPresets(linter.PresetUnused). WithURL("https://github.com/gordonklaus/ineffassign"), @@ -769,7 +769,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/ryanrolds/sqlclosecheck"), linter.NewConfig(golinters.NewStaticcheck(staticcheckCfg)). - EnabledByDefault(). + WithEnabledByDefault(). WithSince("v1.0.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs, linter.PresetMetaLinter). @@ -829,7 +829,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/moricho/tparallel"), linter.NewConfig(golinters.NewTypecheck()). - EnabledByDefault(). + WithEnabledByDefault(). WithSince("v1.3.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs). @@ -848,7 +848,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/mvdan/unparam"), linter.NewConfig(golinters.NewUnused(unusedCfg)). - EnabledByDefault(). + WithEnabledByDefault(). WithSince("v1.20.0"). WithLoadForGoAnalysis(). WithPresets(linter.PresetUnused). @@ -915,7 +915,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { func (m Manager) GetAllEnabledByDefaultLinters() []*linter.Config { var ret []*linter.Config for _, lc := range m.GetAllSupportedLinterConfigs() { - if lc.Default { + if lc.EnabledByDefault { ret = append(ret, lc) } } @@ -966,7 +966,7 @@ func (m Manager) loadCustomLinterConfig(name string, settings config.CustomLinte nil).WithLoadMode(goanalysis.LoadModeTypesInfo) linterConfig := linter.NewConfig(customLinter). - EnabledByDefault(). + WithEnabledByDefault(). WithLoadForGoAnalysis(). WithURL(settings.OriginalURL) diff --git a/scripts/expand_website_templates/main.go b/scripts/expand_website_templates/main.go index ddd7a2763a0d..1d49885fc4c0 100644 --- a/scripts/expand_website_templates/main.go +++ b/scripts/expand_website_templates/main.go @@ -238,7 +238,7 @@ func getLintersListMarkdown(enabled bool) string { var neededLcs []*linter.Config lcs := lintersdb.NewManager(nil, nil).GetAllSupportedLinterConfigs() for _, lc := range lcs { - if lc.Default == enabled { + if lc.EnabledByDefault == enabled { neededLcs = append(neededLcs, lc) } }