forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanager.go
291 lines (225 loc) · 6.86 KB
/
manager.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package lintersdb
import (
"cmp"
"fmt"
"maps"
"os"
"slices"
"sort"
"strings"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/goanalysis"
"github.com/golangci/golangci-lint/pkg/goformatters"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/logutils"
)
type Builder interface {
Build(cfg *config.Config) ([]*linter.Config, error)
}
// Manager is a type of database for all linters (internals or plugins).
// It provides methods to access to the linter sets.
type Manager struct {
log logutils.Log
debugf logutils.DebugFunc
cfg *config.Config
linters []*linter.Config
nameToLCs map[string][]*linter.Config
}
// NewManager creates a new Manager.
// This constructor will call the builders to build and store the linters.
func NewManager(log logutils.Log, cfg *config.Config, builders ...Builder) (*Manager, error) {
m := &Manager{
log: log,
debugf: logutils.Debug(logutils.DebugKeyEnabledLinters),
nameToLCs: make(map[string][]*linter.Config),
}
m.cfg = cfg
if cfg == nil {
m.cfg = config.NewDefault()
}
for _, builder := range builders {
linters, err := builder.Build(m.cfg)
if err != nil {
return nil, fmt.Errorf("build linters: %w", err)
}
m.linters = append(m.linters, linters...)
}
for _, lc := range m.linters {
for _, name := range lc.AllNames() {
m.nameToLCs[name] = append(m.nameToLCs[name], lc)
}
}
err := NewValidator(m).Validate(m.cfg)
if err != nil {
return nil, err
}
return m, nil
}
func (m *Manager) GetLinterConfigs(name string) []*linter.Config {
return m.nameToLCs[name]
}
func (m *Manager) GetAllSupportedLinterConfigs() []*linter.Config {
return m.linters
}
func (m *Manager) GetEnabledLintersMap() (map[string]*linter.Config, error) {
enabledLinters, err := m.build()
if err != nil {
return nil, err
}
if os.Getenv(logutils.EnvTestRun) == "1" {
m.verbosePrintLintersStatus(enabledLinters)
}
return enabledLinters, nil
}
// GetOptimizedLinters returns enabled linters after optimization (merging) of multiple linters into a fewer number of linters.
// E.g. some go/analysis linters can be optimized into one metalinter for data reuse and speed up.
func (m *Manager) GetOptimizedLinters() ([]*linter.Config, error) {
resultLintersSet, err := m.build()
if err != nil {
return nil, err
}
m.verbosePrintLintersStatus(resultLintersSet)
m.combineGoAnalysisLinters(resultLintersSet)
// Make order of execution of linters (go/analysis metalinter and unused) stable.
resultLinters := slices.SortedFunc(maps.Values(resultLintersSet), func(a *linter.Config, b *linter.Config) int {
if b.Name() == linter.LastLinter {
return -1
}
if a.Name() == linter.LastLinter {
return 1
}
if a.DoesChangeTypes != b.DoesChangeTypes {
// move type-changing linters to the end to optimize speed
if b.DoesChangeTypes {
return -1
}
return 1
}
return strings.Compare(a.Name(), b.Name())
})
return resultLinters, nil
}
//nolint:gocyclo // the complexity cannot be reduced.
func (m *Manager) build() (map[string]*linter.Config, error) {
m.debugf("Linters config: %#v", m.cfg.Linters)
resultLintersSet := map[string]*linter.Config{}
groupName := cmp.Or(m.cfg.Linters.Default, config.GroupStandard)
switch groupName {
case config.GroupNone:
// no default linters
case config.GroupAll:
resultLintersSet = linterConfigsToMap(m.linters)
case config.GroupFast:
var selected []*linter.Config
for _, lc := range m.linters {
if lc.IsSlowLinter() {
continue
}
selected = append(selected, lc)
}
resultLintersSet = linterConfigsToMap(selected)
case config.GroupStandard:
var selected []*linter.Config
for _, lc := range m.linters {
if !lc.FromGroup(config.GroupStandard) {
continue
}
selected = append(selected, lc)
}
resultLintersSet = linterConfigsToMap(selected)
default:
return nil, fmt.Errorf("unknown group: %s", groupName)
}
for _, name := range slices.Concat(m.cfg.Linters.Enable, m.cfg.Formatters.Enable) {
for _, lc := range m.GetLinterConfigs(name) {
// it's important to use lc.Name() nor name because name can be alias
resultLintersSet[lc.Name()] = lc
}
}
for _, name := range m.cfg.Linters.Disable {
for _, lc := range m.GetLinterConfigs(name) {
// it's important to use lc.Name() nor name because name can be alias
delete(resultLintersSet, lc.Name())
}
}
if m.cfg.Linters.FastOnly {
for lc := range maps.Values(resultLintersSet) {
if lc.IsSlowLinter() {
// it's important to use lc.Name() nor name because name can be alias
delete(resultLintersSet, lc.Name())
}
}
}
// typecheck is not a real linter and cannot be disabled.
if _, ok := resultLintersSet["typecheck"]; !ok && (m.cfg == nil || !m.cfg.InternalCmdTest) {
for _, lc := range m.GetLinterConfigs("typecheck") {
// it's important to use lc.Name() nor name because name can be alias
resultLintersSet[lc.Name()] = lc
}
}
return resultLintersSet, nil
}
func (m *Manager) combineGoAnalysisLinters(linters map[string]*linter.Config) {
mlConfig := &linter.Config{}
var goanalysisLinters []*goanalysis.Linter
for _, lc := range linters {
lnt, ok := lc.Linter.(*goanalysis.Linter)
if !ok {
continue
}
if lnt.LoadMode() == goanalysis.LoadModeWholeProgram {
// It's ineffective by CPU and memory to run whole-program and incremental analyzers at once.
continue
}
mlConfig.LoadMode |= lc.LoadMode
if lc.IsSlowLinter() {
mlConfig.ConsiderSlow()
}
goanalysisLinters = append(goanalysisLinters, lnt)
}
if len(goanalysisLinters) <= 1 {
m.debugf("Didn't combine go/analysis linters: got only %d linters", len(goanalysisLinters))
return
}
for _, lnt := range goanalysisLinters {
delete(linters, lnt.Name())
}
// Make order of execution of go/analysis analyzers stable.
sort.Slice(goanalysisLinters, func(i, j int) bool {
a, b := goanalysisLinters[i], goanalysisLinters[j]
if b.Name() == linter.LastLinter {
return true
}
if a.Name() == linter.LastLinter {
return false
}
return a.Name() <= b.Name()
})
mlConfig.Linter = goanalysis.NewMetaLinter(goanalysisLinters)
linters[mlConfig.Linter.Name()] = mlConfig
m.debugf("Combined %d go/analysis linters into one metalinter", len(goanalysisLinters))
}
func (m *Manager) verbosePrintLintersStatus(lcs map[string]*linter.Config) {
var linterNames []string
for _, lc := range lcs {
if lc.Internal {
continue
}
linterNames = append(linterNames, lc.Name())
}
sort.Strings(linterNames)
m.log.Infof("Active %d linters: %s", len(linterNames), linterNames)
}
func linterConfigsToMap(lcs []*linter.Config) map[string]*linter.Config {
ret := map[string]*linter.Config{}
for _, lc := range lcs {
if lc.IsDeprecated() && lc.Deprecation.Level > linter.DeprecationWarning {
continue
}
if goformatters.IsFormatter(lc.Name()) {
continue
}
ret[lc.Name()] = lc
}
return ret
}