-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathbuilder_plugin_module.go
More file actions
85 lines (66 loc) · 2.21 KB
/
Copy pathbuilder_plugin_module.go
File metadata and controls
85 lines (66 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package lintersdb
import (
"fmt"
"strings"
"github.com/golangci/plugin-module-register/register"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/logutils"
)
const modulePluginType = "module"
// PluginModuleBuilder builds the custom linters (module plugin) based on the configuration.
type PluginModuleBuilder struct {
log logutils.Log
}
// NewPluginModuleBuilder creates new PluginModuleBuilder.
func NewPluginModuleBuilder(log logutils.Log) *PluginModuleBuilder {
return &PluginModuleBuilder{log: log}
}
// Build loads custom linters that are specified in the golangci-lint config file.
func (b *PluginModuleBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
if cfg == nil || b.log == nil {
return nil, nil
}
var linters []*linter.Config
for name, settings := range cfg.LintersSettings.Custom {
if settings.Type != modulePluginType {
continue
}
b.log.Infof("Loaded %s: %s", settings.Path, name)
newPlugin, err := register.GetPlugin(name)
if err != nil {
return nil, fmt.Errorf("plugin(%s): %w", name, err)
}
p, err := newPlugin(settings.Settings)
if err != nil {
return nil, fmt.Errorf("plugin(%s): newPlugin %w", name, err)
}
analyzers, err := p.BuildAnalyzers()
if err != nil {
return nil, fmt.Errorf("plugin(%s): BuildAnalyzers %w", name, err)
}
customLinter := goanalysis.NewLinter(name, settings.Description, analyzers, nil)
switch strings.ToLower(p.GetLoadMode()) {
case register.LoadModeSyntax:
customLinter = customLinter.WithLoadMode(goanalysis.LoadModeSyntax)
case register.LoadModeTypesInfo:
customLinter = customLinter.WithLoadMode(goanalysis.LoadModeTypesInfo)
default:
customLinter = customLinter.WithLoadMode(goanalysis.LoadModeTypesInfo)
}
lc := linter.NewConfig(customLinter).
WithEnabledByDefault().
WithURL(settings.OriginalURL)
switch strings.ToLower(p.GetLoadMode()) {
case register.LoadModeSyntax:
// noop
case register.LoadModeTypesInfo:
lc = lc.WithLoadForGoAnalysis()
default:
lc = lc.WithLoadForGoAnalysis()
}
linters = append(linters, lc)
}
return linters, nil
}