Add "grouper" linter (#2497)

This commit is contained in:
leonklingele 2022-01-25 01:24:35 +01:00 committed by GitHub
parent 620bd9bb3d
commit 32cf48edc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 0 deletions

View File

@ -806,6 +806,35 @@ linters-settings:
- unusedresult
- unusedwrite
grouper:
# Require the use of a single global 'const' declaration only.
# Default: false
const-require-single-const: true
# Require the use of grouped global 'const' declarations.
# Default: false
const-require-grouping: true
# Require the use of a single 'import' declaration only.
# Default: false
import-require-single-import: true
# Require the use of grouped 'import' declarations.
# Default: false
import-require-grouping: true
# Require the use of a single global 'type' declaration only.
# Default: false
type-require-single-type: true
# Require the use of grouped global 'type' declarations.
# Default: false
type-require-grouping: true
# Require the use of a single global 'var' declaration only.
# Default: false
var-require-single-var: true
# Require the use of grouped global 'var' declarations.
# Default: false
var-require-grouping: true
ifshort:
# Maximum length of variable declaration measured in number of lines, after which linter won't suggest using short syntax.
# Has higher priority than max-decl-chars.

1
go.mod
View File

@ -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

2
go.sum generated
View File

@ -489,6 +489,8 @@ github.com/ldez/gomoddirectives v0.2.2 h1:p9/sXuNFArS2RLc+UpYZSI4KQwGMEDWC/LbtF5
github.com/ldez/gomoddirectives v0.2.2/go.mod h1:cpgBogWITnCfRq2qGoDkKMEVSaarhdBr6g8G04uz6d0=
github.com/ldez/tagliatelle v0.3.0 h1:Aubm2ZsrsjIGFvdxemMPJaXrSJ5Cys6VWyTQFt9k2dI=
github.com/ldez/tagliatelle v0.3.0/go.mod h1:8s6WJQwEYHbKZDsp/LjArytKOG8qaMrKQQ3mFukHs88=
github.com/leonklingele/grouper v1.1.0 h1:tC2y/ygPbMFSBOs3DcyaEMKnnwH7eYKzohOtRrf0SAg=
github.com/leonklingele/grouper v1.1.0/go.mod h1:uk3I3uDfi9B6PeUjsCKi6ndcf63Uy7snXgR4yDYQVDY=
github.com/letsencrypt/pkcs11key/v4 v4.0.0/go.mod h1:EFUvBDay26dErnNb70Nd0/VW3tJiIbETBPTl9ATXQag=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=

View File

@ -131,6 +131,7 @@ type LintersSettings struct {
Gosec GoSecSettings
Gosimple StaticCheckSettings
Govet GovetSettings
Grouper GrouperSettings
Ifshort IfshortSettings
ImportAs ImportAsSettings
Ireturn IreturnSettings
@ -376,6 +377,17 @@ func (cfg GovetSettings) Validate() error {
return nil
}
type GrouperSettings struct {
ConstRequireSingleConst bool `mapstructure:"const-require-single-const"`
ConstRequireGrouping bool `mapstructure:"const-require-grouping"`
ImportRequireSingleImport bool `mapstructure:"import-require-single-import"`
ImportRequireGrouping bool `mapstructure:"import-require-grouping"`
TypeRequireSingleType bool `mapstructure:"type-require-single-type"`
TypeRequireGrouping bool `mapstructure:"type-require-grouping"`
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"`

32
pkg/golinters/grouper.go Normal file
View File

@ -0,0 +1,32 @@
package golinters
import (
grouper "github.com/leonklingele/grouper/pkg/analyzer"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)
func NewGrouper(settings *config.GrouperSettings) *goanalysis.Linter {
linterCfg := map[string]map[string]interface{}{}
if settings != nil {
linterCfg["grouper"] = map[string]interface{}{
"const-require-single-const": settings.ConstRequireSingleConst,
"const-require-grouping": settings.ConstRequireGrouping,
"import-require-single-import": settings.ImportRequireSingleImport,
"import-require-grouping": settings.ImportRequireGrouping,
"type-require-single-type": settings.TypeRequireSingleType,
"type-require-grouping": settings.TypeRequireGrouping,
"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)
}

View File

@ -112,6 +112,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
@ -143,6 +144,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
@ -415,6 +417,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).