index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 hasHole(spread) {
  24. return spread.elements.some(el => el === null);
  25. }
  26. function hasSpread(nodes) {
  27. for (let i = 0; i < nodes.length; i++) {
  28. if (_core.types.isSpreadElement(nodes[i])) {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. function push(_props, nodes) {
  35. if (!_props.length) return _props;
  36. nodes.push(_core.types.arrayExpression(_props));
  37. return [];
  38. }
  39. function build(props, scope, file) {
  40. const nodes = [];
  41. let _props = [];
  42. for (const prop of props) {
  43. if (_core.types.isSpreadElement(prop)) {
  44. _props = push(_props, nodes);
  45. let spreadLiteral = getSpreadLiteral(prop, scope);
  46. if (_core.types.isArrayExpression(spreadLiteral) && hasHole(spreadLiteral)) {
  47. spreadLiteral = _core.types.callExpression(file.addHelper("arrayWithoutHoles"), [spreadLiteral]);
  48. }
  49. nodes.push(spreadLiteral);
  50. } else {
  51. _props.push(prop);
  52. }
  53. }
  54. push(_props, nodes);
  55. return nodes;
  56. }
  57. return {
  58. name: "transform-spread",
  59. visitor: {
  60. ArrayExpression(path) {
  61. const {
  62. node,
  63. scope
  64. } = path;
  65. const elements = node.elements;
  66. if (!hasSpread(elements)) return;
  67. const nodes = build(elements, scope, this.file);
  68. let first = nodes[0];
  69. if (nodes.length === 1 && first !== elements[0].argument) {
  70. path.replaceWith(first);
  71. return;
  72. }
  73. if (!_core.types.isArrayExpression(first)) {
  74. first = _core.types.arrayExpression([]);
  75. } else {
  76. nodes.shift();
  77. }
  78. path.replaceWith(_core.types.callExpression(_core.types.memberExpression(first, _core.types.identifier("concat")), nodes));
  79. },
  80. CallExpression(path) {
  81. const {
  82. node,
  83. scope
  84. } = path;
  85. const args = node.arguments;
  86. if (!hasSpread(args)) return;
  87. const calleePath = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("callee"));
  88. if (calleePath.isSuper()) {
  89. 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.");
  90. }
  91. let contextLiteral = scope.buildUndefinedNode();
  92. node.arguments = [];
  93. let nodes;
  94. if (args.length === 1 && _core.types.isIdentifier(args[0].argument, {
  95. name: "arguments"
  96. })) {
  97. nodes = [args[0].argument];
  98. } else {
  99. nodes = build(args, scope, this.file);
  100. }
  101. const first = nodes.shift();
  102. if (nodes.length) {
  103. node.arguments.push(_core.types.callExpression(_core.types.memberExpression(first, _core.types.identifier("concat")), nodes));
  104. } else {
  105. node.arguments.push(first);
  106. }
  107. const callee = calleePath.node;
  108. if (_core.types.isMemberExpression(callee)) {
  109. const temp = scope.maybeGenerateMemoised(callee.object);
  110. if (temp) {
  111. callee.object = _core.types.assignmentExpression("=", temp, callee.object);
  112. contextLiteral = temp;
  113. } else {
  114. contextLiteral = _core.types.cloneNode(callee.object);
  115. }
  116. }
  117. node.callee = _core.types.memberExpression(node.callee, _core.types.identifier("apply"));
  118. if (_core.types.isSuper(contextLiteral)) {
  119. contextLiteral = _core.types.thisExpression();
  120. }
  121. node.arguments.unshift(_core.types.cloneNode(contextLiteral));
  122. },
  123. NewExpression(path) {
  124. const {
  125. node,
  126. scope
  127. } = path;
  128. if (!hasSpread(node.arguments)) return;
  129. const nodes = build(node.arguments, scope, this.file);
  130. const first = nodes.shift();
  131. let args;
  132. if (nodes.length) {
  133. args = _core.types.callExpression(_core.types.memberExpression(first, _core.types.identifier("concat")), nodes);
  134. } else {
  135. args = first;
  136. }
  137. path.replaceWith(_core.types.callExpression(path.hub.addHelper("construct"), [node.callee, args]));
  138. }
  139. }
  140. };
  141. });
  142. exports.default = _default;
  143. //# sourceMappingURL=index.js.map