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

To help you get started, we’ve selected a few babel-core 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 kentcdodds / asts-workshop / other / old-exercises / 08_codemod.js View on Github external
function exportsAsESModule(modulePath) {
  const contents = fs.readFileSync(modulePath, 'utf8')
  try {
    const ast = babylon.parse(contents, {
      sourceType: 'module',
    })
    let hasExportSpecifier = false
    babel.traverse(ast, {
      ExportSpecifier(path) {
        hasExportSpecifier = true
        path.stop()
      },
    })
    return hasExportSpecifier
  } catch (error) {
    // eslint-disable-next-line no-console
    console.warn(`unable to parse "${modulePath}"`, error)
    return false
  }
}
github ifgyong / demo / React-native / Helloword / node_modules / metro-bundler / src / JSTransformer / worker / extract-dependencies.js View on Github external
function pushDependency(nodeArgs, parentType) {
        const arg = nodeArgs[0];
        if (nodeArgs.length != 1 || arg.type !== 'StringLiteral') {
            // Dynamic requires directly inside of a try statement are considered optional dependencies
            if (parentType === 'TryStatement') {
                return;
            }
            throw new Error(
                `require() must have a single string literal argument: ${filename}:${arg.loc.start.line - 1}`);

        }
        dependencyOffsets.push(arg.start);
        dependencies.add(arg.value);
    }

    babel.traverse(ast, {
        CallExpression(path) {
            const node = path.node;
            const callee = node.callee;
            const parent = path.scope.parentBlock;
            if (callee.type === 'Identifier' && callee.name === 'require') {
                pushDependency(node.arguments, parent.type);
            }
            if (callee.type !== 'MemberExpression') {
                return;
            }
            const obj = callee.object;
            const prop = callee.property;
            if (
                obj.type === 'Identifier' &&
                obj.name === 'require' &&
                !callee.computed &&
github tailhook / khufu / src / compiler.js View on Github external
export function compile(txt, options, wpack) {
    let opt = Object.assign({}, DEFAULT_OPTIONS, options)
    if(typeof opt.additional_class == 'function') {
        opt.additional_class = opt.additional_class(wpack)
    }
    opt.always_add_class = new Set(opt.always_add_class || []);
    let parse_tree = parser.parse(txt, opt);
    let ast = T.file(T.program([]));
    babel.traverse(ast, {
        Program: path => {
            parse_tree
                .filter(block => block[0] == 'view')
                .map(([_block, name]) => {
                    // Bind all views in scope, so they can call each other
                    // (including recursively)
                    set_var(path, name, T.identifier(name))
                })

            parse_tree.map(block => compile_block(block, path, opt))
        },
    });
    return ast;
}
github cmstead / js-refactor / modules / shared / scopeFinder.js View on Github external
function findFunctionNodeInAst(ast, selectionCoords) {
        let matchedScope = null;
        let lastScope = null;

        babelCore.traverse(ast, {
            enter: function(nodePath) {
                lastScope = /(Function|MethodDefinition)/.test(nodePath.type) ? nodePath.node : lastScope;
                matchedScope = coordsWithin(selectionCoords, nodePath.node) ? lastScope : matchedScope;
            }
        });

        return matchedScope;
    }
github stryker-mutator / stryker / packages / stryker-javascript-mutator / src / helpers / BabelParser.ts View on Github external
public static getNodes(ast: babel.types.File): NodeWithParent[] {
    const nodes: NodeWithParent[] = [];

    babel.traverse(ast, {
      enter(path: NodePath) {
        const node: NodeWithParent = path.node;
        node.parent = path.parent;
        Object.freeze(node);
        nodes.push(node);
      }
    });

    return nodes;
  }
github facebook / metro / packages / metro / src / ModuleGraph / worker / collect-dependencies.js View on Github external
function collectDependencies(
  ast,
  replacement,
  dependencyMapIdentifier,
): {
  dependencies: $ReadOnlyArray,
  dependencyMapName: string,
} {
  const visited = new WeakSet();
  const traversalState = {dependencyMapIdentifier};
  traverse(
    ast,
    {
      Program(path, state) {
        if (!state.dependencyMapIdentifier) {
          state.dependencyMapIdentifier = path.scope.generateUidIdentifier(
            'dependencyMap',
          );
        }
      },

      CallExpression(path, state) {
        const node = path.node;
        if (replacement.replaceImports && node.callee.type === 'Import') {
          const reqNode = processImportCall(path, node, replacement, state);
          visited.add(reqNode);
          return;
github babel / babel-eslint / acorn-to-esprima.js View on Github external
exports.toAST = function (ast) {
  ast.sourceType = "module";
  ast.range = [ast.start, ast.end];
  traverse(ast, astTransformVisitor);
};