Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

80
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda