index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. template = _ref.template;
  8. var buildMutatorMapAssign = template("\n MUTATOR_MAP_REF[KEY] = MUTATOR_MAP_REF[KEY] || {};\n MUTATOR_MAP_REF[KEY].KIND = VALUE;\n ");
  9. function getValue(prop) {
  10. if (t.isObjectProperty(prop)) {
  11. return prop.value;
  12. } else if (t.isObjectMethod(prop)) {
  13. return t.functionExpression(null, prop.params, prop.body, prop.generator, prop.async);
  14. }
  15. }
  16. function pushAssign(objId, prop, body) {
  17. if (prop.kind === "get" && prop.kind === "set") {
  18. pushMutatorDefine(objId, prop, body);
  19. } else {
  20. body.push(t.expressionStatement(t.assignmentExpression("=", t.memberExpression(objId, prop.key, prop.computed || t.isLiteral(prop.key)), getValue(prop))));
  21. }
  22. }
  23. function pushMutatorDefine(_ref2, prop) {
  24. var objId = _ref2.objId,
  25. body = _ref2.body,
  26. getMutatorId = _ref2.getMutatorId,
  27. scope = _ref2.scope;
  28. var key = !prop.computed && t.isIdentifier(prop.key) ? t.stringLiteral(prop.key.name) : prop.key;
  29. var maybeMemoise = scope.maybeGenerateMemoised(key);
  30. if (maybeMemoise) {
  31. body.push(t.expressionStatement(t.assignmentExpression("=", maybeMemoise, key)));
  32. key = maybeMemoise;
  33. }
  34. body.push.apply(body, buildMutatorMapAssign({
  35. MUTATOR_MAP_REF: getMutatorId(),
  36. KEY: key,
  37. VALUE: getValue(prop),
  38. KIND: t.identifier(prop.kind)
  39. }));
  40. }
  41. function loose(info) {
  42. for (var _iterator = info.computedProps, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  43. var _ref3;
  44. if (_isArray) {
  45. if (_i >= _iterator.length) break;
  46. _ref3 = _iterator[_i++];
  47. } else {
  48. _i = _iterator.next();
  49. if (_i.done) break;
  50. _ref3 = _i.value;
  51. }
  52. var prop = _ref3;
  53. if (prop.kind === "get" || prop.kind === "set") {
  54. pushMutatorDefine(info, prop);
  55. } else {
  56. pushAssign(info.objId, prop, info.body);
  57. }
  58. }
  59. }
  60. function spec(info) {
  61. var objId = info.objId,
  62. body = info.body,
  63. computedProps = info.computedProps,
  64. state = info.state;
  65. for (var _iterator2 = computedProps, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  66. var _ref4;
  67. if (_isArray2) {
  68. if (_i2 >= _iterator2.length) break;
  69. _ref4 = _iterator2[_i2++];
  70. } else {
  71. _i2 = _iterator2.next();
  72. if (_i2.done) break;
  73. _ref4 = _i2.value;
  74. }
  75. var prop = _ref4;
  76. var key = t.toComputedKey(prop);
  77. if (prop.kind === "get" || prop.kind === "set") {
  78. pushMutatorDefine(info, prop);
  79. } else if (t.isStringLiteral(key, { value: "__proto__" })) {
  80. pushAssign(objId, prop, body);
  81. } else {
  82. if (computedProps.length === 1) {
  83. return t.callExpression(state.addHelper("defineProperty"), [info.initPropExpression, key, getValue(prop)]);
  84. } else {
  85. body.push(t.expressionStatement(t.callExpression(state.addHelper("defineProperty"), [objId, key, getValue(prop)])));
  86. }
  87. }
  88. }
  89. }
  90. return {
  91. visitor: {
  92. ObjectExpression: {
  93. exit: function exit(path, state) {
  94. var node = path.node,
  95. parent = path.parent,
  96. scope = path.scope;
  97. var hasComputed = false;
  98. for (var _iterator3 = node.properties, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
  99. var _ref5;
  100. if (_isArray3) {
  101. if (_i3 >= _iterator3.length) break;
  102. _ref5 = _iterator3[_i3++];
  103. } else {
  104. _i3 = _iterator3.next();
  105. if (_i3.done) break;
  106. _ref5 = _i3.value;
  107. }
  108. var prop = _ref5;
  109. hasComputed = prop.computed === true;
  110. if (hasComputed) break;
  111. }
  112. if (!hasComputed) return;
  113. var initProps = [];
  114. var computedProps = [];
  115. var foundComputed = false;
  116. for (var _iterator4 = node.properties, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : (0, _getIterator3.default)(_iterator4);;) {
  117. var _ref6;
  118. if (_isArray4) {
  119. if (_i4 >= _iterator4.length) break;
  120. _ref6 = _iterator4[_i4++];
  121. } else {
  122. _i4 = _iterator4.next();
  123. if (_i4.done) break;
  124. _ref6 = _i4.value;
  125. }
  126. var _prop = _ref6;
  127. if (_prop.computed) {
  128. foundComputed = true;
  129. }
  130. if (foundComputed) {
  131. computedProps.push(_prop);
  132. } else {
  133. initProps.push(_prop);
  134. }
  135. }
  136. var objId = scope.generateUidIdentifierBasedOnNode(parent);
  137. var initPropExpression = t.objectExpression(initProps);
  138. var body = [];
  139. body.push(t.variableDeclaration("var", [t.variableDeclarator(objId, initPropExpression)]));
  140. var callback = spec;
  141. if (state.opts.loose) callback = loose;
  142. var mutatorRef = void 0;
  143. var getMutatorId = function getMutatorId() {
  144. if (!mutatorRef) {
  145. mutatorRef = scope.generateUidIdentifier("mutatorMap");
  146. body.push(t.variableDeclaration("var", [t.variableDeclarator(mutatorRef, t.objectExpression([]))]));
  147. }
  148. return mutatorRef;
  149. };
  150. var single = callback({
  151. scope: scope,
  152. objId: objId,
  153. body: body,
  154. computedProps: computedProps,
  155. initPropExpression: initPropExpression,
  156. getMutatorId: getMutatorId,
  157. state: state
  158. });
  159. if (mutatorRef) {
  160. body.push(t.expressionStatement(t.callExpression(state.addHelper("defineEnumerableProperties"), [objId, mutatorRef])));
  161. }
  162. if (single) {
  163. path.replaceWith(single);
  164. } else {
  165. body.push(t.expressionStatement(objId));
  166. path.replaceWithMultiple(body);
  167. }
  168. }
  169. }
  170. }
  171. };
  172. };
  173. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  174. module.exports = exports["default"];