ECMAScript (ESTree) AST walker
npx @tessl/cli install tessl/npm-acorn-walk@7.1.0Acorn Walk is an abstract syntax tree (AST) walker for JavaScript code that conforms to the ESTree specification. It provides multiple traversal algorithms for analyzing and transforming JavaScript ASTs, including simple node visiting, recursive walks with custom control, ancestor tracking, and position-based node searching.
npm install acorn-walkimport { simple, ancestor, recursive, full, fullAncestor, findNodeAt, findNodeAround, findNodeAfter, findNodeBefore, make, base } from "acorn-walk";For CommonJS:
const { simple, ancestor, recursive, full, fullAncestor, findNodeAt, findNodeAround, findNodeAfter, findNodeBefore, make, base } = require("acorn-walk");import * as acorn from "acorn";
import { simple, findNodeAt } from "acorn-walk";
// Parse JavaScript code into an AST
const ast = acorn.parse("let x = 10; console.log(x);");
// Simple walk - visit all literal nodes
simple(ast, {
Literal(node) {
console.log(`Found literal: ${node.value}`);
}
});
// Find specific node at position
const found = findNodeAt(ast, 4, 10);
if (found) {
console.log(`Node at position: ${found.node.type}`);
}Acorn Walk is built around several key components:
Core traversal algorithms for walking JavaScript ASTs with different callback patterns and state management approaches.
function simple<TState>(
node: Node,
visitors: SimpleVisitors<TState>,
baseVisitor?: RecursiveVisitors<TState>,
state?: TState,
override?: string
): void;
function ancestor<TState>(
node: Node,
visitors: AncestorVisitors<TState>,
baseVisitor?: RecursiveVisitors<TState>,
state?: TState,
override?: string
): void;
function recursive<TState>(
node: Node,
state: TState,
functions: RecursiveVisitors<TState>,
baseVisitor?: RecursiveVisitors<TState>,
override?: string
): void;
function full<TState>(
node: Node,
callback: FullWalkerCallback<TState>,
baseVisitor?: RecursiveVisitors<TState>,
state?: TState,
override?: string
): void;
function fullAncestor<TState>(
node: Node,
callback: FullAncestorWalkerCallback<TState>,
baseVisitor?: RecursiveVisitors<TState>,
state?: TState
): void;Position-based search utilities for locating specific nodes within an AST based on source code positions and type predicates.
function findNodeAt<TState>(
node: Node,
start: number | null,
end?: number | null,
test?: string | FindPredicate,
baseVisitor?: RecursiveVisitors<TState>,
state?: TState
): Found<TState> | undefined;
function findNodeAround<TState>(
node: Node,
pos: number,
test?: string | FindPredicate,
baseVisitor?: RecursiveVisitors<TState>,
state?: TState
): Found<TState> | undefined;
function findNodeAfter<TState>(
node: Node,
pos: number,
test?: string | FindPredicate,
baseVisitor?: RecursiveVisitors<TState>,
state?: TState
): Found<TState> | undefined;
function findNodeBefore<TState>(
node: Node,
pos: number,
test?: string | FindPredicate,
baseVisitor?: RecursiveVisitors<TState>,
state?: TState
): Found<TState> | undefined;Utility functions for creating custom walkers and accessing the default base walker containing handlers for all ESTree node types.
function make<TState>(
functions: RecursiveVisitors<TState>,
baseVisitor?: RecursiveVisitors<TState>
): RecursiveVisitors<TState>;
const base: RecursiveVisitors<any>;type SimpleWalkerFn<TState> = (node: Node, state: TState) => void;
type AncestorWalkerFn<TState> = (
node: Node,
state: TState | Node[],
ancestors: Node[]
) => void;
type RecursiveWalkerFn<TState> = (
node: Node,
state: TState,
callback: WalkerCallback<TState>
) => void;
type FullWalkerCallback<TState> = (
node: Node,
state: TState,
type: string
) => void;
type FullAncestorWalkerCallback<TState> = (
node: Node,
state: TState | Node[],
ancestors: Node[],
type: string
) => void;
type SimpleVisitors<TState> = {
[type: string]: SimpleWalkerFn<TState>;
};
type AncestorVisitors<TState> = {
[type: string]: AncestorWalkerFn<TState>;
};
type RecursiveVisitors<TState> = {
[type: string]: RecursiveWalkerFn<TState>;
};
type FindPredicate = (type: string, node: Node) => boolean;
interface Found<TState> {
node: Node;
state: TState;
}
interface Node {
type: string;
start?: number;
end?: number;
[key: string]: any;
}TypeScript Definition Note: The official TypeScript definitions for acorn-walk are incomplete. The findNodeBefore function is missing from the definitions, findNodeAfter is incorrectly defined, and the base constant is not exported. This documentation reflects the actual source code implementation.