Skip to content

Commit

Permalink
Add "grouper" linter
Browse files Browse the repository at this point in the history
grouper — a Go linter to analyze expression groups
https://github.com/leonklingele/grouper
  • Loading branch information
leonklingele committed Jan 20, 2022
1 parent e3d0247 commit 62b4d6a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .golangci.example.yml
Expand Up @@ -516,6 +516,20 @@ linters-settings:
- shadow
disable-all: false

grouper:
# const
const-require-single-const: false
const-require-grouping: false
# import
import-require-single-import: false
import-require-grouping: false
# type
type-require-single-type: false
type-require-grouping: false
# var
var-require-single-var: false
var-require-grouping: false

depguard:
list-type: denylist
include-go-root: false
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -50,6 +50,7 @@ require (
github.com/kyoh86/exportloopref v0.1.8
github.com/ldez/gomoddirectives v0.2.2
github.com/ldez/tagliatelle v0.3.0
github.com/leonklingele/grouper v1.1.0
github.com/maratori/testpackage v1.0.1
github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0
github.com/mattn/go-colorable v0.1.12
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -127,6 +127,7 @@ type LintersSettings struct {
Gosec GoSecSettings
Gosimple StaticCheckSettings
Govet GovetSettings
Grouper GrouperSettings
Ifshort IfshortSettings
ImportAs ImportAsSettings
Ireturn IreturnSettings
Expand Down Expand Up @@ -368,6 +369,21 @@ func (cfg GovetSettings) Validate() error {
return nil
}

type GrouperSettings struct {
// const
ConstRequireSingleConst bool `mapstructure:"const-require-single-const"`
ConstRequireGrouping bool `mapstructure:"const-require-grouping"`
// import
ImportRequireSingleImport bool `mapstructure:"import-require-single-import"`
ImportRequireGrouping bool `mapstructure:"import-require-grouping"`
// type
TypeRequireSingleType bool `mapstructure:"type-require-single-type"`
TypeRequireGrouping bool `mapstructure:"type-require-grouping"`
// var
VarRequireSingleVar bool `mapstructure:"var-require-single-var"`
VarRequireGrouping bool `mapstructure:"var-require-grouping"`
}

type IfshortSettings struct {
MaxDeclLines int `mapstructure:"max-decl-lines"`
MaxDeclChars int `mapstructure:"max-decl-chars"`
Expand Down
37 changes: 37 additions & 0 deletions pkg/golinters/grouper.go
@@ -0,0 +1,37 @@
package golinters

import (
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"

grouper "github.com/leonklingele/grouper/pkg/analyzer"
)

func NewGrouper(settings *config.GrouperSettings) *goanalysis.Linter {
linterCfg := map[string]map[string]interface{}{}
if settings != nil {
linterCfg["grouper"] = map[string]interface{}{
// const
"const-require-single-const": settings.ConstRequireSingleConst,
"const-require-grouping": settings.ConstRequireGrouping,
// import
"import-require-single-import": settings.ImportRequireSingleImport,
"import-require-grouping": settings.ImportRequireGrouping,
// type
"type-require-single-type": settings.TypeRequireSingleType,
"type-require-grouping": settings.TypeRequireGrouping,
// var
"var-require-single-var": settings.VarRequireSingleVar,
"var-require-grouping": settings.VarRequireGrouping,
}
}

return goanalysis.NewLinter(
"grouper",
"An analyzer to analyze expression groups.",
[]*analysis.Analyzer{grouper.New()},
linterCfg,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -111,6 +111,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var gosecCfg *config.GoSecSettings
var gosimpleCfg *config.StaticCheckSettings
var govetCfg *config.GovetSettings
var grouperCfg *config.GrouperSettings
var ifshortCfg *config.IfshortSettings
var importAsCfg *config.ImportAsSettings
var ireturnCfg *config.IreturnSettings
Expand Down Expand Up @@ -141,6 +142,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
gosecCfg = &m.cfg.LintersSettings.Gosec
gosimpleCfg = &m.cfg.LintersSettings.Gosimple
govetCfg = &m.cfg.LintersSettings.Govet
grouperCfg = &m.cfg.LintersSettings.Grouper
ifshortCfg = &m.cfg.LintersSettings.Ifshort
importAsCfg = &m.cfg.LintersSettings.ImportAs
ireturnCfg = &m.cfg.LintersSettings.Ireturn
Expand Down Expand Up @@ -408,6 +410,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithAlternativeNames("vet", "vetshadow").
WithURL("https://golang.org/cmd/vet/"),

linter.NewConfig(golinters.NewGrouper(grouperCfg)).
WithSince("v1.44.0").
WithPresets(linter.PresetStyle).
WithURL("https://github.com/leonklingele/grouper"),

linter.NewConfig(golinters.NewIfshort(ifshortCfg)).
WithSince("v1.36.0").
WithPresets(linter.PresetStyle).
Expand Down

0 comments on commit 62b4d6a

Please sign in to comment.