Walk
@swc-next/walker provides pure JavaScript traversal in Node.js and browsers. @swc-next/core also exports walk.
Inputs and return value
The walk mutates the tree in place and returns the original root object. The root cannot be replaced or removed. Traversal is structural; it does not provide bindings, scope-aware renaming, or grammar repair.
Hooks
A node-type hook may be a function (enter only) or an object with enter and leave callbacks. Global enter and leave callbacks run for every node. Function and Class aliases group their supported syntax node types.
The order is global enter, alias enters in registration order, concrete enter, children, concrete leave, alias leaves in reverse order, and global leave. Hooks must return undefined. Async callbacks, unknown hook names, and unsupported node types throw. Exceptions propagate and edits are not rolled back.
WalkContext
Use the context only inside its receiving synchronous hook; it is reused and must not be retained. Call methods through the context object. Edit scalar values directly, and use context methods for structural changes to the current position. Replacement and inserted nodes must be valid for their destination.
createWalker
Import createWalker from @swc-next/walker. It validates and snapshots hooks once for reuse across trees. Each call to its walk method has a fresh context; later changes to the hooks object do not affect the compiled walker.
visitorKeys and types
visitorKeys is the frozen child-field table used by the walker, exported from @swc-next/walker. It skips metadata, comments, and arbitrary custom properties.
The package exports AstNode, NodeType, WalkHooks, WalkContext, and CompiledWalker types. Default hooks use a structural node type; supply a discriminated node union through WalkHooks<State, Node> for field-level typing. See AST types.