forked from go-toolset/pepperlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visitor.go
350 lines (304 loc) · 7.87 KB
/
visitor.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
package pepperlint
import (
"go/ast"
"go/token"
"path/filepath"
)
// Visitor is used to traferse a node and run the proper validaters
// based on the node that is passed in.
type Visitor struct {
Rules Rules
Errors Errors
PackagesCache *Cache
FileSet *token.FileSet
currentPkgImportPath string
}
// NewVisitor returns a new visitor and instantiates a new rule set from
// the adders parameter.
func NewVisitor(fset *token.FileSet, cache *Cache, opts ...Option) *Visitor {
v := &Visitor{
FileSet: fset,
PackagesCache: cache,
}
for _, o := range opts {
if opt, ok := o.(FileSetOption); ok {
opt.WithFileSet(fset)
}
if opt, ok := o.(RulesAdder); ok {
opt.AddRules(&v.Rules)
}
if opt, ok := o.(CacheOption); ok {
opt.WithCache(v.PackagesCache)
}
}
return v
}
// Visit is our generic visitor that will visit each ast type and call
// the appropriate rules based on what type the node is.
func (v *Visitor) Visit(node ast.Node) ast.Visitor {
if node == nil {
return v
}
//Log("VISITING %p %T %v", node, node, node)
switch t := node.(type) {
case *ast.Package:
for k := range t.Files {
v.PackagesCache.CurrentPkgImportPath = GetImportPathFromFullPath(filepath.Dir(k))
break
}
v.visitPackage(t)
case *ast.File:
v.PackagesCache.CurrentASTFile = t
v.visitFile(t)
case ast.Decl:
v.visitDecl(t)
case ast.Expr:
// ignored due to visiting of ExprStmt in
// visitStmt
if binExpr, ok := t.(*ast.BinaryExpr); ok {
v.visitBinaryExpr(binExpr)
}
case ast.Spec:
v.visitSpec(t)
case ast.Stmt:
v.visitStmt(t)
// TOOD: May contain a bug that visits twice for both visitField
// and visitFieldList
case *ast.Field:
v.visitField(t)
case *ast.FieldList:
v.visitFieldList(t)
case *ast.Comment:
case *ast.CommentGroup:
default:
if t != nil {
Log("TODO: visit %T\n", t)
}
return v
}
return v
}
func (v *Visitor) visitDecl(decl ast.Decl) {
switch t := decl.(type) {
case *ast.BadDecl:
// TODO: add error of bad declaration here
Log("TODO: visit %T\n", t)
case *ast.FuncDecl:
v.visitFuncDecl(t)
case *ast.GenDecl:
v.visitGenDecl(t)
default:
Log("TODO: visitDecl %T\n", t)
}
}
func (v *Visitor) visitExpr(expr ast.Expr) {
switch t := expr.(type) {
// covered by visitSpec which is why these do nothing
case *ast.Ident:
case *ast.ParenExpr:
case *ast.SelectorExpr:
case *ast.StarExpr:
case *ast.ArrayType:
case *ast.ChanType:
case *ast.FuncType:
v.visitFuncType(t)
case *ast.InterfaceType:
case *ast.MapType:
case *ast.StructType:
// Not covered by visitSpec
case *ast.BasicLit:
Log("TODO: visitExpr %T\n", t)
case *ast.CompositeLit:
Log("TODO: visitExpr %T\n", t)
case *ast.CallExpr:
v.visitCallExpr(t)
case *ast.BinaryExpr:
v.visitBinaryExpr(t)
case *ast.IndexExpr:
Log("TODO: visitExpr %T\n", t)
case *ast.KeyValueExpr:
Log("TODO: visitExpr %T\n", t)
default:
Log("T %T\n", t)
}
}
func (v *Visitor) visitSpec(spec ast.Spec) {
switch t := spec.(type) {
case *ast.ImportSpec:
//log.Println("TODO: visit *ast.ImportSpec")
case *ast.TypeSpec:
v.visitTypeSpec(t)
case *ast.ValueSpec:
v.visitValueSpec(t)
default:
Log("TODO: visitDecl %T\n", t)
}
}
func (v *Visitor) visitStmt(stmt ast.Stmt) {
switch t := stmt.(type) {
case *ast.AssignStmt:
v.visitAssignStmt(t)
case *ast.BlockStmt:
v.visitBlockStmt(t)
case *ast.ExprStmt:
v.visitExpr(t.X)
case *ast.ReturnStmt:
v.visitReturnStmt(t)
case *ast.IncDecStmt:
v.visitIncDecStmt(t)
case *ast.RangeStmt:
v.visitRangeStmt(t)
default:
Log("TODO: visitStmt %T\n", t)
}
}
func (v *Visitor) visitTypeSpec(spec *ast.TypeSpec) {
if err := v.Rules.TypeSpecRules.ValidateTypeSpec(spec); err != nil {
v.Errors.Add(err)
}
switch t := spec.Type.(type) {
case *ast.Ident:
Log("TODO: visit *ast.Ident")
case *ast.ParenExpr:
Log("TODO: visit *ast.ParenExpr")
case *ast.SelectorExpr:
Log("TODO: visit *ast.SelectorExpr")
case *ast.StarExpr:
Log("TODO: visit *ast.StarExpr")
// Types
case *ast.ArrayType:
v.visitArrayType(t)
case *ast.ChanType:
v.visitChanType(t)
case *ast.FuncType:
v.visitFuncType(t)
case *ast.InterfaceType:
v.visitInterfaceType(t)
case *ast.MapType:
v.visitMapType(t)
case *ast.StructType:
v.visitStructType(t)
default:
Log("TODO: visitType %T\n", t)
}
}
func (v *Visitor) visitValueSpec(spec *ast.ValueSpec) {
if err := v.Rules.ValueSpecRules.ValidateValueSpec(spec); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitStructType(s *ast.StructType) {
if err := v.Rules.StructTypeRules.ValidateStructType(s); err != nil {
v.Errors.Add(err)
}
v.visitFieldList(s.Fields)
}
func (v *Visitor) visitFieldList(fields *ast.FieldList) {
if err := v.Rules.FieldListRules.ValidateFieldList(fields); err != nil {
v.Errors.Add(err)
}
for _, field := range fields.List {
v.visitField(field)
}
}
func (v *Visitor) visitField(field *ast.Field) {
if err := v.Rules.FieldRules.ValidateField(field); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitArrayType(array *ast.ArrayType) {
if err := v.Rules.ArrayTypeRules.ValidateArrayType(array); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitMapType(m *ast.MapType) {
if err := v.Rules.MapTypeRules.ValidateMapType(m); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitChanType(ch *ast.ChanType) {
if err := v.Rules.ChanTypeRules.ValidateChanType(ch); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitFuncType(fn *ast.FuncType) {
if err := v.Rules.FuncTypeRules.ValidateFuncType(fn); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitInterfaceType(iface *ast.InterfaceType) {
if err := v.Rules.InterfaceTypeRules.ValidateInterfaceType(iface); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitFuncDecl(fnDecl *ast.FuncDecl) {
if err := v.Rules.FuncDeclRules.ValidateFuncDecl(fnDecl); err != nil {
v.Errors.Add(err)
}
}
// visitGenDecl will happen before any visiting of more specific specs, ie XXXSpec.
// This function can be used to grab documentation or other metadata to further
// validation used on more specific rules.
//
// An example of this would be how DeprecateStructRule works. The rule will visit
// general declarations first to populate the documentation of the type spec. Docs
// need to be pulled from GenDecl due to the semantic meaning of `type name struct`.
//
// type foo struct{} is a shortcut for
//
// // GenDecl docs
// type (
// // TypeSpec docs!
// foo struct{}
// )
func (v *Visitor) visitGenDecl(decl *ast.GenDecl) *Visitor {
if err := v.Rules.GenDeclRules.ValidateGenDecl(decl); err != nil {
v.Errors.Add(err)
}
return v
}
func (v *Visitor) visitAssignStmt(stmt *ast.AssignStmt) {
if err := v.Rules.AssignStmtRules.ValidateAssignStmt(stmt); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitBlockStmt(stmt *ast.BlockStmt) {
if err := v.Rules.BlockStmtRules.ValidateBlockStmt(stmt); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitCallExpr(expr *ast.CallExpr) {
if err := v.Rules.CallExprRules.ValidateCallExpr(expr); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitReturnStmt(stmt *ast.ReturnStmt) {
if err := v.Rules.ReturnStmtRules.ValidateReturnStmt(stmt); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitFile(f *ast.File) {
if err := v.Rules.FileRules.ValidateFile(f); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitPackage(pkg *ast.Package) {
if err := v.Rules.PackageRules.ValidatePackage(pkg); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitBinaryExpr(expr *ast.BinaryExpr) {
if err := v.Rules.BinaryExprRules.ValidateBinaryExpr(expr); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitIncDecStmt(stmt *ast.IncDecStmt) {
if err := v.Rules.IncDecStmtRules.ValidateIncDecStmt(stmt); err != nil {
v.Errors.Add(err)
}
}
func (v *Visitor) visitRangeStmt(stmt *ast.RangeStmt) {
if err := v.Rules.RangeStmtRules.ValidateRangeStmt(stmt); err != nil {
v.Errors.Add(err)
}
}