acorn-walk
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Acorn-walk is an abstract syntax tree (AST) walker designed for the ESTree format. This package facilitates the recursion through a syntax tree by storing an algorithm as an object with respective properties for each tree node type. These properties, holding functions that persist through the node, allow Acorn-walk to walk over a tree and navigate through all statements and expressions. Acorn-walk provides varied ways with which you can run these walker operations.
To use Acorn-walk you will first need to install it, which can be easily done through npm by running the command npm install acorn-walk
. After installation, you can require the package along with the acorn
in your JavaScript file. Acorn-walk provides different methods for tree walking, for example:
Simple walk:
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 walk:
const acorn = require("acorn")
const walk = require("acorn-walk")
walk.ancestor(acorn.parse("foo('hi')"), {
Literal(_, ancestors) {
console.log("This literal's ancestors are:", ancestors.map(n => n.type))
}
})
Full walk:
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}`)
})
Each method functions differently according to the node and returning state as specified in the examples above.
For the detailed documentation on Acorn-walk, you can refer to the Acorn GitHub repository. Here you will find the comprehensive interface of the package along with guides and examples on how to interact with Acorn-walk. Further discussions, questions and bug reports are welcomed on Acorn's GitHub repository issues page or on the Tern discussion forum.