From e3e118433ef8d3300103d07b40db08b56b818ea2 Mon Sep 17 00:00:00 2001 From: Florian Bosdorff Date: Mon, 3 Jan 2022 20:08:28 +0100 Subject: [PATCH 1/5] add decorder linter --- .golangci.example.yml | 18 +++++++++++++++++ go.mod | 1 + go.sum | 4 ++++ pkg/config/linters_settings.go | 11 +++++++++++ pkg/golinters/decorder.go | 36 ++++++++++++++++++++++++++++++++++ pkg/lint/lintersdb/manager.go | 7 +++++++ test/testdata/decorder.go | 19 ++++++++++++++++++ 7 files changed, 96 insertions(+) create mode 100644 pkg/golinters/decorder.go create mode 100644 test/testdata/decorder.go diff --git a/.golangci.example.yml b/.golangci.example.yml index 2457f27c0324..d881619dc670 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -103,6 +103,24 @@ linters-settings: # should ignore tests (default false) skip-tests: false + decorder: + # required order of type, const, var and func declarations inside a file + # default: types before constants before variables before functions + dec-order: + - type + - const + - var + - func + + # if true, order of declarations is not checked at all + disable-dec-order-check: false + + # if true, init func can be anywhere in file (must not be declared before all other functions) + disable-init-func-first-check: false + + # if true, multiple global type, const and var declarations are allowed + disable-dec-num-check: false + dogsled: # checks assignments with too many blank identifiers; default is 2 max-blank-identifiers: 2 diff --git a/go.mod b/go.mod index 439c914f76db..cc6da7ca5040 100644 --- a/go.mod +++ b/go.mod @@ -92,6 +92,7 @@ require ( github.com/uudashr/gocognit v1.0.5 github.com/valyala/quicktemplate v1.7.0 github.com/yeya24/promlinter v0.1.0 + gitlab.com/bosi/decorder v0.2.0 golang.org/x/tools v0.1.8 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b honnef.co/go/tools v0.2.2 diff --git a/go.sum b/go.sum index ef43abf83ce2..58c90593a19d 100644 --- a/go.sum +++ b/go.sum @@ -829,6 +829,8 @@ github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1 github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg= github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +gitlab.com/bosi/decorder v0.2.0 h1:MqNxlLZFUixZsksPm7w3xIrLOLo66WuNaqbDoysPHQ0= +gitlab.com/bosi/decorder v0.2.0/go.mod h1:6C/nhLSbF6qZbYD8bRmISBwc6vcWdNsiIBkRvjJFrH0= go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= go.etcd.io/etcd v0.0.0-20200513171258-e048e166ab9c/go.mod h1:xCI7ZzBfRuGgBXyXO6yfWfDmlWd35khcWpUa4L0xI/k= @@ -1084,6 +1086,8 @@ golang.org/x/sys v0.0.0-20211105183446-c75c47738b0c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7qVW4r4ctbWpURyuOD0E= golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index b04196d19120..5aecd77111e0 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -80,11 +80,15 @@ var defaultLintersSettings = LintersSettings{ Forbidigo: ForbidigoSettings{ ExcludeGodocExamples: true, }, + Decorder: DecorderSettings{ + DecOrder: []string{"type", "const", "var", "func"}, + }, } type LintersSettings struct { BiDiChk BiDiChkSettings Cyclop Cyclop + Decorder DecorderSettings Depguard DepGuardSettings Dogsled DogsledSettings Dupl DuplSettings @@ -178,6 +182,13 @@ type DepGuardSettings struct { PackagesWithErrorMessage map[string]string `mapstructure:"packages-with-error-message"` } +type DecorderSettings struct { + DecOrder []string `mapstructure:"dec-order"` + DisableDecNumCheck bool `mapstructure:"disable-dec-num-check"` + DisableDecOrderCheck bool `mapstructure:"disable-dec-order-check"` + DisableInitFuncFirstCheck bool `mapstructure:"disable-init-func-first-check"` +} + type DogsledSettings struct { MaxBlankIdentifiers int `mapstructure:"max-blank-identifiers"` } diff --git a/pkg/golinters/decorder.go b/pkg/golinters/decorder.go new file mode 100644 index 000000000000..eafdb6965058 --- /dev/null +++ b/pkg/golinters/decorder.go @@ -0,0 +1,36 @@ +package golinters + +import ( + "strings" + + "gitlab.com/bosi/decorder" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewDecorder(settings *config.DecorderSettings) *goanalysis.Linter { + a := decorder.Analyzer + + analyzers := []*analysis.Analyzer{a} + + var cfg map[string]map[string]interface{} + if settings != nil { + cfg = map[string]map[string]interface{}{ + a.Name: { + "dec-order": strings.Join(settings.DecOrder, ","), + "disable-dec-num-check": settings.DisableDecNumCheck, + "disable-dec-order-check": settings.DisableDecOrderCheck, + "disable-init-func-first-check": settings.DisableInitFuncFirstCheck, + }, + } + } + + return goanalysis.NewLinter( + a.Name, + a.Doc, + analyzers, + cfg, + ).WithLoadMode(goanalysis.LoadModeSyntax) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index b853f3ab6dfa..3255ef9f7bbc 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -126,6 +126,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var varnamelenCfg *config.VarnamelenSettings var wrapcheckCfg *config.WrapcheckSettings var nlreturnCfg *config.NlreturnSettings + var decorderCfg *config.DecorderSettings if m.cfg != nil { bidichkCfg = &m.cfg.LintersSettings.BiDiChk @@ -154,6 +155,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { varnamelenCfg = &m.cfg.LintersSettings.Varnamelen wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck nlreturnCfg = &m.cfg.LintersSettings.Nlreturn + decorderCfg = &m.cfg.LintersSettings.Decorder } const megacheckName = "megacheck" @@ -315,6 +317,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetPerformance). WithURL("https://github.com/mdempsky/maligned"). Deprecated("The repository of the linter has been archived by the owner.", "v1.38.0", "govet 'fieldalignment'"), + linter.NewConfig(golinters.NewDecorder(decorderCfg)). + WithSince("v1.44.0"). + WithLoadForGoAnalysis(). + WithPresets(linter.PresetFormatting, linter.PresetStyle). + WithURL("https://gitlab.com/bosi/decorder"), linter.NewConfig(golinters.NewDepguard()). WithSince("v1.4.0"). WithLoadForGoAnalysis(). diff --git a/test/testdata/decorder.go b/test/testdata/decorder.go new file mode 100644 index 000000000000..8d2bc170898c --- /dev/null +++ b/test/testdata/decorder.go @@ -0,0 +1,19 @@ +// args: -Edecorder + +package testdata + +const ( + decoc = 1 + decod = 1 +) + +var decoa = 1 +var decob = 1 // ERROR "multiple \"var\" declarations are not allowed; use parentheses instead" + +type decoe int // ERROR "type must not be placed after const" + +func decof() { + const decog = 1 +} + +func init() {} // ERROR "init func must be the first function in file" From be7a182a97f679658e05ba991636b18800bdf8af Mon Sep 17 00:00:00 2001 From: Florian Bosdorff Date: Tue, 4 Jan 2022 19:21:46 +0100 Subject: [PATCH 2/5] disable rules of decorder by default --- .golangci.example.yml | 6 +++--- pkg/config/linters_settings.go | 5 ++++- pkg/lint/lintersdb/manager.go | 1 - test/testdata/configs/decorder.yml | 10 ++++++++++ test/testdata/decorder.go | 2 +- test/testdata/decorder_default.go | 18 ++++++++++++++++++ 6 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 test/testdata/configs/decorder.yml create mode 100644 test/testdata/decorder_default.go diff --git a/.golangci.example.yml b/.golangci.example.yml index d881619dc670..c9f292c089c6 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -113,13 +113,13 @@ linters-settings: - func # if true, order of declarations is not checked at all - disable-dec-order-check: false + disable-dec-order-check: true # if true, init func can be anywhere in file (must not be declared before all other functions) - disable-init-func-first-check: false + disable-init-func-first-check: true # if true, multiple global type, const and var declarations are allowed - disable-dec-num-check: false + disable-dec-num-check: true dogsled: # checks assignments with too many blank identifiers; default is 2 diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 36340df5081f..49f20cf4ddb0 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -4,7 +4,10 @@ import "github.com/pkg/errors" var defaultLintersSettings = LintersSettings{ Decorder: DecorderSettings{ - DecOrder: []string{"type", "const", "var", "func"}, + DecOrder: []string{"type", "const", "var", "func"}, + DisableDecNumCheck: true, + DisableDecOrderCheck: true, + DisableInitFuncFirstCheck: true, }, Dogsled: DogsledSettings{ MaxBlankIdentifiers: 2, diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index ed94d703394e..57d5bd884943 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -156,7 +156,6 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { unusedCfg = &m.cfg.LintersSettings.Unused varnamelenCfg = &m.cfg.LintersSettings.Varnamelen wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck - nlreturnCfg = &m.cfg.LintersSettings.Nlreturn } const megacheckName = "megacheck" diff --git a/test/testdata/configs/decorder.yml b/test/testdata/configs/decorder.yml new file mode 100644 index 000000000000..0ef8e49b3976 --- /dev/null +++ b/test/testdata/configs/decorder.yml @@ -0,0 +1,10 @@ +linters-settings: + decorder: + dec-order: + - type + - const + - var + - func + disable-dec-order-check: false + disable-init-func-first-check: false + disable-dec-num-check: false diff --git a/test/testdata/decorder.go b/test/testdata/decorder.go index 8d2bc170898c..ca6ad5c50006 100644 --- a/test/testdata/decorder.go +++ b/test/testdata/decorder.go @@ -1,5 +1,5 @@ // args: -Edecorder - +//config_path: testdata/configs/decorder.yml package testdata const ( diff --git a/test/testdata/decorder_default.go b/test/testdata/decorder_default.go new file mode 100644 index 000000000000..9071add76c46 --- /dev/null +++ b/test/testdata/decorder_default.go @@ -0,0 +1,18 @@ +// args: -Edecorder +package testdata + +const ( + decoh = 1 + decoi = 1 +) + +var decoj = 1 +var decok = 1 + +type decol int + +func decom() { + const decon = 1 +} + +func init() {} From e0ad1dfad535934d18aad1314d8f683e1d54860e Mon Sep 17 00:00:00 2001 From: Florian Bosdorff Date: Tue, 4 Jan 2022 19:37:55 +0100 Subject: [PATCH 3/5] disable rules of decorder when no config is given --- .golangci.example.yml | 9 ++++++--- pkg/golinters/decorder.go | 22 ++++++++++++---------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.golangci.example.yml b/.golangci.example.yml index c9f292c089c6..18fdf5280f22 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -113,13 +113,16 @@ linters-settings: - func # if true, order of declarations is not checked at all - disable-dec-order-check: true + # default: true (disabled) + disable-dec-order-check: false # if true, init func can be anywhere in file (must not be declared before all other functions) - disable-init-func-first-check: true + # default: true (disabled) + disable-init-func-first-check: false # if true, multiple global type, const and var declarations are allowed - disable-dec-num-check: true + # default: true (disabled) + disable-dec-num-check: false dogsled: # checks assignments with too many blank identifiers; default is 2 diff --git a/pkg/golinters/decorder.go b/pkg/golinters/decorder.go index eafdb6965058..672f206ea35c 100644 --- a/pkg/golinters/decorder.go +++ b/pkg/golinters/decorder.go @@ -15,22 +15,24 @@ func NewDecorder(settings *config.DecorderSettings) *goanalysis.Linter { analyzers := []*analysis.Analyzer{a} - var cfg map[string]map[string]interface{} + // disable all rules/checks by default + cfg := map[string]interface{}{ + "disable-dec-num-check": true, + "disable-dec-order-check": true, + "disable-init-func-first-check": true, + } + if settings != nil { - cfg = map[string]map[string]interface{}{ - a.Name: { - "dec-order": strings.Join(settings.DecOrder, ","), - "disable-dec-num-check": settings.DisableDecNumCheck, - "disable-dec-order-check": settings.DisableDecOrderCheck, - "disable-init-func-first-check": settings.DisableInitFuncFirstCheck, - }, - } + cfg["dec-order"] = strings.Join(settings.DecOrder, ",") + cfg["disable-dec-num-check"] = settings.DisableDecNumCheck + cfg["disable-dec-order-check"] = settings.DisableDecOrderCheck + cfg["disable-init-func-first-check"] = settings.DisableInitFuncFirstCheck } return goanalysis.NewLinter( a.Name, a.Doc, analyzers, - cfg, + map[string]map[string]interface{}{a.Name: cfg}, ).WithLoadMode(goanalysis.LoadModeSyntax) } From cc587f82612ee2f8079f6239877fc92362b5abfa Mon Sep 17 00:00:00 2001 From: Florian Bosdorff Date: Tue, 4 Jan 2022 21:59:48 +0100 Subject: [PATCH 4/5] add import to decorder test to prevent side effects --- pkg/lint/lintersdb/manager.go | 1 - test/testdata/decorder.go | 4 +++- test/testdata/decorder_default.go | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 57d5bd884943..db386a17d58d 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -193,7 +193,6 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { linter.NewConfig(golinters.NewDecorder(decorderCfg)). WithSince("v1.44.0"). - WithLoadForGoAnalysis(). WithPresets(linter.PresetFormatting, linter.PresetStyle). WithURL("https://gitlab.com/bosi/decorder"), diff --git a/test/testdata/decorder.go b/test/testdata/decorder.go index ca6ad5c50006..98d5ef6c0e2b 100644 --- a/test/testdata/decorder.go +++ b/test/testdata/decorder.go @@ -2,8 +2,10 @@ //config_path: testdata/configs/decorder.yml package testdata +import "math" + const ( - decoc = 1 + decoc = math.MaxInt decod = 1 ) diff --git a/test/testdata/decorder_default.go b/test/testdata/decorder_default.go index 9071add76c46..06aa3801399c 100644 --- a/test/testdata/decorder_default.go +++ b/test/testdata/decorder_default.go @@ -1,8 +1,10 @@ // args: -Edecorder package testdata +import "math" + const ( - decoh = 1 + decoh = math.MaxInt decoi = 1 ) From 29302cb134912d71ab34485e305924e5f8ca4d0c Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 5 Jan 2022 03:43:46 +0100 Subject: [PATCH 5/5] review: compatibility with go1.16 --- test/testdata/decorder.go | 4 ++-- test/testdata/decorder_default.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/testdata/decorder.go b/test/testdata/decorder.go index 98d5ef6c0e2b..5cc325cce2e4 100644 --- a/test/testdata/decorder.go +++ b/test/testdata/decorder.go @@ -1,11 +1,11 @@ // args: -Edecorder -//config_path: testdata/configs/decorder.yml +// config_path: testdata/configs/decorder.yml package testdata import "math" const ( - decoc = math.MaxInt + decoc = math.MaxInt64 decod = 1 ) diff --git a/test/testdata/decorder_default.go b/test/testdata/decorder_default.go index 06aa3801399c..69e85a7dac10 100644 --- a/test/testdata/decorder_default.go +++ b/test/testdata/decorder_default.go @@ -4,7 +4,7 @@ package testdata import "math" const ( - decoh = math.MaxInt + decoh = math.MaxInt64 decoi = 1 )