index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /*istanbul ignore next*/var template = _ref.template;
  8. var buildMutatorMapAssign = template( /*istanbul ignore next*/"\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( /*istanbul ignore next*/_ref2, prop) {
  24. /*istanbul ignore next*/var objId = _ref2.objId;
  25. /*istanbul ignore next*/var body = _ref2.body;
  26. /*istanbul ignore next*/var getMutatorId = _ref2.getMutatorId;
  27. /*istanbul ignore next*/var 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. /*istanbul ignore next*/body.push. /*istanbul ignore next*/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 ( /*istanbul ignore next*/var _iterator = info.computedProps, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  43. /*istanbul ignore next*/
  44. var _ref3;
  45. if (_isArray) {
  46. if (_i >= _iterator.length) break;
  47. _ref3 = _iterator[_i++];
  48. } else {
  49. _i = _iterator.next();
  50. if (_i.done) break;
  51. _ref3 = _i.value;
  52. }
  53. var prop = _ref3;
  54. if (prop.kind === "get" || prop.kind === "set") {
  55. pushMutatorDefine(info, prop);
  56. } else {
  57. pushAssign(info.objId, prop, info.body);
  58. }
  59. }
  60. }
  61. function spec(info) {
  62. /*istanbul ignore next*/var objId = info.objId;
  63. /*istanbul ignore next*/var body = info.body;
  64. /*istanbul ignore next*/var computedProps = info.computedProps;
  65. /*istanbul ignore next*/var state = info.state;
  66. for ( /*istanbul ignore next*/var _iterator2 = computedProps, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  67. /*istanbul ignore next*/
  68. var _ref4;
  69. if (_isArray2) {
  70. if (_i2 >= _iterator2.length) break;
  71. _ref4 = _iterator2[_i2++];
  72. } else {
  73. _i2 = _iterator2.next();
  74. if (_i2.done) break;
  75. _ref4 = _i2.value;
  76. }
  77. var prop = _ref4;
  78. var key = t.toComputedKey(prop);
  79. if (prop.kind === "get" || prop.kind === "set") {
  80. pushMutatorDefine(info, prop);
  81. } else if (t.isStringLiteral(key, { value: "__proto__" })) {
  82. pushAssign(objId, prop, body);
  83. } else {
  84. if (computedProps.length === 1) {
  85. return t.callExpression(state.addHelper("defineProperty"), [info.initPropExpression, key, getValue(prop)]);
  86. } else {
  87. body.push(t.expressionStatement(t.callExpression(state.addHelper("defineProperty"), [objId, key, getValue(prop)])));
  88. }
  89. }
  90. }
  91. }
  92. return {
  93. visitor: {
  94. ObjectExpression: { /*istanbul ignore next*/
  95. exit: function exit(path, state) {
  96. /*istanbul ignore next*/var node = path.node;
  97. /*istanbul ignore next*/var parent = path.parent;
  98. /*istanbul ignore next*/var scope = path.scope;
  99. var hasComputed = false;
  100. for ( /*istanbul ignore next*/var _iterator3 = node.properties, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
  101. /*istanbul ignore next*/
  102. var _ref5;
  103. if (_isArray3) {
  104. if (_i3 >= _iterator3.length) break;
  105. _ref5 = _iterator3[_i3++];
  106. } else {
  107. _i3 = _iterator3.next();
  108. if (_i3.done) break;
  109. _ref5 = _i3.value;
  110. }
  111. var prop = _ref5;
  112. hasComputed = prop.computed === true;
  113. if (hasComputed) break;
  114. }
  115. if (!hasComputed) return;
  116. // put all getters/setters into the first object expression as well as all initialisers up
  117. // to the first computed property
  118. var initProps = [];
  119. var computedProps = [];
  120. var foundComputed = false;
  121. for ( /*istanbul ignore next*/var _iterator4 = node.properties, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : (0, _getIterator3.default)(_iterator4);;) {
  122. /*istanbul ignore next*/
  123. var _ref6;
  124. if (_isArray4) {
  125. if (_i4 >= _iterator4.length) break;
  126. _ref6 = _iterator4[_i4++];
  127. } else {
  128. _i4 = _iterator4.next();
  129. if (_i4.done) break;
  130. _ref6 = _i4.value;
  131. }
  132. var _prop = _ref6;
  133. if (_prop.computed) {
  134. foundComputed = true;
  135. }
  136. if (foundComputed) {
  137. computedProps.push(_prop);
  138. } else {
  139. initProps.push(_prop);
  140. }
  141. }
  142. var objId = scope.generateUidIdentifierBasedOnNode(parent);
  143. var initPropExpression = t.objectExpression(initProps);
  144. var body = [];
  145. body.push(t.variableDeclaration("var", [t.variableDeclarator(objId, initPropExpression)]));
  146. var callback = spec;
  147. if (state.opts.loose) callback = loose;
  148. var mutatorRef = /*istanbul ignore next*/void 0;
  149. var getMutatorId = function getMutatorId() {
  150. if (!mutatorRef) {
  151. mutatorRef = scope.generateUidIdentifier("mutatorMap");
  152. body.push(t.variableDeclaration("var", [t.variableDeclarator(mutatorRef, t.objectExpression([]))]));
  153. }
  154. return mutatorRef;
  155. };
  156. var single = callback({
  157. scope: scope,
  158. objId: objId,
  159. body: body,
  160. computedProps: computedProps,
  161. initPropExpression: initPropExpression,
  162. getMutatorId: getMutatorId,
  163. state: state
  164. });
  165. if (mutatorRef) {
  166. body.push(t.expressionStatement(t.callExpression(state.addHelper("defineEnumerableProperties"), [objId, mutatorRef])));
  167. }
  168. if (single) {
  169. path.replaceWith(single);
  170. } else {
  171. body.push(t.expressionStatement(objId));
  172. path.replaceWithMultiple(body);
  173. }
  174. }
  175. }
  176. }
  177. };
  178. };
  179. /*istanbul ignore next*/
  180. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  181. module.exports = exports["default"];