Skip to content

Commit 0c0e3e9

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

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
@@ -559,6 +559,20 @@ linters-settings:
559559
- shadow
560560
disable-all: false
561561

562+
grouper:
563+
# const
564+
const-require-single-const: false
565+
const-require-grouping: false
566+
# import
567+
import-require-single-import: false
568+
import-require-grouping: false
569+
# type
570+
type-require-single-type: false
571+
type-require-grouping: false
572+
# var
573+
var-require-single-var: false
574+
var-require-grouping: false
575+
562576
ifshort:
563577
# Maximum length of variable declaration measured in number of lines, after which linter won't suggest using short syntax.
564578
# Has higher priority than max-decl-chars.

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
@@ -131,6 +131,7 @@ type LintersSettings struct {
131131
Gosec GoSecSettings
132132
Gosimple StaticCheckSettings
133133
Govet GovetSettings
134+
Grouper GrouperSettings
134135
Ifshort IfshortSettings
135136
ImportAs ImportAsSettings
136137
Ireturn IreturnSettings
@@ -376,6 +377,21 @@ func (cfg GovetSettings) Validate() error {
376377
return nil
377378
}
378379

380+
type GrouperSettings struct {
381+
// const
382+
ConstRequireSingleConst bool `mapstructure:"const-require-single-const"`
383+
ConstRequireGrouping bool `mapstructure:"const-require-grouping"`
384+
// import
385+
ImportRequireSingleImport bool `mapstructure:"import-require-single-import"`
386+
ImportRequireGrouping bool `mapstructure:"import-require-grouping"`
387+
// type
388+
TypeRequireSingleType bool `mapstructure:"type-require-single-type"`
389+
TypeRequireGrouping bool `mapstructure:"type-require-grouping"`
390+
// var
391+
VarRequireSingleVar bool `mapstructure:"var-require-single-var"`
392+
VarRequireGrouping bool `mapstructure:"var-require-grouping"`
393+
}
394+
379395
type IfshortSettings struct {
380396
MaxDeclLines int `mapstructure:"max-decl-lines"`
381397
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
@@ -112,6 +112,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
112112
var gosecCfg *config.GoSecSettings
113113
var gosimpleCfg *config.StaticCheckSettings
114114
var govetCfg *config.GovetSettings
115+
var grouperCfg *config.GrouperSettings
115116
var ifshortCfg *config.IfshortSettings
116117
var importAsCfg *config.ImportAsSettings
117118
var ireturnCfg *config.IreturnSettings
@@ -143,6 +144,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
143144
gosecCfg = &m.cfg.LintersSettings.Gosec
144145
gosimpleCfg = &m.cfg.LintersSettings.Gosimple
145146
govetCfg = &m.cfg.LintersSettings.Govet
147+
grouperCfg = &m.cfg.LintersSettings.Grouper
146148
ifshortCfg = &m.cfg.LintersSettings.Ifshort
147149
importAsCfg = &m.cfg.LintersSettings.ImportAs
148150
ireturnCfg = &m.cfg.LintersSettings.Ireturn
@@ -415,6 +417,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
415417
WithAlternativeNames("vet", "vetshadow").
416418
WithURL("https://golang.org/cmd/vet/"),
417419

420+
linter.NewConfig(golinters.NewGrouper(grouperCfg)).
421+
WithSince("v1.44.0").
422+
WithPresets(linter.PresetStyle).
423+
WithURL("https://github.com/leonklingele/grouper"),
424+
418425
linter.NewConfig(golinters.NewIfshort(ifshortCfg)).
419426
WithSince("v1.36.0").
420427
WithPresets(linter.PresetStyle).

0 commit comments

Comments
 (0)