-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
393 lines (317 loc) · 8.75 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package uncalled
import (
"bytes"
_ "embed"
"fmt"
"go/ast"
"go/types"
"io"
"os"
"regexp"
"strconv"
"strings"
"gopkg.in/yaml.v3"
)
const (
anyType = "_"
)
var (
// reName is pattern which validates rule names.
reName = regexp.MustCompile("^[a-z0-9-]+$")
)
//go:embed .uncalled.yaml
var defaultConfig []byte
// quote quotes s.
func quote(s string) string {
if strconv.CanBackquote(s) {
return "`" + s + "`"
}
return strconv.Quote(s)
}
// loadDefaultConfig loads the default embedded configuration.
func loadDefaultConfig() (*Config, error) {
cfg := &Config{}
if err := cfg.load(bytes.NewBuffer(defaultConfig)); err != nil {
return nil, fmt.Errorf("decode config %s: %w", quote(string(defaultConfig)), err)
}
return cfg, nil
}
// Config represents the configuration for uncalled Analyzer.
type Config struct {
// DisableAll disables all rules.
DisableAll bool `mapstructure:"disable-all" yaml:"disable-all"`
// Disabled disables the given rules.
Disabled []string
// Enabled enables specific rules, in combination with disable all.
Enabled []string
// Rules are the rules to process, disabled rules will be skipped.
Rules []Rule
// rules lists all rules and their index in Rules.
rules map[string]Rule
// active lists active rules.
active map[string]Rule
}
// loadFile loads the analyzer config from file.
func (c *Config) loadFile(file string) error {
f, err := os.Open(file)
if err != nil {
// No file in wrap as that's in err already.
return fmt.Errorf("load config: %w", err)
}
defer f.Close()
return c.load(f)
}
// load loads the analyzer config from r.
func (c *Config) load(r io.Reader) error {
dec := yaml.NewDecoder(r)
if err := dec.Decode(c); err != nil {
return fmt.Errorf("decode config: %q: %w", "file", err)
}
return c.validate()
}
// string returns a YAML string representation of c.
// If an error occurs it is returned instead.
func (c *Config) string() string {
s, err := c.yaml()
if err != nil {
return err.Error()
}
return string(s)
}
// yaml returns a yaml representation of c.
func (c *Config) yaml() ([]byte, error) {
b, err := yaml.Marshal(c)
if err != nil {
return nil, fmt.Errorf("encode config: %w", err)
}
return b, nil
}
// copy returns a deep copy of c.
func (c *Config) copy() (*Config, error) {
// Leverage yaml serialisation to create a deep copy.
data, err := c.yaml()
if err != nil {
return nil, err
}
var cfg Config
if err := cfg.load(bytes.NewBuffer(data)); err != nil {
return nil, err
}
// Validate after copy to ensure we don't cause any data races.
if err := cfg.validate(); err != nil {
return nil, err
}
return &cfg, nil
}
// merge merges other into c if not nil.
func (c *Config) merge(other *Config) error {
if other == nil {
return nil
}
c.DisableAll = other.DisableAll
c.Disabled = other.Disabled
c.Enabled = other.Enabled
for _, otherRule := range other.Rules {
rule, ok := c.rules[otherRule.Name]
if ok {
// Existing rule overwrite.
c.Rules[rule.idx] = otherRule
} else {
// New rule append
c.Rules = append(c.Rules, otherRule)
}
}
return c.validate()
}
// validate validates the configuration.
func (c *Config) validate() error {
c.active = make(map[string]Rule)
c.rules = make(map[string]Rule)
disabled := make(map[string]struct{})
for i, r := range c.Rules {
if err := r.validate(); err != nil {
return err
}
r.idx = i
if !c.DisableAll {
c.active[r.Name] = r
}
c.rules[r.Name] = r
}
for _, r := range c.Disabled {
if _, ok := c.rules[r]; !ok {
return fmt.Errorf("rule %q: in disabled unknown", r)
}
disabled[r] = struct{}{}
delete(c.active, r)
}
for _, name := range c.Enabled {
r, ok := c.rules[name]
if !ok {
return fmt.Errorf("rule %q: in enabled unknown", name)
}
if _, ok := disabled[name]; ok {
return fmt.Errorf("rule %q: in both enabled and disabled", name)
}
c.active[name] = r
}
return nil
}
// Rule represents an individual rule for uncalled Analyzer.
type Rule struct {
// Name is the name of the rule.
Name string
// Category is the category used to report failures for this rule.
Category string
// Packages is the list of package imports which to be considered
// When processing this rule. If one of the listed packages isn't
// imported by the code being checked the rule is automatically
// skipped. At least one package must be specified.
Packages []string
// Results represents the results the matched methods return.
Results []*Result
// idx represents the index at which this rule was in Config.Rules.
idx int
// expects references the result which specifies a Method.
expects *Result
// expected calls is a map of fully qualified calls we expect.
expectedCalls map[string]struct{}
// expectedType is a map of fully qualified types to monitor.
expectedTypes map[string]struct{}
}
// name returns the expected string based on ident.
func (r Rule) name(ident string) string {
if ident == "" {
ident = strings.TrimLeft(r.expects.Type, ".")
}
return fmt.Sprintf("%s%s(%s)", ident, r.expects.Expect.Call, strings.Join(r.expects.Expect.Args, ","))
}
// validate returns an error if r isn't valid, nil otherwise.
func (r *Rule) validate() error {
switch {
case !reName.MatchString(r.Name):
return fmt.Errorf("rule %q: contains non alpha numeric or uppercase characters", r.Name)
case len(r.Packages) == 0:
return fmt.Errorf("rule %q: no packages", r.Name)
case len(r.Results) == 0:
return fmt.Errorf("rule %q: no call results", r.Name)
}
for i, res := range r.Results {
if res.Expect != nil {
if r.expects != nil {
return fmt.Errorf("rule %q: multiple results expecting a method", r.Name)
}
res.idx = i
r.expects = res
}
}
if r.expects == nil {
return fmt.Errorf("rule %q: no result expecting a method", r.Name)
}
r.expectedCalls = make(map[string]struct{})
r.expectedTypes = make(map[string]struct{})
for _, res := range r.Results {
if err := res.build(r); err != nil {
return err
}
}
return nil
}
// matchesResults returns true if the res matches the Results of this rule,
// false otherwise.
func (r *Rule) matchesResults(res *types.Tuple) bool {
if res.Len() != len(r.Results) {
return false // Function results length does match.
}
for i, r := range r.Results {
if !r.match(res.At(i).Type()) {
return false
}
}
return true
}
// matchesCall returns true if call and name match the expected call for
// this rule, false otherwise.
func (r *Rule) matchesCall(call *ast.CallExpr, name string) bool {
if len(call.Args) != len(r.expects.Expect.Args) {
return false
}
if strings.HasPrefix(r.expects.Expect.Call, ".") {
return r.expects.Expect.Call[1:] == name
}
return r.expects.Expect.Call == name
}
// Result is a result expected from a rule call.
type Result struct {
// Type is name of the type.
// If "_" then matches any type.
// If prefixed by "." then matches any type of specified
// in its Rule.Packages.
Type string
// Pointer specifies if this type should be a pointer to
// the named TypeName.
Pointer bool
// Expect sets the expectation for the result.
// At least one Result in a rule must have a method specified.
// If not specified no check it performed.
Expect *Expect
idx int
match resultMatcher
}
// resultMatcher is a function which returns true if t matches, false otherwise.
type resultMatcher func(t types.Type) bool
// build builds the matcher for this result.
func (r *Result) build(rule *Rule) error {
resultTypes := make(map[string]struct{}, len(rule.Packages))
for _, p := range rule.Packages {
name := r.name(p)
resultTypes[name] = struct{}{}
if r.Expect == nil {
continue // Not expecting a method on this result to be called.
}
// Expected result type.
if r.Type == anyType {
return fmt.Errorf("rule %q: result idx %d is expected and wildcard", rule.Name, r.idx)
}
name += r.Expect.Call
rule.expectedCalls[name] = struct{}{}
parts := strings.Split(name, ".")
name = strings.Join(parts[:len(parts)-1], ".")
rule.expectedTypes[name] = struct{}{}
}
r.match = func(t types.Type) bool {
if t == nil {
return false
}
if r.Type == anyType {
return true // Matches any type.
}
_, ok := resultTypes[t.String()]
return ok
}
return nil
}
// name returns the fully qualified type name for given pkg.
func (r Result) name(pkg string) string {
if r.Type == anyType {
return ""
}
var ptr string
if r.Pointer {
ptr = "*"
}
if strings.HasPrefix(r.Type, ".") {
return fmt.Sprintf("%s%s%s", ptr, pkg, r.Type)
}
return fmt.Sprintf("%s%s", ptr, r.Type)
}
// Expect represents a result call expectation.
type Expect struct {
// Call is the call to expect on this result.
// Methods called on the result should start with a "."
// for example .Err
Call string
// Args are the arguments passed to the method.
// Currently on the count matters.
Args []string
}