-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.go
265 lines (219 loc) · 5.77 KB
/
base.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
// Copyright 2015 Jesse Meek.
// Licensed under the AGPLv3, see LICENCE file for details.
package tenet
import (
"fmt"
"go/token"
"strings"
"github.com/juju/errors"
"github.com/lingo-reviews/tenets/go/dev/api"
"github.com/lingo-reviews/tenets/go/dev/tenet/log"
)
// Base implements the Tenet interface and is intended to be composed
// with 3rd party tenets.
type Base struct {
// all issues this tenet looks for.
registeredIssues map[string]*Issue
// all issues this tenet found.
issuesc chan *Issue
// errors holds non-fatal errors. // TODO(waigani) currently the server
// just logs these. We need to make logs visable to user and provide
// options for the user around errors.
errorsc chan error
// tmpdir is the dir for tenets to work in.
tmpdir string
astVisitors astVisitors
lineVisitors lineVisitors
info *Info
}
type astVisitors []astVisitor
func (p *astVisitors) deleteRange(a, z int) {
s := *p
s = append(s[:a], s[z+1:]...)
*p = s
}
type lineVisitors []lineVisitor
func (p *lineVisitors) deleteRange(a, z int) {
s := *p
s = append(s[:a], s[z+1:]...)
*p = s
}
// base allows us to access the base struct when it's embeded in another
// struct with a Tenet interface type.
func base(t Tenet) *Base {
return t.(BaseTenet).base()
}
func (b *Base) base() *Base {
return b
}
func (b *Base) Init() {
b.errorsc = make(chan error, 1)
}
func (b *Base) NewReview() *review {
r := &review{
tenet: b,
issuesc: make(chan *Issue),
filesc: make(chan *api.File),
waitc: make(chan struct{}),
fileDoneMap: map[string]bool{},
}
go func() {
<-r.waitc
if r.issuesc != nil {
log.Println("closing issuesc")
close(r.issuesc)
}
r.waitc = nil
}()
return r
}
func (b *Base) SendError(err error) {
log.Println("sending error", err)
log.Println(b.errorsc == nil)
select {
case b.errorsc <- err:
default:
}
}
func (b *Base) Errors() chan error {
return b.errorsc
}
func (b *Base) SmellNode(f smellNodeFunc) Tenet {
b.astVisitors = append(b.astVisitors, astVisitor{
smellNode: f,
fileDone: map[string]bool{},
})
return b
}
func (b *Base) SmellLine(f smellLineFunc) Tenet {
b.lineVisitors = append(b.lineVisitors, lineVisitor{
visit: f,
fileDone: map[string]bool{},
})
return b
}
func (b *Base) MixinConfigOptions(opts []*api.Option) error {
for _, opt := range opts {
if err := b.setOpt(opt); err != nil {
return errors.Trace(err)
}
}
return nil
}
func (b *Base) setOpt(opt *api.Option) error {
if b.info == nil {
return errors.New("tenet info is nil")
}
for _, bOpt := range b.info.Options {
if bOpt.name == opt.Name {
*bOpt.value = opt.Value
return nil
}
}
return errors.Errorf("tenet has no option %q", opt.Name)
}
func AddComment(comment string, ctx ...CommentContext) RegisterIssueOption {
return func(issue *Issue) {
issue.addComment(comment, ctx...)
}
}
type option struct {
name string
value *string
usage string
}
// TODO(waigani) support interface values
func (b *Base) RegisterOption(name string, value string, usage string) *string {
// toml doesn't support "-"
blacklistChars := "- "
if strings.ContainsAny(name, blacklistChars) {
// Yes panic, this is a developer error.
msg := fmt.Sprintf("option name %q cannot contain any of the following characters: %q", name, blacklistChars)
panic(msg)
}
if b.info == nil {
// Yes panic, this is a developer error.
panic("options cannot be registered before tenet.Info is set, please do that first.")
}
v := &value
b.info.Options = append(b.info.Options, &option{
name: name,
value: v,
usage: usage,
})
// return the value from the pointer for this option which will either be the
// default value passed in or one updated by the user.
return v
}
// RegisterMetric registers a metric key name that can be used when raising an issue.
func (b *Base) RegisterMetric(key string) func(val interface{}) RaiseIssueOption {
b.info.metrics = append(b.info.metrics, key)
return func(val interface{}) RaiseIssueOption {
return func(issue *Issue) {
if issue.Metrics == nil {
issue.Metrics = map[string]interface{}{}
}
issue.Metrics[key] = val
}
}
}
// RegisterTag registers a tag name that can be used when registering an issue.
func (b *Base) RegisterTag(tag string) RaiseIssueOption {
b.info.tags = append(b.info.tags, tag)
return func(issue *Issue) {
issue.Tags = append(issue.Tags, tag)
}
}
func (b *Base) RegisterIssue(issueName string, opts ...RegisterIssueOption) string {
issue := &Issue{
Name: issueName,
CommVars: map[string]interface{}{},
}
for _, opt := range opts {
opt(issue)
}
// Add a defualt comment if tenet has not added any.
if len(issue.comments) == 0 {
issue.addComment("Issue Found")
}
if b.registeredIssues == nil {
b.registeredIssues = map[string]*Issue{}
}
b.registeredIssues[issueName] = issue
return issueName
}
type errWithContext struct {
err error
errLine *token.Position
}
func (e *errWithContext) Error() string {
return e.err.Error()
}
func (e *errWithContext) Line() string {
return e.errLine.String()
}
// TODO(waigani) call this handleTenetError and make a TenetError type - only
// those can be passed in.
// posOfErr is the position of the node/line that was being parsed when the
// error occoured.
func (b *Base) addErrOnErr(err error, f File, posOfErr interface{}) bool {
if err != nil {
// TODO(waigani) this log is a quick hack. We should read all the errs off errorsc.
log.Println(err.Error())
errCtx := &errWithContext{err: err}
switch p := posOfErr.(type) {
case token.Pos:
fset := f.Fset()
pos := fset.Position(p)
errCtx.errLine = &pos
case int:
line := f.(BaseFile).linePosition(p)
errCtx.errLine = &line
default:
panic(fmt.Sprintf("unknown posOfErr type: %T", posOfErr))
}
b.errorsc <- errCtx
return true
}
return false
}