index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  7. var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers");
  8. var _core = require("@babel/core");
  9. var _default = (0, _helperPluginUtils.declare)((api, options) => {
  10. var _api$assumption, _options$allowArrayLi;
  11. api.assertVersion(7);
  12. const iterableIsArray = (_api$assumption = api.assumption("iterableIsArray")) != null ? _api$assumption : options.loose;
  13. const arrayLikeIsIterable = (_options$allowArrayLi = options.allowArrayLike) != null ? _options$allowArrayLi : api.assumption("arrayLikeIsIterable");
  14. function getSpreadLiteral(spread, scope) {
  15. if (iterableIsArray && !_core.types.isIdentifier(spread.argument, {
  16. name: "arguments"
  17. })) {
  18. return spread.argument;
  19. } else {
  20. return scope.toArray(spread.argument, true, arrayLikeIsIterable);
  21. }
  22. }
  23. function hasSpread(nodes) {
  24. for (let i = 0; i < nodes.length; i++) {
  25. if (_core.types.isSpreadElement(nodes[i])) {
  26. return true;
  27. }
  28. }
  29. return false;
  30. }
  31. function push(_props, nodes) {
  32. if (!_props.length) return _props;
  33. nodes.push(_core.types.arrayExpression(_props));
  34. return [];
  35. }
  36. function build(props, scope) {
  37. const nodes = [];
  38. let _props = [];
  39. for (const prop of props) {
  40. if (_core.types.isSpreadElement(prop)) {
  41. _props = push(_props, nodes);
  42. nodes.push(getSpreadLiteral(prop, scope));
  43. } else {
  44. _props.push(prop);
  45. }
  46. }
  47. push(_props, nodes);
  48. return nodes;
  49. }
  50. return {
  51. name: "transform-spread",
  52. visitor: {
  53. ArrayExpression(path) {
  54. const {
  55. node,
  56. scope
  57. } = path;
  58. const elements = node.elements;
  59. if (!hasSpread(elements)) return;
  60. const nodes = build(elements, scope);
  61. let first = nodes[0];
  62. if (nodes.length === 1 && first !== elements[0].argument) {
  63. path.replaceWith(first);
  64. return;
  65. }
  66. if (!_core.types.isArrayExpression(first)) {
  67. first = _core.types.arrayExpression([]);
  68. } else {
  69. nodes.shift();
  70. }
  71. path.replaceWith(_core.types.callExpression(_core.types.memberExpression(first, _core.types.identifier("concat")), nodes));
  72. },
  73. CallExpression(path) {
  74. const {
  75. node,
  76. scope
  77. } = path;
  78. const args = node.arguments;
  79. if (!hasSpread(args)) return;
  80. const calleePath = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("callee"));
  81. if (calleePath.isSuper()) {
  82. throw path.buildCodeFrameError("It's not possible to compile spread arguments in `super()` without compiling classes.\n" + "Please add '@babel/plugin-transform-classes' to your Babel configuration.");
  83. }
  84. let contextLiteral = scope.buildUndefinedNode();
  85. node.arguments = [];
  86. let nodes;
  87. if (args.length === 1 && args[0].argument.name === "arguments") {
  88. nodes = [args[0].argument];
  89. } else {
  90. nodes = build(args, scope);
  91. }
  92. const first = nodes.shift();
  93. if (nodes.length) {
  94. node.arguments.push(_core.types.callExpression(_core.types.memberExpression(first, _core.types.identifier("concat")), nodes));
  95. } else {
  96. node.arguments.push(first);
  97. }
  98. const callee = calleePath.node;
  99. if (calleePath.isMemberExpression()) {
  100. const temp = scope.maybeGenerateMemoised(callee.object);
  101. if (temp) {
  102. callee.object = _core.types.assignmentExpression("=", temp, callee.object);
  103. contextLiteral = temp;
  104. } else {
  105. contextLiteral = _core.types.cloneNode(callee.object);
  106. }
  107. }
  108. node.callee = _core.types.memberExpression(node.callee, _core.types.identifier("apply"));
  109. if (_core.types.isSuper(contextLiteral)) {
  110. contextLiteral = _core.types.thisExpression();
  111. }
  112. node.arguments.unshift(_core.types.cloneNode(contextLiteral));
  113. },
  114. NewExpression(path) {
  115. const {
  116. node,
  117. scope
  118. } = path;
  119. let args = node.arguments;
  120. if (!hasSpread(args)) return;
  121. const nodes = build(args, scope);
  122. const first = nodes.shift();
  123. if (nodes.length) {
  124. args = _core.types.callExpression(_core.types.memberExpression(first, _core.types.identifier("concat")), nodes);
  125. } else {
  126. args = first;
  127. }
  128. path.replaceWith(_core.types.callExpression(path.hub.addHelper("construct"), [node.callee, args]));
  129. }
  130. }
  131. };
  132. });
  133. exports.default = _default;