-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathresolveProxyCalls.js
52 lines (51 loc) · 1.52 KB
/
resolveProxyCalls.js
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
/**
* Remove redundant call expressions which only pass the arguments to other call expression.
* E.g.
* function call1(a, b) {
* return a + b;
* }
* function call2(c, d) {
* return call1(c, d); // will be changed to call1(c, d);
* }
* function call3(e, f) {
* return call2(e, f); // will be changed to call1(e, f);
* }
* const three = call3(1, 2); // will be changed to call1(1, 2);
* @param {Arborist} arb
* @param {Function} candidateFilter (optional) a filter to apply on the candidates list
* @return {Arborist}
*/
function resolveProxyCalls(arb, candidateFilter = () => true) {
const relevantNodes = [
...(arb.ast[0].typeMap.FunctionDeclaration || []),
];
for (let i = 0; i < relevantNodes.length; i++) {
const n = relevantNodes[i];
if (n?.body?.body?.[0]?.type === 'ReturnStatement' &&
n.body.body[0].argument?.type === 'CallExpression' &&
n.body.body[0].argument.arguments?.length === n.params?.length &&
n.body.body[0].argument.callee.type === 'Identifier' &&
candidateFilter(n)) {
const funcName = n.id;
const ret = n.body.body[0].argument;
let transitiveArguments = true;
try {
for (let j = 0; j < n.params.length; j++) {
if (n.params[j]?.name !== ret?.arguments[j]?.name) {
transitiveArguments = false;
break;
}
}
} catch {
transitiveArguments = false;
}
if (transitiveArguments) {
for (const ref of funcName.references || []) {
arb.markNode(ref, ret.callee);
}
}
}
}
return arb;
}
export default resolveProxyCalls;