-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_rewriter.go
198 lines (185 loc) · 5.05 KB
/
error_rewriter.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
package main
import (
"go/ast"
"go/parser"
"go/printer"
"go/token"
"log"
"os"
)
func main() {
if len(os.Args) != 2 {
log.Fatal("Need the file and only the file to be parsed as a command line argument")
}
fileName := os.Args[1]
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, fileName, nil, parser.ParseComments)
if err != nil {
log.Fatal(err)
}
parseErrorExpr(file)
f, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
log.Fatal(err)
}
err = printer.Fprint(f, fset, file)
if err != nil {
log.Fatal(err)
}
if err = f.Close(); err != nil {
log.Fatal(err)
}
}
// checks that n represents an if statement with a t.Errorf call within
func checkErrorSignature(n ast.Node) bool {
if ifstmt, ok := n.(*ast.IfStmt); ok {
if expr, ok1 := ifstmt.Body.List[0].(*ast.ExprStmt); ok1 && len(ifstmt.Body.List) == 1 {
if call, ok2 := expr.X.(*ast.CallExpr); ok2 {
if selector, ok3 := call.Fun.(*ast.SelectorExpr); ok3 {
if id, ok4 := selector.X.(*ast.Ident); ok4 {
if id.Name == "t" && selector.Sel.Name == "Errorf" {
return true
}
}
}
}
}
}
return false
}
// gets the signature for the new require function call
func getNewErrorSignature(errorSignature *ast.CallExpr, funcName string, newArgs []ast.Expr) *ast.ExprStmt {
// get rid of old position for old args (only for BasicLit, Ident, and SelectorExpr cases for now)
for _, arg := range errorSignature.Args {
if basicLit, ok := arg.(*ast.BasicLit); ok {
basicLit.ValuePos = token.NoPos
} else if ident, ok := arg.(*ast.Ident); ok {
ident.NamePos = token.NoPos
} else if selectorExpr, ok := arg.(*ast.SelectorExpr); ok {
if x, ok1 := selectorExpr.X.(*ast.Ident); ok1 {
x.NamePos = token.NoPos
selectorExpr.Sel.NamePos = token.NoPos
}
}
}
extraArgs := append(newArgs, errorSignature.Args...)
newFunc := &ast.ExprStmt{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{
Name: "assert",
},
Sel: &ast.Ident{
Name: funcName,
},
},
Args: extraArgs,
Ellipsis: errorSignature.Ellipsis,
},
}
return newFunc
}
func parseErrorExpr(file *ast.File) {
ast.Inspect(file, func(n ast.Node) bool {
if block, ok := n.(*ast.BlockStmt); ok {
for i, stmt := range block.List {
if checkErrorSignature(stmt) {
errorSignature := stmt.(*ast.IfStmt).Body.List[0].(*ast.ExprStmt).X.(*ast.CallExpr)
// ignore if statements with initializations
if stmt.(*ast.IfStmt).Init == nil {
if cond, ok1 := stmt.(*ast.IfStmt).Cond.(*ast.BinaryExpr); ok1 {
x, ok2 := cond.X.(*ast.Ident)
y, ok3 := cond.Y.(*ast.Ident)
if ok3 {
if ok2 {
// require.NoError
if x.Name == "err" && cond.Op == token.NEQ && y.Name == "nil" {
newArgs := []ast.Expr{
&ast.Ident{
Name: "t",
},
&ast.Ident{
Name: "err",
},
}
block.List[i] = getNewErrorSignature(errorSignature, "NoError", newArgs)
continue
}
// require.Error
if x.Name == "err" && cond.Op == token.EQL && y.Name == "nil" {
newArgs := []ast.Expr{
&ast.Ident{
Name: "t",
},
&ast.Ident{
Name: "err",
},
}
block.List[i] = getNewErrorSignature(errorSignature, "Error", newArgs)
continue
}
}
// require.Nil
if cond.Op == token.NEQ && y.Name == "nil" {
newArgs := []ast.Expr{
&ast.Ident{
Name: "t",
},
cond.X,
}
block.List[i] = getNewErrorSignature(errorSignature, "Nil", newArgs)
continue
}
// require.NotNil
if cond.Op == token.EQL && y.Name == "nil" {
newArgs := []ast.Expr{
&ast.Ident{
Name: "t",
},
cond.X,
}
block.List[i] = getNewErrorSignature(errorSignature, "NotNil", newArgs)
continue
}
}
newArgs := []ast.Expr{
&ast.Ident{
Name: "t",
},
cond.Y,
cond.X,
}
// require.Equal and require.NotEqual
if cond.Op == token.NEQ {
block.List[i] = getNewErrorSignature(errorSignature, "Equal", newArgs)
} else if cond.Op == token.EQL {
block.List[i] = getNewErrorSignature(errorSignature, "NotEqual", newArgs)
}
} else if cond, ok1 := stmt.(*ast.IfStmt).Cond.(*ast.UnaryExpr); ok1 {
newArgs := []ast.Expr{
&ast.Ident{
Name: "t",
},
cond.X,
}
// require.True
if cond.Op == token.NOT {
block.List[i] = getNewErrorSignature(errorSignature, "True", newArgs)
}
} else if cond, ok1 := stmt.(*ast.IfStmt).Cond.(*ast.Ident); ok1 {
newArgs := []ast.Expr{
&ast.Ident{
Name: "t",
},
cond,
}
// require.False
block.List[i] = getNewErrorSignature(errorSignature, "False", newArgs)
}
}
}
}
}
return true
})
}