Skip to content

Commit

Permalink
1. Added reference
Browse files Browse the repository at this point in the history
2. Added tests
3. Added config
4. Added linter
  • Loading branch information
maranqz committed Nov 12, 2023
1 parent 467d563 commit 7799b5c
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 7 deletions.
9 changes: 9 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,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
Expand Down Expand Up @@ -2336,6 +2343,7 @@ linters:
- godot
- godox
- goerr113
- gofactory
- gofmt
- gofumpt
- goheader
Expand Down Expand Up @@ -2456,6 +2464,7 @@ linters:
- godot
- godox
- goerr113
- gofactory
- gofmt
- gofumpt
- goheader
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -123,7 +124,7 @@ require (
go-simpler.org/sloglint v0.3.0
go.tmz.dev/musttag v0.7.2
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea
golang.org/x/tools v0.14.0
golang.org/x/tools v0.15.0
gopkg.in/yaml.v3 v3.0.1
honnef.co/go/tools v0.4.6
mvdan.cc/gofumpt v0.5.0
Expand Down Expand Up @@ -192,7 +193,7 @@ require (
go.uber.org/zap v1.24.0 // indirect
golang.org/x/exp/typeparams v0.0.0-20230307190834-24139beb5833 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
Expand Down
12 changes: 7 additions & 5 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ type LintersSettings struct {
Gocyclo GoCycloSettings
Godot GodotSettings
Godox GodoxSettings
Gofactory GoFactoryLintSettings
Gofmt GoFmtSettings
Gofumpt GofumptSettings
Goheader GoHeaderSettings
Expand Down Expand Up @@ -473,6 +474,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"`
Expand Down
30 changes: 30 additions & 0 deletions pkg/golinters/gofactorylint.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down
4 changes: 4 additions & 0 deletions test/testdata/configs/gofactory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters-settings:
gofactory:
blockedPkgs: []
onlyBlockedPkgs: false
24 changes: 24 additions & 0 deletions test/testdata/gofactory/app.go
Original file line number Diff line number Diff line change
@@ -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) {}
3 changes: 3 additions & 0 deletions test/testdata/gofactory/nested/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package nested

type Struct struct{}

0 comments on commit 7799b5c

Please sign in to comment.