-
Notifications
You must be signed in to change notification settings - Fork 5
/
handler.go
241 lines (207 loc) · 5.61 KB
/
handler.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
package gomegahandler
import (
"go/ast"
"go/token"
)
const (
importPath = `"github.com/onsi/gomega"`
)
// Handler provide different handling, depend on the way gomega was imported, whether
// in imported with "." name, custom name or without any name.
type Handler interface {
// GetActualFuncName returns the name of the gomega function, e.g. `Expect`
GetActualFuncName(*ast.CallExpr) (string, bool)
// ReplaceFunction replaces the function with another one, for fix suggestions
ReplaceFunction(*ast.CallExpr, *ast.Ident)
getDefFuncName(expr *ast.CallExpr) string
getFieldType(field *ast.Field) string
GetActualExpr(assertionFunc *ast.SelectorExpr) *ast.CallExpr
}
// GetGomegaHandler returns a gomegar handler according to the way gomega was imported in the specific file
func GetGomegaHandler(file *ast.File) Handler {
for _, imp := range file.Imports {
if imp.Path.Value != importPath {
continue
}
switch name := imp.Name.String(); {
case name == ".":
return dotHandler{}
case name == "<nil>": // import with no local name
return nameHandler("gomega")
default:
return nameHandler(name)
}
}
return nil // no gomega import; this file does not use gomega
}
// dotHandler is used when importing gomega with dot; i.e.
// import . "github.com/onsi/gomega"
type dotHandler struct{}
// GetActualFuncName returns the name of the gomega function, e.g. `Expect`
func (h dotHandler) GetActualFuncName(expr *ast.CallExpr) (string, bool) {
switch actualFunc := expr.Fun.(type) {
case *ast.Ident:
return actualFunc.Name, true
case *ast.SelectorExpr:
if isGomegaVar(actualFunc.X, h) {
return actualFunc.Sel.Name, true
}
if x, ok := actualFunc.X.(*ast.CallExpr); ok {
return h.GetActualFuncName(x)
}
case *ast.CallExpr:
return h.GetActualFuncName(actualFunc)
}
return "", false
}
// ReplaceFunction replaces the function with another one, for fix suggestions
func (dotHandler) ReplaceFunction(caller *ast.CallExpr, newExpr *ast.Ident) {
switch f := caller.Fun.(type) {
case *ast.Ident:
caller.Fun = newExpr
case *ast.SelectorExpr:
f.Sel = newExpr
}
}
func (dotHandler) getDefFuncName(expr *ast.CallExpr) string {
if f, ok := expr.Fun.(*ast.Ident); ok {
return f.Name
}
return ""
}
func (dotHandler) getFieldType(field *ast.Field) string {
switch t := field.Type.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
if name, ok := t.X.(*ast.Ident); ok {
return name.Name
}
}
return ""
}
// nameHandler is used when importing gomega without name; i.e.
// import "github.com/onsi/gomega"
//
// or with a custom name; e.g.
// import customname "github.com/onsi/gomega"
type nameHandler string
// GetActualFuncName returns the name of the gomega function, e.g. `Expect`
func (g nameHandler) GetActualFuncName(expr *ast.CallExpr) (string, bool) {
selector, ok := expr.Fun.(*ast.SelectorExpr)
if !ok {
return "", false
}
switch x := selector.X.(type) {
case *ast.Ident:
if x.Name != string(g) {
if !isGomegaVar(x, g) {
return "", false
}
}
return selector.Sel.Name, true
case *ast.CallExpr:
return g.GetActualFuncName(x)
}
return "", false
}
// ReplaceFunction replaces the function with another one, for fix suggestions
func (nameHandler) ReplaceFunction(caller *ast.CallExpr, newExpr *ast.Ident) {
caller.Fun.(*ast.SelectorExpr).Sel = newExpr
}
func (g nameHandler) getDefFuncName(expr *ast.CallExpr) string {
if sel, ok := expr.Fun.(*ast.SelectorExpr); ok {
if f, ok := sel.X.(*ast.Ident); ok && f.Name == string(g) {
return sel.Sel.Name
}
}
return ""
}
func (g nameHandler) getFieldType(field *ast.Field) string {
switch t := field.Type.(type) {
case *ast.SelectorExpr:
if id, ok := t.X.(*ast.Ident); ok {
if id.Name == string(g) {
return t.Sel.Name
}
}
case *ast.StarExpr:
if sel, ok := t.X.(*ast.SelectorExpr); ok {
if x, ok := sel.X.(*ast.Ident); ok && x.Name == string(g) {
return sel.Sel.Name
}
}
}
return ""
}
func isGomegaVar(x ast.Expr, handler Handler) bool {
if i, ok := x.(*ast.Ident); ok {
if i.Obj != nil && i.Obj.Kind == ast.Var {
switch decl := i.Obj.Decl.(type) {
case *ast.AssignStmt:
if decl.Tok == token.DEFINE {
if defFunc, ok := decl.Rhs[0].(*ast.CallExpr); ok {
fName := handler.getDefFuncName(defFunc)
switch fName {
case "NewGomega", "NewWithT", "NewGomegaWithT":
return true
}
}
}
case *ast.Field:
name := handler.getFieldType(decl)
switch name {
case "Gomega", "WithT", "GomegaWithT":
return true
}
}
}
}
return false
}
func (h dotHandler) GetActualExpr(assertionFunc *ast.SelectorExpr) *ast.CallExpr {
actualExpr, ok := assertionFunc.X.(*ast.CallExpr)
if !ok {
return nil
}
switch fun := actualExpr.Fun.(type) {
case *ast.Ident:
return actualExpr
case *ast.SelectorExpr:
if isHelperMethods(fun.Sel.Name) {
return h.GetActualExpr(fun)
}
if isGomegaVar(fun.X, h) {
return actualExpr
}
}
return nil
}
func (g nameHandler) GetActualExpr(assertionFunc *ast.SelectorExpr) *ast.CallExpr {
actualExpr, ok := assertionFunc.X.(*ast.CallExpr)
if !ok {
return nil
}
switch fun := actualExpr.Fun.(type) {
case *ast.Ident:
return actualExpr
case *ast.SelectorExpr:
if x, ok := fun.X.(*ast.Ident); ok && x.Name == string(g) {
return actualExpr
}
if isHelperMethods(fun.Sel.Name) {
return g.GetActualExpr(fun)
}
if isGomegaVar(fun.X, g) {
return actualExpr
}
}
return nil
}
func isHelperMethods(funcName string) bool {
switch funcName {
case "WithOffset", "WithTimeout", "WithPolling", "Within", "ProbeEvery", "WithContext", "WithArguments", "MustPassRepeatedly":
return true
}
return false
}