forked from babel/babel
/
index.js
53 lines (52 loc) 路 1.54 KB
/
index.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
53
import * as t from "@babel/types";
/**
* A helper function that generates a new call expression with given thisNode.
It will also optimize `(...arguments)` to `.apply(arguments)`
*
* @export
* @param {Node} callee The callee of call expression
* @param {Node} thisNode The desired this of call expression
* @param {Node[]} args The arguments of call expression
* @param {boolean} optional Whether the call expression is optional
* @returns {CallExpression | OptionalCallExpression} The generated new call expression
*/
export default function (
callee: Node,
thisNode: Node,
args: Node[],
optional: boolean,
): CallExpression | OptionalCallExpression {
if (
args.length === 1 &&
t.isSpreadElement(args[0]) &&
t.isIdentifier(args[0].argument, { name: "arguments" })
) {
// a.b?.(...arguments);
if (optional) {
return t.optionalCallExpression(
t.optionalMemberExpression(callee, t.identifier("apply"), false, true),
[thisNode, args[0].argument],
false,
);
}
// a.b(...arguments);
return t.callExpression(t.memberExpression(callee, t.identifier("apply")), [
thisNode,
args[0].argument,
]);
} else {
// a.b?.(arg1, arg2)
if (optional) {
return t.optionalCallExpression(
t.optionalMemberExpression(callee, t.identifier("call"), false, true),
[thisNode, ...args],
false,
);
}
// a.b(arg1, arg2)
return t.callExpression(t.memberExpression(callee, t.identifier("call")), [
thisNode,
...args,
]);
}
}