index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. "use strict";
  2. exports.__esModule = true;
  3. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  4. var _getIterator3 = _interopRequireDefault(_getIterator2);
  5. exports.default = function (_ref) {
  6. var t = _ref.types;
  7. function hasRestProperty(path) {
  8. var foundRestProperty = false;
  9. path.traverse({
  10. RestProperty: function RestProperty() {
  11. foundRestProperty = true;
  12. path.stop();
  13. }
  14. });
  15. return foundRestProperty;
  16. }
  17. function hasSpread(node) {
  18. for (var _iterator = node.properties, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  19. var _ref2;
  20. if (_isArray) {
  21. if (_i >= _iterator.length) break;
  22. _ref2 = _iterator[_i++];
  23. } else {
  24. _i = _iterator.next();
  25. if (_i.done) break;
  26. _ref2 = _i.value;
  27. }
  28. var prop = _ref2;
  29. if (t.isSpreadProperty(prop)) {
  30. return true;
  31. }
  32. }
  33. return false;
  34. }
  35. function createObjectSpread(file, props, objRef) {
  36. var restProperty = props.pop();
  37. var keys = [];
  38. for (var _iterator2 = props, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  39. var _ref3;
  40. if (_isArray2) {
  41. if (_i2 >= _iterator2.length) break;
  42. _ref3 = _iterator2[_i2++];
  43. } else {
  44. _i2 = _iterator2.next();
  45. if (_i2.done) break;
  46. _ref3 = _i2.value;
  47. }
  48. var prop = _ref3;
  49. var key = prop.key;
  50. if (t.isIdentifier(key) && !prop.computed) {
  51. key = t.stringLiteral(prop.key.name);
  52. }
  53. keys.push(key);
  54. }
  55. return [restProperty.argument, t.callExpression(file.addHelper("objectWithoutProperties"), [objRef, t.arrayExpression(keys)])];
  56. }
  57. function replaceRestProperty(parentPath, paramPath, i, numParams) {
  58. if (paramPath.isAssignmentPattern()) {
  59. replaceRestProperty(parentPath, paramPath.get("left"), i, numParams);
  60. return;
  61. }
  62. if (paramPath.isObjectPattern() && hasRestProperty(paramPath)) {
  63. var uid = parentPath.scope.generateUidIdentifier("ref");
  64. var declar = t.variableDeclaration("let", [t.variableDeclarator(paramPath.node, uid)]);
  65. declar._blockHoist = i ? numParams - i : 1;
  66. parentPath.ensureBlock();
  67. parentPath.get("body").unshiftContainer("body", declar);
  68. paramPath.replaceWith(uid);
  69. }
  70. }
  71. return {
  72. inherits: require("babel-plugin-syntax-object-rest-spread"),
  73. visitor: {
  74. Function: function Function(path) {
  75. var params = path.get("params");
  76. for (var i = 0; i < params.length; i++) {
  77. replaceRestProperty(params[i].parentPath, params[i], i, params.length);
  78. }
  79. },
  80. VariableDeclarator: function VariableDeclarator(path, file) {
  81. if (!path.get("id").isObjectPattern()) {
  82. return;
  83. }
  84. var insertionPath = path;
  85. path.get("id").traverse({
  86. RestProperty: function RestProperty(path) {
  87. if (this.originalPath.node.id.properties.length > 1 && !t.isIdentifier(this.originalPath.node.init)) {
  88. var initRef = path.scope.generateUidIdentifierBasedOnNode(this.originalPath.node.init, "ref");
  89. this.originalPath.insertBefore(t.variableDeclarator(initRef, this.originalPath.node.init));
  90. this.originalPath.replaceWith(t.variableDeclarator(this.originalPath.node.id, initRef));
  91. return;
  92. }
  93. var ref = this.originalPath.node.init;
  94. var refPropertyPath = [];
  95. path.findParent(function (path) {
  96. if (path.isObjectProperty()) {
  97. refPropertyPath.unshift(path.node.key.name);
  98. } else if (path.isVariableDeclarator()) {
  99. return true;
  100. }
  101. });
  102. if (refPropertyPath.length) {
  103. refPropertyPath.forEach(function (prop) {
  104. ref = t.memberExpression(ref, t.identifier(prop));
  105. });
  106. }
  107. var _createObjectSpread = createObjectSpread(file, path.parentPath.node.properties, ref),
  108. argument = _createObjectSpread[0],
  109. callExpression = _createObjectSpread[1];
  110. insertionPath.insertAfter(t.variableDeclarator(argument, callExpression));
  111. insertionPath = insertionPath.getSibling(insertionPath.key + 1);
  112. if (path.parentPath.node.properties.length === 0) {
  113. path.findParent(function (path) {
  114. return path.isObjectProperty() || path.isVariableDeclarator();
  115. }).remove();
  116. }
  117. }
  118. }, {
  119. originalPath: path
  120. });
  121. },
  122. ExportNamedDeclaration: function ExportNamedDeclaration(path) {
  123. var declaration = path.get("declaration");
  124. if (!declaration.isVariableDeclaration()) return;
  125. if (!hasRestProperty(declaration)) return;
  126. var specifiers = [];
  127. for (var name in path.getOuterBindingIdentifiers(path)) {
  128. var id = t.identifier(name);
  129. specifiers.push(t.exportSpecifier(id, id));
  130. }
  131. path.replaceWith(declaration.node);
  132. path.insertAfter(t.exportNamedDeclaration(null, specifiers));
  133. },
  134. CatchClause: function CatchClause(path) {
  135. var paramPath = path.get("param");
  136. replaceRestProperty(paramPath.parentPath, paramPath);
  137. },
  138. AssignmentExpression: function AssignmentExpression(path, file) {
  139. var leftPath = path.get("left");
  140. if (leftPath.isObjectPattern() && hasRestProperty(leftPath)) {
  141. var nodes = [];
  142. var ref = void 0;
  143. if (path.isCompletionRecord() || path.parentPath.isExpressionStatement()) {
  144. ref = path.scope.generateUidIdentifierBasedOnNode(path.node.right, "ref");
  145. nodes.push(t.variableDeclaration("var", [t.variableDeclarator(ref, path.node.right)]));
  146. }
  147. var _createObjectSpread2 = createObjectSpread(file, path.node.left.properties, ref),
  148. argument = _createObjectSpread2[0],
  149. callExpression = _createObjectSpread2[1];
  150. var nodeWithoutSpread = t.clone(path.node);
  151. nodeWithoutSpread.right = ref;
  152. nodes.push(t.expressionStatement(nodeWithoutSpread));
  153. nodes.push(t.toStatement(t.assignmentExpression("=", argument, callExpression)));
  154. if (ref) {
  155. nodes.push(t.expressionStatement(ref));
  156. }
  157. path.replaceWithMultiple(nodes);
  158. }
  159. },
  160. ForXStatement: function ForXStatement(path) {
  161. var node = path.node,
  162. scope = path.scope;
  163. var leftPath = path.get("left");
  164. var left = node.left;
  165. if (t.isObjectPattern(left) && hasRestProperty(leftPath)) {
  166. var temp = scope.generateUidIdentifier("ref");
  167. node.left = t.variableDeclaration("var", [t.variableDeclarator(temp)]);
  168. path.ensureBlock();
  169. node.body.body.unshift(t.variableDeclaration("var", [t.variableDeclarator(left, temp)]));
  170. return;
  171. }
  172. if (!t.isVariableDeclaration(left)) return;
  173. var pattern = left.declarations[0].id;
  174. if (!t.isObjectPattern(pattern)) return;
  175. var key = scope.generateUidIdentifier("ref");
  176. node.left = t.variableDeclaration(left.kind, [t.variableDeclarator(key, null)]);
  177. path.ensureBlock();
  178. node.body.body.unshift(t.variableDeclaration(node.left.kind, [t.variableDeclarator(pattern, key)]));
  179. },
  180. ObjectExpression: function ObjectExpression(path, file) {
  181. if (!hasSpread(path.node)) return;
  182. var useBuiltIns = file.opts.useBuiltIns || false;
  183. if (typeof useBuiltIns !== "boolean") {
  184. throw new Error("transform-object-rest-spread currently only accepts a boolean " + "option for useBuiltIns (defaults to false)");
  185. }
  186. var args = [];
  187. var props = [];
  188. function push() {
  189. if (!props.length) return;
  190. args.push(t.objectExpression(props));
  191. props = [];
  192. }
  193. for (var _iterator3 = path.node.properties, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
  194. var _ref4;
  195. if (_isArray3) {
  196. if (_i3 >= _iterator3.length) break;
  197. _ref4 = _iterator3[_i3++];
  198. } else {
  199. _i3 = _iterator3.next();
  200. if (_i3.done) break;
  201. _ref4 = _i3.value;
  202. }
  203. var prop = _ref4;
  204. if (t.isSpreadProperty(prop)) {
  205. push();
  206. args.push(prop.argument);
  207. } else {
  208. props.push(prop);
  209. }
  210. }
  211. push();
  212. if (!t.isObjectExpression(args[0])) {
  213. args.unshift(t.objectExpression([]));
  214. }
  215. var helper = useBuiltIns ? t.memberExpression(t.identifier("Object"), t.identifier("assign")) : file.addHelper("extends");
  216. path.replaceWith(t.callExpression(helper, args));
  217. }
  218. }
  219. };
  220. };
  221. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  222. module.exports = exports["default"];