Skip to content

Commit 7799b5c

Browse files
committed
1. Added reference
2. Added tests 3. Added config 4. Added linter
1 parent 467d563 commit 7799b5c

File tree

9 files changed

+93
-7
lines changed

9 files changed

+93
-7
lines changed

.golangci.reference.yml

+9
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,13 @@ linters-settings:
655655
- OPTIMIZE # marks code that should be optimized before merging
656656
- HACK # marks hack-around that should be removed before merging
657657

658+
gofactory:
659+
# Default: []
660+
blockedPkgs:
661+
- github.com/author/repository/path/to/package
662+
# Default: false
663+
onlyBlockedPkgs: true
664+
658665
gofmt:
659666
# Simplify code: gofmt with `-s` option.
660667
# Default: true
@@ -2336,6 +2343,7 @@ linters:
23362343
- godot
23372344
- godox
23382345
- goerr113
2346+
- gofactory
23392347
- gofmt
23402348
- gofumpt
23412349
- goheader
@@ -2456,6 +2464,7 @@ linters:
24562464
- godot
24572465
- godox
24582466
- goerr113
2467+
- gofactory
24592468
- gofmt
24602469
- gofumpt
24612470
- goheader

go.mod

+3-2
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
@@ -123,7 +124,7 @@ require (
123124
go-simpler.org/sloglint v0.3.0
124125
go.tmz.dev/musttag v0.7.2
125126
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea
126-
golang.org/x/tools v0.14.0
127+
golang.org/x/tools v0.15.0
127128
gopkg.in/yaml.v3 v3.0.1
128129
honnef.co/go/tools v0.4.6
129130
mvdan.cc/gofumpt v0.5.0
@@ -192,7 +193,7 @@ require (
192193
go.uber.org/zap v1.24.0 // indirect
193194
golang.org/x/exp/typeparams v0.0.0-20230307190834-24139beb5833 // indirect
194195
golang.org/x/mod v0.14.0 // indirect
195-
golang.org/x/sync v0.4.0 // indirect
196+
golang.org/x/sync v0.5.0 // indirect
196197
golang.org/x/sys v0.14.0 // indirect
197198
golang.org/x/text v0.13.0 // indirect
198199
google.golang.org/protobuf v1.28.0 // indirect

go.sum

+7-5
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
@@ -196,6 +196,7 @@ type LintersSettings struct {
196196
Gocyclo GoCycloSettings
197197
Godot GodotSettings
198198
Godox GodoxSettings
199+
Gofactory GoFactoryLintSettings
199200
Gofmt GoFmtSettings
200201
Gofumpt GofumptSettings
201202
Goheader GoHeaderSettings
@@ -473,6 +474,11 @@ type GodoxSettings struct {
473474
Keywords []string
474475
}
475476

477+
type GoFactoryLintSettings struct {
478+
BlockedPkgs map[string]map[string]string `mapstructure:"BlockedPkgs"`
479+
OnlyBlockedPkgs string `mapstructure:"OnlyBlockedPkgs"`
480+
}
481+
476482
type GoFmtSettings struct {
477483
Simplify bool
478484
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)