npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@swc-next/walker

v0.8.1

Published

Synchronous ESTree traversal and editing for JavaScript and TypeScript

Downloads

34,341

Readme

@swc-next/walker

Traverse and edit JavaScript / TypeScript ESTree ASTs with synchronous hooks. Pure JavaScript with no runtime dependencies, native bindings, or WASM initialization. The same ESM package works in Node.js and browsers.

import { walk } from "@swc-next/walker";

walk(program, {
  DebuggerStatement(node, ctx) {
    ctx.remove();
  },
});

program is an existing AST. If you use @swc-next/core, you do not need to install @swc-next/walker separately. Import the walker APIs directly from @swc-next/core:

import { parseSync, generateSync, walk } from "@swc-next/core";

const { program } = parseSync("debugger; run();");
walk(program, { DebuggerStatement(node, ctx) { ctx.remove(); } });
const { code } = generateSync(program);
// run();

The walker does not parse, print, or orchestrate plugins. Parser-produced ASTs must use the supported ESTree node names and child fields described below.

walk(root, hooks, state?)

Walk an existing AST synchronously, mutate it in place, and return the original root. Parsing and printing are independent. Import from @swc-next/walker:

import { walk } from "@swc-next/walker";

const dependencies = [];
walk(program, {
  ImportDeclaration(node, ctx) {
    ctx.state.push(node.source.value);
  },
}, dependencies);

Hooks are keyed by ESTree node type. A function is an enter hook; an object may have enter and leave functions. Global enter / leave hooks apply to all nodes. Function groups function declarations, expressions, arrows, TSDeclareFunction, and TSEmptyBodyFunctionExpression; Class groups class declarations and expressions. These are syntax groups, not scope-aware queries.

walk(program, {
  CallExpression: {
    enter(node, ctx) {},
    leave(node, ctx) {},
  },
  Function(node, ctx) {},
  enter(node, ctx) {},
  leave(node, ctx) {},
});

The order is global enter, alias enters in registration order, concrete enter, children, concrete leave, alias leaves in reverse order, global leave. Unknown hook names and node types throw. Hooks must be synchronous and return undefined; use context methods for structural edits. Async hooks and other return values throw. Hook exceptions propagate; edits already made are not rolled back.

Child fields come from visitorKeys, generated alongside the codecs from this repository's AST schema with explicit ESTree projection rules. Their array order is the traversal order (not necessarily source order). Metadata, comments, raw text, and arbitrary plugin properties are not traversed. The frozen key table is also exported. Trees must be acyclic; shared node objects are visited once per position, not deduplicated.

Reuse hooks across files

import { createWalker } from "@swc-next/walker";

const walker = createWalker({
  DebuggerStatement(node, ctx) {
    if (ctx.index !== null) ctx.remove();
  },
});
for (const program of programs) walker.walk(program);

createWalker(hooks) validates and snapshots the hook functions once. Later changes to the hooks object do not affect it. Each walker.walk(root, state) creates a fresh context, supports nested calls, and clears traversal positions on return or failure. Captured callback closures remain alive with the compiled walker. Ordinary walk(root, hooks, state) continues to compile on every call; there is no implicit cache of mutable hook objects.

Context and edits

| API | Semantics | | --- | --- | | node | Current node, updated immediately by replace. The callback's node argument still refers to the original object. | | parent, key, index | Parent node and its child field; index is the current array index or null (also null after removal). All three are null at the root. | | state | The supplied state object; it may be mutated. | | ancestors() | A new array from the root to the parent, excluding the current node. Only the returned snapshot is allocated on demand. | | replace(node) | Replace a non-root node immediately and end the remaining hooks in the current phase. Enter does not run again on the replacement. After an enter replacement, walk its children and run leave hooks for its type. A leave replacement is not traversed. Missing start / end on the replacement root inherit the old values. | | remove() | Splice the current array element. During enter, skip its children and all leave hooks; during leave, stop remaining leave hooks. Later siblings are still visited. Root and scalar-field removal throw. | | insertBefore(nodeOrNodes) | Insert siblings in an array, in supplied/call order. They are not visited during this walk. | | insertAfter(nodeOrNodes) | Insert siblings in an array, in supplied/call order. They are visited later in this walk. | | skip() | Enter only: stop remaining enter hooks and skip children, but run the current node's leave hooks. | | stop() | Finish the current callback, then end the walk, without pending leave hooks. |

One context is reused across the synchronous hooks of each walk. Do not retain it. A saved reference aliases the position of later hooks, so stale-reference use during another hook cannot be detected. Getters and edit methods throw when no hook is active, including after completion or an exception. A nested walk has its own context and does not change the outer callback's position. Node, state, and ancestor snapshot references may be retained; they are ordinary objects. Call methods through ctx (for example ctx.remove()); they are not bound functions and must not be detached from their receiver.

The walker generates direct child accessors and separate scalar/list traversal. Node types without hooks use generated traversal functions; unobserved leaf nodes return immediately. Hooked nodes retain the editing checks below. Context storage is populated only for phases with hooks and reused within each walk. An ancestor stack scales with depth; ancestors() allocates a snapshot only when requested. No runtime code generation or eval is used.

Replace followed by skip traverses no replacement children. Remove or stop prevents further hooks for that node; removal makes subsequent structural edits in the same callback invalid. Inserting before/after then removing the current node is supported. An insertion does not end the current hook phase.

Edit scalar node data (such as an identifier's name) directly. Use the context for changes to the current position and sibling list. Do not splice, reorder, replace, or reparent an active ancestor's child container directly while walking. The walker checks the current position before context edits but does not detect every external mutation. To edit a scalar child, replace it with a valid node; to delete an optional child, set the parent's field to null in a parent hook.

This is structural traversal, without Babel's grammar repair, bindings, rename, reference tracking, or full schema validation. Replacements and inserted nodes must be valid for their destination. Removing the last required array item can produce an invalid AST. Source positions on synthetic descendants remain the caller's responsibility; copying a span does not provide source-map generation. Babel NodePath plugins are not directly compatible.

TypeScript

NodeType is generated from the supported ESTree projections. Default hooks narrow node.type; other fields use the structural AstNode interface. This first version does not generate complete per-node field declarations. Consumers can supply their own discriminated union with WalkHooks<State, Nodes> and walk<Root, State, Nodes>(root, hooks, state) for field-level checking.

Development

# Walker tests, including core parse/edit/print integration.
pnpm --filter @swc-next/core build
pnpm --filter @swc-next/core test:walk

Tests use Rstest and live in npm/core/test/walk. Root pnpm test and pnpm test:wasm include core and walker tests. Tests are excluded from the published package.