Skip to content

Commit 62b4d6a

Browse files
committed
Add "grouper" linter
grouper — a Go linter to analyze expression groups https://github.com/leonklingele/grouper
1 parent e3d0247 commit 62b4d6a

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

.golangci.example.yml

+14
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,20 @@ linters-settings:
516516
- shadow
517517
disable-all: false
518518

519+
grouper:
520+
# const
521+
const-require-single-const: false
522+
const-require-grouping: false
523+
# import
524+
import-require-single-import: false
525+
import-require-grouping: false
526+
# type
527+
type-require-single-type: false
528+
type-require-grouping: false
529+
# var
530+
var-require-single-var: false
531+
var-require-grouping: false
532+
519533
depguard:
520534
list-type: denylist
521535
include-go-root: false

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ require (
5050
github.com/kyoh86/exportloopref v0.1.8
5151
github.com/ldez/gomoddirectives v0.2.2
5252
github.com/ldez/tagliatelle v0.3.0
53+
github.com/leonklingele/grouper v1.1.0
5354
github.com/maratori/testpackage v1.0.1
5455
github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0
5556
github.com/mattn/go-colorable v0.1.12

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

+16
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ type LintersSettings struct {
127127
Gosec GoSecSettings
128128
Gosimple StaticCheckSettings
129129
Govet GovetSettings
130+
Grouper GrouperSettings
130131
Ifshort IfshortSettings
131132
ImportAs ImportAsSettings
132133
Ireturn IreturnSettings
@@ -368,6 +369,21 @@ func (cfg GovetSettings) Validate() error {
368369
return nil
369370
}
370371

372+
type GrouperSettings struct {
373+
// const
374+
ConstRequireSingleConst bool `mapstructure:"const-require-single-const"`
375+
ConstRequireGrouping bool `mapstructure:"const-require-grouping"`
376+
// import
377+
ImportRequireSingleImport bool `mapstructure:"import-require-single-import"`
378+
ImportRequireGrouping bool `mapstructure:"import-require-grouping"`
379+
// type
380+
TypeRequireSingleType bool `mapstructure:"type-require-single-type"`
381+
TypeRequireGrouping bool `mapstructure:"type-require-grouping"`
382+
// var
383+
VarRequireSingleVar bool `mapstructure:"var-require-single-var"`
384+
VarRequireGrouping bool `mapstructure:"var-require-grouping"`
385+
}
386+
371387
type IfshortSettings struct {
372388
MaxDeclLines int `mapstructure:"max-decl-lines"`
373389
MaxDeclChars int `mapstructure:"max-decl-chars"`

pkg/golinters/grouper.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package golinters
2+
3+
import (
4+
"golang.org/x/tools/go/analysis"
5+
6+
"github.com/golangci/golangci-lint/pkg/config"
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
9+
grouper "github.com/leonklingele/grouper/pkg/analyzer"
10+
)
11+
12+
func NewGrouper(settings *config.GrouperSettings) *goanalysis.Linter {
13+
linterCfg := map[string]map[string]interface{}{}
14+
if settings != nil {
15+
linterCfg["grouper"] = map[string]interface{}{
16+
// const
17+
"const-require-single-const": settings.ConstRequireSingleConst,
18+
"const-require-grouping": settings.ConstRequireGrouping,
19+
// import
20+
"import-require-single-import": settings.ImportRequireSingleImport,
21+
"import-require-grouping": settings.ImportRequireGrouping,
22+
// type
23+
"type-require-single-type": settings.TypeRequireSingleType,
24+
"type-require-grouping": settings.TypeRequireGrouping,
25+
// var
26+
"var-require-single-var": settings.VarRequireSingleVar,
27+
"var-require-grouping": settings.VarRequireGrouping,
28+
}
29+
}
30+
31+
return goanalysis.NewLinter(
32+
"grouper",
33+
"An analyzer to analyze expression groups.",
34+
[]*analysis.Analyzer{grouper.New()},
35+
linterCfg,
36+
).WithLoadMode(goanalysis.LoadModeSyntax)
37+
}

pkg/lint/lintersdb/manager.go

+7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
111111
var gosecCfg *config.GoSecSettings
112112
var gosimpleCfg *config.StaticCheckSettings
113113
var govetCfg *config.GovetSettings
114+
var grouperCfg *config.GrouperSettings
114115
var ifshortCfg *config.IfshortSettings
115116
var importAsCfg *config.ImportAsSettings
116117
var ireturnCfg *config.IreturnSettings
@@ -141,6 +142,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
141142
gosecCfg = &m.cfg.LintersSettings.Gosec
142143
gosimpleCfg = &m.cfg.LintersSettings.Gosimple
143144
govetCfg = &m.cfg.LintersSettings.Govet
145+
grouperCfg = &m.cfg.LintersSettings.Grouper
144146
ifshortCfg = &m.cfg.LintersSettings.Ifshort
145147
importAsCfg = &m.cfg.LintersSettings.ImportAs
146148
ireturnCfg = &m.cfg.LintersSettings.Ireturn
@@ -408,6 +410,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
408410
WithAlternativeNames("vet", "vetshadow").
409411
WithURL("https://golang.org/cmd/vet/"),
410412

413+
linter.NewConfig(golinters.NewGrouper(grouperCfg)).
414+
WithSince("v1.44.0").
415+
WithPresets(linter.PresetStyle).
416+
WithURL("https://github.com/leonklingele/grouper"),
417+
411418
linter.NewConfig(golinters.NewIfshort(ifshortCfg)).
412419
WithSince("v1.36.0").
413420
WithPresets(linter.PresetStyle).

0 commit comments

Comments
 (0)