index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 = exports.default = (0, _helperPluginUtils.declare)(api => {
  9. api.assertVersion("^7.0.0-0 || >8.0.0-alpha <8.0.0-beta");
  10. return {
  11. name: "transform-logical-assignment-operators",
  12. manipulateOptions: (_, parser) => parser.plugins.push("logicalAssignment"),
  13. visitor: {
  14. AssignmentExpression(path) {
  15. const {
  16. node,
  17. scope
  18. } = path;
  19. const {
  20. operator,
  21. left,
  22. right
  23. } = node;
  24. const operatorTrunc = operator.slice(0, -1);
  25. if (!_core.types.LOGICAL_OPERATORS.includes(operatorTrunc)) {
  26. return;
  27. }
  28. const lhs = _core.types.cloneNode(left);
  29. if (_core.types.isMemberExpression(left)) {
  30. const {
  31. object,
  32. property,
  33. computed
  34. } = left;
  35. const memo = scope.maybeGenerateMemoised(object);
  36. if (memo) {
  37. left.object = memo;
  38. lhs.object = _core.types.assignmentExpression("=", _core.types.cloneNode(memo), object);
  39. }
  40. if (computed) {
  41. const memo = scope.maybeGenerateMemoised(property);
  42. if (memo) {
  43. left.property = memo;
  44. lhs.property = _core.types.assignmentExpression("=", _core.types.cloneNode(memo), property);
  45. }
  46. }
  47. }
  48. path.replaceWith(_core.types.logicalExpression(operatorTrunc, lhs, _core.types.assignmentExpression("=", left, right)));
  49. }
  50. }
  51. };
  52. });
  53. //# sourceMappingURL=index.js.map