How to use the babel-traverse.visitors function in babel-traverse

To help you get started, we’ve selected a few babel-traverse examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bradtraversy / youtube_es2015_source / 01_babel / node_modules / babel-plugin-transform-es2015-classes / lib / vanilla.js View on Github external
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var buildDerivedConstructor = /*istanbul ignore next*/(0, _babelTemplate2.default)( /*istanbul ignore next*/"\n  (function () {\n    super(...arguments);\n  })\n"); /* eslint max-len: 0 */

var noMethodVisitor = { /*istanbul ignore next*/
  "FunctionExpression|FunctionDeclaration": function FunctionExpressionFunctionDeclaration(path) {
    if (!path.is("shadow")) {
      path.skip();
    }
  },
  /*istanbul ignore next*/Method: function Method(path) {
    path.skip();
  }
};

var verifyConstructorVisitor = /*istanbul ignore next*/_babelTraverse.visitors.merge([noMethodVisitor, { /*istanbul ignore next*/
  Super: function Super(path) {
    if (this.isDerived && !this.hasBareSuper && !path.parentPath.isCallExpression({ callee: path.node })) {
      throw path.buildCodeFrameError("'super.*' is not allowed before super()");
    }
  },


  CallExpression: { /*istanbul ignore next*/
    exit: function exit(path) {
      if (path.get("callee").isSuper()) {
        this.hasBareSuper = true;

        if (!this.isDerived) {
          throw path.buildCodeFrameError("super() is only allowed in a derived constructor");
        }
      }
github TeamFleet / WhoCallsTheFleet / dev-output / js-source / node_modules / babel-core / lib / transformation / transformers / es6 / parameters / index.js View on Github external
var destructuring = _interopRequireWildcard(_destructuring);

var _default = require("./default");

var def = _interopRequireWildcard(_default);

var _rest = require("./rest");

var rest = _interopRequireWildcard(_rest);

var metadata = {
  group: "builtin-advanced"
};

exports.metadata = metadata;
var visitor = _babelTraverse.visitors.merge([{
  ArrowFunctionExpression: function ArrowFunctionExpression(path) {
    // default/rest visitors require access to `arguments`
    var params /*: Array*/ = path.get("params");
    for (var _iterator = params, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _getIterator(_iterator);;) {
      var _ref;

      if (_isArray) {
        if (_i >= _iterator.length) break;
        _ref = _iterator[_i++];
      } else {
        _i = _iterator.next();
        if (_i.done) break;
        _ref = _i.value;
      }

      var param = _ref;
github babel / babel / packages / babel-core / src / transformation / transformers / es6 / block-scoping / index.js View on Github external
function traverseReplace(node, parent, scope, remaps) {
  if (t.isIdentifier(node)) {
    replace(node, parent, scope, remaps);
  }

  if (t.isAssignmentExpression(node)) {
    var ids = t.getBindingIdentifiers(node);
    for (var name in ids) {
      replace(ids[name], parent, scope, remaps);
    }
  }

  scope.traverse(node, replaceVisitor, remaps);
}

var letReferenceBlockVisitor = traverse.visitors.merge([{
  Function(node, parent, scope, state) {
    this.traverse(letReferenceFunctionVisitor, state);
    return this.skip();
  }
}, tdzVisitor]);

var letReferenceFunctionVisitor = traverse.visitors.merge([{
  ReferencedIdentifier(node, parent, scope, state) {
    var ref = state.letReferences[node.name];

    // not a part of our scope
    if (!ref) return;

    // this scope has a variable with the same name so it couldn't belong
    // to our let scope
    var localBinding = scope.getBindingIdentifier(node.name);
github babel / babel / packages / babel-core / src / transformation / file / index.js View on Github external
const pass = new PluginPass(this, plugin.key, pluginOpts);

        passPairs.push([plugin, pass]);
        passes.push(pass);
        visitors.push(plugin.visitor);
      }

      for (const [plugin, pass] of passPairs) {
        const fn = plugin.pre;
        if (fn) fn.call(pass, this);
      }

      debug(this.opts, "Start transform traverse");

      // merge all plugin visitors into a single visitor
      const visitor = traverse.visitors.merge(
        visitors,
        passes,
        this.opts.wrapPluginVisitorMethod,
      );
      traverse(this.ast, visitor, this.scope);

      debug(this.opts, "End transform traverse");

      for (const [plugin, pass] of passPairs) {
        const fn = plugin.post;
        if (fn) fn.call(pass, this);
      }
    }

    return this.generate();
  }
github codemix / babel-plugin-macros / src / visitors.js View on Github external
const node = path.node,
      parent1 = path.parentPath,
      parent2 = parent1.parentPath
      ;
    if (
      t.isLabeledStatement(parent1) && parent1.node.label.name === 'macro' ||
      t.isLabeledStatement(parent2) && parent2.node.label.name === 'macro'
    ) {
      warning(`FunctionDeclaration converted to macro. Incompatible with native JS behaviour${getLocationMessage(node)}`);
      Macro.register(node.id.name, path, parent2.scope);
      path.remove();
    }
  }
};

export const collectMacros = traverse.visitors.merge([
  collectMacros1,
  collectMacros2
]);

export const processMacros = {
  CallExpression(path, state) {
    "use strict";
    const {node} = path;
    if (node[$processedByMacro]) {
      return;
    }
    _processMacro(path, state || false);
    node[$processedByMacro] = true;
  }
};
github codemix / babel-plugin-macros / src / Macro.js View on Github external
constructor({name, macroBody, scope, location}) {
    location = location || '';
    traverse(
      macroBody,
      traverse.visitors.merge([
        collectMacros,
        {
          ThisExpression() {
            throw new Error("Can not use `this` in macro" + location);
          },
          Identifier({node}) {
            if ("arguments" === node.name) {
              throw new Error("Can not use `arguments` in macro" + location);
            }
          }
        }
      ]),
      scope
    );
    this.name = name;
    this.macroBody = macroBody;
github mendersoftware / gui / node_modules / babel-plugin-transform-es2015-classes / lib / vanilla.js View on Github external
throw path.buildCodeFrameError("super() is only allowed in a derived constructor");
        }
      }
    }
  },

  ThisExpression: function ThisExpression(path) {
    if (this.isDerived && !this.hasBareSuper) {
      if (!path.inShadow("this")) {
        throw path.buildCodeFrameError("'this' is not allowed before super()");
      }
    }
  }
}]);

var findThisesVisitor = _babelTraverse.visitors.merge([noMethodVisitor, {
  ThisExpression: function ThisExpression(path) {
    this.superThises.push(path);
  }
}]);

var ClassTransformer = function () {
  function ClassTransformer(path, file) {
    (0, _classCallCheck3.default)(this, ClassTransformer);

    this.parent = path.parent;
    this.scope = path.scope;
    this.node = path.node;
    this.path = path;
    this.file = file;

    this.clearDescriptors();
github codemix / babel-plugin-macros / src / builtin.js View on Github external
const name = id && id.name;
  const macroBody = node.arguments[1];
  const subScope = path.get('arguments')[1].scope;
  const location = node.loc ? `, at Line: ${node.loc.start.line} Column: ${node.loc.start.column}` : '';
  if (!(id && t.isIdentifier(id))) {
    throw new Error("First argument to DEFINE_MACRO must be an identifier" + location);
  }
  if (!t.isFunction(macroBody)) {
    throw new Error("Second argument to DEFINE_MACRO must be a FunctionExpression or ArrowFunctionExpression" + location);
  }

  scope[$registeredMacros] = scope[$registeredMacros] || {};
  scope[$registeredMacros][name] = new Macro({name: name, macroBody, scope: subScope});
  traverse(
    macroBody,
    traverse.visitors.merge([
      collectMacros,
      {
        ThisExpression() {
          throw new Error("Can not use `this` in macro" + location);
        },
        Identifier({node}) {
          if ("arguments" === node.name) {
            throw new Error("Can not use `arguments` in macro" + location);
          }
        }
      }
    ]),
    subScope
  );
  path.remove();
};
github elyukai / optolith-client / node_modules / babel-plugin-transform-es2015-classes / lib / vanilla.js View on Github external
throw path.buildCodeFrameError("super() is only allowed in a derived constructor");
        }
      }
    }
  },

  ThisExpression: function ThisExpression(path) {
    if (this.isDerived && !this.hasBareSuper) {
      if (!path.inShadow("this")) {
        throw path.buildCodeFrameError("'this' is not allowed before super()");
      }
    }
  }
}]);

var findThisesVisitor = _babelTraverse.visitors.merge([noMethodVisitor, {
  ThisExpression: function ThisExpression(path) {
    this.superThises.push(path);
  }
}]);

var ClassTransformer = function () {
  function ClassTransformer(path, file) {
    (0, _classCallCheck3.default)(this, ClassTransformer);

    this.parent = path.parent;
    this.scope = path.scope;
    this.node = path.node;
    this.path = path;
    this.file = file;

    this.clearDescriptors();
github mendersoftware / gui / node_modules / babel-plugin-transform-es2015-parameters / lib / index.js View on Github external
exports.default = function () {
  return {
    visitor: _babelTraverse.visitors.merge([{
      ArrowFunctionExpression: function ArrowFunctionExpression(path) {
        var params = path.get("params");
        for (var _iterator = params, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
          var _ref;

          if (_isArray) {
            if (_i >= _iterator.length) break;
            _ref = _iterator[_i++];
          } else {
            _i = _iterator.next();
            if (_i.done) break;
            _ref = _i.value;
          }

          var param = _ref;