forked from go-toolset/pepperlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
306 lines (257 loc) · 5.9 KB
/
utils.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
package pepperlint
import (
"go/ast"
"log"
"os"
"path/filepath"
"strings"
)
// Helper is used to make determing properties of an ast.Node
type Helper struct {
PackagesCache *Cache
}
// NewHelper will return a new helper with a populated cache.
func NewHelper(cache *Cache) Helper {
return Helper{
PackagesCache: cache,
}
}
// IsStruct will return whether or not an ast.Expr is a
// struct type.
func (h Helper) IsStruct(expr ast.Expr) bool {
switch t := expr.(type) {
// Chcek if it is a selector expression, meaning it potentially
// could be an imported shape
case *ast.SelectorExpr:
ident, ok := t.X.(*ast.Ident)
if !ok {
return false
}
pkg, ok := h.PackagesCache.Packages.Get(ident.Name)
if !ok {
return false
}
info, ok := pkg.Files.GetTypeInfo(t.Sel.Name)
if !ok {
return false
}
if info.Spec == nil {
return false
}
return h.IsStruct(info.Spec.Type)
case *ast.StructType:
return true
case *ast.Ident:
if t.Obj == nil {
return false
}
if t.Obj.Decl == nil {
return false
}
decl := t.Obj.Decl
switch d := decl.(type) {
case *ast.TypeSpec:
return h.IsStruct(d.Type)
}
case *ast.StarExpr:
return h.IsStruct(t.X)
}
return false
}
// GetStructType will return a struct from the given expr.
func (h Helper) GetStructType(expr ast.Expr) *ast.StructType {
switch t := expr.(type) {
// Chcek if it is a selector expression, meaning it potentially
// could be an imported shape
case *ast.SelectorExpr:
ident, ok := t.X.(*ast.Ident)
if !ok {
return nil
}
pkg, ok := h.PackagesCache.Packages.Get(ident.Name)
if !ok {
return nil
}
info, ok := pkg.Files.GetTypeInfo(t.Sel.Name)
if !ok || info.Spec == nil {
return nil
}
return h.GetStructType(info.Spec.Type)
case *ast.StructType:
return t
case *ast.Ident:
if t.Obj == nil {
return nil
}
if t.Obj.Decl == nil {
return nil
}
decl := t.Obj.Decl
switch d := decl.(type) {
case *ast.TypeSpec:
return h.GetStructType(d.Type)
}
case *ast.StarExpr:
return h.GetStructType(t.X)
}
return nil
}
// GetTypeSpec will return the given type spec for an expression. nil will
// be returned if one could not be found
func (h Helper) GetTypeSpec(expr ast.Expr) *ast.TypeSpec {
switch t := expr.(type) {
case *ast.SelectorExpr:
ident, ok := t.X.(*ast.Ident)
if !ok {
return nil
}
file, ok := h.PackagesCache.CurrentFile()
if !ok {
return nil
}
pkgImportPath := file.Imports[ident.Name]
pkg, ok := h.PackagesCache.Packages.Get(pkgImportPath)
if !ok {
return nil
}
info, ok := pkg.Files.GetTypeInfo(t.Sel.Name)
if !ok {
return nil
}
return info.Spec
case *ast.Ident:
if t.Obj == nil {
return nil
}
if t.Obj.Decl == nil {
return nil
}
decl := t.Obj.Decl
switch d := decl.(type) {
case *ast.TypeSpec:
return d
}
case *ast.StarExpr:
return h.GetTypeSpec(t.X)
}
return nil
}
// GetStructName will return a struct from the given expr.
func (h Helper) GetStructName(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.SelectorExpr:
ident, ok := t.X.(*ast.Ident)
if !ok {
return ""
}
pkg, ok := h.PackagesCache.Packages.Get(ident.Name)
if !ok {
return ""
}
info, ok := pkg.Files.GetTypeInfo(t.Sel.Name)
if !ok || info.Spec == nil {
return ""
}
return info.Spec.Name.Name
case *ast.Ident:
if t.Obj == nil {
return ""
}
if t.Obj.Decl == nil {
return ""
}
decl := t.Obj.Decl
switch d := decl.(type) {
case *ast.TypeSpec:
return d.Name.Name
}
case *ast.StarExpr:
return h.GetStructName(t.X)
}
return ""
}
// IsMethod will return whether or not something is a method.
func IsMethod(expr ast.Decl) bool {
switch t := expr.(type) {
case *ast.FuncDecl:
if t.Recv != nil {
return true
}
}
return false
}
// GetOpFromType will iterate and find the field of name 'name'. Once that
// field has been found, it will return the call expr. If that field is not found,
// nil will be returned
func GetOpFromType(spec *ast.TypeSpec, name string) *ast.CallExpr {
switch t := spec.Type.(type) {
case *ast.StructType:
for _, field := range t.Fields.List {
if field.Type == nil {
continue
}
callExpr, ok := field.Type.(*ast.CallExpr)
if !ok {
continue
}
for _, n := range field.Names {
if name == n.Name {
return callExpr
}
}
}
default:
log.Printf("TODO: GetOpFromType %T", t)
}
return nil
}
// GetFieldByName will retrieve the ast.Field off of an ast.TypeSpec and
// field name.
func (h Helper) GetFieldByName(fieldName string, spec *ast.TypeSpec) *ast.Field {
if !h.IsStruct(spec.Type) {
return nil
}
sType := h.GetStructType(spec.Type)
for _, depField := range sType.Fields.List {
for _, name := range depField.Names {
if name.Name == fieldName {
return depField
}
}
}
return nil
}
// GetFieldByIndex will retrieve the ast.Field off of an ast.TypeSpec and
// field index.
func (h Helper) GetFieldByIndex(index int, spec *ast.TypeSpec) *ast.Field {
if !h.IsStruct(spec.Type) {
return nil
}
sType := h.GetStructType(spec.Type)
if index < 0 || index >= len(sType.Fields.List) {
return nil
}
return sType.Fields.List[index]
}
// GetImportPathFromFullPath will return the import path from the given full path
func GetImportPathFromFullPath(path string) string {
gopath := filepath.Join(os.Getenv("GOPATH"), "src")
gopath += "/"
// strip of the gopath
// TODO: Should eventually iterate through multiple gopaths if provided
// ie, GOPATH=foo/bar:bar/baz
return getImportPathFromFullPath([]string{gopath}, path)
}
func getImportPathFromFullPath(prefixes []string, path string) string {
for _, prefix := range prefixes {
if !strings.HasPrefix(path, prefix) {
continue
}
return path[len(prefix):]
}
return path
}
// GetPackageNameFromImportPath will return the package name from the import path
func GetPackageNameFromImportPath(importPath string) string {
return filepath.Base(importPath)
}