index.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 _default = exports.default = (0, _helperPluginUtils.declare)(api => {
  8. api.assertVersion("^7.0.0-0 || >8.0.0-alpha <8.0.0-beta");
  9. const {
  10. types: t,
  11. template
  12. } = api;
  13. function build(left, right) {
  14. return t.callExpression(t.memberExpression(t.identifier("Math"), t.identifier("pow")), [left, right]);
  15. }
  16. function maybeMemoize(node, scope) {
  17. if (scope.isStatic(node)) {
  18. return {
  19. assign: node,
  20. ref: t.cloneNode(node)
  21. };
  22. }
  23. if (scope.path.isPattern()) {
  24. return null;
  25. }
  26. const id = scope.generateUidIdentifierBasedOnNode(node);
  27. scope.push({
  28. id
  29. });
  30. return {
  31. assign: t.assignmentExpression("=", t.cloneNode(id), node),
  32. ref: t.cloneNode(id)
  33. };
  34. }
  35. return {
  36. name: "transform-exponentiation-operator",
  37. visitor: {
  38. AssignmentExpression(path) {
  39. const {
  40. node,
  41. scope
  42. } = path;
  43. if (node.operator !== "**=") return;
  44. if (t.isMemberExpression(node.left)) {
  45. let member1;
  46. let member2;
  47. const object = maybeMemoize(node.left.object, scope);
  48. if (!object) {
  49. path.replaceWith(template.expression.ast`(() => ${path.node})()`);
  50. return;
  51. }
  52. const {
  53. property,
  54. computed
  55. } = node.left;
  56. if (computed) {
  57. const prop = maybeMemoize(property, scope);
  58. member1 = t.memberExpression(object.assign, prop.assign, true);
  59. member2 = t.memberExpression(object.ref, prop.ref, true);
  60. } else {
  61. member1 = t.memberExpression(object.assign, property, false);
  62. member2 = t.memberExpression(object.ref, t.cloneNode(property), false);
  63. }
  64. path.replaceWith(t.assignmentExpression("=", member1, build(member2, node.right)));
  65. } else {
  66. path.replaceWith(t.assignmentExpression("=", node.left, build(t.cloneNode(node.left), node.right)));
  67. }
  68. },
  69. BinaryExpression(path) {
  70. const {
  71. node
  72. } = path;
  73. if (node.operator === "**") {
  74. path.replaceWith(build(node.left, node.right));
  75. }
  76. }
  77. }
  78. };
  79. });
  80. //# sourceMappingURL=index.js.map