Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Aug 19, 2022
1 parent 4f5bc60 commit 932adf6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .golangci.reference.yml
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -151,6 +151,7 @@ type LintersSettings struct {
Grouper GrouperSettings
Ifshort IfshortSettings
ImportAs ImportAsSettings
InterfaceBloat InterfaceBloatSettings
Ireturn IreturnSettings
Lll LllSettings
MaintIdx MaintIdxSettings
Expand Down Expand Up @@ -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"`
Expand Down
12 changes: 10 additions & 2 deletions pkg/golinters/interfacebloat.go
Expand Up @@ -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)
}
7 changes: 4 additions & 3 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"),

Expand Down
14 changes: 8 additions & 6 deletions 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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 932adf6

Please sign in to comment.