191c900689857299c50ecc8ccb4b18d2c6f2df4b7431ac07dfdf506a4087a3ff5ce4779c2e9c39fbc2b25e14a6dc395d8aa731e0853fb6414b5ae728d34820 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. exports.__esModule = true;
  3. var _symbol = require("babel-runtime/core-js/symbol");
  4. var _symbol2 = _interopRequireDefault(_symbol);
  5. exports.default = function (_ref) {
  6. var t = _ref.types;
  7. var IGNORE = (0, _symbol2.default)();
  8. return {
  9. visitor: {
  10. Scope: function Scope(_ref2) {
  11. var scope = _ref2.scope;
  12. if (!scope.getBinding("Symbol")) {
  13. return;
  14. }
  15. scope.rename("Symbol");
  16. },
  17. UnaryExpression: function UnaryExpression(path) {
  18. var node = path.node,
  19. parent = path.parent;
  20. if (node[IGNORE]) return;
  21. if (path.find(function (path) {
  22. return path.node && !!path.node._generated;
  23. })) return;
  24. if (path.parentPath.isBinaryExpression() && t.EQUALITY_BINARY_OPERATORS.indexOf(parent.operator) >= 0) {
  25. var opposite = path.getOpposite();
  26. if (opposite.isLiteral() && opposite.node.value !== "symbol" && opposite.node.value !== "object") {
  27. return;
  28. }
  29. }
  30. if (node.operator === "typeof") {
  31. var call = t.callExpression(this.addHelper("typeof"), [node.argument]);
  32. if (path.get("argument").isIdentifier()) {
  33. var undefLiteral = t.stringLiteral("undefined");
  34. var unary = t.unaryExpression("typeof", node.argument);
  35. unary[IGNORE] = true;
  36. path.replaceWith(t.conditionalExpression(t.binaryExpression("===", unary, undefLiteral), undefLiteral, call));
  37. } else {
  38. path.replaceWith(call);
  39. }
  40. }
  41. }
  42. }
  43. };
  44. };
  45. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  46. module.exports = exports["default"];