Skip to content

Commit bbe0eb9

Browse files
committed
1. Added reference
2. Added tests 3. Added config 4. Added linter
1 parent 0baed20 commit bbe0eb9

File tree

9 files changed

+86
-0
lines changed

9 files changed

+86
-0
lines changed

.golangci.reference.yml

+9
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,13 @@ linters-settings:
661661
- OPTIMIZE # marks code that should be optimized before merging
662662
- HACK # marks hack-around that should be removed before merging
663663

664+
gofactory:
665+
# Default: []
666+
blockedPkgs:
667+
- github.com/author/repository/path/to/package
668+
# Default: false
669+
onlyBlockedPkgs: true
670+
664671
gofmt:
665672
# Simplify code: gofmt with `-s` option.
666673
# Default: true
@@ -2351,6 +2358,7 @@ linters:
23512358
- godot
23522359
- godox
23532360
- goerr113
2361+
- gofactory
23542362
- gofmt
23552363
- gofumpt
23562364
- goheader
@@ -2471,6 +2479,7 @@ linters:
24712479
- godot
24722480
- godox
24732481
- goerr113
2482+
- gofactory
24742483
- gofmt
24752484
- gofumpt
24762485
- goheader

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ require (
6969
github.com/leonklingele/grouper v1.1.1
7070
github.com/lufeee/execinquery v1.2.1
7171
github.com/macabu/inamedparam v0.1.2
72+
github.com/maranqz/go-factory-lint v1.0.3
7273
github.com/maratori/testableexamples v1.0.0
7374
github.com/maratori/testpackage v1.1.1
7475
github.com/matoous/godox v0.0.0-20230222163458-006bad1f9d26

go.sum

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

pkg/config/linters_settings.go

+6
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ type LintersSettings struct {
199199
Gocyclo GoCycloSettings
200200
Godot GodotSettings
201201
Godox GodoxSettings
202+
Gofactory GoFactoryLintSettings
202203
Gofmt GoFmtSettings
203204
Gofumpt GofumptSettings
204205
Goheader GoHeaderSettings
@@ -478,6 +479,11 @@ type GodoxSettings struct {
478479
Keywords []string
479480
}
480481

482+
type GoFactoryLintSettings struct {
483+
BlockedPkgs map[string]map[string]string `mapstructure:"BlockedPkgs"`
484+
OnlyBlockedPkgs string `mapstructure:"OnlyBlockedPkgs"`
485+
}
486+
481487
type GoFmtSettings struct {
482488
Simplify bool
483489
RewriteRules []GoFmtRewriteRule `mapstructure:"rewrite-rules"`

pkg/golinters/gofactorylint.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package golinters
2+
3+
import (
4+
"github.com/maranqz/go-factory-lint"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/config"
8+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
9+
)
10+
11+
func NewGoFactoryLint(settings *config.GoFactoryLintSettings) *goanalysis.Linter {
12+
a := factory.NewAnalyzer()
13+
14+
cfg := make(map[string]map[string]any)
15+
if settings != nil {
16+
cfg[a.Name] = map[string]any{}
17+
18+
if len(settings.BlockedPkgs) > 0 {
19+
cfg[a.Name]["blockedPkgs"] = settings.BlockedPkgs
20+
cfg[a.Name]["onlyBlockedPkgs"] = settings.OnlyBlockedPkgs
21+
}
22+
}
23+
24+
return goanalysis.NewLinter(
25+
a.Name,
26+
a.Doc,
27+
[]*analysis.Analyzer{a},
28+
cfg,
29+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
30+
}

pkg/lint/lintersdb/manager.go

+7
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
9090
gocycloCfg *config.GoCycloSettings
9191
godotCfg *config.GodotSettings
9292
godoxCfg *config.GodoxSettings
93+
goFactoryCfg *config.GoFactoryLintSettings
9394
gofmtCfg *config.GoFmtSettings
9495
gofumptCfg *config.GofumptSettings
9596
goheaderCfg *config.GoHeaderSettings
@@ -174,6 +175,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
174175
gocycloCfg = &m.cfg.LintersSettings.Gocyclo
175176
godotCfg = &m.cfg.LintersSettings.Godot
176177
godoxCfg = &m.cfg.LintersSettings.Godox
178+
goFactoryCfg = &m.cfg.LintersSettings.Gofactory
177179
gofmtCfg = &m.cfg.LintersSettings.Gofmt
178180
gofumptCfg = &m.cfg.LintersSettings.Gofumpt
179181
goheaderCfg = &m.cfg.LintersSettings.Goheader
@@ -488,6 +490,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
488490
WithLoadForGoAnalysis().
489491
WithURL("https://github.com/Djarvur/go-err113"),
490492

493+
linter.NewConfig(golinters.NewGoFactoryLint(goFactoryCfg)).
494+
WithSince("next_version").
495+
WithPresets(linter.PresetStyle).
496+
WithURL("https://github.com/maranqz/go-factory-lint"),
497+
491498
linter.NewConfig(golinters.NewGofmt(gofmtCfg)).
492499
WithSince("v1.0.0").
493500
WithPresets(linter.PresetFormatting).

test/testdata/configs/gofactory.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
linters-settings:
2+
gofactory:
3+
blockedPkgs: []
4+
onlyBlockedPkgs: false

test/testdata/gofactory/app.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package gofactory
2+
3+
import "github.com/golangci/golangci-lint/test/testdata/gofactory/nested"
4+
5+
type Struct struct{}
6+
7+
var (
8+
globalStruct = nested.Struct{} // want `Use factory for nested.Struct`
9+
globalStructPtr = &nested.Struct{} // want `Use factory for nested.Struct`
10+
)
11+
12+
func fn() {
13+
_ = nested.Struct{} // want `Use factory for nested.Struct`
14+
_ = &nested.Struct{} // want `Use factory for nested.Struct`
15+
16+
_ = []nested.Struct{{}, nested.Struct{}} // want `Use factory for nested.Struct`
17+
_ = []*nested.Struct{{}, &nested.Struct{}} // want `Use factory for nested.Struct`
18+
19+
call(nested.Struct{}) // want `Use factory for nested.Struct`
20+
21+
_ = []Struct{{}, {}}
22+
}
23+
24+
func call(_ nested.Struct) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package nested
2+
3+
type Struct struct{}

0 commit comments

Comments
 (0)