index.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*istanbul ignore next*/"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 ( /*istanbul ignore next*/_ref) {
  6. /*istanbul ignore next*/var t = _ref.types;
  7. function hasSpread(node) {
  8. for ( /*istanbul ignore next*/var _iterator = node.properties, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  9. /*istanbul ignore next*/
  10. var _ref2;
  11. if (_isArray) {
  12. if (_i >= _iterator.length) break;
  13. _ref2 = _iterator[_i++];
  14. } else {
  15. _i = _iterator.next();
  16. if (_i.done) break;
  17. _ref2 = _i.value;
  18. }
  19. var prop = _ref2;
  20. if (t.isSpreadProperty(prop)) {
  21. return true;
  22. }
  23. }
  24. return false;
  25. }
  26. return {
  27. inherits: require("babel-plugin-syntax-object-rest-spread"),
  28. visitor: { /*istanbul ignore next*/
  29. ObjectExpression: function ObjectExpression(path, file) {
  30. if (!hasSpread(path.node)) return;
  31. var args = [];
  32. var props = [];
  33. function push() {
  34. if (!props.length) return;
  35. args.push(t.objectExpression(props));
  36. props = [];
  37. }
  38. for ( /*istanbul ignore next*/var _iterator2 = path.node.properties, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  39. /*istanbul ignore next*/
  40. var _ref3;
  41. if (_isArray2) {
  42. if (_i2 >= _iterator2.length) break;
  43. _ref3 = _iterator2[_i2++];
  44. } else {
  45. _i2 = _iterator2.next();
  46. if (_i2.done) break;
  47. _ref3 = _i2.value;
  48. }
  49. var prop = _ref3;
  50. if (t.isSpreadProperty(prop)) {
  51. push();
  52. args.push(prop.argument);
  53. } else {
  54. props.push(prop);
  55. }
  56. }
  57. push();
  58. if (!t.isObjectExpression(args[0])) {
  59. args.unshift(t.objectExpression([]));
  60. }
  61. path.replaceWith(t.callExpression(file.addHelper("extends"), args));
  62. }
  63. }
  64. };
  65. };
  66. /*istanbul ignore next*/
  67. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  68. module.exports = exports["default"];