Skip to content

Commit 1fb0b2a

Browse files
committed
feat: add new plugin builder (db)
1 parent 30feef0 commit 1fb0b2a

File tree

3 files changed

+104
-10
lines changed

3 files changed

+104
-10
lines changed

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ require (
4444
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
4545
github.com/golangci/gofmt v0.0.0-20231018234816-f50ced29576e
4646
github.com/golangci/misspell v0.4.1
47+
github.com/golangci/plugin-module-register v0.0.0-20240305222101-f76272ec86ee
4748
github.com/golangci/revgrep v0.5.2
4849
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
4950
github.com/gordonklaus/ineffassign v0.1.0
@@ -119,7 +120,7 @@ require (
119120
go-simpler.org/sloglint v0.4.0
120121
go.uber.org/automaxprocs v1.5.3
121122
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
122-
golang.org/x/tools v0.18.0
123+
golang.org/x/tools v0.19.0
123124
gopkg.in/yaml.v3 v3.0.1
124125
honnef.co/go/tools v0.4.7
125126
mvdan.cc/gofumpt v0.6.0
@@ -186,9 +187,9 @@ require (
186187
go.uber.org/multierr v1.6.0 // indirect
187188
go.uber.org/zap v1.24.0 // indirect
188189
golang.org/x/exp/typeparams v0.0.0-20240213143201-ec583247a57a // indirect
189-
golang.org/x/mod v0.15.0 // indirect
190+
golang.org/x/mod v0.16.0 // indirect
190191
golang.org/x/sync v0.6.0 // indirect
191-
golang.org/x/sys v0.17.0 // indirect
192+
golang.org/x/sys v0.18.0 // indirect
192193
golang.org/x/text v0.14.0 // indirect
193194
google.golang.org/protobuf v1.31.0 // indirect
194195
gopkg.in/ini.v1 v1.67.0 // indirect

go.sum

Lines changed: 10 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package lintersdb
2+
3+
import (
4+
"strings"
5+
6+
"github.com/golangci/plugin-module-register/register"
7+
8+
"github.com/golangci/golangci-lint/pkg/config"
9+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
10+
"github.com/golangci/golangci-lint/pkg/lint/linter"
11+
"github.com/golangci/golangci-lint/pkg/logutils"
12+
)
13+
14+
const modulePluginType = "module"
15+
16+
// PluginModuleBuilder builds the custom linters (module plugin) based on the configuration.
17+
type PluginModuleBuilder struct {
18+
log logutils.Log
19+
}
20+
21+
// NewPluginModuleBuilder creates new PluginModuleBuilder.
22+
func NewPluginModuleBuilder(log logutils.Log) *PluginModuleBuilder {
23+
return &PluginModuleBuilder{log: log}
24+
}
25+
26+
// Build loads custom linters that are specified in the golangci-lint config file.
27+
func (b *PluginModuleBuilder) Build(cfg *config.Config) []*linter.Config {
28+
if cfg == nil || b.log == nil {
29+
return nil
30+
}
31+
32+
var linters []*linter.Config
33+
34+
for name, settings := range cfg.LintersSettings.Custom {
35+
if settings.Type != modulePluginType {
36+
continue
37+
}
38+
39+
b.log.Infof("Loaded %s: %s", settings.Path, name)
40+
41+
newPlugin, err := register.GetPlugin(name)
42+
if err != nil {
43+
// FIXME error
44+
b.log.Fatalf("plugin(%s): %v", name, err)
45+
return nil
46+
}
47+
48+
p, err := newPlugin(settings.Settings)
49+
if err != nil {
50+
// FIXME error
51+
b.log.Fatalf("plugin(%s): newPlugin %v", name, err)
52+
return nil
53+
}
54+
55+
analyzers, err := p.BuildAnalyzers()
56+
if err != nil {
57+
// FIXME error
58+
b.log.Fatalf("plugin(%s): BuildAnalyzers %v", name, err)
59+
return nil
60+
}
61+
62+
customLinter := goanalysis.NewLinter(name, settings.Description, analyzers, nil)
63+
64+
switch strings.ToLower(p.GetLoadMode()) {
65+
case register.LoadModeSyntax:
66+
customLinter = customLinter.WithLoadMode(goanalysis.LoadModeSyntax)
67+
case register.LoadModeTypesInfo:
68+
customLinter = customLinter.WithLoadMode(goanalysis.LoadModeTypesInfo)
69+
default:
70+
customLinter = customLinter.WithLoadMode(goanalysis.LoadModeTypesInfo)
71+
}
72+
73+
lc := linter.NewConfig(customLinter).
74+
WithEnabledByDefault().
75+
WithURL(settings.OriginalURL)
76+
77+
switch strings.ToLower(p.GetLoadMode()) {
78+
case register.LoadModeSyntax:
79+
// noop
80+
case register.LoadModeTypesInfo:
81+
lc = lc.WithLoadForGoAnalysis()
82+
default:
83+
lc = lc.WithLoadForGoAnalysis()
84+
}
85+
86+
linters = append(linters, lc)
87+
}
88+
89+
return linters
90+
}

0 commit comments

Comments
 (0)