or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdsearch-functions.mdwalker-utilities.mdwalking-functions.md
tile.json

index.mddocs/

Acorn Walk

Acorn 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.

Package Information

  • Package Name: acorn-walk
  • Package Type: npm
  • Language: JavaScript (with TypeScript definitions)
  • Installation: npm install acorn-walk

Core Imports

import { 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");

Basic Usage

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}`);
}

Architecture

Acorn Walk is built around several key components:

  • Walker Functions: Different algorithms for traversing ASTs (simple, ancestor, recursive, full)
  • Base Walker Object: Default handlers for all ESTree node types that define traversal behavior
  • Visitor Pattern: Callback-based system where user functions are called for matching node types
  • Search Functions: Position-based node finding utilities for code analysis tools
  • State Management: Optional state threading through traversal for accumulating data

Capabilities

AST Walking Functions

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;

AST Walking Functions

Node Search Functions

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;

Node Search Functions

Walker Utilities

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>;

Walker Utilities

Types

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.