Skip to content

Commit 63f150e

Browse files
authored
Add decorder linter (#2453)
1 parent d209389 commit 63f150e

File tree

9 files changed

+136
-0
lines changed

9 files changed

+136
-0
lines changed

.golangci.example.yml

+21
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ linters-settings:
107107
# should ignore tests (default false)
108108
skip-tests: false
109109

110+
decorder:
111+
# required order of type, const, var and func declarations inside a file
112+
# default: types before constants before variables before functions
113+
dec-order:
114+
- type
115+
- const
116+
- var
117+
- func
118+
119+
# if true, order of declarations is not checked at all
120+
# default: true (disabled)
121+
disable-dec-order-check: false
122+
123+
# if true, init func can be anywhere in file (must not be declared before all other functions)
124+
# default: true (disabled)
125+
disable-init-func-first-check: false
126+
127+
# if true, multiple global type, const and var declarations are allowed
128+
# default: true (disabled)
129+
disable-dec-num-check: false
130+
110131
dogsled:
111132
# checks assignments with too many blank identifiers; default is 2
112133
max-blank-identifiers: 2

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ require (
9292
github.com/uudashr/gocognit v1.0.5
9393
github.com/valyala/quicktemplate v1.7.0
9494
github.com/yeya24/promlinter v0.1.0
95+
gitlab.com/bosi/decorder v0.2.0
9596
golang.org/x/tools v0.1.8
9697
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
9798
honnef.co/go/tools v0.2.2

go.sum

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

pkg/config/linters_settings.go

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ package config
33
import "github.com/pkg/errors"
44

55
var defaultLintersSettings = LintersSettings{
6+
Decorder: DecorderSettings{
7+
DecOrder: []string{"type", "const", "var", "func"},
8+
DisableDecNumCheck: true,
9+
DisableDecOrderCheck: true,
10+
DisableInitFuncFirstCheck: true,
11+
},
612
Dogsled: DogsledSettings{
713
MaxBlankIdentifiers: 2,
814
},
@@ -85,6 +91,7 @@ var defaultLintersSettings = LintersSettings{
8591
type LintersSettings struct {
8692
BiDiChk BiDiChkSettings
8793
Cyclop Cyclop
94+
Decorder DecorderSettings
8895
Depguard DepGuardSettings
8996
Dogsled DogsledSettings
9097
Dupl DuplSettings
@@ -173,6 +180,13 @@ type DepGuardSettings struct {
173180
PackagesWithErrorMessage map[string]string `mapstructure:"packages-with-error-message"`
174181
}
175182

183+
type DecorderSettings struct {
184+
DecOrder []string `mapstructure:"dec-order"`
185+
DisableDecNumCheck bool `mapstructure:"disable-dec-num-check"`
186+
DisableDecOrderCheck bool `mapstructure:"disable-dec-order-check"`
187+
DisableInitFuncFirstCheck bool `mapstructure:"disable-init-func-first-check"`
188+
}
189+
176190
type DogsledSettings struct {
177191
MaxBlankIdentifiers int `mapstructure:"max-blank-identifiers"`
178192
}

pkg/golinters/decorder.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package golinters
2+
3+
import (
4+
"strings"
5+
6+
"gitlab.com/bosi/decorder"
7+
"golang.org/x/tools/go/analysis"
8+
9+
"github.com/golangci/golangci-lint/pkg/config"
10+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
11+
)
12+
13+
func NewDecorder(settings *config.DecorderSettings) *goanalysis.Linter {
14+
a := decorder.Analyzer
15+
16+
analyzers := []*analysis.Analyzer{a}
17+
18+
// disable all rules/checks by default
19+
cfg := map[string]interface{}{
20+
"disable-dec-num-check": true,
21+
"disable-dec-order-check": true,
22+
"disable-init-func-first-check": true,
23+
}
24+
25+
if settings != nil {
26+
cfg["dec-order"] = strings.Join(settings.DecOrder, ",")
27+
cfg["disable-dec-num-check"] = settings.DisableDecNumCheck
28+
cfg["disable-dec-order-check"] = settings.DisableDecOrderCheck
29+
cfg["disable-init-func-first-check"] = settings.DisableInitFuncFirstCheck
30+
}
31+
32+
return goanalysis.NewLinter(
33+
a.Name,
34+
a.Doc,
35+
analyzers,
36+
map[string]map[string]interface{}{a.Name: cfg},
37+
).WithLoadMode(goanalysis.LoadModeSyntax)
38+
}

pkg/lint/lintersdb/manager.go

+7
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config)
102102
func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
103103
var bidichkCfg *config.BiDiChkSettings
104104
var cyclopCfg *config.Cyclop
105+
var decorderCfg *config.DecorderSettings
105106
var errchkjsonCfg *config.ErrChkJSONSettings
106107
var errorlintCfg *config.ErrorLintSettings
107108
var exhaustiveCfg *config.ExhaustiveSettings
@@ -131,6 +132,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
131132
bidichkCfg = &m.cfg.LintersSettings.BiDiChk
132133
cyclopCfg = &m.cfg.LintersSettings.Cyclop
133134
errchkjsonCfg = &m.cfg.LintersSettings.ErrChkJSON
135+
decorderCfg = &m.cfg.LintersSettings.Decorder
134136
errorlintCfg = &m.cfg.LintersSettings.ErrorLint
135137
exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive
136138
exhaustiveStructCfg = &m.cfg.LintersSettings.ExhaustiveStruct
@@ -189,6 +191,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
189191
WithPresets(linter.PresetComplexity).
190192
WithURL("https://github.com/bkielbasa/cyclop"),
191193

194+
linter.NewConfig(golinters.NewDecorder(decorderCfg)).
195+
WithSince("v1.44.0").
196+
WithPresets(linter.PresetFormatting, linter.PresetStyle).
197+
WithURL("https://gitlab.com/bosi/decorder"),
198+
192199
linter.NewConfig(golinters.NewDeadcode()).
193200
WithSince("v1.0.0").
194201
WithLoadForGoAnalysis().

test/testdata/configs/decorder.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
linters-settings:
2+
decorder:
3+
dec-order:
4+
- type
5+
- const
6+
- var
7+
- func
8+
disable-dec-order-check: false
9+
disable-init-func-first-check: false
10+
disable-dec-num-check: false

test/testdata/decorder.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// args: -Edecorder
2+
// config_path: testdata/configs/decorder.yml
3+
package testdata
4+
5+
import "math"
6+
7+
const (
8+
decoc = math.MaxInt64
9+
decod = 1
10+
)
11+
12+
var decoa = 1
13+
var decob = 1 // ERROR "multiple \"var\" declarations are not allowed; use parentheses instead"
14+
15+
type decoe int // ERROR "type must not be placed after const"
16+
17+
func decof() {
18+
const decog = 1
19+
}
20+
21+
func init() {} // ERROR "init func must be the first function in file"

test/testdata/decorder_default.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// args: -Edecorder
2+
package testdata
3+
4+
import "math"
5+
6+
const (
7+
decoh = math.MaxInt64
8+
decoi = 1
9+
)
10+
11+
var decoj = 1
12+
var decok = 1
13+
14+
type decol int
15+
16+
func decom() {
17+
const decon = 1
18+
}
19+
20+
func init() {}

0 commit comments

Comments
 (0)