From 932adf61f9a96028939c9781f580eeef4cde6f5b Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 20 Aug 2022 00:26:49 +0200 Subject: [PATCH] review --- .golangci.reference.yml | 4 +++- pkg/config/linters_settings.go | 5 +++++ pkg/golinters/interfacebloat.go | 12 ++++++++++-- pkg/lint/lintersdb/manager.go | 7 ++++--- test/testdata/interfacebloat.go | 14 ++++++++------ 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/.golangci.reference.yml b/.golangci.reference.yml index ec805b1cbaad..d2fd03079557 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1073,7 +1073,9 @@ linters-settings: alias: $1$2 interfacebloat: - interface-len: 10 + # The number of allowed methods for an interface. + # Default: 10 + len: 5 ireturn: # ireturn allows using `allow` and `reject` settings at the same time. diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 1e923409a05d..2e5cef9707d2 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -151,6 +151,7 @@ type LintersSettings struct { Grouper GrouperSettings Ifshort IfshortSettings ImportAs ImportAsSettings + InterfaceBloat InterfaceBloatSettings Ireturn IreturnSettings Lll LllSettings MaintIdx MaintIdxSettings @@ -454,6 +455,10 @@ type ImportAsAlias struct { Alias string } +type InterfaceBloatSettings struct { + Len int `mapstructure:"len"` +} + type IreturnSettings struct { Allow []string `mapstructure:"allow"` Reject []string `mapstructure:"reject"` diff --git a/pkg/golinters/interfacebloat.go b/pkg/golinters/interfacebloat.go index c04eee7091ff..709436c7e74a 100644 --- a/pkg/golinters/interfacebloat.go +++ b/pkg/golinters/interfacebloat.go @@ -4,16 +4,24 @@ import ( "github.com/sashamelentyev/interfacebloat/pkg/analyzer" "golang.org/x/tools/go/analysis" + "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" ) -func NewInterfaceBloat() *goanalysis.Linter { +func NewInterfaceBloat(settings *config.InterfaceBloatSettings) *goanalysis.Linter { a := analyzer.New() + cfgMap := make(map[string]map[string]interface{}) + if settings != nil { + cfgMap[a.Name] = map[string]interface{}{ + analyzer.InterfaceLenFlag: settings.Len, + } + } + return goanalysis.NewLinter( a.Name, a.Doc, []*analysis.Analyzer{a}, nil, - ) + ).WithLoadMode(goanalysis.LoadModeSyntax) } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 507eedbc4e2e..4d69aac6f6d2 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -137,6 +137,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { grouperCfg *config.GrouperSettings ifshortCfg *config.IfshortSettings importAsCfg *config.ImportAsSettings + interfaceBloatCfg *config.InterfaceBloatSettings ireturnCfg *config.IreturnSettings lllCfg *config.LllSettings maintIdxCfg *config.MaintIdxSettings @@ -209,6 +210,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { grouperCfg = &m.cfg.LintersSettings.Grouper ifshortCfg = &m.cfg.LintersSettings.Ifshort importAsCfg = &m.cfg.LintersSettings.ImportAs + interfaceBloatCfg = &m.cfg.LintersSettings.InterfaceBloat ireturnCfg = &m.cfg.LintersSettings.Ireturn lllCfg = &m.cfg.LintersSettings.Lll maintIdxCfg = &m.cfg.LintersSettings.MaintIdx @@ -557,9 +559,8 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetUnused). WithURL("https://github.com/gordonklaus/ineffassign"), - linter.NewConfig(golinters.NewInterfaceBloat()). - WithSince("v1.48.0"). - WithLoadForGoAnalysis(). + linter.NewConfig(golinters.NewInterfaceBloat(interfaceBloatCfg)). + WithSince("v1.49.0"). WithPresets(linter.PresetStyle). WithURL("https://github.com/sashamelentyev/interfacebloat"), diff --git a/test/testdata/interfacebloat.go b/test/testdata/interfacebloat.go index d562e20ccc19..696e220b79fd 100644 --- a/test/testdata/interfacebloat.go +++ b/test/testdata/interfacebloat.go @@ -1,8 +1,10 @@ //golangcitest:args -Einterfacebloat package testdata -type _ interface { // want "length of interface greater than 10" - a() +import "time" + +type _ interface { // ERROR "length of interface greater than 10" + a() time.Duration b() c() d() @@ -16,8 +18,8 @@ type _ interface { // want "length of interface greater than 10" } func _() { - var _ interface { // want "length of interface greater than 10" - a() + var _ interface { // ERROR "length of interface greater than 10" + a() time.Duration b() c() d() @@ -31,8 +33,8 @@ func _() { } } -func __() interface { // want "length of interface greater than 10" - a() +func __() interface { // ERROR "length of interface greater than 10" + a() time.Duration b() c() d()