index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 _pluginSyntaxLogicalAssignmentOperators = require("@babel/plugin-syntax-logical-assignment-operators");
  8. var _core = require("@babel/core");
  9. var _default = (0, _helperPluginUtils.declare)(api => {
  10. api.assertVersion(7);
  11. return {
  12. name: "proposal-logical-assignment-operators",
  13. inherits: _pluginSyntaxLogicalAssignmentOperators.default,
  14. visitor: {
  15. AssignmentExpression(path) {
  16. const {
  17. node,
  18. scope
  19. } = path;
  20. const {
  21. operator,
  22. left,
  23. right
  24. } = node;
  25. const operatorTrunc = operator.slice(0, -1);
  26. if (!_core.types.LOGICAL_OPERATORS.includes(operatorTrunc)) {
  27. return;
  28. }
  29. const lhs = _core.types.cloneNode(left);
  30. if (_core.types.isMemberExpression(left)) {
  31. const {
  32. object,
  33. property,
  34. computed
  35. } = left;
  36. const memo = scope.maybeGenerateMemoised(object);
  37. if (memo) {
  38. left.object = memo;
  39. lhs.object = _core.types.assignmentExpression("=", _core.types.cloneNode(memo), object);
  40. }
  41. if (computed) {
  42. const memo = scope.maybeGenerateMemoised(property);
  43. if (memo) {
  44. left.property = memo;
  45. lhs.property = _core.types.assignmentExpression("=", _core.types.cloneNode(memo), property);
  46. }
  47. }
  48. }
  49. path.replaceWith(_core.types.logicalExpression(operatorTrunc, lhs, _core.types.assignmentExpression("=", left, right)));
  50. }
  51. }
  52. };
  53. });
  54. exports.default = _default;