-
Notifications
You must be signed in to change notification settings - Fork 265
/
config.go
231 lines (210 loc) 路 5.33 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
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
package config
import (
"errors"
"fmt"
"io/ioutil"
"github.com/mgechev/revive/formatter"
"github.com/BurntSushi/toml"
"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/rule"
)
var defaultRules = []lint.Rule{
&rule.VarDeclarationsRule{},
&rule.PackageCommentsRule{},
&rule.DotImportsRule{},
&rule.BlankImportsRule{},
&rule.ExportedRule{},
&rule.VarNamingRule{},
&rule.IndentErrorFlowRule{},
&rule.RangeRule{},
&rule.ErrorfRule{},
&rule.ErrorNamingRule{},
&rule.ErrorStringsRule{},
&rule.ReceiverNamingRule{},
&rule.IncrementDecrementRule{},
&rule.ErrorReturnRule{},
&rule.UnexportedReturnRule{},
&rule.TimeNamingRule{},
&rule.ContextKeysType{},
&rule.ContextAsArgumentRule{},
}
var allRules = append([]lint.Rule{
&rule.ArgumentsLimitRule{},
&rule.CyclomaticRule{},
&rule.FileHeaderRule{},
&rule.EmptyBlockRule{},
&rule.SuperfluousElseRule{},
&rule.ConfusingNamingRule{},
&rule.GetReturnRule{},
&rule.ModifiesParamRule{},
&rule.ConfusingResultsRule{},
&rule.DeepExitRule{},
&rule.UnusedParamRule{},
&rule.UnreachableCodeRule{},
&rule.AddConstantRule{},
&rule.FlagParamRule{},
&rule.UnnecessaryStmtRule{},
&rule.StructTagRule{},
&rule.ModifiesValRecRule{},
&rule.ConstantLogicalExprRule{},
&rule.BoolLiteralRule{},
&rule.RedefinesBuiltinIDRule{},
&rule.ImportsBlacklistRule{},
&rule.FunctionResultsLimitRule{},
&rule.MaxPublicStructsRule{},
&rule.RangeValInClosureRule{},
&rule.RangeValAddress{},
&rule.WaitGroupByValueRule{},
&rule.AtomicRule{},
&rule.EmptyLinesRule{},
&rule.LineLengthLimitRule{},
&rule.CallToGCRule{},
&rule.DuplicatedImportsRule{},
&rule.ImportShadowingRule{},
&rule.BareReturnRule{},
&rule.UnusedReceiverRule{},
&rule.UnhandledErrorRule{},
&rule.CognitiveComplexityRule{},
&rule.StringOfIntRule{},
&rule.StringFormatRule{},
&rule.EarlyReturnRule{},
&rule.UnconditionalRecursionRule{},
&rule.IdenticalBranchesRule{},
&rule.DeferRule{},
&rule.UnexportedNamingRule{},
&rule.FunctionLength{},
&rule.NestedStructs{},
&rule.IfReturnRule{},
&rule.UselessBreak{},
&rule.TimeEqualRule{},
&rule.BannedCharsRule{},
&rule.OptimizeOperandsOrderRule{},
&rule.UseAnyRule{},
}, defaultRules...)
var allFormatters = []lint.Formatter{
&formatter.Stylish{},
&formatter.Friendly{},
&formatter.JSON{},
&formatter.NDJSON{},
&formatter.Default{},
&formatter.Unix{},
&formatter.Checkstyle{},
&formatter.Plain{},
&formatter.Sarif{},
}
func getFormatters() map[string]lint.Formatter {
result := map[string]lint.Formatter{}
for _, f := range allFormatters {
result[f.Name()] = f
}
return result
}
// GetLintingRules yields the linting rules that must be applied by the linter
func GetLintingRules(config *lint.Config, extraRules []lint.Rule) ([]lint.Rule, error) {
rulesMap := map[string]lint.Rule{}
for _, r := range allRules {
rulesMap[r.Name()] = r
}
for _, r := range extraRules {
if _, ok := rulesMap[r.Name()]; ok {
continue
}
rulesMap[r.Name()] = r
}
var lintingRules []lint.Rule
for name, ruleConfig := range config.Rules {
rule, ok := rulesMap[name]
if !ok {
return nil, fmt.Errorf("cannot find rule: %s", name)
}
if ruleConfig.Disabled {
continue // skip disabled rules
}
lintingRules = append(lintingRules, rule)
}
return lintingRules, nil
}
func parseConfig(path string, config *lint.Config) error {
file, err := ioutil.ReadFile(path)
if err != nil {
return errors.New("cannot read the config file")
}
_, err = toml.Decode(string(file), config)
if err != nil {
return fmt.Errorf("cannot parse the config file: %v", err)
}
return nil
}
func normalizeConfig(config *lint.Config) {
if len(config.Rules) == 0 {
config.Rules = map[string]lint.RuleConfig{}
}
if config.EnableAllRules {
// Add to the configuration all rules not yet present in it
for _, rule := range allRules {
ruleName := rule.Name()
_, alreadyInConf := config.Rules[ruleName]
if alreadyInConf {
continue
}
// Add the rule with an empty conf for
config.Rules[ruleName] = lint.RuleConfig{}
}
}
severity := config.Severity
if severity != "" {
for k, v := range config.Rules {
if v.Severity == "" {
v.Severity = severity
}
config.Rules[k] = v
}
for k, v := range config.Directives {
if v.Severity == "" {
v.Severity = severity
}
config.Directives[k] = v
}
}
}
const defaultConfidence = 0.8
// GetConfig yields the configuration
func GetConfig(configPath string) (*lint.Config, error) {
config := &lint.Config{}
switch {
case configPath != "":
config.Confidence = defaultConfidence
err := parseConfig(configPath, config)
if err != nil {
return nil, err
}
default: // no configuration provided
config = defaultConfig()
}
normalizeConfig(config)
return config, nil
}
// GetFormatter yields the formatter for lint failures
func GetFormatter(formatterName string) (lint.Formatter, error) {
formatters := getFormatters()
formatter := formatters["default"]
if formatterName != "" {
f, ok := formatters[formatterName]
if !ok {
return nil, fmt.Errorf("unknown formatter %v", formatterName)
}
formatter = f
}
return formatter, nil
}
func defaultConfig() *lint.Config {
defaultConfig := lint.Config{
Confidence: defaultConfidence,
Severity: lint.SeverityWarning,
Rules: map[string]lint.RuleConfig{},
}
for _, r := range defaultRules {
defaultConfig.Rules[r.Name()] = lint.RuleConfig{}
}
return &defaultConfig
}