日本語のREADMEはこちらです: README.ja.md
Estraverse is an ECMAScript (JavaScript) AST traversal library. It provides functions to visit and manipulate nodes in an abstract syntax tree compliant with the ESTree specification. This project was originally part of the esmangle project.
npm install estraverseThe primary API is estraverse.traverse(), which accepts an AST and a visitor object. The visitor's enter and leave methods are called for each node in the tree.
import estraverse from 'estraverse';
import { parseScript } from 'esprima'; // Or any other ESTree-compliant parser
const ast = parseScript('const answer = 42;');
estraverse.traverse(ast, {
enter: function (node, parent) {
console.log('Entering:', node.type);
// 'Program'
// 'VariableDeclaration'
// 'VariableDeclarator'
// 'Identifier'
// 'Literal'
},
leave: function (node, parent) {
console.log('Leaving:', node.type);
// 'Identifier'
// 'Literal'
// 'VariableDeclarator'
// 'VariableDeclaration'
// 'Program'
}
});Traverses the AST and calls the visitor's enter and/or leave functions for each node.
Traverses the AST and allows nodes to be replaced. If an enter or leave function returns a valid