Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "grouper" linter #2497

Merged
merged 5 commits into from Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions .golangci.example.yml
Expand Up @@ -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.
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.

12 changes: 12 additions & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -131,6 +131,7 @@ type LintersSettings struct {
Gosec GoSecSettings
Gosimple StaticCheckSettings
Govet GovetSettings
Grouper GrouperSettings
Ifshort IfshortSettings
ImportAs ImportAsSettings
Ireturn IreturnSettings
Expand Down Expand Up @@ -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"`
Expand Down
32 changes: 32 additions & 0 deletions pkg/golinters/grouper.go
@@ -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)
}
7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down