4d524fc21e9ba538e9be87a9e4ea52d8f11bb2490dfdbb2fdd8775e0d0f559549ca20840d192651268b55dd728f2519980047d1ed05ce460775666b082ea8c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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);
  10. return {
  11. name: "transform-typeof-symbol",
  12. visitor: {
  13. Scope({
  14. scope
  15. }) {
  16. if (!scope.getBinding("Symbol")) {
  17. return;
  18. }
  19. scope.rename("Symbol");
  20. },
  21. UnaryExpression(path) {
  22. const {
  23. node,
  24. parent
  25. } = path;
  26. if (node.operator !== "typeof") return;
  27. if (path.parentPath.isBinaryExpression() && _core.types.EQUALITY_BINARY_OPERATORS.includes(parent.operator)) {
  28. const opposite = path.getOpposite();
  29. if (opposite.isStringLiteral() && opposite.node.value !== "symbol" && opposite.node.value !== "object") {
  30. return;
  31. }
  32. }
  33. let isUnderHelper = path.findParent(path => {
  34. if (path.isFunctionDeclaration()) {
  35. var _path$get;
  36. return ((_path$get = path.get("body.directives.0")) == null ? void 0 : _path$get.node.value.value) === "@babel/helpers - typeof";
  37. }
  38. });
  39. if (isUnderHelper) return;
  40. const helper = this.addHelper("typeof");
  41. {
  42. isUnderHelper = path.findParent(path => {
  43. return path.isVariableDeclarator() && path.node.id === helper || path.isFunctionDeclaration() && path.node.id && path.node.id.name === helper.name;
  44. });
  45. if (isUnderHelper) {
  46. return;
  47. }
  48. }
  49. const call = _core.types.callExpression(helper, [node.argument]);
  50. const arg = path.get("argument");
  51. if (arg.isIdentifier() && !path.scope.hasBinding(arg.node.name, true)) {
  52. const unary = _core.types.unaryExpression("typeof", _core.types.cloneNode(node.argument));
  53. path.replaceWith(_core.types.conditionalExpression(_core.types.binaryExpression("===", unary, _core.types.stringLiteral("undefined")), _core.types.stringLiteral("undefined"), call));
  54. } else {
  55. path.replaceWith(call);
  56. }
  57. }
  58. }
  59. };
  60. });
  61. //# sourceMappingURL=index.js.map