pugjs/babel-walk sits at 44 stars on GitHub, written primarily in TypeScript. Lightweight Babylon AST traversal tools.
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Babel supplies the wonderful babel-traverse module for walking Babel ASTs. Problem is, babel-traverse is very heavyweight, as it is designed to supply utilities to make all sorts of AST transformations possible. For simple AST walking without transformation, babel-traverse brings a lot of overhead.
This module loosely implements the API of Acorn parser's walk module, which is a lightweight AST walker for the ESTree AST format.
In my tests, babel-walk's ancestor walker (the most complex walker provided by this module) is about 8 times faster than babel-traverse, if the visitors are cached and the same AST is used for all runs. It is about 16 times faster if a fresh AST is used every run.
Installation
$ npm install babel-walk
API
var walk = require('babel-walk');
walk.simple(visitors)(node, state)
Do a simple walk over the AST. node should be the AST node to walk, and visitors an object containing Babel visitors. Each visitor function will be called as (node, state), where node is the AST node, and state is the same state passed to walk.simple.
When walk.simple is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding Visitor(node, state) {} to Visitor() { enter(node, state) {} }). This exploding process can take some time, so it is recommended to cache the result of calling walk.simple(visitors) and communicate state leveraging the state parameter.
All babel-types aliases (e.g. Expression) work, but the union syntax (e.g. 'Identifier|AssignmentPattern'(node, state) {}) does not.
walk.ancestor(visitors)(node, state)
Do a simple walk over the AST, but memoizing the ancestors of the node and making them available to the visitors. node should be the AST node to walk, and visitors an object containing Babel visitors. Each visitor function will be called as (node, state, ancestors), where node is the AST node, state is the same state passed to walk.ancestor, and ancestors is an array of ancestors to the node (with the outermost node being [0] and the current node being [ancestors.length - 1]). If state is not specified in the call to walk.ancestor, the state parameter will be set to ancestors.
When walk.ancestor is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding Visitor(node, state) {} to Visitor() { enter(node, state) {} }). This exploding process can take some time, so it is recommended to cache the result of calling walk.ancestor(visitors) and communicate state leveraging the state parameter.
All babel-types aliases (e.g. Expression) work, but the union syntax (e.g. 'Identifier|AssignmentPattern'(node, state) {}) does not.
walk.recursive(visitors)(node, state)
Do a recursive walk over the AST, where the visitors are responsible for continuing the walk on the child nodes of their target node. node should be the AST node to walk, and visitors an object containing Babel visitors. Each visitor function will be called as (node, state, c), where node is the AST node, state is the same state passed to walk.recursive, and c is a function that takes a single node as argument and continues walking that node. If no visitor for a node is provided, the default walker algorithm will still be used.
When walk.recursive is called with a fresh set of visitors, it will first "explode" the visitors (e.g. expanding Visitor(node, state) {} to Visitor() { enter(node, state) {} }). This exploding process can take some time, so it is recommended to cache the result of calling walk.recursive(visitors) and communicate state leveraging the state parameter.
Unlike other babel-walk walkers, walk.recursive does not call the exit visitor, only the enter (the default) visitor, of a specific node type.
All babel-types aliases (e.g. Expression) work, but the union syntax (e.g. 'Identifier|AssignmentPattern'(node, state) {}) does not.
In the following example, we are trying to count the number of functions in the outermost scope. This means, that we can simply walk all the statements and increment a counter if it is a function declaration or expression, and then stop walking. Note that we do not specify a visitor for the Program node, and the default algorithm for walking Program nodes is used (which is what we want). Also of note is how I bring the visitors object outside of countFunctions so that the object can be cached to improve performance.
import * as t from 'babel-types';
import {parse} from 'babel';
import * as walk from 'babel-walk';
const visitors = walk.recursive({
Statement(node, state, c) {
if (t.isVariableDeclaration(node)) {
for (let declarator of node.declarations) {
// Continue walking the declarator
c(declarator);
}
} else if (t.isFunctionDeclaration(node)) {
state.counter++;
}
},
VariableDeclarator(node, state) {
if (t.isFunction(node.init)) {
state.counter++;
}
},
});
function countFunctions(node) {
const state = {
counter: 0,
};
visitors(node, state);
return state.counter;
}
const ast = parse(`
// Counts
var a = () => {};
// Counts
function b() {
// Doesn't count
function c() {
}
}
// Counts
const c = function d() {};
`);
countFunctions(ast);
// = 3
Caveat
For those of you migrating from Acorn to Babel, there are a few things to be aware of.
The visitor caching suggestions do not apply to Acorn's walk module, but do for babel-walk.
babel-walk does not provide any of the other functions Acorn's walk module provides (e.g. make, findNode*).
babel-walk does not use a base variable. The walker algorithm is the same as what babel-traverse uses.
That means certain nodes that are not walked by Acorn, such as the property property of a non-computed MemberExpression, are walked by babel-walk.
TopGit's last sync did not record any GitHub topics for pugjs/babel-walk. GitHub topics appear in the right sidebar of a repository page; that's the authoritative place to check.
How active is development on pugjs/babel-walk?
The most recent commit recorded on pugjs/babel-walk was 2.0 years ago, based on the GitHub push timestamp. The repository has 6 forks — one of the better signals of community interest.
How many stars does pugjs/babel-walk have?
pugjs/babel-walk has 44 GitHub stars — refresh the page for the live number, or check github.com/pugjs/babel-walk. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
What is pugjs/babel-walk?
pugjs/babel-walk (pugjs/babel-walk) is a TypeScript project on GitHub. From the project's own README: Lightweight Babylon AST traversal tools.
What language is pugjs/babel-walk written in?
pugjs/babel-walk is written primarily in TypeScript. GitHub's language field is based on the largest share of bytes in the default branch.
Read full README in the tab above.
Want a second opinion on babel-walk?
Ask an AI that can read this page — one click and you get its take on babel-walk.