Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

acorn-walk

acornjs161mMIT8.3.4TypeScript support: included

ECMAScript (ESTree) AST walker

readme

Acorn AST walker

An abstract syntax tree walker for the ESTree format.

Community

Acorn is open source software released under an MIT license.

You are welcome to report bugs or create pull requests on github.

Installation

The easiest way to install acorn is from npm:

npm install acorn-walk

Alternately, you can download the source and build acorn yourself:

git clone https://github.com/acornjs/acorn.git
cd acorn
npm install

Interface

An algorithm for recursing through a syntax tree is stored as an object, with a property for each tree node type holding a function that will recurse through such a node. There are several ways to run such a walker.

simple(node, visitors, base, state) does a 'simple' walk over a tree. node should be the AST node to walk, and visitors an object with properties whose names correspond to node types in the ESTree spec. The properties should contain functions that will be called with the node object and, if applicable the state at that point. The last two arguments are optional. base is a walker algorithm, and state is a start state. The default walker will simply visit all statements and expressions and not produce a meaningful state. (An example of a use of state is to track scope at each point in the tree.)

const acorn = require("acorn")
const walk = require("acorn-walk")

walk.simple(acorn.parse("let x = 10"), {
  Literal(node) {
    console.log(`Found a literal: ${node.value}`)
  }
})

ancestor(node, visitors, base, state) does a 'simple' walk over a tree, building up an array of ancestor nodes (including the current node) and passing the array to the callbacks as a third parameter.

const acorn = require("acorn")
const walk = require("acorn-walk")

walk.ancestor(acorn.parse("foo('hi')"), {
  Literal(_node, _state, ancestors) {
    console.log("This literal's ancestors are:", ancestors.map(n => n.type))
  }
})

recursive(node, state, functions, base) does a 'recursive' walk, where the walker functions are responsible for continuing the walk on the child nodes of their target node. state is the start state, and functions should contain an object that maps node types to walker functions. Such functions are called with (node, state, c) arguments, and can cause the walk to continue on a sub-node by calling the c argument on it with (node, state) arguments. The optional base argument provides the fallback walker functions for node types that aren't handled in the functions object. If not given, the default walkers will be used.

make(functions, base) builds a new walker object by using the walker functions in functions and filling in the missing ones by taking defaults from base.

full(node, callback, base, state) does a 'full' walk over a tree, calling the callback with the arguments (node, state, type) for each node

fullAncestor(node, callback, base, state) does a 'full' walk over a tree, building up an array of ancestor nodes (including the current node) and passing the array to the callbacks as a third parameter.

const acorn = require("acorn")
const walk = require("acorn-walk")

walk.full(acorn.parse("1 + 1"), node => {
  console.log(`There's a ${node.type} node at ${node.ch}`)
})

findNodeAt(node, start, end, test, base, state) tries to locate a node in a tree at the given start and/or end offsets, which satisfies the predicate test. start and end can be either null (as wildcard) or a number. test may be a string (indicating a node type) or a function that takes (nodeType, node) arguments and returns a boolean indicating whether this node is interesting. base and state are optional, and can be used to specify a custom walker. Nodes are tested from inner to outer, so if two nodes match the boundaries, the inner one will be preferred.

findNodeAround(node, pos, test, base, state) is a lot like findNodeAt, but will match any node that exists 'around' (spanning) the given position.

findNodeAfter(node, pos, test, base, state) is similar to findNodeAround, but will match all nodes after the given position (testing outer nodes before inner nodes).

changelog

8.3.4 (2024-09-09)

Bug fixes

Walk SwitchCase nodes as separate nodes.

8.3.3 (2024-01-11)

Bug fixes

Make acorn a dependency because acorn-walk uses the types from that package.

8.3.2 (2024-01-11)

Bug fixes

Add missing type for findNodeBefore.

8.3.1 (2023-12-06)

Bug fixes

Add Function and Class to the AggregateType type, so that they can be used in walkers without raising a type error.

Visitor functions are now called in such a way that their this refers to the object they are part of.

8.3.0 (2023-10-26)

New features

Use a set of new, much more precise, TypeScript types.

8.2.0 (2021-09-06)

New features

Add support for walking ES2022 class static blocks.

8.1.1 (2021-06-29)

Bug fixes

Include base in the type declarations.

8.1.0 (2021-04-24)

New features

Support node types for class fields and private methods.

8.0.2 (2021-01-25)

Bug fixes

Adjust package.json to work with Node 12.16.0 and 13.0-13.6.

8.0.0 (2021-01-05)

Bug fixes

Fix a bug where full and fullAncestor would skip nodes with overridden types.

8.0.0 (2020-08-12)

New features

The package can now be loaded directly as an ECMAScript module in node 13+.

7.2.0 (2020-06-17)

New features

Support optional chaining and nullish coalescing.

Support import.meta.

Add support for export * as ns from "source".

7.1.1 (2020-02-13)

Bug fixes

Clean up the type definitions to actually work well with the main parser.

7.1.0 (2020-02-11)

New features

Add a TypeScript definition file for the library.

7.0.0 (2017-08-12)

New features

Support walking ImportExpression nodes.

6.2.0 (2017-07-04)

New features

Add support for Import nodes.

6.1.0 (2018-09-28)

New features

The walker now walks TemplateElement nodes.

6.0.1 (2018-09-14)

Bug fixes

Fix bad "main" field in package.json.

6.0.0 (2018-09-14)

Breaking changes

This is now a separate package, acorn-walk, rather than part of the main acorn package.

The ScopeBody and ScopeExpression meta-node-types are no longer supported.

5.7.1 (2018-06-15)

Bug fixes

Make sure the walker and bin files are rebuilt on release (the previous release didn't get the up-to-date versions).

5.7.0 (2018-06-15)

Bug fixes

Fix crash in walker when walking a binding-less catch node.

5.6.2 (2018-06-05)

Bug fixes

In the walker, go back to allowing the baseVisitor argument to be null to default to the default base everywhere.

5.6.1 (2018-06-01)

Bug fixes

Fix regression when passing null as fourth argument to walk.recursive.

5.6.0 (2018-05-31)

Bug fixes

Fix a bug in the walker that caused a crash when walking an object pattern spread.

5.5.1 (2018-03-06)

Bug fixes

Fix regression in walker causing property values in object patterns to be walked as expressions.

5.5.0 (2018-02-27)

Bug fixes

Support object spread in the AST walker.

5.4.1 (2018-02-02)

Bug fixes

5.4.0 somehow accidentally included an old version of walk.js.

5.2.0 (2017-10-30)

Bug fixes

The full and fullAncestor walkers no longer visit nodes multiple times.

5.1.0 (2017-07-05)

New features

New walker functions full and fullAncestor.

3.2.0 (2016-06-07)

New features

Make it possible to use visit.ancestor with a walk state.

3.1.0 (2016-04-18)

New features

The walker now allows defining handlers for CatchClause nodes.

2.5.2 (2015-10-27)

Fixes

Fix bug where the walker walked an exported let statement as an expression.