index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. loose = false
  10. }) => {
  11. var _api$assumption;
  12. api.assertVersion("^7.0.0-0 || >8.0.0-alpha <8.0.0-beta");
  13. const noDocumentAll = (_api$assumption = api.assumption("noDocumentAll")) != null ? _api$assumption : loose;
  14. return {
  15. name: "transform-nullish-coalescing-operator",
  16. manipulateOptions: (_, parser) => parser.plugins.push("nullishCoalescingOperator"),
  17. visitor: {
  18. LogicalExpression(path) {
  19. const {
  20. node,
  21. scope
  22. } = path;
  23. if (node.operator !== "??") {
  24. return;
  25. }
  26. let ref;
  27. let assignment;
  28. if (scope.isStatic(node.left)) {
  29. ref = node.left;
  30. assignment = _core.types.cloneNode(node.left);
  31. } else if (scope.path.isPattern()) {
  32. path.replaceWith(_core.template.statement.ast`(() => ${path.node})()`);
  33. return;
  34. } else {
  35. ref = scope.generateUidIdentifierBasedOnNode(node.left);
  36. scope.push({
  37. id: _core.types.cloneNode(ref)
  38. });
  39. assignment = _core.types.assignmentExpression("=", ref, node.left);
  40. }
  41. path.replaceWith(_core.types.conditionalExpression(noDocumentAll ? _core.types.binaryExpression("!=", assignment, _core.types.nullLiteral()) : _core.types.logicalExpression("&&", _core.types.binaryExpression("!==", assignment, _core.types.nullLiteral()), _core.types.binaryExpression("!==", _core.types.cloneNode(ref), scope.buildUndefinedNode())), _core.types.cloneNode(ref), node.right));
  42. }
  43. }
  44. };
  45. });
  46. //# sourceMappingURL=index.js.map