-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtransform-rename-functions.ts
236 lines (215 loc) · 6.71 KB
/
transform-rename-functions.ts
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
/**
* Rename some function calls but keep the params
* Currently it will find and replace once only.
* You may need to run many times until it replace all the function.
*/
import {
FileInfo,
API,
Options,
ASTPath,
CallExpression,
ExpressionStatement,
} from "jscodeshift";
import * as recast from "recast";
import {
convertAllCallExpressionToAsync,
findParentFunction,
setFunctionAsync,
} from "./utils";
type MethodsMappingItemType = {
from: string;
to: string;
};
const methodsMapping: MethodsMappingItemType[] = [
{
from: "Factory.create()",
to: "await Factory.createAsync()",
},
// {
// from: "build()",
// to: "await buildAsync()",
// },
// {
// from: "create()",
// to: "await Factory.createAsync()",
// },
];
const debug = require("debug")("transform:script-rename");
const debug2 = require("debug")("transform:print:script-rename");
module.exports = function (fileInfo: FileInfo, { j }: API, options: Options) {
debug(
`**************************************************
*** ${fileInfo.path}
**************************************************`
);
let fileChanged = false;
const rootCollection = j(fileInfo.source);
// debug(rootCollection)
const MAX_REPLACEMENT = 1;
let replaceCounter = 0;
const getExpressionStatementFromString = (
source: string
): ASTPath<ExpressionStatement> | undefined => {
let sourceNode: ASTPath<ExpressionStatement> | undefined = undefined;
try {
const n = recast.parse(source).program.body[0];
debug("***recast***", source, n);
debug(j(n));
} catch (e) {
// debug("parse error", e);
}
j(source)
.find(j.ExpressionStatement)
.at(0)
.map((item) => {
sourceNode = item;
return null;
});
// debug("sourceNode", sourceNode);
return sourceNode;
};
const replaceFunction = (
p: ASTPath<CallExpression>,
byNode: ASTPath<ExpressionStatement>,
args: CallExpression["arguments"]
) => {
if (MAX_REPLACEMENT && replaceCounter >= MAX_REPLACEMENT) {
return;
}
// debug("+++replace", j(p).toSource(), j(args).toSource());
// debug("+++byNode before modified args", byNode, byNode.value.loc?.start);
j(byNode)
.find(j.CallExpression)
.at(0)
.map((tp) => {
// debug("+++tp", tp);
tp.value.arguments = args;
return null;
});
// debug("+++modified byNode arguments", byNode, j(byNode).toSource());
// now update the code
let replaceThisPath;
if (p.value.type === "CallExpression") {
replaceThisPath = p;
} else {
replaceThisPath = p.parentPath;
}
debug(
"\n+++replace this",
j(replaceThisPath).toSource(),
p.value.loc?.start,
p.value.loc?.end
);
debug("+++by this", j(byNode.value).toSource());
j(replaceThisPath).replaceWith(byNode.value);
fileChanged = true;
// increase the counter
replaceCounter += 1;
// check if byNode is await expression
if (byNode.value.expression.type === "AwaitExpression") {
// find the parent function
const parentFunctionPath = findParentFunction(replaceThisPath);
if (parentFunctionPath) {
if (setFunctionAsync(parentFunctionPath, j)) {
fileChanged = true;
}
// then find all functions which use this async function
switch (parentFunctionPath.value.type) {
case "FunctionDeclaration": {
debug("+++the parent", parentFunctionPath.value?.id?.loc?.start);
if (parentFunctionPath.value.id?.type === "Identifier") {
if (
convertAllCallExpressionToAsync(
parentFunctionPath.value.id?.name,
rootCollection,
j
)
) {
fileChanged = true;
}
}
break;
}
default:
debug(
"++++Unhandled parent function type:",
parentFunctionPath.value.type
);
}
}
}
};
// walk through methods mapping
methodsMapping.map(({ from, to }) => {
debug(`======= ${from} ==> ${to} =======`);
const fromNode = getExpressionStatementFromString(from);
const toNode = getExpressionStatementFromString(to);
// debug("fromNode value expression", fromNode?.value.expression);
// debug("toNode value expression", toNode?.value.expression);
if (!fromNode || !toNode) {
return null;
}
// find all the current expressions which match from code
switch (fromNode.value.expression.type) {
case "CallExpression": {
// get the callee
switch (fromNode.value.expression.callee.type) {
case "Identifier": {
// find all call expression with callee type is Identifier
const calleeName = fromNode.value.expression.callee.name;
// debug("+fromNode.value.expression.callee.name", calleeName);
rootCollection.find(j.CallExpression).map((p) => {
if (
p.value.callee?.type === "Identifier" &&
calleeName === p.value.callee.name
) {
// debug("++found", p);
replaceFunction(p, toNode, p.value.arguments);
}
return null;
});
break;
}
case "MemberExpression": {
const { object, property } = fromNode.value.expression.callee;
// debug("+callee", object, property);
// find all member expression
rootCollection.find(j.MemberExpression).map((p) => {
if (
p.value.object.type === "Identifier" &&
p.value.property.type === "Identifier" &&
p.value.object.type === object.type &&
p.value.property.type === property.type &&
p.value.object.name === object.name &&
p.value.property.name === property.name
) {
// debug("++found 2", p.parentPath);
replaceFunction(
p.parentPath,
toNode,
p.parentPath.value.arguments
);
}
return null;
});
break;
}
default:
debug(
`+Unhandled callee type: ${fromNode.value.expression.callee.type}`
);
}
break;
}
default:
debug(`Unhandled expression type: ${fromNode.value.expression.type}`);
}
debug(`==END== ${from} ==> ${to} ==END==`);
});
debug("**************************************************");
if (fileChanged) {
debug2("file changed:", fileInfo.path);
return rootCollection.toSource();
}
};