-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_body.go
219 lines (184 loc) · 5.2 KB
/
func_body.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
package gorefactor
import (
"github.com/dave/dst"
"github.com/dave/dst/dstutil"
)
// HasStmtInsideFuncBody checks if the body of function has given statement
func HasStmtInsideFuncBody(df *dst.File, funcName string, stmt dst.Stmt) (ret bool) {
var inside bool
pre := func(c *dstutil.Cursor) bool {
node := c.Node()
switch node.(type) {
case *dst.FuncDecl:
if nn := node.(*dst.FuncDecl); nn.Name.Name == funcName {
inside = true
}
case dst.Stmt:
if inside && nodesEqual(node, stmt) {
ret = true
}
}
return true
}
post := func(c *dstutil.Cursor) bool {
node := c.Node()
switch node.(type) {
case *dst.FuncDecl:
if nn := node.(*dst.FuncDecl); nn.Name.Name == funcName && inside {
inside = false
}
}
return true
}
dstutil.Apply(df, pre, post)
return
}
// DeleteStmtFromFuncBody deletes any statement, inside the body of function,
// that is semantically equal to the given statement.
func DeleteStmtFromFuncBody(df *dst.File, funcName string, stmt dst.Stmt) (modified bool) {
var inside bool
pre := func(c *dstutil.Cursor) bool {
node := c.Node()
switch node.(type) {
case *dst.FuncDecl:
if nn := node.(*dst.FuncDecl); nn.Name.Name == funcName {
inside = true
}
case dst.Stmt:
if inside && nodesEqual(node, stmt) {
c.Delete()
modified = true
}
}
return true
}
post := func(c *dstutil.Cursor) bool {
node := c.Node()
switch node.(type) {
case *dst.FuncDecl:
if nn := node.(*dst.FuncDecl); nn.Name.Name == funcName && inside {
inside = false
}
}
return true
}
dstutil.Apply(df, pre, post)
return
}
// DeleteCallExprFromFuncBody deletes any SelectorExpr equal to the given one, inside the body of function.
func DeleteSelectorExprFromFuncBody(df *dst.File, funcName string, selectorExpr dst.Expr) (modified bool) {
var inside bool
var found bool
pre := func(c *dstutil.Cursor) bool {
node := c.Node()
switch node.(type) {
case *dst.FuncDecl:
if nn := node.(*dst.FuncDecl); nn.Name.Name == funcName {
inside = true
}
case *dst.SelectorExpr:
nn := node.(*dst.SelectorExpr)
if inside && nodesEqual(nn, selectorExpr) {
found = true
}
}
return true
}
post := func(c *dstutil.Cursor) bool {
node := c.Node()
switch node.(type) {
case *dst.FuncDecl:
if nn := node.(*dst.FuncDecl); nn.Name.Name == funcName && inside {
inside = false
}
case *dst.ExprStmt:
if found {
found, modified = false, true
c.Delete()
}
}
return true
}
dstutil.Apply(df, pre, post)
return
}
// AddStmtToFuncBody adds given statement, to the body of function, in the given position
func AddStmtToFuncBody(df *dst.File, funcName string, stmt dst.Stmt, pos int) (modified bool) {
pre := func(c *dstutil.Cursor) bool {
node := c.Node()
switch node.(type) {
case *dst.FuncDecl:
if nn := node.(*dst.FuncDecl); nn.Name.Name == funcName {
stmtList := nn.Body.List
pos = normalizePos(pos, len(stmtList))
nn.Body.List = append(
stmtList[:pos],
append([]dst.Stmt{dst.Clone(stmt).(dst.Stmt)}, stmtList[pos:]...)...)
modified = true
return false
}
}
return true
}
dstutil.Apply(df, pre, nil)
return
}
// AddStmtToFuncBodyStart adds given statement, to the start of function body
func AddStmtToFuncBodyStart(df *dst.File, funcName string, stmt dst.Stmt) (modified bool) {
return AddStmtToFuncBody(df, funcName, stmt, 0)
}
// AddStmtToFuncBodyEnd adds given statement, to the end of function body
func AddStmtToFuncBodyEnd(df *dst.File, funcName string, stmt dst.Stmt) (modified bool) {
return AddStmtToFuncBody(df, funcName, stmt, -1)
}
const (
relativeDirectionBefore = iota
relativeDirectionAfter
)
func addStmtToFuncBodyRelativeTo(df *dst.File, funcName string, stmt, refStmt dst.Stmt, relDirection int) (modified bool) {
var inside bool
pre := func(c *dstutil.Cursor) bool {
node := c.Node()
switch node.(type) {
case *dst.FuncDecl:
if nn := node.(*dst.FuncDecl); nn.Name.Name == funcName {
inside = true
}
case dst.Stmt:
ss := node.(dst.Stmt)
if inside && nodesEqual(ss, refStmt) {
switch relDirection {
case relativeDirectionBefore:
c.InsertBefore(dst.Clone(stmt))
modified = true
case relativeDirectionAfter:
c.InsertAfter(dst.Clone(stmt))
modified = true
}
}
}
return true
}
post := func(c *dstutil.Cursor) bool {
node := c.Node()
switch node.(type) {
case *dst.FuncDecl:
if nn := node.(*dst.FuncDecl); nn.Name.Name == funcName && inside {
inside = false
}
}
return true
}
dstutil.Apply(df, pre, post)
return
}
// AddStmtToFuncBodyBefore adds given statement, to the function body, before the position of refStmt.
// if refStmt not found, nothing will happen
func AddStmtToFuncBodyBefore(df *dst.File, funcName string, stmt, refStmt dst.Stmt) (modified bool) {
return addStmtToFuncBodyRelativeTo(df, funcName, stmt, refStmt, relativeDirectionBefore)
}
// AddStmtToFuncBodyAfter adds given statement, to the function body, after the position of refStmt,
// if refStmt not found, nothing will happen
func AddStmtToFuncBodyAfter(df *dst.File, funcName string, stmt, refStmt dst.Stmt) (modified bool) {
return addStmtToFuncBodyRelativeTo(df, funcName, stmt, refStmt, relativeDirectionAfter)
}