index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  7. var _core = require("@babel/core");
  8. var _default = (0, _helperPluginUtils.declare)((api, options) => {
  9. var _api$assumption;
  10. api.assertVersion(7);
  11. const setComputedProperties = (_api$assumption = api.assumption("setComputedProperties")) != null ? _api$assumption : options.loose;
  12. const pushComputedProps = setComputedProperties ? pushComputedPropsLoose : pushComputedPropsSpec;
  13. const buildMutatorMapAssign = (0, _core.template)(`
  14. MUTATOR_MAP_REF[KEY] = MUTATOR_MAP_REF[KEY] || {};
  15. MUTATOR_MAP_REF[KEY].KIND = VALUE;
  16. `);
  17. function getValue(prop) {
  18. if (_core.types.isObjectProperty(prop)) {
  19. return prop.value;
  20. } else if (_core.types.isObjectMethod(prop)) {
  21. return _core.types.functionExpression(null, prop.params, prop.body, prop.generator, prop.async);
  22. }
  23. }
  24. function pushAssign(objId, prop, body) {
  25. if (prop.kind === "get" && prop.kind === "set") {
  26. pushMutatorDefine(objId, prop);
  27. } else {
  28. body.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.cloneNode(objId), prop.key, prop.computed || _core.types.isLiteral(prop.key)), getValue(prop))));
  29. }
  30. }
  31. function pushMutatorDefine({
  32. body,
  33. getMutatorId,
  34. scope
  35. }, prop) {
  36. let key = !prop.computed && _core.types.isIdentifier(prop.key) ? _core.types.stringLiteral(prop.key.name) : prop.key;
  37. const maybeMemoise = scope.maybeGenerateMemoised(key);
  38. if (maybeMemoise) {
  39. body.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", maybeMemoise, key)));
  40. key = maybeMemoise;
  41. }
  42. body.push(...buildMutatorMapAssign({
  43. MUTATOR_MAP_REF: getMutatorId(),
  44. KEY: _core.types.cloneNode(key),
  45. VALUE: getValue(prop),
  46. KIND: _core.types.identifier(prop.kind)
  47. }));
  48. }
  49. function pushComputedPropsLoose(info) {
  50. for (const prop of info.computedProps) {
  51. if (prop.kind === "get" || prop.kind === "set") {
  52. pushMutatorDefine(info, prop);
  53. } else {
  54. pushAssign(_core.types.cloneNode(info.objId), prop, info.body);
  55. }
  56. }
  57. }
  58. function pushComputedPropsSpec(info) {
  59. const {
  60. objId,
  61. body,
  62. computedProps,
  63. state
  64. } = info;
  65. for (const prop of computedProps) {
  66. const key = _core.types.toComputedKey(prop);
  67. if (prop.kind === "get" || prop.kind === "set") {
  68. pushMutatorDefine(info, prop);
  69. } else {
  70. if (computedProps.length === 1) {
  71. return _core.types.callExpression(state.addHelper("defineProperty"), [info.initPropExpression, key, getValue(prop)]);
  72. } else {
  73. body.push(_core.types.expressionStatement(_core.types.callExpression(state.addHelper("defineProperty"), [_core.types.cloneNode(objId), key, getValue(prop)])));
  74. }
  75. }
  76. }
  77. }
  78. return {
  79. name: "transform-computed-properties",
  80. visitor: {
  81. ObjectExpression: {
  82. exit(path, state) {
  83. const {
  84. node,
  85. parent,
  86. scope
  87. } = path;
  88. let hasComputed = false;
  89. for (const prop of node.properties) {
  90. hasComputed = prop.computed === true;
  91. if (hasComputed) break;
  92. }
  93. if (!hasComputed) return;
  94. const initProps = [];
  95. const computedProps = [];
  96. let foundComputed = false;
  97. for (const prop of node.properties) {
  98. if (prop.computed) {
  99. foundComputed = true;
  100. }
  101. if (foundComputed) {
  102. computedProps.push(prop);
  103. } else {
  104. initProps.push(prop);
  105. }
  106. }
  107. const objId = scope.generateUidIdentifierBasedOnNode(parent);
  108. const initPropExpression = _core.types.objectExpression(initProps);
  109. const body = [];
  110. body.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(objId, initPropExpression)]));
  111. let mutatorRef;
  112. const getMutatorId = function () {
  113. if (!mutatorRef) {
  114. mutatorRef = scope.generateUidIdentifier("mutatorMap");
  115. body.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(mutatorRef, _core.types.objectExpression([]))]));
  116. }
  117. return _core.types.cloneNode(mutatorRef);
  118. };
  119. const single = pushComputedProps({
  120. scope,
  121. objId,
  122. body,
  123. computedProps,
  124. initPropExpression,
  125. getMutatorId,
  126. state
  127. });
  128. if (mutatorRef) {
  129. body.push(_core.types.expressionStatement(_core.types.callExpression(state.addHelper("defineEnumerableProperties"), [_core.types.cloneNode(objId), _core.types.cloneNode(mutatorRef)])));
  130. }
  131. if (single) {
  132. path.replaceWith(single);
  133. } else {
  134. body.push(_core.types.expressionStatement(_core.types.cloneNode(objId)));
  135. path.replaceWithMultiple(body);
  136. }
  137. }
  138. }
  139. }
  140. };
  141. });
  142. exports.default = _default;