ECMAScript (ESTree) AST walker
npm install acorn-walkAn abstract syntax tree walker for the
ESTree format.
Acorn is open source software released under an
MIT license.
You are welcome to
report bugs or create pull
requests on github.
The easiest way to install acorn is from npm:
``sh`
npm install acorn-walk
Alternately, you can download the source and build acorn yourself:
`sh`
git clone https://github.com/acornjs/acorn.git
cd acorn
npm install
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 anode
tree. should be the AST node to walk, and visitors an objectbase
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. state
is a walker algorithm, and 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.)
`js
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.
`js
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'state
walk, where the walker functions are responsible for continuing the
walk on the child nodes of their target node. is the startfunctions
state, and should contain an object that maps node types(node, state, c)
to walker functions. Such functions are called with c
arguments, and can cause the walk to continue on a sub-node by calling
the argument on it with (node, state) arguments. The optionalbase argument provides the fallback walker functions for node typesfunctions
that aren't handled in the object. If not given, the
default walkers will be used.
make(functions, base) builds a new walker object by using thefunctions
walker functions in and filling in the missing ones bybase
taking defaults from .
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.
`js
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 locatetest
a node in a tree at the given start and/or end offsets, which
satisfies the predicate . start and end can be either nulltest
(as wildcard) or a number. may be a string (indicating a node(nodeType, node)
type) or a function that takes arguments andbase
returns a boolean indicating whether this node is interesting. state
and 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 likefindNodeAt, but will match any node that exists 'around' (spanning)
the given position.
findNodeAfter(node, pos, test, base, state) is similar tofindNodeAround`, but will match all nodes after the given position
(testing outer nodes before inner nodes).