From 033e7be9c6d13f1f50471f82eed6299a1a1d5cb4 Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Sat, 22 Mar 2025 21:22:10 +0100 Subject: [PATCH 01/21] adding gofuncor linter --- go.mod | 1 + go.sum | 2 ++ pkg/config/formatters_settings.go | 3 ++ pkg/golinters/gofuncor/gofuncor.go | 19 +++++++++++ .../constructors_after_struct_methods.go | 24 ++++++++++++++ .../testdata/constructors_before_struct.go | 33 +++++++++++++++++++ .../testdata/struct_not_declared_in_file.go | 13 ++++++++ pkg/lint/lintersdb/builder_linter.go | 6 ++++ 8 files changed, 101 insertions(+) create mode 100644 pkg/golinters/gofuncor/gofuncor.go create mode 100644 pkg/golinters/gofuncor/testdata/constructors_after_struct_methods.go create mode 100644 pkg/golinters/gofuncor/testdata/constructors_before_struct.go create mode 100644 pkg/golinters/gofuncor/testdata/struct_not_declared_in_file.go diff --git a/go.mod b/go.mod index db1aad903395..b5e006cd185f 100644 --- a/go.mod +++ b/go.mod @@ -73,6 +73,7 @@ require ( github.com/ldez/usetesting v0.4.2 github.com/leonklingele/grouper v1.1.2 github.com/macabu/inamedparam v0.2.0 + github.com/manuelarte/gofuncor v0.0.3 github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.1 github.com/matoous/godox v1.1.0 diff --git a/go.sum b/go.sum index 88596045b43d..b8bcbf60dbfd 100644 --- a/go.sum +++ b/go.sum @@ -389,6 +389,8 @@ github.com/macabu/inamedparam v0.2.0 h1:VyPYpOc10nkhI2qeNUdh3Zket4fcZjEWe35poddB github.com/macabu/inamedparam v0.2.0/go.mod h1:+Pee9/YfGe5LJ62pYXqB89lJ+0k5bsR8Wgz/C0Zlq3U= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= +github.com/manuelarte/gofuncor v0.0.3 h1:C9uZtY1JZM8zzMK/s0ZPYXP78Zdrw3EC+CXl1o3upIs= +github.com/manuelarte/gofuncor v0.0.3/go.mod h1:qAJxRc8ekJFW8yz3/T9eb6Png1TJGoLBLUuRC+Zley8= github.com/maratori/testableexamples v1.0.0 h1:dU5alXRrD8WKSjOUnmJZuzdxWOEQ57+7s93SLMxb2vI= github.com/maratori/testableexamples v1.0.0/go.mod h1:4rhjL1n20TUTT4vdh3RDqSizKLyXp7K2u6HgraZCGzE= github.com/maratori/testpackage v1.1.1 h1:S58XVV5AD7HADMmD0fNnziNHqKvSdDuEKdPD1rNTU04= diff --git a/pkg/config/formatters_settings.go b/pkg/config/formatters_settings.go index d99354ba3f1e..219fe3ebfe5d 100644 --- a/pkg/config/formatters_settings.go +++ b/pkg/config/formatters_settings.go @@ -19,6 +19,7 @@ type FormatterSettings struct { Gci GciSettings `mapstructure:"gci"` GoFmt GoFmtSettings `mapstructure:"gofmt"` GoFumpt GoFumptSettings `mapstructure:"gofumpt"` + GoFuncOr GoFuncOrSettings `mapstructure:"gofuncor"` GoImports GoImportsSettings `mapstructure:"goimports"` GoLines GoLinesSettings `mapstructure:"golines"` } @@ -48,6 +49,8 @@ type GoFumptSettings struct { LangVersion string `mapstructure:"-"` } +type GoFuncOrSettings struct{} + type GoImportsSettings struct { LocalPrefixes []string `mapstructure:"local-prefixes"` } diff --git a/pkg/golinters/gofuncor/gofuncor.go b/pkg/golinters/gofuncor/gofuncor.go new file mode 100644 index 000000000000..8ffe51bfd54f --- /dev/null +++ b/pkg/golinters/gofuncor/gofuncor.go @@ -0,0 +1,19 @@ +package gofuncor + +import ( + "github.com/golangci/golangci-lint/v2/pkg/config" + "github.com/golangci/golangci-lint/v2/pkg/goanalysis" + "github.com/manuelarte/gofuncor/pkg/analyzer" + "golang.org/x/tools/go/analysis" +) + +func New(_ *config.GoFuncOrSettings) *goanalysis.Linter { + a := analyzer.NewAnalyzer() + + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + nil, + ).WithLoadMode(goanalysis.LoadModeSyntax) +} diff --git a/pkg/golinters/gofuncor/testdata/constructors_after_struct_methods.go b/pkg/golinters/gofuncor/testdata/constructors_after_struct_methods.go new file mode 100644 index 000000000000..397455ba5478 --- /dev/null +++ b/pkg/golinters/gofuncor/testdata/constructors_after_struct_methods.go @@ -0,0 +1,24 @@ +package simple + +//nolint:recvcheck // testing linter +type MyStruct2 struct { + Name string +} + +func (m MyStruct2) GetName() string { + return m.Name +} + +func (m *MyStruct2) SetName(name string) { + m.Name = name +} + +//nolint:nonamedreturns // testing linter +func NewOtherMyStruct2() (m *MyStruct2) { // want `constructor 'NewOtherMyStruct2' for struct 'MyStruct2' should be placed before struct method 'GetName'` + m = &MyStruct2{Name: "John"} + return +} + +func NewMyStruct2() *MyStruct2 { // want `constructor \"NewMyStruct2\" for struct \"MyStruct2\" should be placed before struct method \"GetName\"` + return &MyStruct2{Name: "John"} +} diff --git a/pkg/golinters/gofuncor/testdata/constructors_before_struct.go b/pkg/golinters/gofuncor/testdata/constructors_before_struct.go new file mode 100644 index 000000000000..d95b02393f2a --- /dev/null +++ b/pkg/golinters/gofuncor/testdata/constructors_before_struct.go @@ -0,0 +1,33 @@ +package simple + +//nolint:nonamedreturns // testing linter +func NewOtherMyStruct() (m *MyStruct) { // want "should be placed after the struct declaration" + m = &MyStruct{Name: "John"} + return +} + +func NewMyStruct() *MyStruct { // want "should be placed after the struct declaration" + return &MyStruct{Name: "John"} +} + +func MustMyStruct() *MyStruct { // want `function \"MustMyStruct\" for struct \"MyStruct\" should be placed after the struct declaration` + return NewMyStruct() +} + +//nolint:recvcheck // testing linter +type MyStruct struct { + Name string +} + +//nolint:unused // testing linter +func (m MyStruct) lenName() int { // want `unexported method "lenName" for struct "MyStruct" should be placed after the exported method "GetName"` + return len(m.Name) +} + +func (m MyStruct) GetName() string { + return m.Name +} + +func (m *MyStruct) SetName(name string) { + m.Name = name +} diff --git a/pkg/golinters/gofuncor/testdata/struct_not_declared_in_file.go b/pkg/golinters/gofuncor/testdata/struct_not_declared_in_file.go new file mode 100644 index 000000000000..8d6ecc3e434d --- /dev/null +++ b/pkg/golinters/gofuncor/testdata/struct_not_declared_in_file.go @@ -0,0 +1,13 @@ +package simple + +import ( + "time" +) + +func NewOtherWayMyStruct() MyStruct { + return MyStruct{Name: "John"} +} + +func NewTimeStruct() time.Time { + return time.Now() +} diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index 6c533d2e4878..f07cadc88c32 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -44,6 +44,7 @@ import ( "github.com/golangci/golangci-lint/v2/pkg/golinters/godox" "github.com/golangci/golangci-lint/v2/pkg/golinters/gofmt" "github.com/golangci/golangci-lint/v2/pkg/golinters/gofumpt" + "github.com/golangci/golangci-lint/v2/pkg/golinters/gofuncor" "github.com/golangci/golangci-lint/v2/pkg/golinters/goheader" "github.com/golangci/golangci-lint/v2/pkg/golinters/goimports" "github.com/golangci/golangci-lint/v2/pkg/golinters/golines" @@ -335,6 +336,11 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithAutoFix(). WithURL("https://github.com/mvdan/gofumpt"), + linter.NewConfig(gofuncor.New(&cfg.Linters.Settings.GoFuncOr)). + WithSince("v1.65.0"). + WithAutoFix(). + WithURL("https://github.com/manuelarte/gofuncor"), + linter.NewConfig(golines.New(&cfg.Linters.Settings.GoLines)). WithSince("v2.0.0"). WithAutoFix(). From b89e1bdcc19cad8108437640aa4f9b8d79c9f3f4 Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Tue, 25 Mar 2025 20:38:24 +0100 Subject: [PATCH 02/21] newlinter: Adding gofuncor linter --- go.mod | 2 +- go.sum | 4 +-- .../testdata/constructors_before_struct.go | 33 ------------------- ...rs_after_struct_methods.go => gofuncor.go} | 25 +++++++++----- .../testdata/struct_not_declared_in_file.go | 13 -------- pkg/lint/lintersdb/builder_linter.go | 2 +- 6 files changed, 21 insertions(+), 58 deletions(-) delete mode 100644 pkg/golinters/gofuncor/testdata/constructors_before_struct.go rename pkg/golinters/gofuncor/testdata/{constructors_after_struct_methods.go => gofuncor.go} (50%) delete mode 100644 pkg/golinters/gofuncor/testdata/struct_not_declared_in_file.go diff --git a/go.mod b/go.mod index 755e3f6235a4..e30aeef0da6c 100644 --- a/go.mod +++ b/go.mod @@ -73,7 +73,7 @@ require ( github.com/ldez/usetesting v0.4.2 github.com/leonklingele/grouper v1.1.2 github.com/macabu/inamedparam v0.2.0 - github.com/manuelarte/gofuncor v0.0.3 + github.com/manuelarte/gofuncor v0.0.4 github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.1 github.com/matoous/godox v1.1.0 diff --git a/go.sum b/go.sum index e80287b35e80..7817d5f0b3d1 100644 --- a/go.sum +++ b/go.sum @@ -389,8 +389,8 @@ github.com/macabu/inamedparam v0.2.0 h1:VyPYpOc10nkhI2qeNUdh3Zket4fcZjEWe35poddB github.com/macabu/inamedparam v0.2.0/go.mod h1:+Pee9/YfGe5LJ62pYXqB89lJ+0k5bsR8Wgz/C0Zlq3U= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= -github.com/manuelarte/gofuncor v0.0.3 h1:C9uZtY1JZM8zzMK/s0ZPYXP78Zdrw3EC+CXl1o3upIs= -github.com/manuelarte/gofuncor v0.0.3/go.mod h1:qAJxRc8ekJFW8yz3/T9eb6Png1TJGoLBLUuRC+Zley8= +github.com/manuelarte/gofuncor v0.0.4 h1:a3NnfYT279d2NzDvp/UnpMRBxTCki7HbzIOIvjo75aI= +github.com/manuelarte/gofuncor v0.0.4/go.mod h1:82VUujTxNIDRv1GyEYe1f5xn6OMn9Xza8Lb5CljsvRY= github.com/maratori/testableexamples v1.0.0 h1:dU5alXRrD8WKSjOUnmJZuzdxWOEQ57+7s93SLMxb2vI= github.com/maratori/testableexamples v1.0.0/go.mod h1:4rhjL1n20TUTT4vdh3RDqSizKLyXp7K2u6HgraZCGzE= github.com/maratori/testpackage v1.1.1 h1:S58XVV5AD7HADMmD0fNnziNHqKvSdDuEKdPD1rNTU04= diff --git a/pkg/golinters/gofuncor/testdata/constructors_before_struct.go b/pkg/golinters/gofuncor/testdata/constructors_before_struct.go deleted file mode 100644 index d95b02393f2a..000000000000 --- a/pkg/golinters/gofuncor/testdata/constructors_before_struct.go +++ /dev/null @@ -1,33 +0,0 @@ -package simple - -//nolint:nonamedreturns // testing linter -func NewOtherMyStruct() (m *MyStruct) { // want "should be placed after the struct declaration" - m = &MyStruct{Name: "John"} - return -} - -func NewMyStruct() *MyStruct { // want "should be placed after the struct declaration" - return &MyStruct{Name: "John"} -} - -func MustMyStruct() *MyStruct { // want `function \"MustMyStruct\" for struct \"MyStruct\" should be placed after the struct declaration` - return NewMyStruct() -} - -//nolint:recvcheck // testing linter -type MyStruct struct { - Name string -} - -//nolint:unused // testing linter -func (m MyStruct) lenName() int { // want `unexported method "lenName" for struct "MyStruct" should be placed after the exported method "GetName"` - return len(m.Name) -} - -func (m MyStruct) GetName() string { - return m.Name -} - -func (m *MyStruct) SetName(name string) { - m.Name = name -} diff --git a/pkg/golinters/gofuncor/testdata/constructors_after_struct_methods.go b/pkg/golinters/gofuncor/testdata/gofuncor.go similarity index 50% rename from pkg/golinters/gofuncor/testdata/constructors_after_struct_methods.go rename to pkg/golinters/gofuncor/testdata/gofuncor.go index 397455ba5478..5b112a8d0f48 100644 --- a/pkg/golinters/gofuncor/testdata/constructors_after_struct_methods.go +++ b/pkg/golinters/gofuncor/testdata/gofuncor.go @@ -1,6 +1,21 @@ -package simple +package testdata + +type MyStruct struct { + Name string +} + +func (m MyStruct) lenName() int { // want `unexported method "lenName" for struct "MyStruct" should be placed after the exported method "GetName"` + return len(m.Name) +} + +func (m MyStruct) GetName() string { + return m.Name +} + +func (m *MyStruct) SetName(name string) { + m.Name = name +} -//nolint:recvcheck // testing linter type MyStruct2 struct { Name string } @@ -13,12 +28,6 @@ func (m *MyStruct2) SetName(name string) { m.Name = name } -//nolint:nonamedreturns // testing linter -func NewOtherMyStruct2() (m *MyStruct2) { // want `constructor 'NewOtherMyStruct2' for struct 'MyStruct2' should be placed before struct method 'GetName'` - m = &MyStruct2{Name: "John"} - return -} - func NewMyStruct2() *MyStruct2 { // want `constructor \"NewMyStruct2\" for struct \"MyStruct2\" should be placed before struct method \"GetName\"` return &MyStruct2{Name: "John"} } diff --git a/pkg/golinters/gofuncor/testdata/struct_not_declared_in_file.go b/pkg/golinters/gofuncor/testdata/struct_not_declared_in_file.go deleted file mode 100644 index 8d6ecc3e434d..000000000000 --- a/pkg/golinters/gofuncor/testdata/struct_not_declared_in_file.go +++ /dev/null @@ -1,13 +0,0 @@ -package simple - -import ( - "time" -) - -func NewOtherWayMyStruct() MyStruct { - return MyStruct{Name: "John"} -} - -func NewTimeStruct() time.Time { - return time.Now() -} diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index f07cadc88c32..529a315ce6cb 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -337,7 +337,7 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithURL("https://github.com/mvdan/gofumpt"), linter.NewConfig(gofuncor.New(&cfg.Linters.Settings.GoFuncOr)). - WithSince("v1.65.0"). + WithSince("v2.0.2"). WithAutoFix(). WithURL("https://github.com/manuelarte/gofuncor"), From 59190f5c496ba3880e1e1eef864aee1f7ca66730 Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Wed, 26 Mar 2025 09:29:44 +0100 Subject: [PATCH 03/21] newlinter: updating .golangci.next.reference.yml --- .golangci.next.reference.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 1fd85366eed1..43b5c2be32da 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -56,6 +56,7 @@ linters: - godox - gofmt - gofumpt + - gofuncor - goheader - goimports - gomoddirectives @@ -167,6 +168,7 @@ linters: - godox - gofmt - gofumpt + - gofuncor - goheader - goimports - gomoddirectives From 74ca9fed84b3a90be107bd11115098a4a7cf7f4e Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Wed, 26 Mar 2025 09:30:49 +0100 Subject: [PATCH 04/21] newlinter: updating to next minor version --- pkg/lint/lintersdb/builder_linter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index 529a315ce6cb..313bb2048b40 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -337,7 +337,7 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithURL("https://github.com/mvdan/gofumpt"), linter.NewConfig(gofuncor.New(&cfg.Linters.Settings.GoFuncOr)). - WithSince("v2.0.2"). + WithSince("v2.1.0"). WithAutoFix(). WithURL("https://github.com/manuelarte/gofuncor"), From 1bf0125f07a8a06e46f10e4f9c616fd39d8ad74b Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Wed, 26 Mar 2025 15:05:25 +0100 Subject: [PATCH 05/21] newlinter: fixing the last checks --- go.mod | 2 +- go.sum | 4 ++-- pkg/golinters/gofuncor/testdata/gofuncor.go | 6 ++++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 66655517bb1c..3ceea1518733 100644 --- a/go.mod +++ b/go.mod @@ -73,7 +73,7 @@ require ( github.com/ldez/usetesting v0.4.2 github.com/leonklingele/grouper v1.1.2 github.com/macabu/inamedparam v0.2.0 - github.com/manuelarte/gofuncor v0.0.4 + github.com/manuelarte/gofuncor v0.1.0 github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.1 github.com/matoous/godox v1.1.0 diff --git a/go.sum b/go.sum index 3544c69b68a3..76512d56ee59 100644 --- a/go.sum +++ b/go.sum @@ -389,8 +389,8 @@ github.com/macabu/inamedparam v0.2.0 h1:VyPYpOc10nkhI2qeNUdh3Zket4fcZjEWe35poddB github.com/macabu/inamedparam v0.2.0/go.mod h1:+Pee9/YfGe5LJ62pYXqB89lJ+0k5bsR8Wgz/C0Zlq3U= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= -github.com/manuelarte/gofuncor v0.0.4 h1:a3NnfYT279d2NzDvp/UnpMRBxTCki7HbzIOIvjo75aI= -github.com/manuelarte/gofuncor v0.0.4/go.mod h1:82VUujTxNIDRv1GyEYe1f5xn6OMn9Xza8Lb5CljsvRY= +github.com/manuelarte/gofuncor v0.1.0 h1:I1bkfWx39/pR7FEXKdxx/8Qh0aIyZM4AmuGCiRK72X8= +github.com/manuelarte/gofuncor v0.1.0/go.mod h1:82VUujTxNIDRv1GyEYe1f5xn6OMn9Xza8Lb5CljsvRY= github.com/maratori/testableexamples v1.0.0 h1:dU5alXRrD8WKSjOUnmJZuzdxWOEQ57+7s93SLMxb2vI= github.com/maratori/testableexamples v1.0.0/go.mod h1:4rhjL1n20TUTT4vdh3RDqSizKLyXp7K2u6HgraZCGzE= github.com/maratori/testpackage v1.1.1 h1:S58XVV5AD7HADMmD0fNnziNHqKvSdDuEKdPD1rNTU04= diff --git a/pkg/golinters/gofuncor/testdata/gofuncor.go b/pkg/golinters/gofuncor/testdata/gofuncor.go index 5b112a8d0f48..787dd80c5778 100644 --- a/pkg/golinters/gofuncor/testdata/gofuncor.go +++ b/pkg/golinters/gofuncor/testdata/gofuncor.go @@ -1,5 +1,7 @@ package testdata +import "time" + type MyStruct struct { Name string } @@ -31,3 +33,7 @@ func (m *MyStruct2) SetName(name string) { func NewMyStruct2() *MyStruct2 { // want `constructor \"NewMyStruct2\" for struct \"MyStruct2\" should be placed before struct method \"GetName\"` return &MyStruct2{Name: "John"} } + +func NewTime() time.Time { + return time.Now() +} From 47e23e588a2ecde79d49588a7cb0acfbc8858b1e Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Wed, 26 Mar 2025 15:19:42 +0100 Subject: [PATCH 06/21] newlinter: applying reviewer's comments --- pkg/config/formatters_settings.go | 3 --- pkg/golinters/gofuncor/gofuncor.go | 3 +-- pkg/lint/lintersdb/builder_linter.go | 3 +-- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/pkg/config/formatters_settings.go b/pkg/config/formatters_settings.go index 219fe3ebfe5d..d99354ba3f1e 100644 --- a/pkg/config/formatters_settings.go +++ b/pkg/config/formatters_settings.go @@ -19,7 +19,6 @@ type FormatterSettings struct { Gci GciSettings `mapstructure:"gci"` GoFmt GoFmtSettings `mapstructure:"gofmt"` GoFumpt GoFumptSettings `mapstructure:"gofumpt"` - GoFuncOr GoFuncOrSettings `mapstructure:"gofuncor"` GoImports GoImportsSettings `mapstructure:"goimports"` GoLines GoLinesSettings `mapstructure:"golines"` } @@ -49,8 +48,6 @@ type GoFumptSettings struct { LangVersion string `mapstructure:"-"` } -type GoFuncOrSettings struct{} - type GoImportsSettings struct { LocalPrefixes []string `mapstructure:"local-prefixes"` } diff --git a/pkg/golinters/gofuncor/gofuncor.go b/pkg/golinters/gofuncor/gofuncor.go index 8ffe51bfd54f..58c7483f28f5 100644 --- a/pkg/golinters/gofuncor/gofuncor.go +++ b/pkg/golinters/gofuncor/gofuncor.go @@ -1,13 +1,12 @@ package gofuncor import ( - "github.com/golangci/golangci-lint/v2/pkg/config" "github.com/golangci/golangci-lint/v2/pkg/goanalysis" "github.com/manuelarte/gofuncor/pkg/analyzer" "golang.org/x/tools/go/analysis" ) -func New(_ *config.GoFuncOrSettings) *goanalysis.Linter { +func New() *goanalysis.Linter { a := analyzer.NewAnalyzer() return goanalysis.NewLinter( diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index 313bb2048b40..97ed2a811f45 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -336,9 +336,8 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithAutoFix(). WithURL("https://github.com/mvdan/gofumpt"), - linter.NewConfig(gofuncor.New(&cfg.Linters.Settings.GoFuncOr)). + linter.NewConfig(gofuncor.New()). WithSince("v2.1.0"). - WithAutoFix(). WithURL("https://github.com/manuelarte/gofuncor"), linter.NewConfig(golines.New(&cfg.Linters.Settings.GoLines)). From 455d8ee45179781a51a431be76d23a73a11eff60 Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Wed, 26 Mar 2025 19:45:18 +0100 Subject: [PATCH 07/21] newlinter: fixing lint --- pkg/golinters/gofuncor/gofuncor.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/golinters/gofuncor/gofuncor.go b/pkg/golinters/gofuncor/gofuncor.go index 58c7483f28f5..cb73a627d876 100644 --- a/pkg/golinters/gofuncor/gofuncor.go +++ b/pkg/golinters/gofuncor/gofuncor.go @@ -1,9 +1,10 @@ package gofuncor import ( - "github.com/golangci/golangci-lint/v2/pkg/goanalysis" "github.com/manuelarte/gofuncor/pkg/analyzer" "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/v2/pkg/goanalysis" ) func New() *goanalysis.Linter { From 5d1fb67494b75408d84b36f153ef8fa69df1be88 Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Wed, 26 Mar 2025 20:01:26 +0100 Subject: [PATCH 08/21] newlinter: adding gofuncor integration test --- pkg/golinters/gofuncor/gofuncor_integration_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 pkg/golinters/gofuncor/gofuncor_integration_test.go diff --git a/pkg/golinters/gofuncor/gofuncor_integration_test.go b/pkg/golinters/gofuncor/gofuncor_integration_test.go new file mode 100644 index 000000000000..d45bdad41c9b --- /dev/null +++ b/pkg/golinters/gofuncor/gofuncor_integration_test.go @@ -0,0 +1,10 @@ +package gofuncor + +import ( + "github.com/golangci/golangci-lint/v2/test/testshared/integration" + "testing" +) + +func TestFromTestdata(t *testing.T) { + integration.RunTestdata(t) +} From 944a3f62df3bb9ef65fb81ef7f481ea8aa943188 Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Wed, 26 Mar 2025 20:05:09 +0100 Subject: [PATCH 09/21] newlinter: adding gofuncor integration test --- pkg/golinters/gofuncor/testdata/gofuncor.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/golinters/gofuncor/testdata/gofuncor.go b/pkg/golinters/gofuncor/testdata/gofuncor.go index 787dd80c5778..89e4e99c0426 100644 --- a/pkg/golinters/gofuncor/testdata/gofuncor.go +++ b/pkg/golinters/gofuncor/testdata/gofuncor.go @@ -1,3 +1,4 @@ +//golangcitest:args -Egofuncor package testdata import "time" @@ -6,7 +7,7 @@ type MyStruct struct { Name string } -func (m MyStruct) lenName() int { // want `unexported method "lenName" for struct "MyStruct" should be placed after the exported method "GetName"` +func (m MyStruct) lenName() int { // want `unexported method "lenName" for struct "MyStruct" should be placed after the exported method "SetName"` return len(m.Name) } From 79d5f8f4f3965805706ae8f5e993a16f7686e633 Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Wed, 26 Mar 2025 21:32:45 +0100 Subject: [PATCH 10/21] newlinter: applying comments --- pkg/golinters/gofuncor/gofuncor_integration_test.go | 3 ++- pkg/golinters/gofuncor/testdata/gofuncor.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/golinters/gofuncor/gofuncor_integration_test.go b/pkg/golinters/gofuncor/gofuncor_integration_test.go index d45bdad41c9b..6bd05e69cb30 100644 --- a/pkg/golinters/gofuncor/gofuncor_integration_test.go +++ b/pkg/golinters/gofuncor/gofuncor_integration_test.go @@ -1,8 +1,9 @@ package gofuncor import ( - "github.com/golangci/golangci-lint/v2/test/testshared/integration" "testing" + + "github.com/golangci/golangci-lint/v2/test/testshared/integration" ) func TestFromTestdata(t *testing.T) { diff --git a/pkg/golinters/gofuncor/testdata/gofuncor.go b/pkg/golinters/gofuncor/testdata/gofuncor.go index 89e4e99c0426..c453260c0bb7 100644 --- a/pkg/golinters/gofuncor/testdata/gofuncor.go +++ b/pkg/golinters/gofuncor/testdata/gofuncor.go @@ -31,7 +31,7 @@ func (m *MyStruct2) SetName(name string) { m.Name = name } -func NewMyStruct2() *MyStruct2 { // want `constructor \"NewMyStruct2\" for struct \"MyStruct2\" should be placed before struct method \"GetName\"` +func NewMyStruct2() *MyStruct2 { // want `constructor "NewMyStruct2" for struct "MyStruct2" should be placed before struct method "GetName"` return &MyStruct2{Name: "John"} } From 84a4968dda250c01a940bba5d2231eb4c55611d4 Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Wed, 26 Mar 2025 22:39:14 +0100 Subject: [PATCH 11/21] newlinter: using new release v0.1.1 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 3ceea1518733..89f9db2f6737 100644 --- a/go.mod +++ b/go.mod @@ -73,7 +73,7 @@ require ( github.com/ldez/usetesting v0.4.2 github.com/leonklingele/grouper v1.1.2 github.com/macabu/inamedparam v0.2.0 - github.com/manuelarte/gofuncor v0.1.0 + github.com/manuelarte/gofuncor v0.1.1 github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.1 github.com/matoous/godox v1.1.0 diff --git a/go.sum b/go.sum index 76512d56ee59..eb3a1fdb5387 100644 --- a/go.sum +++ b/go.sum @@ -389,8 +389,8 @@ github.com/macabu/inamedparam v0.2.0 h1:VyPYpOc10nkhI2qeNUdh3Zket4fcZjEWe35poddB github.com/macabu/inamedparam v0.2.0/go.mod h1:+Pee9/YfGe5LJ62pYXqB89lJ+0k5bsR8Wgz/C0Zlq3U= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= -github.com/manuelarte/gofuncor v0.1.0 h1:I1bkfWx39/pR7FEXKdxx/8Qh0aIyZM4AmuGCiRK72X8= -github.com/manuelarte/gofuncor v0.1.0/go.mod h1:82VUujTxNIDRv1GyEYe1f5xn6OMn9Xza8Lb5CljsvRY= +github.com/manuelarte/gofuncor v0.1.1 h1:h/HOzmYwItmSlBfvh5paN6Bktjn0H5BxFZxoR5TPLMc= +github.com/manuelarte/gofuncor v0.1.1/go.mod h1:82VUujTxNIDRv1GyEYe1f5xn6OMn9Xza8Lb5CljsvRY= github.com/maratori/testableexamples v1.0.0 h1:dU5alXRrD8WKSjOUnmJZuzdxWOEQ57+7s93SLMxb2vI= github.com/maratori/testableexamples v1.0.0/go.mod h1:4rhjL1n20TUTT4vdh3RDqSizKLyXp7K2u6HgraZCGzE= github.com/maratori/testpackage v1.1.1 h1:S58XVV5AD7HADMmD0fNnziNHqKvSdDuEKdPD1rNTU04= From 4c6bfa32a5466f8fe5689fa4bbac973e35047599 Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Wed, 26 Mar 2025 23:52:23 +0100 Subject: [PATCH 12/21] newlinter: renaming gofuncor to funcorder --- .golangci.next.reference.yml | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- .../{gofuncor/gofuncor.go => funcorder/funcorder.go} | 4 ++-- .../funcorder_integration_test.go} | 2 +- .../gofuncor.go => funcorder/testdata/funcorder.go} | 2 +- pkg/lint/lintersdb/builder_linter.go | 10 +++++----- 7 files changed, 14 insertions(+), 14 deletions(-) rename pkg/golinters/{gofuncor/gofuncor.go => funcorder/funcorder.go} (81%) rename pkg/golinters/{gofuncor/gofuncor_integration_test.go => funcorder/funcorder_integration_test.go} (89%) rename pkg/golinters/{gofuncor/testdata/gofuncor.go => funcorder/testdata/funcorder.go} (95%) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index f30a83cdc67b..bdda18bf2c2a 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -43,6 +43,7 @@ linters: - fatcontext - forbidigo - forcetypeassert + - funcorder - funlen - ginkgolinter - gocheckcompilerdirectives @@ -55,7 +56,6 @@ linters: - gocyclo - godot - godox - - gofuncor - goheader - gomoddirectives - gomodguard @@ -151,6 +151,7 @@ linters: - fatcontext - forbidigo - forcetypeassert + - funcorder - funlen - ginkgolinter - gocheckcompilerdirectives @@ -163,7 +164,6 @@ linters: - gocyclo - godot - godox - - gofuncor - goheader - gomoddirectives - gomodguard diff --git a/go.mod b/go.mod index 89f9db2f6737..ae822f155f20 100644 --- a/go.mod +++ b/go.mod @@ -73,7 +73,7 @@ require ( github.com/ldez/usetesting v0.4.2 github.com/leonklingele/grouper v1.1.2 github.com/macabu/inamedparam v0.2.0 - github.com/manuelarte/gofuncor v0.1.1 + github.com/manuelarte/funcorder v0.1.2 github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.1 github.com/matoous/godox v1.1.0 diff --git a/go.sum b/go.sum index eb3a1fdb5387..94a0e71c65cb 100644 --- a/go.sum +++ b/go.sum @@ -389,8 +389,8 @@ github.com/macabu/inamedparam v0.2.0 h1:VyPYpOc10nkhI2qeNUdh3Zket4fcZjEWe35poddB github.com/macabu/inamedparam v0.2.0/go.mod h1:+Pee9/YfGe5LJ62pYXqB89lJ+0k5bsR8Wgz/C0Zlq3U= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= -github.com/manuelarte/gofuncor v0.1.1 h1:h/HOzmYwItmSlBfvh5paN6Bktjn0H5BxFZxoR5TPLMc= -github.com/manuelarte/gofuncor v0.1.1/go.mod h1:82VUujTxNIDRv1GyEYe1f5xn6OMn9Xza8Lb5CljsvRY= +github.com/manuelarte/funcorder v0.1.2 h1:nhrOVexfWRW5l9/noD2lqeu/TpCQ6zYI/kyLvKL8NTI= +github.com/manuelarte/funcorder v0.1.2/go.mod h1:BQQ0yW57+PF9ZpjpeJDKOffEsQbxDFKW8F8zSMe/Zd0= github.com/maratori/testableexamples v1.0.0 h1:dU5alXRrD8WKSjOUnmJZuzdxWOEQ57+7s93SLMxb2vI= github.com/maratori/testableexamples v1.0.0/go.mod h1:4rhjL1n20TUTT4vdh3RDqSizKLyXp7K2u6HgraZCGzE= github.com/maratori/testpackage v1.1.1 h1:S58XVV5AD7HADMmD0fNnziNHqKvSdDuEKdPD1rNTU04= diff --git a/pkg/golinters/gofuncor/gofuncor.go b/pkg/golinters/funcorder/funcorder.go similarity index 81% rename from pkg/golinters/gofuncor/gofuncor.go rename to pkg/golinters/funcorder/funcorder.go index cb73a627d876..8ceff45c607a 100644 --- a/pkg/golinters/gofuncor/gofuncor.go +++ b/pkg/golinters/funcorder/funcorder.go @@ -1,7 +1,7 @@ -package gofuncor +package funcorder import ( - "github.com/manuelarte/gofuncor/pkg/analyzer" + "github.com/manuelarte/funcorder/pkg/analyzer" "golang.org/x/tools/go/analysis" "github.com/golangci/golangci-lint/v2/pkg/goanalysis" diff --git a/pkg/golinters/gofuncor/gofuncor_integration_test.go b/pkg/golinters/funcorder/funcorder_integration_test.go similarity index 89% rename from pkg/golinters/gofuncor/gofuncor_integration_test.go rename to pkg/golinters/funcorder/funcorder_integration_test.go index 6bd05e69cb30..31ee6ec82d4b 100644 --- a/pkg/golinters/gofuncor/gofuncor_integration_test.go +++ b/pkg/golinters/funcorder/funcorder_integration_test.go @@ -1,4 +1,4 @@ -package gofuncor +package funcorder import ( "testing" diff --git a/pkg/golinters/gofuncor/testdata/gofuncor.go b/pkg/golinters/funcorder/testdata/funcorder.go similarity index 95% rename from pkg/golinters/gofuncor/testdata/gofuncor.go rename to pkg/golinters/funcorder/testdata/funcorder.go index c453260c0bb7..b9ae4e81fafa 100644 --- a/pkg/golinters/gofuncor/testdata/gofuncor.go +++ b/pkg/golinters/funcorder/testdata/funcorder.go @@ -1,4 +1,4 @@ -//golangcitest:args -Egofuncor +//golangcitest:args -Efuncorder package testdata import "time" diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index 97ed2a811f45..99a9dcbebf03 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -29,6 +29,7 @@ import ( "github.com/golangci/golangci-lint/v2/pkg/golinters/fatcontext" "github.com/golangci/golangci-lint/v2/pkg/golinters/forbidigo" "github.com/golangci/golangci-lint/v2/pkg/golinters/forcetypeassert" + "github.com/golangci/golangci-lint/v2/pkg/golinters/funcorder" "github.com/golangci/golangci-lint/v2/pkg/golinters/funlen" "github.com/golangci/golangci-lint/v2/pkg/golinters/gci" "github.com/golangci/golangci-lint/v2/pkg/golinters/ginkgolinter" @@ -44,7 +45,6 @@ import ( "github.com/golangci/golangci-lint/v2/pkg/golinters/godox" "github.com/golangci/golangci-lint/v2/pkg/golinters/gofmt" "github.com/golangci/golangci-lint/v2/pkg/golinters/gofumpt" - "github.com/golangci/golangci-lint/v2/pkg/golinters/gofuncor" "github.com/golangci/golangci-lint/v2/pkg/golinters/goheader" "github.com/golangci/golangci-lint/v2/pkg/golinters/goimports" "github.com/golangci/golangci-lint/v2/pkg/golinters/golines" @@ -255,6 +255,10 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithLoadForGoAnalysis(). WithURL("https://github.com/gostaticanalysis/forcetypeassert"), + linter.NewConfig(funcorder.New()). + WithSince("v2.1.0"). + WithURL("https://github.com/manuelarte/funcorder"), + linter.NewConfig(fatcontext.New(&cfg.Linters.Settings.Fatcontext)). WithSince("v1.58.0"). WithLoadForGoAnalysis(). @@ -336,10 +340,6 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithAutoFix(). WithURL("https://github.com/mvdan/gofumpt"), - linter.NewConfig(gofuncor.New()). - WithSince("v2.1.0"). - WithURL("https://github.com/manuelarte/gofuncor"), - linter.NewConfig(golines.New(&cfg.Linters.Settings.GoLines)). WithSince("v2.0.0"). WithAutoFix(). From 59de295c426014d40b39e477668c9f8ab33f98c4 Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Fri, 28 Mar 2025 21:11:07 +0100 Subject: [PATCH 13/21] newlinter: adding funcorder settings --- .golangci.next.reference.yml | 6 ++++++ go.mod | 2 +- go.sum | 4 ++-- pkg/config/linters_settings.go | 6 ++++++ pkg/golinters/funcorder/funcorder.go | 26 +++++++++++++++++++++++--- pkg/lint/lintersdb/builder_linter.go | 2 +- 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index bdda18bf2c2a..c46a2275799b 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -531,6 +531,12 @@ linters: # Default: false analyze-types: true + funcorder: + # Enable/disable feature to check constructors are placed after struct declaration + constructor: true + # Enable/disable feature to check whether the exported struct's methods are placed before the non-exported + struct-method: true + funlen: # Checks the number of lines in a function. # If lower than 0, disable the check. diff --git a/go.mod b/go.mod index ae822f155f20..5666f76f2c68 100644 --- a/go.mod +++ b/go.mod @@ -73,7 +73,7 @@ require ( github.com/ldez/usetesting v0.4.2 github.com/leonklingele/grouper v1.1.2 github.com/macabu/inamedparam v0.2.0 - github.com/manuelarte/funcorder v0.1.2 + github.com/manuelarte/funcorder v0.2.0 github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.1 github.com/matoous/godox v1.1.0 diff --git a/go.sum b/go.sum index 94a0e71c65cb..fcbdbcd44e15 100644 --- a/go.sum +++ b/go.sum @@ -389,8 +389,8 @@ github.com/macabu/inamedparam v0.2.0 h1:VyPYpOc10nkhI2qeNUdh3Zket4fcZjEWe35poddB github.com/macabu/inamedparam v0.2.0/go.mod h1:+Pee9/YfGe5LJ62pYXqB89lJ+0k5bsR8Wgz/C0Zlq3U= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= -github.com/manuelarte/funcorder v0.1.2 h1:nhrOVexfWRW5l9/noD2lqeu/TpCQ6zYI/kyLvKL8NTI= -github.com/manuelarte/funcorder v0.1.2/go.mod h1:BQQ0yW57+PF9ZpjpeJDKOffEsQbxDFKW8F8zSMe/Zd0= +github.com/manuelarte/funcorder v0.2.0 h1:SrV3vKzKwlFa99CEgm6AnE5AINxqdYA2bzLZVNNIysY= +github.com/manuelarte/funcorder v0.2.0/go.mod h1:BQQ0yW57+PF9ZpjpeJDKOffEsQbxDFKW8F8zSMe/Zd0= github.com/maratori/testableexamples v1.0.0 h1:dU5alXRrD8WKSjOUnmJZuzdxWOEQ57+7s93SLMxb2vI= github.com/maratori/testableexamples v1.0.0/go.mod h1:4rhjL1n20TUTT4vdh3RDqSizKLyXp7K2u6HgraZCGzE= github.com/maratori/testpackage v1.1.1 h1:S58XVV5AD7HADMmD0fNnziNHqKvSdDuEKdPD1rNTU04= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 86ee4fea48d2..473512f7df0f 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -217,6 +217,7 @@ type LintersSettings struct { Exhaustruct ExhaustructSettings `mapstructure:"exhaustruct"` Fatcontext FatcontextSettings `mapstructure:"fatcontext"` Forbidigo ForbidigoSettings `mapstructure:"forbidigo"` + FuncOrder FuncOrderSettings `mapstructure:"funcorder"` Funlen FunlenSettings `mapstructure:"funlen"` GinkgoLinter GinkgoLinterSettings `mapstructure:"ginkgolinter"` Gocognit GocognitSettings `mapstructure:"gocognit"` @@ -420,6 +421,11 @@ type ForbidigoPattern struct { Msg string `yaml:"msg,omitempty" mapstructure:"msg,omitempty"` } +type FuncOrderSettings struct { + Constructor *bool `mapstructure:"constructor,omitempty"` + StructMethod *bool `mapstructure:"struct-method,omitempty"` +} + type FunlenSettings struct { Lines int `mapstructure:"lines"` Statements int `mapstructure:"statements"` diff --git a/pkg/golinters/funcorder/funcorder.go b/pkg/golinters/funcorder/funcorder.go index 8ceff45c607a..51011e8e9069 100644 --- a/pkg/golinters/funcorder/funcorder.go +++ b/pkg/golinters/funcorder/funcorder.go @@ -1,19 +1,39 @@ package funcorder import ( - "github.com/manuelarte/funcorder/pkg/analyzer" + "github.com/manuelarte/funcorder/analyzer" "golang.org/x/tools/go/analysis" + "github.com/golangci/golangci-lint/v2/pkg/config" + "github.com/golangci/golangci-lint/v2/pkg/goanalysis" ) -func New() *goanalysis.Linter { +func New(settings *config.FuncOrderSettings) *goanalysis.Linter { a := analyzer.NewAnalyzer() + cfg := map[string]map[string]any{} + + if settings != nil { + constructor := true + if settings.Constructor != nil { + constructor = *settings.Constructor + } + structMethod := true + if settings.StructMethod != nil { + structMethod = *settings.StructMethod + } + + cfg[a.Name] = map[string]any{ + "constructor": constructor, + "struct-method": structMethod, + } + } + return goanalysis.NewLinter( a.Name, a.Doc, []*analysis.Analyzer{a}, - nil, + cfg, ).WithLoadMode(goanalysis.LoadModeSyntax) } diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index 99a9dcbebf03..2067e204fb3e 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -255,7 +255,7 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithLoadForGoAnalysis(). WithURL("https://github.com/gostaticanalysis/forcetypeassert"), - linter.NewConfig(funcorder.New()). + linter.NewConfig(funcorder.New(&cfg.Linters.Settings.FuncOrder)). WithSince("v2.1.0"). WithURL("https://github.com/manuelarte/funcorder"), From 26c7b00abdbc47394101bb4f07b14abcb54d34bf Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Fri, 28 Mar 2025 21:26:30 +0100 Subject: [PATCH 14/21] newlinter: using settings exported constants --- go.mod | 2 +- go.sum | 4 ++-- pkg/golinters/funcorder/funcorder.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 5666f76f2c68..52a70d524cef 100644 --- a/go.mod +++ b/go.mod @@ -73,7 +73,7 @@ require ( github.com/ldez/usetesting v0.4.2 github.com/leonklingele/grouper v1.1.2 github.com/macabu/inamedparam v0.2.0 - github.com/manuelarte/funcorder v0.2.0 + github.com/manuelarte/funcorder v0.2.1 github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.1 github.com/matoous/godox v1.1.0 diff --git a/go.sum b/go.sum index fcbdbcd44e15..37715820db95 100644 --- a/go.sum +++ b/go.sum @@ -389,8 +389,8 @@ github.com/macabu/inamedparam v0.2.0 h1:VyPYpOc10nkhI2qeNUdh3Zket4fcZjEWe35poddB github.com/macabu/inamedparam v0.2.0/go.mod h1:+Pee9/YfGe5LJ62pYXqB89lJ+0k5bsR8Wgz/C0Zlq3U= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= -github.com/manuelarte/funcorder v0.2.0 h1:SrV3vKzKwlFa99CEgm6AnE5AINxqdYA2bzLZVNNIysY= -github.com/manuelarte/funcorder v0.2.0/go.mod h1:BQQ0yW57+PF9ZpjpeJDKOffEsQbxDFKW8F8zSMe/Zd0= +github.com/manuelarte/funcorder v0.2.1 h1:7QJsw3qhljoZ5rH0xapIvjw31EcQeFbF31/7kQ/xS34= +github.com/manuelarte/funcorder v0.2.1/go.mod h1:BQQ0yW57+PF9ZpjpeJDKOffEsQbxDFKW8F8zSMe/Zd0= github.com/maratori/testableexamples v1.0.0 h1:dU5alXRrD8WKSjOUnmJZuzdxWOEQ57+7s93SLMxb2vI= github.com/maratori/testableexamples v1.0.0/go.mod h1:4rhjL1n20TUTT4vdh3RDqSizKLyXp7K2u6HgraZCGzE= github.com/maratori/testpackage v1.1.1 h1:S58XVV5AD7HADMmD0fNnziNHqKvSdDuEKdPD1rNTU04= diff --git a/pkg/golinters/funcorder/funcorder.go b/pkg/golinters/funcorder/funcorder.go index 51011e8e9069..2a6d2385c4e1 100644 --- a/pkg/golinters/funcorder/funcorder.go +++ b/pkg/golinters/funcorder/funcorder.go @@ -25,8 +25,8 @@ func New(settings *config.FuncOrderSettings) *goanalysis.Linter { } cfg[a.Name] = map[string]any{ - "constructor": constructor, - "struct-method": structMethod, + analyzer.ConstructorCheckName: constructor, + analyzer.StructMethodCheckName: structMethod, } } From 616d82466b6c9039710c6c36b092e207cfda0e1a Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Fri, 28 Mar 2025 21:33:15 +0100 Subject: [PATCH 15/21] newlinter: fmt --- pkg/golinters/funcorder/funcorder.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/golinters/funcorder/funcorder.go b/pkg/golinters/funcorder/funcorder.go index 2a6d2385c4e1..4d4431e76381 100644 --- a/pkg/golinters/funcorder/funcorder.go +++ b/pkg/golinters/funcorder/funcorder.go @@ -5,7 +5,6 @@ import ( "golang.org/x/tools/go/analysis" "github.com/golangci/golangci-lint/v2/pkg/config" - "github.com/golangci/golangci-lint/v2/pkg/goanalysis" ) From a427d22291cc30506b3f73f15a6ac658013070c3 Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Fri, 28 Mar 2025 22:12:03 +0100 Subject: [PATCH 16/21] newlinter: fixing settings check --- .golangci.next.reference.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index c46a2275799b..4096c5003f6a 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -533,9 +533,11 @@ linters: funcorder: # Enable/disable feature to check constructors are placed after struct declaration - constructor: true + # Default: true + constructor: false # Enable/disable feature to check whether the exported struct's methods are placed before the non-exported - struct-method: true + # Default: true + struct-method: false funlen: # Checks the number of lines in a function. From 6603c255d12dc96dabb3d860da6637c3cb993589 Mon Sep 17 00:00:00 2001 From: Manuel Doncel Martos Date: Sat, 29 Mar 2025 15:40:26 +0100 Subject: [PATCH 17/21] adding final '.' at the end of the comments --- .golangci.next.reference.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 4096c5003f6a..28ce5bf183a2 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -532,10 +532,10 @@ linters: analyze-types: true funcorder: - # Enable/disable feature to check constructors are placed after struct declaration + # Enable/disable feature to check constructors are placed after struct declaration. # Default: true constructor: false - # Enable/disable feature to check whether the exported struct's methods are placed before the non-exported + # Enable/disable feature to check whether the exported struct's methods are placed before the non-exported. # Default: true struct-method: false From ba56a63636c53fecbaeb3dce1307a65f9df185cf Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 11 Apr 2025 18:08:38 +0200 Subject: [PATCH 18/21] review: update JSONSchema --- jsonschema/golangci.next.jsonschema.json | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index 5dc6b9d9570d..c726955c9571 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -741,6 +741,7 @@ "fatcontext", "forbidigo", "forcetypeassert", + "funcorder", "funlen", "ginkgolinter", "gocheckcompilerdirectives", @@ -1302,6 +1303,22 @@ } } }, + "funcorderSettings": { + "type": "object", + "additionalProperties": false, + "properties": { + "constructor": { + "description": "Enable/disable feature to check constructors are placed after struct declaration.", + "type": "boolean", + "default": true + }, + "struct-method": { + "description": "Enable/disable feature to check whether the exported struct's methods are placed before the non-exported.", + "type": "boolean", + "default": true + } + } + }, "funlenSettings": { "type": "object", "additionalProperties": false, @@ -4345,6 +4362,9 @@ "forbidigo": { "$ref": "#/definitions/settings/definitions/forbidigoSettings" }, + "funcorder": { + "$ref": "#/definitions/settings/definitions/funcorderSettings" + }, "funlen": { "$ref": "#/definitions/settings/definitions/funlenSettings" }, From 3a48f332ff115c3a83daeb7a8879c097e9bc04e4 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 11 Apr 2025 18:28:51 +0200 Subject: [PATCH 19/21] review --- pkg/config/linters_settings.go | 8 +++- pkg/golinters/funcorder/funcorder.go | 13 +----- .../testdata/funcorder_disable_constructor.go | 41 +++++++++++++++++++ .../funcorder_disable_constructor.yml | 6 +++ .../testdata/funcorder_struct_method.go | 41 +++++++++++++++++++ .../testdata/funcorder_struct_method.yml | 6 +++ 6 files changed, 102 insertions(+), 13 deletions(-) create mode 100644 pkg/golinters/funcorder/testdata/funcorder_disable_constructor.go create mode 100644 pkg/golinters/funcorder/testdata/funcorder_disable_constructor.yml create mode 100644 pkg/golinters/funcorder/testdata/funcorder_struct_method.go create mode 100644 pkg/golinters/funcorder/testdata/funcorder_struct_method.yml diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index a823f28ce1b4..16a8bb9501b9 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -41,6 +41,10 @@ var defaultLintersSettings = LintersSettings{ Forbidigo: ForbidigoSettings{ ExcludeGodocExamples: true, }, + FuncOrder: FuncOrderSettings{ + Constructor: true, + StructMethod: true, + }, Funlen: FunlenSettings{ IgnoreComments: true, }, @@ -422,8 +426,8 @@ type ForbidigoPattern struct { } type FuncOrderSettings struct { - Constructor *bool `mapstructure:"constructor,omitempty"` - StructMethod *bool `mapstructure:"struct-method,omitempty"` + Constructor bool `mapstructure:"constructor,omitempty"` + StructMethod bool `mapstructure:"struct-method,omitempty"` } type FunlenSettings struct { diff --git a/pkg/golinters/funcorder/funcorder.go b/pkg/golinters/funcorder/funcorder.go index 4d4431e76381..7b8e9689e587 100644 --- a/pkg/golinters/funcorder/funcorder.go +++ b/pkg/golinters/funcorder/funcorder.go @@ -14,18 +14,9 @@ func New(settings *config.FuncOrderSettings) *goanalysis.Linter { cfg := map[string]map[string]any{} if settings != nil { - constructor := true - if settings.Constructor != nil { - constructor = *settings.Constructor - } - structMethod := true - if settings.StructMethod != nil { - structMethod = *settings.StructMethod - } - cfg[a.Name] = map[string]any{ - analyzer.ConstructorCheckName: constructor, - analyzer.StructMethodCheckName: structMethod, + analyzer.ConstructorCheckName: settings.Constructor, + analyzer.StructMethodCheckName: settings.StructMethod, } } diff --git a/pkg/golinters/funcorder/testdata/funcorder_disable_constructor.go b/pkg/golinters/funcorder/testdata/funcorder_disable_constructor.go new file mode 100644 index 000000000000..d131d5dee356 --- /dev/null +++ b/pkg/golinters/funcorder/testdata/funcorder_disable_constructor.go @@ -0,0 +1,41 @@ +//golangcitest:args -Efuncorder +//golangcitest:config_path testdata/funcorder_disable_constructor.yml +package testdata + +import "time" + +type MyStruct struct { + Name string +} + +func (m MyStruct) lenName() int { // want `unexported method "lenName" for struct "MyStruct" should be placed after the exported method "SetName"` + return len(m.Name) +} + +func (m MyStruct) GetName() string { + return m.Name +} + +func (m *MyStruct) SetName(name string) { + m.Name = name +} + +type MyStruct2 struct { + Name string +} + +func (m MyStruct2) GetName() string { + return m.Name +} + +func (m *MyStruct2) SetName(name string) { + m.Name = name +} + +func NewMyStruct2() *MyStruct2 { + return &MyStruct2{Name: "John"} +} + +func NewTime() time.Time { + return time.Now() +} diff --git a/pkg/golinters/funcorder/testdata/funcorder_disable_constructor.yml b/pkg/golinters/funcorder/testdata/funcorder_disable_constructor.yml new file mode 100644 index 000000000000..df43fb02f4d4 --- /dev/null +++ b/pkg/golinters/funcorder/testdata/funcorder_disable_constructor.yml @@ -0,0 +1,6 @@ +version: "2" + +linters: + settings: + funcorder: + constructor: false diff --git a/pkg/golinters/funcorder/testdata/funcorder_struct_method.go b/pkg/golinters/funcorder/testdata/funcorder_struct_method.go new file mode 100644 index 000000000000..a44f980e8b9e --- /dev/null +++ b/pkg/golinters/funcorder/testdata/funcorder_struct_method.go @@ -0,0 +1,41 @@ +//golangcitest:args -Efuncorder +//golangcitest:config_path testdata/funcorder_struct_method.yml +package testdata + +import "time" + +type MyStruct struct { + Name string +} + +func (m MyStruct) lenName() int { + return len(m.Name) +} + +func (m MyStruct) GetName() string { + return m.Name +} + +func (m *MyStruct) SetName(name string) { + m.Name = name +} + +type MyStruct2 struct { + Name string +} + +func (m MyStruct2) GetName() string { + return m.Name +} + +func (m *MyStruct2) SetName(name string) { + m.Name = name +} + +func NewMyStruct2() *MyStruct2 { // want `constructor "NewMyStruct2" for struct "MyStruct2" should be placed before struct method "GetName"` + return &MyStruct2{Name: "John"} +} + +func NewTime() time.Time { + return time.Now() +} diff --git a/pkg/golinters/funcorder/testdata/funcorder_struct_method.yml b/pkg/golinters/funcorder/testdata/funcorder_struct_method.yml new file mode 100644 index 000000000000..70b27b44f9cd --- /dev/null +++ b/pkg/golinters/funcorder/testdata/funcorder_struct_method.yml @@ -0,0 +1,6 @@ +version: "2" + +linters: + settings: + funcorder: + struct-method: false From bf7892b6303ebaaed65d5589178786ae960b6652 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 11 Apr 2025 19:07:38 +0200 Subject: [PATCH 20/21] review --- .golangci.next.reference.yml | 4 ++-- jsonschema/golangci.next.jsonschema.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 9a29e2b141bb..fae9cc36e5b1 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -533,10 +533,10 @@ linters: analyze-types: true funcorder: - # Enable/disable feature to check constructors are placed after struct declaration. + # Checks that constructors are placed after the structure declaration. # Default: true constructor: false - # Enable/disable feature to check whether the exported struct's methods are placed before the non-exported. + # Checks if the methods of an exported structure are placed before the non-exported ones. # Default: true struct-method: false diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index c726955c9571..b9292ebb88d9 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -1308,12 +1308,12 @@ "additionalProperties": false, "properties": { "constructor": { - "description": "Enable/disable feature to check constructors are placed after struct declaration.", + "description": "Checks that constructors are placed after the structure declaration.", "type": "boolean", "default": true }, "struct-method": { - "description": "Enable/disable feature to check whether the exported struct's methods are placed before the non-exported.", + "description": "Checks if the methods of an exported structure are placed before the non-exported ones.", "type": "boolean", "default": true } From d0db109d7fc7e939efde6cd78d2ed2ec80887d2f Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 11 Apr 2025 19:19:15 +0200 Subject: [PATCH 21/21] review --- .golangci.next.reference.yml | 2 +- jsonschema/golangci.next.jsonschema.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index fae9cc36e5b1..cababbea228f 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -536,7 +536,7 @@ linters: # Checks that constructors are placed after the structure declaration. # Default: true constructor: false - # Checks if the methods of an exported structure are placed before the non-exported ones. + # Checks if the exported methods of a structure are placed before the non-exported ones. # Default: true struct-method: false diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index b9292ebb88d9..996a5ec3721f 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -1313,7 +1313,7 @@ "default": true }, "struct-method": { - "description": "Checks if the methods of an exported structure are placed before the non-exported ones.", + "description": "Checks if the exported methods of a structure are placed before the non-exported ones.", "type": "boolean", "default": true }