From e0e4bc01c28b5302671eacc12e926e4adf71dd4b Mon Sep 17 00:00:00 2001 From: Vladislav Fursov Date: Fri, 27 Oct 2023 16:21:51 +0400 Subject: [PATCH] protogetter: added the ability to configure the linter --- .golangci.reference.yml | 16 ++++++++++++++++ go.mod | 2 +- go.sum | 2 ++ pkg/config/linters_settings.go | 7 +++++++ pkg/golinters/protogetter.go | 20 +++++++++++++++++--- pkg/lint/lintersdb/manager.go | 4 +++- 6 files changed, 46 insertions(+), 5 deletions(-) diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 8e8823758879..1835a1d9b2f7 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1417,6 +1417,22 @@ linters-settings: - CamelCase - UnitAbbreviations + protogetter: + # Skip files generated by specified generators from the checking. + # Checks only the file's initial comment, which must follow the format: "// Code generated by ". + # Files generated by protoc-gen-go, protoc-gen-go-grpc, and protoc-gen-grpc-gateway are always excluded automatically. + # Default: [] + skip-generated-by: ["protoc-gen-go-my-own-generator"] + # Skip files matching the specified glob pattern from the checking. + # Default: [] + skip-files: + - "*.pb.go" + - "*/vendor/*" + - "/full/path/to/file.go" + # Skip any generated files from the checking. + # Default: false + skip-any-generated: true + reassign: # Patterns for global variable names that are checked for reassignment. # See https://github.com/curioswitch/go-reassign#usage diff --git a/go.mod b/go.mod index 6414f87ec2c3..01fc2290a1f5 100644 --- a/go.mod +++ b/go.mod @@ -36,7 +36,7 @@ require ( github.com/fatih/color v1.15.0 github.com/firefart/nonamedreturns v1.0.4 github.com/fzipp/gocyclo v0.6.0 - github.com/ghostiam/protogetter v0.2.3 + github.com/ghostiam/protogetter v0.3.1 github.com/go-critic/go-critic v0.9.0 github.com/go-xmlfmt/xmlfmt v1.1.2 github.com/gofrs/flock v0.8.1 diff --git a/go.sum b/go.sum index b77b953f97b0..f2ba18bb1836 100644 --- a/go.sum +++ b/go.sum @@ -154,6 +154,8 @@ github.com/fzipp/gocyclo v0.6.0 h1:lsblElZG7d3ALtGMx9fmxeTKZaLLpU8mET09yN4BBLo= github.com/fzipp/gocyclo v0.6.0/go.mod h1:rXPyn8fnlpa0R2csP/31uerbiVBugk5whMdlyaLkLoA= github.com/ghostiam/protogetter v0.2.3 h1:qdv2pzo3BpLqezwqfGDLZ+nHEYmc5bUpIdsMbBVwMjw= github.com/ghostiam/protogetter v0.2.3/go.mod h1:KmNLOsy1v04hKbvZs8EfGI1fk39AgTdRDxWNYPfXVc4= +github.com/ghostiam/protogetter v0.3.1 h1:4BQ2YZu9v3C+0QUzVmzjh9iVd/b2SgpLBjv9hvyIAdM= +github.com/ghostiam/protogetter v0.3.1/go.mod h1:A0JgIhs0fgVnotGinjQiKaFVG3waItLJNwPmcMzDnvk= github.com/go-critic/go-critic v0.9.0 h1:Pmys9qvU3pSML/3GEQ2Xd9RZ/ip+aXHKILuxczKGV/U= github.com/go-critic/go-critic v0.9.0/go.mod h1:5P8tdXL7m/6qnyG6oRAlYLORvoXH0WDypYgAEmagT40= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 0fee9f81ef53..81dad3ce1a46 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -225,6 +225,7 @@ type LintersSettings struct { Prealloc PreallocSettings Predeclared PredeclaredSettings Promlinter PromlinterSettings + ProtoGetter ProtoGetterSettings Reassign ReassignSettings Revive ReviveSettings RowsErrCheck RowsErrCheckSettings @@ -696,6 +697,12 @@ type PromlinterSettings struct { DisabledLinters []string `mapstructure:"disabled-linters"` } +type ProtoGetterSettings struct { + SkipGeneratedBy []string `mapstructure:"skip-generated-by"` + SkipFiles []string `mapstructure:"skip-files"` + SkipAnyGenerated bool `mapstructure:"skip-any-generated"` +} + type ReassignSettings struct { Patterns []string `mapstructure:"patterns"` } diff --git a/pkg/golinters/protogetter.go b/pkg/golinters/protogetter.go index 23325ad55e73..fbd760f1e9f8 100644 --- a/pkg/golinters/protogetter.go +++ b/pkg/golinters/protogetter.go @@ -6,18 +6,32 @@ import ( "github.com/ghostiam/protogetter" "golang.org/x/tools/go/analysis" + "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" "github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/result" ) -func NewProtoGetter() *goanalysis.Linter { +func NewProtoGetter(settings *config.ProtoGetterSettings) *goanalysis.Linter { var mu sync.Mutex var resIssues []goanalysis.Issue - a := protogetter.NewAnalyzer() + var cfg protogetter.Config + if settings != nil { + cfg = protogetter.Config{ + SkipGeneratedBy: settings.SkipGeneratedBy, + SkipFiles: settings.SkipFiles, + SkipAnyGenerated: settings.SkipAnyGenerated, + } + } + cfg.Mode = protogetter.GolangciLintMode + + a := protogetter.NewAnalyzer(&cfg) a.Run = func(pass *analysis.Pass) (any, error) { - pgIssues := protogetter.Run(pass, protogetter.GolangciLintMode) + pgIssues, err := protogetter.Run(pass, &cfg) + if err != nil { + return nil, err + } issues := make([]goanalysis.Issue, len(pgIssues)) for i, issue := range pgIssues { diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index fd329ce57fcf..447aa5e7150c 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -124,6 +124,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { preallocCfg *config.PreallocSettings predeclaredCfg *config.PredeclaredSettings promlinterCfg *config.PromlinterSettings + protogetterCfg *config.ProtoGetterSettings reassignCfg *config.ReassignSettings reviveCfg *config.ReviveSettings rowserrcheckCfg *config.RowsErrCheckSettings @@ -206,6 +207,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { parallelTestCfg = &m.cfg.LintersSettings.ParallelTest predeclaredCfg = &m.cfg.LintersSettings.Predeclared promlinterCfg = &m.cfg.LintersSettings.Promlinter + protogetterCfg = &m.cfg.LintersSettings.ProtoGetter reassignCfg = &m.cfg.LintersSettings.Reassign reviveCfg = &m.cfg.LintersSettings.Revive rowserrcheckCfg = &m.cfg.LintersSettings.RowsErrCheck @@ -733,7 +735,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetStyle). WithURL("https://github.com/yeya24/promlinter"), - linter.NewConfig(golinters.NewProtoGetter()). + linter.NewConfig(golinters.NewProtoGetter(protogetterCfg)). WithSince("v1.55.0"). WithPresets(linter.PresetBugs). WithLoadForGoAnalysis().