index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _helperPluginUtils() {
  7. const data = require("@babel/helper-plugin-utils");
  8. _helperPluginUtils = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _core() {
  14. const data = require("@babel/core");
  15. _core = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. var _default = (0, _helperPluginUtils().declare)((api, options) => {
  21. api.assertVersion(7);
  22. const {
  23. loose
  24. } = options;
  25. function getSpreadLiteral(spread, scope) {
  26. if (loose && !_core().types.isIdentifier(spread.argument, {
  27. name: "arguments"
  28. })) {
  29. return spread.argument;
  30. } else {
  31. return scope.toArray(spread.argument, true);
  32. }
  33. }
  34. function hasSpread(nodes) {
  35. for (let i = 0; i < nodes.length; i++) {
  36. if (_core().types.isSpreadElement(nodes[i])) {
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. function push(_props, nodes) {
  43. if (!_props.length) return _props;
  44. nodes.push(_core().types.arrayExpression(_props));
  45. return [];
  46. }
  47. function build(props, scope) {
  48. const nodes = [];
  49. let _props = [];
  50. for (const prop of props) {
  51. if (_core().types.isSpreadElement(prop)) {
  52. _props = push(_props, nodes);
  53. nodes.push(getSpreadLiteral(prop, scope));
  54. } else {
  55. _props.push(prop);
  56. }
  57. }
  58. push(_props, nodes);
  59. return nodes;
  60. }
  61. return {
  62. name: "transform-spread",
  63. visitor: {
  64. ArrayExpression(path) {
  65. const {
  66. node,
  67. scope
  68. } = path;
  69. const elements = node.elements;
  70. if (!hasSpread(elements)) return;
  71. const nodes = build(elements, scope);
  72. let first = nodes[0];
  73. if (nodes.length === 1 && first !== elements[0].argument) {
  74. path.replaceWith(first);
  75. return;
  76. }
  77. if (!_core().types.isArrayExpression(first)) {
  78. first = _core().types.arrayExpression([]);
  79. } else {
  80. nodes.shift();
  81. }
  82. path.replaceWith(_core().types.callExpression(_core().types.memberExpression(first, _core().types.identifier("concat")), nodes));
  83. },
  84. CallExpression(path) {
  85. const {
  86. node,
  87. scope
  88. } = path;
  89. const args = node.arguments;
  90. if (!hasSpread(args)) return;
  91. const calleePath = path.get("callee");
  92. if (calleePath.isSuper()) return;
  93. let contextLiteral = scope.buildUndefinedNode();
  94. node.arguments = [];
  95. let nodes;
  96. if (args.length === 1 && args[0].argument.name === "arguments") {
  97. nodes = [args[0].argument];
  98. } else {
  99. nodes = build(args, scope);
  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 = node.callee;
  108. if (calleePath.isMemberExpression()) {
  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. _core().types.appendToMemberExpression(callee, _core().types.identifier("apply"));
  117. } else {
  118. node.callee = _core().types.memberExpression(node.callee, _core().types.identifier("apply"));
  119. }
  120. if (_core().types.isSuper(contextLiteral)) {
  121. contextLiteral = _core().types.thisExpression();
  122. }
  123. node.arguments.unshift(_core().types.cloneNode(contextLiteral));
  124. },
  125. NewExpression(path) {
  126. const {
  127. node,
  128. scope
  129. } = path;
  130. let args = node.arguments;
  131. if (!hasSpread(args)) return;
  132. const nodes = build(args, scope);
  133. const first = nodes.shift();
  134. if (nodes.length) {
  135. args = _core().types.callExpression(_core().types.memberExpression(first, _core().types.identifier("concat")), nodes);
  136. } else {
  137. args = first;
  138. }
  139. path.replaceWith(_core().types.callExpression(path.hub.addHelper("construct"), [node.callee, args]));
  140. }
  141. }
  142. };
  143. });
  144. exports.default = _default;