Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

79
Vistas
Traversing an AST callback when finished?

I have an AST created by babelParser. I want to traverse the nodes to create an array of nodes that match a certain criteria. The problem I seem to have is knowing when the traversing as reached its final point. The code would look something like this...

traverseNodes(ast, () => {
 // some act on each node that adds it to an array
}.then((arrayOfMatchNodes) => {
 // some action when ALL nodes have been handled.
}

What module could I use 'traverseNodes' here? Every example I have seen of traversing nodes, doesn't seem to have a callback for when its actually finished.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Traverse is synchronous so when it's done next code will be executed.

import parser from "@babel/parser";
import traverse from "@babel/traverse";

const code = `function square(n) {
    return n * n;
}`;

const ast = parser.parse(code);
const nodes = [];

traverse(ast, {
    Identifier(path) {
        nodes.push(path);
    }
});

// show all the nodes
console.log(nodes);

You can achieve it even simpler with help of my library 🐊Putout (no need to use parse and traverse):

import putout from 'putout';

const source = `function square(n) {
    return n * n;
}`;

const nodes = [];

putout(source, {
    plugins: [
        ['collect', {
            report: () => 'collect nodes',
            fix: (path) => nodes.push(path),
            include: () => [
                'Identifier'
            ]
        }]
    ]
});

console.log(nodes);

You can find more information about traversing and working with AST in Babel Handbook.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda