-
Notifications
You must be signed in to change notification settings - Fork 2
/
checkers_db.go
135 lines (116 loc) · 2.97 KB
/
checkers_db.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
package lintpack
import (
"fmt"
"regexp"
"sort"
"strings"
"github.com/go-toolsmith/astfmt"
)
type checkerProto struct {
info *CheckerInfo
constructor func(*Context) *Checker
}
// prototypes is a set of registered checkers that are not yet instantiated.
// Registration should be done with AddChecker function.
// Initialized checkers can be obtained with NewChecker function.
var prototypes = make(map[string]checkerProto)
func getCheckersInfo() []*CheckerInfo {
infoList := make([]*CheckerInfo, 0, len(prototypes))
for _, proto := range prototypes {
infoCopy := *proto.info
infoList = append(infoList, &infoCopy)
}
sort.Slice(infoList, func(i, j int) bool {
return infoList[i].Name < infoList[j].Name
})
return infoList
}
func addChecker(info *CheckerInfo, constructor func(*CheckerContext) FileWalker) {
if _, ok := prototypes[info.Name]; ok {
panic(fmt.Sprintf("checker with name %q already registered", info.Name))
}
// Validate param value type.
for pname, param := range info.Params {
switch param.Value.(type) {
case string, int, bool:
// OK.
default:
panic(fmt.Sprintf("unsupported %q param type value: %T",
pname, param.Value))
}
}
trimDocumentation := func(info *CheckerInfo) {
fields := []*string{
&info.Summary,
&info.Details,
&info.Before,
&info.After,
&info.Note,
}
for _, f := range fields {
*f = strings.TrimSpace(*f)
}
}
trimDocumentation(info)
if err := validateCheckerInfo(info); err != nil {
panic(err)
}
proto := checkerProto{
info: info,
constructor: func(ctx *Context) *Checker {
var c Checker
c.Info = info
c.ctx = CheckerContext{
Context: ctx,
printer: astfmt.NewPrinter(ctx.FileSet),
}
c.fileWalker = constructor(&c.ctx)
return &c
},
}
prototypes[info.Name] = proto
}
func newChecker(ctx *Context, info *CheckerInfo) *Checker {
proto, ok := prototypes[info.Name]
if !ok {
panic(fmt.Sprintf("checker with name %q not registered", info.Name))
}
return proto.constructor(ctx)
}
func validateCheckerInfo(info *CheckerInfo) error {
steps := []func(*CheckerInfo) error{
validateCheckerName,
validateCheckerDocumentation,
validateCheckerTags,
}
for _, step := range steps {
if err := step(info); err != nil {
return fmt.Errorf("%q validation error: %v", info.Name, err)
}
}
return nil
}
var validIdentRE = regexp.MustCompile(`^\w+$`)
func validateCheckerName(info *CheckerInfo) error {
if !validIdentRE.MatchString(info.Name) {
return fmt.Errorf("checker name contains illegal chars")
}
return nil
}
func validateCheckerDocumentation(info *CheckerInfo) error {
// TODO(Quasilyte): validate documentation.
return nil
}
func validateCheckerTags(info *CheckerInfo) error {
tagSet := make(map[string]bool)
for _, tag := range info.Tags {
if tagSet[tag] {
return fmt.Errorf("duplicated tag %q", tag)
}
if !validIdentRE.MatchString(tag) {
return fmt.Errorf("checker tag %q contains illegal chars", tag)
}
tagSet[tag] = true
}
return nil
}