Sha256: 3541df2b48ca7659db5059d6332c1b7726a72f63f4bd4dac3ddbe0ebe3471cb7

Contents?: true

Size: 1.87 KB

Versions: 7

Compression:

Stored size: 1.87 KB

Contents

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _helperPluginUtils = require("@babel/helper-plugin-utils");

var _core = require("@babel/core");

var _default = (0, _helperPluginUtils.declare)(api => {
  api.assertVersion(7);
  return {
    name: "transform-typeof-symbol",
    visitor: {
      Scope({
        scope
      }) {
        if (!scope.getBinding("Symbol")) {
          return;
        }

        scope.rename("Symbol");
      },

      UnaryExpression(path) {
        const {
          node,
          parent
        } = path;
        if (node.operator !== "typeof") return;

        if (path.parentPath.isBinaryExpression() && _core.types.EQUALITY_BINARY_OPERATORS.indexOf(parent.operator) >= 0) {
          const opposite = path.getOpposite();

          if (opposite.isLiteral() && opposite.node.value !== "symbol" && opposite.node.value !== "object") {
            return;
          }
        }

        const helper = this.addHelper("typeof");
        const isUnderHelper = path.findParent(path => {
          return path.isVariableDeclarator() && path.node.id === helper || path.isFunctionDeclaration() && path.node.id && path.node.id.name === helper.name;
        });

        if (isUnderHelper) {
          return;
        }

        const call = _core.types.callExpression(helper, [node.argument]);

        const arg = path.get("argument");

        if (arg.isIdentifier() && !path.scope.hasBinding(arg.node.name, true)) {
          const unary = _core.types.unaryExpression("typeof", _core.types.cloneNode(node.argument));

          path.replaceWith(_core.types.conditionalExpression(_core.types.binaryExpression("===", unary, _core.types.stringLiteral("undefined")), _core.types.stringLiteral("undefined"), call));
        } else {
          path.replaceWith(call);
        }
      }

    }
  };
});

exports.default = _default;

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
enju_library-0.3.8 spec/dummy/node_modules/@babel/plugin-transform-typeof-symbol/lib/index.js
condenser-0.2 lib/condenser/processors/node_modules/@babel/plugin-transform-typeof-symbol/lib/index.js
condenser-0.1 lib/condenser/processors/node_modules/@babel/plugin-transform-typeof-symbol/lib/index.js
condenser-0.0.12 lib/condenser/processors/node_modules/@babel/plugin-transform-typeof-symbol/lib/index.js
condenser-0.0.11 lib/condenser/processors/node_modules/@babel/plugin-transform-typeof-symbol/lib/index.js
condenser-0.0.10 lib/condenser/processors/node_modules/@babel/plugin-transform-typeof-symbol/lib/index.js
condenser-0.0.9 lib/condenser/processors/node_modules/@babel/plugin-transform-typeof-symbol/lib/index.js