How to use the babel-traverse.cheap 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 interlockjs / interlock / src / server / hot-patch-plugin / index.js View on Github external
transform("constructRuntime", runtimeBody => {
    // Attach `hot` property to module in `require`.
    traverse.cheap(runtimeBody, node => {
      if (
        node.type === "ObjectProperty" &&
        node.key.name === "require"
      ) {
        const fnBody = node.value.body.body;
        const moduleObjIdx = fnBody.findIndex(fnBody, bodyNode =>
          bodyNode.type === "VariableDeclaration" &&
          bodyNode.declarations[0] &&
          bodyNode.declarations[0].id.name === "moduleObj"
        );
        fnBody.splice(moduleObjIdx + 1, 0, moduleHot);
      }
    });

    // Attach the hot-patch run-time to the end of the run-time body.
    return runtimeBody.concat(hotPatchRuntime);
github interlockjs / interlock / src / util / template.js View on Github external
let getAst = function () {
    let ast;

    try {
      ast = babylon.parse(code, {
        allowReturnOutsideFunction: true,
        allowSuperOutsideMethod: true
      });

      traverse.cheap(ast, function (node) {
        clearNode(node);
        if (node.leadingComments) { node.leadingComments.forEach(clearNode); }
        if (node.trailingComments) { node.trailingComments.forEach(clearNode); }
        node[FROM_TEMPLATE] = true;
      });
    } catch (err) {
      err.stack = `${err.stack}from\n${stack}`;
      throw err;
    }

    getAst = function () {
      return ast;
    };

    return ast;
  };
github GeekyAnts / vue-native-core / src / platforms / vue-native / scripts / transformerPlugin.js View on Github external
(consumer) => {
      traverse.cheap(babelAst, node => {
        if (node.loc) {
          const originalStart = consumer.originalPositionFor(node.loc.start);
          if (originalStart.line) {
            node.loc.start.line = originalStart.line;
            node.loc.start.column = originalStart.column;
          }
          const originalEnd = consumer.originalPositionFor(node.loc.end);
          if (originalEnd.line) {
            node.loc.end.line = originalEnd.line;
            node.loc.end.column = originalEnd.column;
          }
        }
      });
    }
  );
github interlockjs / interlock / src / compile / modules / update-references.js View on Github external
export default pluggable(function updateReferences (module) {
  traverse.cheap(module.ast, node => {
    if (
      node.type === "CallExpression" &&
      node.callee.name === "require"
    ) {
      const originalVal = node.arguments[0].value;
      const correspondingModule = module.dependenciesByInternalRef[originalVal];
      node.arguments[0].value = correspondingModule.hash;
      node.arguments[0].raw = `"${correspondingModule.hash}"`;
    }
  });

  return assign({}, module, { ast: module.ast });
});