index.js (babel-7.12.6) | : | index.js (babel-7.12.7) | ||
---|---|---|---|---|
import * as t from "@babel/types"; | import * as t from "@babel/types"; | |||
export default function (callee, thisNode, args, optional) { | /** | |||
* 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 exp | ||||
ression | ||||
*/ | ||||
export default function ( | ||||
callee: Node, | ||||
thisNode: Node, | ||||
args: Node[], | ||||
optional: boolean, | ||||
): CallExpression | OptionalCallExpression { | ||||
if ( | if ( | |||
args.length === 1 && | args.length === 1 && | |||
t.isSpreadElement(args[0]) && | t.isSpreadElement(args[0]) && | |||
t.isIdentifier(args[0].argument, { name: "arguments" }) | t.isIdentifier(args[0].argument, { name: "arguments" }) | |||
) { | ) { | |||
// eg. super(...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")), [ | return t.callExpression(t.memberExpression(callee, t.identifier("apply")), [ | |||
thisNode, | thisNode, | |||
args[0].argument, | args[0].argument, | |||
]); | ]); | |||
} else { | } else { | |||
// a.b?.(arg1, arg2) | ||||
if (optional) { | if (optional) { | |||
return t.optionalCallExpression( | return t.optionalCallExpression( | |||
t.optionalMemberExpression(callee, t.identifier("call"), false, true), | t.optionalMemberExpression(callee, t.identifier("call"), false, true), | |||
[thisNode, ...args], | [thisNode, ...args], | |||
false, | false, | |||
); | ); | |||
} | } | |||
// a.b(arg1, arg2) | ||||
return t.callExpression(t.memberExpression(callee, t.identifier("call")), [ | return t.callExpression(t.memberExpression(callee, t.identifier("call")), [ | |||
thisNode, | thisNode, | |||
...args, | ...args, | |||
]); | ]); | |||
} | } | |||
} | } | |||
End of changes. 4 change blocks. | ||||
2 lines changed or deleted | 29 lines changed or added |