From bbe0eb9a1afaa2009c32e9df0d355276dc6df883 Mon Sep 17 00:00:00 2001 From: Ilia Sergunin Date: Sun, 12 Nov 2023 12:08:22 +0300 Subject: [PATCH 01/12] 1. Added reference 2. Added tests 3. Added config 4. Added linter --- .golangci.reference.yml | 9 +++++++ go.mod | 1 + go.sum | 2 ++ pkg/config/linters_settings.go | 6 +++++ pkg/golinters/gofactorylint.go | 30 +++++++++++++++++++++++ pkg/lint/lintersdb/manager.go | 7 ++++++ test/testdata/configs/gofactory.yml | 4 +++ test/testdata/gofactory/app.go | 24 ++++++++++++++++++ test/testdata/gofactory/nested/factory.go | 3 +++ 9 files changed, 86 insertions(+) create mode 100644 pkg/golinters/gofactorylint.go create mode 100644 test/testdata/configs/gofactory.yml create mode 100644 test/testdata/gofactory/app.go create mode 100644 test/testdata/gofactory/nested/factory.go diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 878597c38f45..df85e38d3bbc 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -661,6 +661,13 @@ linters-settings: - OPTIMIZE # marks code that should be optimized before merging - HACK # marks hack-around that should be removed before merging + gofactory: + # Default: [] + blockedPkgs: + - github.com/author/repository/path/to/package + # Default: false + onlyBlockedPkgs: true + gofmt: # Simplify code: gofmt with `-s` option. # Default: true @@ -2351,6 +2358,7 @@ linters: - godot - godox - goerr113 + - gofactory - gofmt - gofumpt - goheader @@ -2471,6 +2479,7 @@ linters: - godot - godox - goerr113 + - gofactory - gofmt - gofumpt - goheader diff --git a/go.mod b/go.mod index 66c1dd461952..6990d2bc81d8 100644 --- a/go.mod +++ b/go.mod @@ -69,6 +69,7 @@ require ( github.com/leonklingele/grouper v1.1.1 github.com/lufeee/execinquery v1.2.1 github.com/macabu/inamedparam v0.1.2 + github.com/maranqz/go-factory-lint v1.0.3 github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.1 github.com/matoous/godox v0.0.0-20230222163458-006bad1f9d26 diff --git a/go.sum b/go.sum index a2ed22f700b1..8aac9840e152 100644 --- a/go.sum +++ b/go.sum @@ -362,6 +362,8 @@ github.com/macabu/inamedparam v0.1.2 h1:RR5cnayM6Q7cDhQol32DE2BGAPGMnffJ31LFE+Uk github.com/macabu/inamedparam v0.1.2/go.mod h1:Xg25QvY7IBRl1KLPV9Rbml8JOMZtF/iAkNkmV7eQgjw= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= +github.com/maranqz/go-factory-lint v1.0.3 h1:GdJ+/uHAgaZV8e87U/Wk0HmGd/WD5hAb6NSRDX4f3p8= +github.com/maranqz/go-factory-lint v1.0.3/go.mod h1:PhCUIl545+e923NZUAr1lu7MWBt/Hsk5MFQ3GXLfvwU= 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 5f85059b5873..ff22dda817e0 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -199,6 +199,7 @@ type LintersSettings struct { Gocyclo GoCycloSettings Godot GodotSettings Godox GodoxSettings + Gofactory GoFactoryLintSettings Gofmt GoFmtSettings Gofumpt GofumptSettings Goheader GoHeaderSettings @@ -478,6 +479,11 @@ type GodoxSettings struct { Keywords []string } +type GoFactoryLintSettings struct { + BlockedPkgs map[string]map[string]string `mapstructure:"BlockedPkgs"` + OnlyBlockedPkgs string `mapstructure:"OnlyBlockedPkgs"` +} + type GoFmtSettings struct { Simplify bool RewriteRules []GoFmtRewriteRule `mapstructure:"rewrite-rules"` diff --git a/pkg/golinters/gofactorylint.go b/pkg/golinters/gofactorylint.go new file mode 100644 index 000000000000..e8881571eddd --- /dev/null +++ b/pkg/golinters/gofactorylint.go @@ -0,0 +1,30 @@ +package golinters + +import ( + "github.com/maranqz/go-factory-lint" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewGoFactoryLint(settings *config.GoFactoryLintSettings) *goanalysis.Linter { + a := factory.NewAnalyzer() + + cfg := make(map[string]map[string]any) + if settings != nil { + cfg[a.Name] = map[string]any{} + + if len(settings.BlockedPkgs) > 0 { + cfg[a.Name]["blockedPkgs"] = settings.BlockedPkgs + cfg[a.Name]["onlyBlockedPkgs"] = settings.OnlyBlockedPkgs + } + } + + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + cfg, + ).WithLoadMode(goanalysis.LoadModeTypesInfo) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 4f7848005964..ec18bba5c808 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -90,6 +90,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { gocycloCfg *config.GoCycloSettings godotCfg *config.GodotSettings godoxCfg *config.GodoxSettings + goFactoryCfg *config.GoFactoryLintSettings gofmtCfg *config.GoFmtSettings gofumptCfg *config.GofumptSettings goheaderCfg *config.GoHeaderSettings @@ -174,6 +175,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { gocycloCfg = &m.cfg.LintersSettings.Gocyclo godotCfg = &m.cfg.LintersSettings.Godot godoxCfg = &m.cfg.LintersSettings.Godox + goFactoryCfg = &m.cfg.LintersSettings.Gofactory gofmtCfg = &m.cfg.LintersSettings.Gofmt gofumptCfg = &m.cfg.LintersSettings.Gofumpt goheaderCfg = &m.cfg.LintersSettings.Goheader @@ -488,6 +490,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithLoadForGoAnalysis(). WithURL("https://github.com/Djarvur/go-err113"), + linter.NewConfig(golinters.NewGoFactoryLint(goFactoryCfg)). + WithSince("next_version"). + WithPresets(linter.PresetStyle). + WithURL("https://github.com/maranqz/go-factory-lint"), + linter.NewConfig(golinters.NewGofmt(gofmtCfg)). WithSince("v1.0.0"). WithPresets(linter.PresetFormatting). diff --git a/test/testdata/configs/gofactory.yml b/test/testdata/configs/gofactory.yml new file mode 100644 index 000000000000..b26a9dcfeab2 --- /dev/null +++ b/test/testdata/configs/gofactory.yml @@ -0,0 +1,4 @@ +linters-settings: + gofactory: + blockedPkgs: [] + onlyBlockedPkgs: false diff --git a/test/testdata/gofactory/app.go b/test/testdata/gofactory/app.go new file mode 100644 index 000000000000..e6281d34a799 --- /dev/null +++ b/test/testdata/gofactory/app.go @@ -0,0 +1,24 @@ +package gofactory + +import "github.com/golangci/golangci-lint/test/testdata/gofactory/nested" + +type Struct struct{} + +var ( + globalStruct = nested.Struct{} // want `Use factory for nested.Struct` + globalStructPtr = &nested.Struct{} // want `Use factory for nested.Struct` +) + +func fn() { + _ = nested.Struct{} // want `Use factory for nested.Struct` + _ = &nested.Struct{} // want `Use factory for nested.Struct` + + _ = []nested.Struct{{}, nested.Struct{}} // want `Use factory for nested.Struct` + _ = []*nested.Struct{{}, &nested.Struct{}} // want `Use factory for nested.Struct` + + call(nested.Struct{}) // want `Use factory for nested.Struct` + + _ = []Struct{{}, {}} +} + +func call(_ nested.Struct) {} diff --git a/test/testdata/gofactory/nested/factory.go b/test/testdata/gofactory/nested/factory.go new file mode 100644 index 000000000000..3e620d5d9b67 --- /dev/null +++ b/test/testdata/gofactory/nested/factory.go @@ -0,0 +1,3 @@ +package nested + +type Struct struct{} From 70cd3e5454a2b8810855b599a30a985cb0824ee6 Mon Sep 17 00:00:00 2001 From: Ilia Sergunin Date: Wed, 15 Nov 2023 10:53:37 +0400 Subject: [PATCH 02/12] 1. Update version after type fixes --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 6990d2bc81d8..0bde3ac606e6 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,7 @@ require ( github.com/leonklingele/grouper v1.1.1 github.com/lufeee/execinquery v1.2.1 github.com/macabu/inamedparam v0.1.2 - github.com/maranqz/go-factory-lint v1.0.3 + github.com/maranqz/go-factory-lint v1.0.4 github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.1 github.com/matoous/godox v0.0.0-20230222163458-006bad1f9d26 diff --git a/go.sum b/go.sum index 8aac9840e152..17726670b4e5 100644 --- a/go.sum +++ b/go.sum @@ -364,6 +364,8 @@ github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamh github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/maranqz/go-factory-lint v1.0.3 h1:GdJ+/uHAgaZV8e87U/Wk0HmGd/WD5hAb6NSRDX4f3p8= github.com/maranqz/go-factory-lint v1.0.3/go.mod h1:PhCUIl545+e923NZUAr1lu7MWBt/Hsk5MFQ3GXLfvwU= +github.com/maranqz/go-factory-lint v1.0.4 h1:Uo7EJNnqhrMNGTnTM2YpflDyjxJb4IfBVsh06Q3GLjk= +github.com/maranqz/go-factory-lint v1.0.4/go.mod h1:PhCUIl545+e923NZUAr1lu7MWBt/Hsk5MFQ3GXLfvwU= 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 5a86fb88df2b4d0cf3a0a16fde575b0fa791f4ab Mon Sep 17 00:00:00 2001 From: Ilia Sergunin Date: Wed, 15 Nov 2023 11:22:08 +0400 Subject: [PATCH 03/12] 1. Added tests in subdir --- pkg/golinters/gofactorylint.go | 14 +++++++------- pkg/lint/lintersdb/manager.go | 1 + test/linters_test.go | 1 + test/testdata/gofactory/app.go | 2 ++ 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pkg/golinters/gofactorylint.go b/pkg/golinters/gofactorylint.go index e8881571eddd..ad53042da35d 100644 --- a/pkg/golinters/gofactorylint.go +++ b/pkg/golinters/gofactorylint.go @@ -9,22 +9,22 @@ import ( ) func NewGoFactoryLint(settings *config.GoFactoryLintSettings) *goanalysis.Linter { - a := factory.NewAnalyzer() + analyzer := factory.NewAnalyzer() cfg := make(map[string]map[string]any) if settings != nil { - cfg[a.Name] = map[string]any{} + cfg[analyzer.Name] = map[string]any{} if len(settings.BlockedPkgs) > 0 { - cfg[a.Name]["blockedPkgs"] = settings.BlockedPkgs - cfg[a.Name]["onlyBlockedPkgs"] = settings.OnlyBlockedPkgs + cfg[analyzer.Name]["blockedPkgs"] = settings.BlockedPkgs + cfg[analyzer.Name]["onlyBlockedPkgs"] = settings.OnlyBlockedPkgs } } return goanalysis.NewLinter( - a.Name, - a.Doc, - []*analysis.Analyzer{a}, + analyzer.Name, + analyzer.Doc, + []*analysis.Analyzer{analyzer}, cfg, ).WithLoadMode(goanalysis.LoadModeTypesInfo) } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index ec18bba5c808..5539a2e614e3 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -493,6 +493,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { linter.NewConfig(golinters.NewGoFactoryLint(goFactoryCfg)). WithSince("next_version"). WithPresets(linter.PresetStyle). + WithLoadForGoAnalysis(). WithURL("https://github.com/maranqz/go-factory-lint"), linter.NewConfig(golinters.NewGofmt(gofmtCfg)). diff --git a/test/linters_test.go b/test/linters_test.go index 75d4f44adf1d..ee45d543b793 100644 --- a/test/linters_test.go +++ b/test/linters_test.go @@ -33,6 +33,7 @@ func TestSourcesFromTestdataSubDir(t *testing.T) { "ginkgolinter", "zerologlint", "protogetter", + "gofactory", } for _, dir := range subDirs { diff --git a/test/testdata/gofactory/app.go b/test/testdata/gofactory/app.go index e6281d34a799..82c6ad15ca29 100644 --- a/test/testdata/gofactory/app.go +++ b/test/testdata/gofactory/app.go @@ -1,3 +1,5 @@ +//golangcitest:args -Egofactory + package gofactory import "github.com/golangci/golangci-lint/test/testdata/gofactory/nested" From 2a31b53b1a56f4044449c907fcf5c16ac96df3fc Mon Sep 17 00:00:00 2001 From: Ilia Sergunin Date: Wed, 15 Nov 2023 14:26:23 +0400 Subject: [PATCH 04/12] 1. Bump golangci-lint version --- pkg/lint/lintersdb/manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 5539a2e614e3..38f4d598a652 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -491,7 +491,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/Djarvur/go-err113"), linter.NewConfig(golinters.NewGoFactoryLint(goFactoryCfg)). - WithSince("next_version"). + WithSince("1.56.0"). WithPresets(linter.PresetStyle). WithLoadForGoAnalysis(). WithURL("https://github.com/maranqz/go-factory-lint"), From 22e29b77985dcf9dca3dfb289a45d19dfe6c66b9 Mon Sep 17 00:00:00 2001 From: Ilia Sergunin Date: Wed, 15 Nov 2023 23:13:36 +0400 Subject: [PATCH 05/12] 1. Fixed settings name 2. Added test with options --- pkg/config/linters_settings.go | 4 ++-- pkg/golinters/gofactorylint.go | 2 ++ test/testdata/configs/gofactory.yml | 4 ---- test/testdata/gofactory/blocked.go | 24 +++++++++++++++++++ test/testdata/gofactory/blocked/blocked.go | 3 +++ .../configs/go_factory_only_blocked.yml | 5 ++++ .../testdata/gofactory/{app.go => default.go} | 16 +++++++++---- test/testdata/gofactory/go.mod | 4 ++++ .../nested/{factory.go => nested.go} | 0 9 files changed, 51 insertions(+), 11 deletions(-) delete mode 100644 test/testdata/configs/gofactory.yml create mode 100644 test/testdata/gofactory/blocked.go create mode 100644 test/testdata/gofactory/blocked/blocked.go create mode 100644 test/testdata/gofactory/configs/go_factory_only_blocked.yml rename test/testdata/gofactory/{app.go => default.go} (59%) create mode 100644 test/testdata/gofactory/go.mod rename test/testdata/gofactory/nested/{factory.go => nested.go} (100%) diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index ff22dda817e0..cd4003b64c35 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -480,8 +480,8 @@ type GodoxSettings struct { } type GoFactoryLintSettings struct { - BlockedPkgs map[string]map[string]string `mapstructure:"BlockedPkgs"` - OnlyBlockedPkgs string `mapstructure:"OnlyBlockedPkgs"` + BlockedPkgs []string `mapstructure:"blockedPkgs"` + OnlyBlockedPkgs bool `mapstructure:"onlyBlockedPkgs"` } type GoFmtSettings struct { diff --git a/pkg/golinters/gofactorylint.go b/pkg/golinters/gofactorylint.go index ad53042da35d..f9d412e308e7 100644 --- a/pkg/golinters/gofactorylint.go +++ b/pkg/golinters/gofactorylint.go @@ -21,6 +21,8 @@ func NewGoFactoryLint(settings *config.GoFactoryLintSettings) *goanalysis.Linter } } + // TODO add options to configuration + return goanalysis.NewLinter( analyzer.Name, analyzer.Doc, diff --git a/test/testdata/configs/gofactory.yml b/test/testdata/configs/gofactory.yml deleted file mode 100644 index b26a9dcfeab2..000000000000 --- a/test/testdata/configs/gofactory.yml +++ /dev/null @@ -1,4 +0,0 @@ -linters-settings: - gofactory: - blockedPkgs: [] - onlyBlockedPkgs: false diff --git a/test/testdata/gofactory/blocked.go b/test/testdata/gofactory/blocked.go new file mode 100644 index 000000000000..7f8394c2ef44 --- /dev/null +++ b/test/testdata/gofactory/blocked.go @@ -0,0 +1,24 @@ +//golangcitest:args -Egofactory +//golangcitest:config_path configs/go_factory_only_blocked.yml +package gofactory + +import ( + "gofactory/blocked" + "gofactory/nested" +) + +var ( + nestedGlobalStruct = nested.Struct{} + nestedGlobalStructPtr = &nested.Struct{} + + blockedGlobalStruct = blocked.Struct{} + blockedGlobalStructPtr = &blocked.Struct{} +) + +func Blocked() { + _ = nested.Struct{} + _ = &nested.Struct{} + + _ = blocked.Struct{} // want `Use factory for nested.Struct` + _ = &blocked.Struct{} // want `Use factory for nested.Struct` +} diff --git a/test/testdata/gofactory/blocked/blocked.go b/test/testdata/gofactory/blocked/blocked.go new file mode 100644 index 000000000000..13bc576205b9 --- /dev/null +++ b/test/testdata/gofactory/blocked/blocked.go @@ -0,0 +1,3 @@ +package blocked + +type Struct struct{} diff --git a/test/testdata/gofactory/configs/go_factory_only_blocked.yml b/test/testdata/gofactory/configs/go_factory_only_blocked.yml new file mode 100644 index 000000000000..f23c98de84f1 --- /dev/null +++ b/test/testdata/gofactory/configs/go_factory_only_blocked.yml @@ -0,0 +1,5 @@ +linters-settings: + gofactory: + blockedPkgs: + - blocked + onlyBlockedPkgs: true diff --git a/test/testdata/gofactory/app.go b/test/testdata/gofactory/default.go similarity index 59% rename from test/testdata/gofactory/app.go rename to test/testdata/gofactory/default.go index 82c6ad15ca29..1d984fd8311d 100644 --- a/test/testdata/gofactory/app.go +++ b/test/testdata/gofactory/default.go @@ -1,17 +1,19 @@ //golangcitest:args -Egofactory - package gofactory -import "github.com/golangci/golangci-lint/test/testdata/gofactory/nested" +import ( + alias_blocked "gofactory/blocked" + "gofactory/nested" +) type Struct struct{} var ( - globalStruct = nested.Struct{} // want `Use factory for nested.Struct` - globalStructPtr = &nested.Struct{} // want `Use factory for nested.Struct` + defaultGlobalStruct = nested.Struct{} // want `Use factory for nested.Struct` + defaultGlobalStructPtr = &nested.Struct{} // want `Use factory for nested.Struct` ) -func fn() { +func Default() { _ = nested.Struct{} // want `Use factory for nested.Struct` _ = &nested.Struct{} // want `Use factory for nested.Struct` @@ -24,3 +26,7 @@ func fn() { } func call(_ nested.Struct) {} + +func alias() { + _ = alias_blocked.Struct{} // want `Use factory for blocked.Struct` +} diff --git a/test/testdata/gofactory/go.mod b/test/testdata/gofactory/go.mod new file mode 100644 index 000000000000..9f6be767e6ba --- /dev/null +++ b/test/testdata/gofactory/go.mod @@ -0,0 +1,4 @@ +module gofactory + +go 1.18 + diff --git a/test/testdata/gofactory/nested/factory.go b/test/testdata/gofactory/nested/nested.go similarity index 100% rename from test/testdata/gofactory/nested/factory.go rename to test/testdata/gofactory/nested/nested.go From faecc6b82f61fac0cf3417403941d2c7745f84f3 Mon Sep 17 00:00:00 2001 From: Ilia Sergunin Date: Wed, 15 Nov 2023 23:42:55 +0400 Subject: [PATCH 06/12] 1. Fixed test with config file --- pkg/golinters/gofactorylint.go | 2 -- test/testdata/gofactory/blocked.go | 8 ++++---- .../gofactory/configs/go_factory_only_blocked.yml | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/pkg/golinters/gofactorylint.go b/pkg/golinters/gofactorylint.go index f9d412e308e7..ad53042da35d 100644 --- a/pkg/golinters/gofactorylint.go +++ b/pkg/golinters/gofactorylint.go @@ -21,8 +21,6 @@ func NewGoFactoryLint(settings *config.GoFactoryLintSettings) *goanalysis.Linter } } - // TODO add options to configuration - return goanalysis.NewLinter( analyzer.Name, analyzer.Doc, diff --git a/test/testdata/gofactory/blocked.go b/test/testdata/gofactory/blocked.go index 7f8394c2ef44..d7da0ef63954 100644 --- a/test/testdata/gofactory/blocked.go +++ b/test/testdata/gofactory/blocked.go @@ -11,14 +11,14 @@ var ( nestedGlobalStruct = nested.Struct{} nestedGlobalStructPtr = &nested.Struct{} - blockedGlobalStruct = blocked.Struct{} - blockedGlobalStructPtr = &blocked.Struct{} + blockedGlobalStruct = blocked.Struct{} // want `Use factory for blocked.Struct` + blockedGlobalStructPtr = &blocked.Struct{} // want `Use factory for blocked.Struct` ) func Blocked() { _ = nested.Struct{} _ = &nested.Struct{} - _ = blocked.Struct{} // want `Use factory for nested.Struct` - _ = &blocked.Struct{} // want `Use factory for nested.Struct` + _ = blocked.Struct{} // want `Use factory for blocked.Struct` + _ = &blocked.Struct{} // want `Use factory for blocked.Struct` } diff --git a/test/testdata/gofactory/configs/go_factory_only_blocked.yml b/test/testdata/gofactory/configs/go_factory_only_blocked.yml index f23c98de84f1..132d9db19665 100644 --- a/test/testdata/gofactory/configs/go_factory_only_blocked.yml +++ b/test/testdata/gofactory/configs/go_factory_only_blocked.yml @@ -1,5 +1,5 @@ linters-settings: gofactory: blockedPkgs: - - blocked + - gofactory/blocked onlyBlockedPkgs: true From 2fde9ceda0e00b01ec3d708da03a7f6451dad750 Mon Sep 17 00:00:00 2001 From: Ilia Sergunin Date: Sun, 19 Nov 2023 12:37:53 +0400 Subject: [PATCH 07/12] 1. Update config after linter fixing --- .golangci.reference.yml | 8 +++++--- go.mod | 2 +- go.sum | 6 ++---- pkg/config/linters_settings.go | 4 ++-- pkg/golinters/gofactorylint.go | 8 ++++---- .../gofactory/configs/go_factory_only_blocked.yml | 6 +++--- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/.golangci.reference.yml b/.golangci.reference.yml index df85e38d3bbc..bf5fb6efc96e 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -662,11 +662,13 @@ linters-settings: - HACK # marks hack-around that should be removed before merging gofactory: + # List of glob packages, which can create structures without factories inside the glob package. # Default: [] - blockedPkgs: - - github.com/author/repository/path/to/package + packageGlobs: + - github.com/author/repository/path/to/package/** + # Use a factory to initiate a structure for glob packages only. # Default: false - onlyBlockedPkgs: true + onlyPackageGlobs: true gofmt: # Simplify code: gofmt with `-s` option. diff --git a/go.mod b/go.mod index 0bde3ac606e6..bb75a11f0f76 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,7 @@ require ( github.com/leonklingele/grouper v1.1.1 github.com/lufeee/execinquery v1.2.1 github.com/macabu/inamedparam v0.1.2 - github.com/maranqz/go-factory-lint v1.0.4 + github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.2 github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.1 github.com/matoous/godox v0.0.0-20230222163458-006bad1f9d26 diff --git a/go.sum b/go.sum index 17726670b4e5..b1450c0c221b 100644 --- a/go.sum +++ b/go.sum @@ -362,10 +362,8 @@ github.com/macabu/inamedparam v0.1.2 h1:RR5cnayM6Q7cDhQol32DE2BGAPGMnffJ31LFE+Uk github.com/macabu/inamedparam v0.1.2/go.mod h1:Xg25QvY7IBRl1KLPV9Rbml8JOMZtF/iAkNkmV7eQgjw= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= -github.com/maranqz/go-factory-lint v1.0.3 h1:GdJ+/uHAgaZV8e87U/Wk0HmGd/WD5hAb6NSRDX4f3p8= -github.com/maranqz/go-factory-lint v1.0.3/go.mod h1:PhCUIl545+e923NZUAr1lu7MWBt/Hsk5MFQ3GXLfvwU= -github.com/maranqz/go-factory-lint v1.0.4 h1:Uo7EJNnqhrMNGTnTM2YpflDyjxJb4IfBVsh06Q3GLjk= -github.com/maranqz/go-factory-lint v1.0.4/go.mod h1:PhCUIl545+e923NZUAr1lu7MWBt/Hsk5MFQ3GXLfvwU= +github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.2 h1:or28a4wmTr7SKvI9Fko1No7IN8nU+XzyN2o9Do0bwvM= +github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.2/go.mod h1:VoGuji9zEVg9wyWWJDI/tZWbJQLkDEyJjfh1Es0UuoI= 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 cd4003b64c35..36a34dd950f9 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -480,8 +480,8 @@ type GodoxSettings struct { } type GoFactoryLintSettings struct { - BlockedPkgs []string `mapstructure:"blockedPkgs"` - OnlyBlockedPkgs bool `mapstructure:"onlyBlockedPkgs"` + PackageGlobs []string `mapstructure:"packageGlobs"` + OnlyPackageGlobs bool `mapstructure:"onlyPackageGlobs"` } type GoFmtSettings struct { diff --git a/pkg/golinters/gofactorylint.go b/pkg/golinters/gofactorylint.go index ad53042da35d..e57c9c91a373 100644 --- a/pkg/golinters/gofactorylint.go +++ b/pkg/golinters/gofactorylint.go @@ -1,7 +1,7 @@ package golinters import ( - "github.com/maranqz/go-factory-lint" + "github.com/maranqz/go-factory-lint/v2" "golang.org/x/tools/go/analysis" "github.com/golangci/golangci-lint/pkg/config" @@ -15,9 +15,9 @@ func NewGoFactoryLint(settings *config.GoFactoryLintSettings) *goanalysis.Linter if settings != nil { cfg[analyzer.Name] = map[string]any{} - if len(settings.BlockedPkgs) > 0 { - cfg[analyzer.Name]["blockedPkgs"] = settings.BlockedPkgs - cfg[analyzer.Name]["onlyBlockedPkgs"] = settings.OnlyBlockedPkgs + if len(settings.PackageGlobs) > 0 { + cfg[analyzer.Name]["packageGlobs"] = settings.PackageGlobs + cfg[analyzer.Name]["onlyPackageGlobs"] = settings.OnlyPackageGlobs } } diff --git a/test/testdata/gofactory/configs/go_factory_only_blocked.yml b/test/testdata/gofactory/configs/go_factory_only_blocked.yml index 132d9db19665..c669bea719f5 100644 --- a/test/testdata/gofactory/configs/go_factory_only_blocked.yml +++ b/test/testdata/gofactory/configs/go_factory_only_blocked.yml @@ -1,5 +1,5 @@ linters-settings: gofactory: - blockedPkgs: - - gofactory/blocked - onlyBlockedPkgs: true + packageGlobs: + - gofactory/blocked/** + onlyPackageGlobs: true From bb4065b512c146ec8879ec611a4b9bcba3c9847b Mon Sep 17 00:00:00 2001 From: Ilia Sergunin Date: Mon, 20 Nov 2023 20:09:21 +0400 Subject: [PATCH 08/12] 1. Remove slog to pass go1.20 test --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index bb75a11f0f76..43cf12f5aa26 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,7 @@ require ( github.com/leonklingele/grouper v1.1.1 github.com/lufeee/execinquery v1.2.1 github.com/macabu/inamedparam v0.1.2 - github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.2 + github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.4 github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.1 github.com/matoous/godox v0.0.0-20230222163458-006bad1f9d26 diff --git a/go.sum b/go.sum index b1450c0c221b..a9a90cbc989e 100644 --- a/go.sum +++ b/go.sum @@ -362,8 +362,8 @@ github.com/macabu/inamedparam v0.1.2 h1:RR5cnayM6Q7cDhQol32DE2BGAPGMnffJ31LFE+Uk github.com/macabu/inamedparam v0.1.2/go.mod h1:Xg25QvY7IBRl1KLPV9Rbml8JOMZtF/iAkNkmV7eQgjw= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= -github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.2 h1:or28a4wmTr7SKvI9Fko1No7IN8nU+XzyN2o9Do0bwvM= -github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.2/go.mod h1:VoGuji9zEVg9wyWWJDI/tZWbJQLkDEyJjfh1Es0UuoI= +github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.4 h1:C+IL0IVZyejGCnH+zG1hSGDAy14T/qa1x+YYeuCt4cQ= +github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.4/go.mod h1:VoGuji9zEVg9wyWWJDI/tZWbJQLkDEyJjfh1Es0UuoI= 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 930e2b2ed1e088e0b07dbeebfb4fbd02cfa442f7 Mon Sep 17 00:00:00 2001 From: Ilia Sergunin Date: Sun, 26 Nov 2023 12:48:49 +0300 Subject: [PATCH 09/12] 1. Added casting function 2. Removed nested directory in test --- go.mod | 2 +- go.sum | 4 +- test/testdata/gofactory/blocked.go | 42 ++++++++++++++----- test/testdata/gofactory/blocked/blocked.go | 3 -- .../configs/go_factory_only_blocked.yml | 2 +- test/testdata/gofactory/default.go | 22 +++++----- test/testdata/gofactory/nested/nested.go | 3 -- 7 files changed, 47 insertions(+), 31 deletions(-) delete mode 100644 test/testdata/gofactory/blocked/blocked.go delete mode 100644 test/testdata/gofactory/nested/nested.go diff --git a/go.mod b/go.mod index 00671d0cf58f..2d695bd0dabd 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,7 @@ require ( github.com/leonklingele/grouper v1.1.1 github.com/lufeee/execinquery v1.2.1 github.com/macabu/inamedparam v0.1.2 - github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.4 + github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.5 github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.1 github.com/matoous/godox v0.0.0-20230222163458-006bad1f9d26 diff --git a/go.sum b/go.sum index 58b7aeb3154e..a99472908d20 100644 --- a/go.sum +++ b/go.sum @@ -362,8 +362,8 @@ github.com/macabu/inamedparam v0.1.2 h1:RR5cnayM6Q7cDhQol32DE2BGAPGMnffJ31LFE+Uk github.com/macabu/inamedparam v0.1.2/go.mod h1:Xg25QvY7IBRl1KLPV9Rbml8JOMZtF/iAkNkmV7eQgjw= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= -github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.4 h1:C+IL0IVZyejGCnH+zG1hSGDAy14T/qa1x+YYeuCt4cQ= -github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.4/go.mod h1:VoGuji9zEVg9wyWWJDI/tZWbJQLkDEyJjfh1Es0UuoI= +github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.5 h1:dOftIl8RWVNqm/WlNdEszkQzcrXxjHBaRZzagETiBFs= +github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.5/go.mod h1:VoGuji9zEVg9wyWWJDI/tZWbJQLkDEyJjfh1Es0UuoI= 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/test/testdata/gofactory/blocked.go b/test/testdata/gofactory/blocked.go index d7da0ef63954..a27f146c1ccd 100644 --- a/test/testdata/gofactory/blocked.go +++ b/test/testdata/gofactory/blocked.go @@ -3,22 +3,44 @@ package gofactory import ( - "gofactory/blocked" - "gofactory/nested" + "net/http" + "net/url" ) var ( - nestedGlobalStruct = nested.Struct{} - nestedGlobalStructPtr = &nested.Struct{} + nestedGlobalRequest = http.Request{} + nestedGlobalRequestPtr = &http.Request{} - blockedGlobalStruct = blocked.Struct{} // want `Use factory for blocked.Struct` - blockedGlobalStructPtr = &blocked.Struct{} // want `Use factory for blocked.Struct` + blockedGlobalURL = url.URL{} // want `Use factory for url.URL` + blockedGlobalURLPtr = &url.URL{} // want `Use factory for url.URL` ) func Blocked() { - _ = nested.Struct{} - _ = &nested.Struct{} + _ = http.Request{} + _ = &http.Request{} - _ = blocked.Struct{} // want `Use factory for blocked.Struct` - _ = &blocked.Struct{} // want `Use factory for blocked.Struct` + _ = url.URL{} // want `Use factory for url.URL` + _ = &url.URL{} // want `Use factory for url.URL` +} + +type URL struct { + Scheme string + Opaque string + User *url.Userinfo + Host string + Path string + RawPath string + OmitHost bool + ForceQuery bool + RawQuery string + Fragment string + RawFragment string +} + +func Casting() { + _ = url.URL(URL{}) // want `Use factory for url.URL` + + uPtr, _ := url.Parse("") + u := *uPtr + _ = URL(u) } diff --git a/test/testdata/gofactory/blocked/blocked.go b/test/testdata/gofactory/blocked/blocked.go deleted file mode 100644 index 13bc576205b9..000000000000 --- a/test/testdata/gofactory/blocked/blocked.go +++ /dev/null @@ -1,3 +0,0 @@ -package blocked - -type Struct struct{} diff --git a/test/testdata/gofactory/configs/go_factory_only_blocked.yml b/test/testdata/gofactory/configs/go_factory_only_blocked.yml index c669bea719f5..572f4e43598f 100644 --- a/test/testdata/gofactory/configs/go_factory_only_blocked.yml +++ b/test/testdata/gofactory/configs/go_factory_only_blocked.yml @@ -1,5 +1,5 @@ linters-settings: gofactory: packageGlobs: - - gofactory/blocked/** + - net/url/** onlyPackageGlobs: true diff --git a/test/testdata/gofactory/default.go b/test/testdata/gofactory/default.go index 1d984fd8311d..8ddb553bd956 100644 --- a/test/testdata/gofactory/default.go +++ b/test/testdata/gofactory/default.go @@ -2,31 +2,31 @@ package gofactory import ( - alias_blocked "gofactory/blocked" - "gofactory/nested" + "net/http" + alias_blocked "net/http" ) type Struct struct{} var ( - defaultGlobalStruct = nested.Struct{} // want `Use factory for nested.Struct` - defaultGlobalStructPtr = &nested.Struct{} // want `Use factory for nested.Struct` + defaultGlobalRequest = http.Request{} // want `Use factory for http.Request` + defaultGlobalRequestPtr = &http.Request{} // want `Use factory for http.Request` ) func Default() { - _ = nested.Struct{} // want `Use factory for nested.Struct` - _ = &nested.Struct{} // want `Use factory for nested.Struct` + _ = http.Request{} // want `Use factory for http.Request` + _ = &http.Request{} // want `Use factory for http.Request` - _ = []nested.Struct{{}, nested.Struct{}} // want `Use factory for nested.Struct` - _ = []*nested.Struct{{}, &nested.Struct{}} // want `Use factory for nested.Struct` + _ = []http.Request{{}, http.Request{}} // want `Use factory for http.Request` + _ = []*http.Request{{}, &http.Request{}} // want `Use factory for http.Request` - call(nested.Struct{}) // want `Use factory for nested.Struct` + call(http.Request{}) // want `Use factory for http.Request` _ = []Struct{{}, {}} } -func call(_ nested.Struct) {} +func call(_ http.Request) {} func alias() { - _ = alias_blocked.Struct{} // want `Use factory for blocked.Struct` + _ = alias_blocked.Request{} // want `Use factory for http.Request` } diff --git a/test/testdata/gofactory/nested/nested.go b/test/testdata/gofactory/nested/nested.go deleted file mode 100644 index 3e620d5d9b67..000000000000 --- a/test/testdata/gofactory/nested/nested.go +++ /dev/null @@ -1,3 +0,0 @@ -package nested - -type Struct struct{} From c1195cf8dacfde098970695c8b4f900744fbbb6f Mon Sep 17 00:00:00 2001 From: Ilia Sergunin Date: Fri, 15 Dec 2023 07:57:49 +0400 Subject: [PATCH 10/12] 1. renamed linter 2. renamed options 3. changed linter version --- .golangci.reference.yml | 2 +- go.mod | 4 ++-- go.sum | 10 +++++----- pkg/config/linters_settings.go | 6 +++--- pkg/golinters/{gofactorylint.go => gofactory.go} | 8 ++++---- pkg/lint/lintersdb/manager.go | 6 +++--- test/testdata/gofactory/blocked.go | 2 +- ...y_blocked.yml => go_factory_package_globs_only.yml} | 2 +- 8 files changed, 20 insertions(+), 20 deletions(-) rename pkg/golinters/{gofactorylint.go => gofactory.go} (70%) rename test/testdata/gofactory/configs/{go_factory_only_blocked.yml => go_factory_package_globs_only.yml} (71%) diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 600872cac686..a31c0bc36fc2 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -672,7 +672,7 @@ linters-settings: - github.com/author/repository/path/to/package/** # Use a factory to initiate a structure for glob packages only. # Default: false - onlyPackageGlobs: true + packageGlobsOnly: true gofmt: # Simplify code: gofmt with `-s` option. diff --git a/go.mod b/go.mod index 2d695bd0dabd..a71dd206a236 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,7 @@ require ( github.com/leonklingele/grouper v1.1.1 github.com/lufeee/execinquery v1.2.1 github.com/macabu/inamedparam v0.1.2 - github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.5 + github.com/maranqz/gofactory v1.0.0-rc1 github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.1 github.com/matoous/godox v0.0.0-20230222163458-006bad1f9d26 @@ -124,7 +124,7 @@ require ( go-simpler.org/musttag v0.8.0 go-simpler.org/sloglint v0.3.0 golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea - golang.org/x/tools v0.15.0 + golang.org/x/tools v0.16.0 gopkg.in/yaml.v3 v3.0.1 honnef.co/go/tools v0.4.6 mvdan.cc/gofumpt v0.5.0 diff --git a/go.sum b/go.sum index a99472908d20..27c881e5e2a6 100644 --- a/go.sum +++ b/go.sum @@ -362,8 +362,8 @@ github.com/macabu/inamedparam v0.1.2 h1:RR5cnayM6Q7cDhQol32DE2BGAPGMnffJ31LFE+Uk github.com/macabu/inamedparam v0.1.2/go.mod h1:Xg25QvY7IBRl1KLPV9Rbml8JOMZtF/iAkNkmV7eQgjw= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= -github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.5 h1:dOftIl8RWVNqm/WlNdEszkQzcrXxjHBaRZzagETiBFs= -github.com/maranqz/go-factory-lint/v2 v2.0.0-beta.5/go.mod h1:VoGuji9zEVg9wyWWJDI/tZWbJQLkDEyJjfh1Es0UuoI= +github.com/maranqz/gofactory v1.0.0-rc1 h1:nZGZtEDGrL7gD9PKHiB1Ng1rhZlgg5/Hb3OfIgpMu84= +github.com/maranqz/gofactory v1.0.0-rc1/go.mod h1:+MwH9KkNAd2Q9R88ECuoGnYCCGmgGebKiJz2Gwjcxu8= 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= @@ -710,7 +710,7 @@ golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= +golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -893,8 +893,8 @@ golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA= golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k= golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8= -golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk= +golang.org/x/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM= +golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 951303a7e368..93467e1bca3f 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -199,7 +199,7 @@ type LintersSettings struct { Gocyclo GoCycloSettings Godot GodotSettings Godox GodoxSettings - Gofactory GoFactoryLintSettings + Gofactory GoFactorySettings Gofmt GoFmtSettings Gofumpt GofumptSettings Goheader GoHeaderSettings @@ -480,9 +480,9 @@ type GodoxSettings struct { Keywords []string } -type GoFactoryLintSettings struct { +type GoFactorySettings struct { PackageGlobs []string `mapstructure:"packageGlobs"` - OnlyPackageGlobs bool `mapstructure:"onlyPackageGlobs"` + PackageGlobsOnly bool `mapstructure:"packageGlobsOnly"` } type GoFmtSettings struct { diff --git a/pkg/golinters/gofactorylint.go b/pkg/golinters/gofactory.go similarity index 70% rename from pkg/golinters/gofactorylint.go rename to pkg/golinters/gofactory.go index e57c9c91a373..9075734a99f8 100644 --- a/pkg/golinters/gofactorylint.go +++ b/pkg/golinters/gofactory.go @@ -1,15 +1,15 @@ package golinters import ( - "github.com/maranqz/go-factory-lint/v2" + "github.com/maranqz/gofactory" "golang.org/x/tools/go/analysis" "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" ) -func NewGoFactoryLint(settings *config.GoFactoryLintSettings) *goanalysis.Linter { - analyzer := factory.NewAnalyzer() +func NewGoFactory(settings *config.GoFactorySettings) *goanalysis.Linter { + analyzer := gofactory.NewAnalyzer() cfg := make(map[string]map[string]any) if settings != nil { @@ -17,7 +17,7 @@ func NewGoFactoryLint(settings *config.GoFactoryLintSettings) *goanalysis.Linter if len(settings.PackageGlobs) > 0 { cfg[analyzer.Name]["packageGlobs"] = settings.PackageGlobs - cfg[analyzer.Name]["onlyPackageGlobs"] = settings.OnlyPackageGlobs + cfg[analyzer.Name]["packageGlobsOnly"] = settings.PackageGlobsOnly } } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 38f4d598a652..54cf08007060 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -90,7 +90,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { gocycloCfg *config.GoCycloSettings godotCfg *config.GodotSettings godoxCfg *config.GodoxSettings - goFactoryCfg *config.GoFactoryLintSettings + goFactoryCfg *config.GoFactorySettings gofmtCfg *config.GoFmtSettings gofumptCfg *config.GofumptSettings goheaderCfg *config.GoHeaderSettings @@ -490,11 +490,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithLoadForGoAnalysis(). WithURL("https://github.com/Djarvur/go-err113"), - linter.NewConfig(golinters.NewGoFactoryLint(goFactoryCfg)). + linter.NewConfig(golinters.NewGoFactory(goFactoryCfg)). WithSince("1.56.0"). WithPresets(linter.PresetStyle). WithLoadForGoAnalysis(). - WithURL("https://github.com/maranqz/go-factory-lint"), + WithURL("https://github.com/maranqz/gofactory"), linter.NewConfig(golinters.NewGofmt(gofmtCfg)). WithSince("v1.0.0"). diff --git a/test/testdata/gofactory/blocked.go b/test/testdata/gofactory/blocked.go index a27f146c1ccd..96f252899911 100644 --- a/test/testdata/gofactory/blocked.go +++ b/test/testdata/gofactory/blocked.go @@ -1,5 +1,5 @@ //golangcitest:args -Egofactory -//golangcitest:config_path configs/go_factory_only_blocked.yml +//golangcitest:config_path configs/go_factory_package_globs_only.yml package gofactory import ( diff --git a/test/testdata/gofactory/configs/go_factory_only_blocked.yml b/test/testdata/gofactory/configs/go_factory_package_globs_only.yml similarity index 71% rename from test/testdata/gofactory/configs/go_factory_only_blocked.yml rename to test/testdata/gofactory/configs/go_factory_package_globs_only.yml index 572f4e43598f..a7c965484e6f 100644 --- a/test/testdata/gofactory/configs/go_factory_only_blocked.yml +++ b/test/testdata/gofactory/configs/go_factory_package_globs_only.yml @@ -2,4 +2,4 @@ linters-settings: gofactory: packageGlobs: - net/url/** - onlyPackageGlobs: true + packageGlobsOnly: true From 2b59b9ddf35e16644927a662ff522a95a8332568 Mon Sep 17 00:00:00 2001 From: Ilia Sergunin Date: Fri, 15 Dec 2023 21:49:53 +0400 Subject: [PATCH 11/12] 1. fixed tests --- test/run_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/run_test.go b/test/run_test.go index 2ec4b64280a0..8141b2df8f9a 100644 --- a/test/run_test.go +++ b/test/run_test.go @@ -137,7 +137,7 @@ func TestCgoOk(t *testing.T) { "--timeout=3m", "--enable-all", "-D", - "nosnakecase,gci", + "nosnakecase,gci,gofactory", ). WithTargetPath(testdataDir, "cgo"). Runner(). @@ -355,7 +355,11 @@ func TestLineDirectiveProcessedFiles(t *testing.T) { func TestUnsafeOk(t *testing.T) { testshared.NewRunnerBuilder(t). WithNoConfig(). - WithArgs("--enable-all"). + WithArgs( + "--enable-all", + "-D", + "gofactory", + ). WithTargetPath(testdataDir, "unsafe"). Runner(). Install(). From eb43dd5d7c5ae609930cf5b9f01c98f02d33d6a0 Mon Sep 17 00:00:00 2001 From: Ilia Sergunin Date: Sun, 17 Dec 2023 13:40:25 +0400 Subject: [PATCH 12/12] 1. moved gofactory test from dedicated to common directory --- test/linters_test.go | 1 - test/run_test.go | 5 ----- .../configs/go_factory_package_globs_only.yml | 0 .../{gofactory/default.go => gofactory.go} | 2 +- test/testdata/gofactory/go.mod | 4 ---- ...cked.go => gofactory_package_globs_only.go} | 18 +++++++++--------- 6 files changed, 10 insertions(+), 20 deletions(-) rename test/testdata/{gofactory => }/configs/go_factory_package_globs_only.yml (100%) rename test/testdata/{gofactory/default.go => gofactory.go} (97%) delete mode 100644 test/testdata/gofactory/go.mod rename test/testdata/{gofactory/blocked.go => gofactory_package_globs_only.go} (57%) diff --git a/test/linters_test.go b/test/linters_test.go index ee45d543b793..75d4f44adf1d 100644 --- a/test/linters_test.go +++ b/test/linters_test.go @@ -33,7 +33,6 @@ func TestSourcesFromTestdataSubDir(t *testing.T) { "ginkgolinter", "zerologlint", "protogetter", - "gofactory", } for _, dir := range subDirs { diff --git a/test/run_test.go b/test/run_test.go index 8141b2df8f9a..7cb9c60454f4 100644 --- a/test/run_test.go +++ b/test/run_test.go @@ -355,11 +355,6 @@ func TestLineDirectiveProcessedFiles(t *testing.T) { func TestUnsafeOk(t *testing.T) { testshared.NewRunnerBuilder(t). WithNoConfig(). - WithArgs( - "--enable-all", - "-D", - "gofactory", - ). WithTargetPath(testdataDir, "unsafe"). Runner(). Install(). diff --git a/test/testdata/gofactory/configs/go_factory_package_globs_only.yml b/test/testdata/configs/go_factory_package_globs_only.yml similarity index 100% rename from test/testdata/gofactory/configs/go_factory_package_globs_only.yml rename to test/testdata/configs/go_factory_package_globs_only.yml diff --git a/test/testdata/gofactory/default.go b/test/testdata/gofactory.go similarity index 97% rename from test/testdata/gofactory/default.go rename to test/testdata/gofactory.go index 8ddb553bd956..7b79703edb50 100644 --- a/test/testdata/gofactory/default.go +++ b/test/testdata/gofactory.go @@ -1,5 +1,5 @@ //golangcitest:args -Egofactory -package gofactory +package testdata import ( "net/http" diff --git a/test/testdata/gofactory/go.mod b/test/testdata/gofactory/go.mod deleted file mode 100644 index 9f6be767e6ba..000000000000 --- a/test/testdata/gofactory/go.mod +++ /dev/null @@ -1,4 +0,0 @@ -module gofactory - -go 1.18 - diff --git a/test/testdata/gofactory/blocked.go b/test/testdata/gofactory_package_globs_only.go similarity index 57% rename from test/testdata/gofactory/blocked.go rename to test/testdata/gofactory_package_globs_only.go index 96f252899911..ebddddbee499 100644 --- a/test/testdata/gofactory/blocked.go +++ b/test/testdata/gofactory_package_globs_only.go @@ -1,32 +1,32 @@ //golangcitest:args -Egofactory //golangcitest:config_path configs/go_factory_package_globs_only.yml -package gofactory +package testdata import ( "net/http" - "net/url" + neturl "net/url" ) var ( nestedGlobalRequest = http.Request{} nestedGlobalRequestPtr = &http.Request{} - blockedGlobalURL = url.URL{} // want `Use factory for url.URL` - blockedGlobalURLPtr = &url.URL{} // want `Use factory for url.URL` + blockedGlobalURL = neturl.URL{} // want `Use factory for url.URL` + blockedGlobalURLPtr = &neturl.URL{} // want `Use factory for url.URL` ) func Blocked() { _ = http.Request{} _ = &http.Request{} - _ = url.URL{} // want `Use factory for url.URL` - _ = &url.URL{} // want `Use factory for url.URL` + _ = neturl.URL{} // want `Use factory for url.URL` + _ = &neturl.URL{} // want `Use factory for url.URL` } type URL struct { Scheme string Opaque string - User *url.Userinfo + User *neturl.Userinfo Host string Path string RawPath string @@ -38,9 +38,9 @@ type URL struct { } func Casting() { - _ = url.URL(URL{}) // want `Use factory for url.URL` + _ = neturl.URL(URL{}) // want `Use factory for url.URL` - uPtr, _ := url.Parse("") + uPtr, _ := neturl.Parse("") u := *uPtr _ = URL(u) }