Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 14c0aba

Browse files
committed
1. Added reference
2. Added tests 3. Added config 4. Added linter
1 parent b314655 commit 14c0aba

File tree

9 files changed

+86
-0
lines changed

9 files changed

+86
-0
lines changed

‎.golangci.reference.yml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,13 @@ linters-settings:
658658
- OPTIMIZE # marks code that should be optimized before merging
659659
- HACK # marks hack-around that should be removed before merging
660660

661+
gofactory:
662+
# Default: []
663+
blockedPkgs:
664+
- github.com/author/repository/path/to/package
665+
# Default: false
666+
onlyBlockedPkgs: true
667+
661668
gofmt:
662669
# Simplify code: gofmt with `-s` option.
663670
# Default: true
@@ -2339,6 +2346,7 @@ linters:
23392346
- godot
23402347
- godox
23412348
- goerr113
2349+
- gofactory
23422350
- gofmt
23432351
- gofumpt
23442352
- goheader
@@ -2459,6 +2467,7 @@ linters:
24592467
- godot
24602468
- godox
24612469
- goerr113
2470+
- gofactory
24622471
- gofmt
24632472
- gofumpt
24642473
- goheader

‎go.mod‎

Lines changed: 1 addition & 0 deletions
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‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/config/linters_settings.go‎

Lines changed: 6 additions & 0 deletions
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
@@ -474,6 +475,11 @@ type GodoxSettings struct {
474475
Keywords []string
475476
}
476477

478+
type GoFactoryLintSettings struct {
479+
BlockedPkgs map[string]map[string]string `mapstructure:"BlockedPkgs"`
480+
OnlyBlockedPkgs string `mapstructure:"OnlyBlockedPkgs"`
481+
}
482+
477483
type GoFmtSettings struct {
478484
Simplify bool
479485
RewriteRules []GoFmtRewriteRule `mapstructure:"rewrite-rules"`

‎pkg/golinters/gofactorylint.go‎

Lines changed: 30 additions & 0 deletions
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‎

Lines changed: 7 additions & 0 deletions
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‎

Lines changed: 4 additions & 0 deletions
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‎

Lines changed: 24 additions & 0 deletions
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) {}
Lines changed: 3 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /