index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _core = require("@babel/core");
  7. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  8. var _template = require("@babel/template");
  9. {
  10. var DefineAccessorHelper = _template.default.expression.ast`
  11. function (type, obj, key, fn) {
  12. var desc = { configurable: true, enumerable: true };
  13. desc[type] = fn;
  14. return Object.defineProperty(obj, key, desc);
  15. }
  16. `;
  17. DefineAccessorHelper._compact = true;
  18. }
  19. var _default = exports.default = (0, _helperPluginUtils.declare)((api, options) => {
  20. var _api$assumption;
  21. api.assertVersion(7);
  22. const setComputedProperties = (_api$assumption = api.assumption("setComputedProperties")) != null ? _api$assumption : options.loose;
  23. const pushComputedProps = setComputedProperties ? pushComputedPropsLoose : pushComputedPropsSpec;
  24. function buildDefineAccessor(state, obj, prop) {
  25. const type = prop.kind;
  26. const key = !prop.computed && _core.types.isIdentifier(prop.key) ? _core.types.stringLiteral(prop.key.name) : prop.key;
  27. const fn = getValue(prop);
  28. {
  29. let helper;
  30. if (state.availableHelper("defineAccessor")) {
  31. helper = state.addHelper("defineAccessor");
  32. } else {
  33. const file = state.file;
  34. helper = file.get("fallbackDefineAccessorHelper");
  35. if (!helper) {
  36. const id = file.scope.generateUidIdentifier("defineAccessor");
  37. file.scope.push({
  38. id,
  39. init: DefineAccessorHelper
  40. });
  41. file.set("fallbackDefineAccessorHelper", helper = id);
  42. }
  43. helper = _core.types.cloneNode(helper);
  44. }
  45. return _core.types.callExpression(helper, [_core.types.stringLiteral(type), obj, key, fn]);
  46. }
  47. }
  48. function getValue(prop) {
  49. if (_core.types.isObjectProperty(prop)) {
  50. return prop.value;
  51. } else if (_core.types.isObjectMethod(prop)) {
  52. return _core.types.functionExpression(null, prop.params, prop.body, prop.generator, prop.async);
  53. }
  54. }
  55. function pushAssign(objId, prop, body) {
  56. 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))));
  57. }
  58. function pushComputedPropsLoose(info) {
  59. const {
  60. computedProps,
  61. state,
  62. initPropExpression,
  63. objId,
  64. body
  65. } = info;
  66. for (const prop of computedProps) {
  67. if (_core.types.isObjectMethod(prop) && (prop.kind === "get" || prop.kind === "set")) {
  68. if (computedProps.length === 1) {
  69. return buildDefineAccessor(state, initPropExpression, prop);
  70. } else {
  71. body.push(_core.types.expressionStatement(buildDefineAccessor(state, _core.types.cloneNode(objId), prop)));
  72. }
  73. } else {
  74. pushAssign(_core.types.cloneNode(objId), prop, body);
  75. }
  76. }
  77. }
  78. function pushComputedPropsSpec(info) {
  79. const {
  80. objId,
  81. body,
  82. computedProps,
  83. state
  84. } = info;
  85. const CHUNK_LENGTH_CAP = 10;
  86. let currentChunk = null;
  87. const computedPropsChunks = [];
  88. for (const prop of computedProps) {
  89. if (!currentChunk || currentChunk.length === CHUNK_LENGTH_CAP) {
  90. currentChunk = [];
  91. computedPropsChunks.push(currentChunk);
  92. }
  93. currentChunk.push(prop);
  94. }
  95. for (const chunk of computedPropsChunks) {
  96. const single = computedPropsChunks.length === 1;
  97. let node = single ? info.initPropExpression : _core.types.cloneNode(objId);
  98. for (const prop of chunk) {
  99. if (_core.types.isObjectMethod(prop) && (prop.kind === "get" || prop.kind === "set")) {
  100. node = buildDefineAccessor(info.state, node, prop);
  101. } else {
  102. node = _core.types.callExpression(state.addHelper("defineProperty"), [node, _core.types.toComputedKey(prop), getValue(prop)]);
  103. }
  104. }
  105. if (single) return node;
  106. body.push(_core.types.expressionStatement(node));
  107. }
  108. }
  109. return {
  110. name: "transform-computed-properties",
  111. visitor: {
  112. ObjectExpression: {
  113. exit(path, state) {
  114. const {
  115. node,
  116. parent,
  117. scope
  118. } = path;
  119. let hasComputed = false;
  120. for (const prop of node.properties) {
  121. hasComputed = prop.computed === true;
  122. if (hasComputed) break;
  123. }
  124. if (!hasComputed) return;
  125. const initProps = [];
  126. const computedProps = [];
  127. let foundComputed = false;
  128. for (const prop of node.properties) {
  129. if (_core.types.isSpreadElement(prop)) {
  130. continue;
  131. }
  132. if (prop.computed) {
  133. foundComputed = true;
  134. }
  135. if (foundComputed) {
  136. computedProps.push(prop);
  137. } else {
  138. initProps.push(prop);
  139. }
  140. }
  141. const objId = scope.generateUidIdentifierBasedOnNode(parent);
  142. const initPropExpression = _core.types.objectExpression(initProps);
  143. const body = [];
  144. body.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(objId, initPropExpression)]));
  145. const single = pushComputedProps({
  146. scope,
  147. objId,
  148. body,
  149. computedProps,
  150. initPropExpression,
  151. state
  152. });
  153. if (single) {
  154. path.replaceWith(single);
  155. } else {
  156. if (setComputedProperties) {
  157. body.push(_core.types.expressionStatement(_core.types.cloneNode(objId)));
  158. }
  159. path.replaceWithMultiple(body);
  160. }
  161. }
  162. }
  163. }
  164. };
  165. });
  166. //# sourceMappingURL=index.js.map