index.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = optimiseCallExpression;
  6. var _t = require("@babel/types");
  7. const {
  8. callExpression,
  9. identifier,
  10. isIdentifier,
  11. isSpreadElement,
  12. memberExpression,
  13. optionalCallExpression,
  14. optionalMemberExpression
  15. } = _t;
  16. function optimiseCallExpression(callee, thisNode, args, optional) {
  17. if (args.length === 1 && isSpreadElement(args[0]) && isIdentifier(args[0].argument, {
  18. name: "arguments"
  19. })) {
  20. if (optional) {
  21. return optionalCallExpression(optionalMemberExpression(callee, identifier("apply"), false, true), [thisNode, args[0].argument], false);
  22. }
  23. return callExpression(memberExpression(callee, identifier("apply")), [thisNode, args[0].argument]);
  24. } else {
  25. if (optional) {
  26. return optionalCallExpression(optionalMemberExpression(callee, identifier("call"), false, true), [thisNode, ...args], false);
  27. }
  28. return callExpression(memberExpression(callee, identifier("call")), [thisNode, ...args]);
  29. }
  30. }