-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathconfig.go
185 lines (147 loc) · 4.6 KB
/
config.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
package linter
import (
"fmt"
"golang.org/x/tools/go/packages"
"github.com/golangci/golangci-lint/pkg/config"
)
const (
PresetBugs = "bugs" // Related to bugs detection.
PresetComment = "comment" // Related to comments analysis.
PresetComplexity = "complexity" // Related to code complexity analysis.
PresetError = "error" // Related to error handling analysis.
PresetFormatting = "format" // Related to code formatting.
PresetImport = "import" // Related to imports analysis.
PresetMetaLinter = "metalinter" // Related to linter that contains multiple rules or multiple linters.
PresetModule = "module" // Related to Go modules analysis.
PresetPerformance = "performance" // Related to performance.
PresetSQL = "sql" // Related to SQL.
PresetStyle = "style" // Related to coding style.
PresetTest = "test" // Related to the analysis of the code of the tests.
PresetUnused = "unused" // Related to the detection of unused code.
)
// LastLinter nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives.
const LastLinter = "nolintlint"
type DeprecationLevel int
const (
DeprecationNone DeprecationLevel = iota
DeprecationWarning
DeprecationError
)
type Deprecation struct {
Since string
Message string
Replacement string
Level DeprecationLevel
}
type Config struct {
Linter Linter
EnabledByDefault bool
LoadMode packages.LoadMode
InPresets []string
AlternativeNames []string
OriginalURL string // URL of original (not forked) repo, needed for autogenerated README
Internal bool // Internal linters cannot be disabled (ex: typecheck).
CanAutoFix bool
IsSlow bool
DoesChangeTypes bool
Since string
Deprecation *Deprecation
}
func (lc *Config) WithEnabledByDefault() *Config {
lc.EnabledByDefault = true
return lc
}
func (lc *Config) WithInternal() *Config {
lc.Internal = true
return lc
}
func (lc *Config) ConsiderSlow() *Config {
lc.IsSlow = true
return lc
}
func (lc *Config) IsSlowLinter() bool {
return lc.IsSlow
}
func (lc *Config) WithLoadFiles() *Config {
lc.LoadMode |= packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedModule
return lc
}
func (lc *Config) WithLoadForGoAnalysis() *Config {
lc = lc.WithLoadFiles()
lc.LoadMode |= packages.NeedImports | packages.NeedDeps | packages.NeedExportFile | packages.NeedTypesSizes
lc.IsSlow = true
return lc
}
func (lc *Config) WithPresets(presets ...string) *Config {
lc.InPresets = presets
return lc
}
func (lc *Config) WithURL(url string) *Config {
lc.OriginalURL = url
return lc
}
func (lc *Config) WithAlternativeNames(names ...string) *Config {
lc.AlternativeNames = names
return lc
}
func (lc *Config) WithAutoFix() *Config {
lc.CanAutoFix = true
return lc
}
func (lc *Config) WithChangeTypes() *Config {
lc.DoesChangeTypes = true
return lc
}
func (lc *Config) WithSince(version string) *Config {
lc.Since = version
return lc
}
func (lc *Config) Deprecated(message, version, replacement string, level DeprecationLevel) *Config {
lc.Deprecation = &Deprecation{
Since: version,
Message: message,
Replacement: replacement,
Level: level,
}
return lc
}
func (lc *Config) DeprecatedWarning(message, version, replacement string) *Config {
return lc.Deprecated(message, version, replacement, DeprecationWarning)
}
func (lc *Config) DeprecatedError(message, version, replacement string) *Config {
return lc.Deprecated(message, version, replacement, DeprecationError)
}
func (lc *Config) IsDeprecated() bool {
return lc.Deprecation != nil
}
func (lc *Config) AllNames() []string {
return append([]string{lc.Name()}, lc.AlternativeNames...)
}
func (lc *Config) Name() string {
return lc.Linter.Name()
}
func (lc *Config) WithNoopFallback(cfg *config.Config, cond func(cfg *config.Config) error) *Config {
if err := cond(cfg); err != nil {
lc.Linter = NewNoop(lc.Linter, err.Error())
lc.LoadMode = 0
return lc.WithLoadFiles()
}
return lc
}
func IsGoLowerThanGo122() func(cfg *config.Config) error {
return isGoLowerThanGo("1.22")
}
func isGoLowerThanGo(v string) func(cfg *config.Config) error {
return func(cfg *config.Config) error {
if cfg == nil || config.IsGoGreaterThanOrEqual(cfg.Run.Go, v) {
return nil
}
return fmt.Errorf("this linter is disabled because the Go version (%s) of your project is lower than Go %s", cfg.Run.Go, v)
}
}
func NewConfig(linter Linter) *Config {
lc := &Config{
Linter: linter,
}
return lc.WithLoadFiles()
}