Simple but flexible AST traversal with pre and post visitors. Works in node and browsers.
// ast is a Mozilla Parser API compatible structure// generated by Esprima or another parservar ast = ;var traverse = ;// print AST node types, pre-order (node first, then its children);console;/*=>ProgramExpressionStatement from parent Program via bodyBinaryExpression from parent ExpressionStatement via expressionCallExpression from parent BinaryExpression via leftIdentifier from parent CallExpression via calleeLiteral from parent CallExpression via argumentsIdentifier from parent CallExpression via argumentsLiteral from parent BinaryExpression via right*/// you can also visit post-order, or both// all four arguments are provided to both visitors (left out unused below)var indent = 0;;console;/*=>ProgramExpressionStatementBinaryExpressionCallExpressionIdentifierLiteralIdentifierLiteral*/// return false from the pre-visitor to skip traversing its children// throw an exception to abort traversal// by default node property names beginning with $ are skipped// but you can supply your own skipProperty function instead;/*=>ProgramExpressionStatement*/
Install using npm
npm install ast-traverse
var traverse = ;
Clone the repo and include it in a script tag
git clone https://github.com/olov/ast-traverse.git