Acorn AST walker——模具行业从业者必备知识手册
-
<h2>Acorn AST walker</h2>
<p>An abstract syntax tree walker for the</p>
<p><a href="https://github.com/estree/estree">ESTree</a> format.</p><h3>Community</h3>
<p>Acorn is open source software released under an</p>
<p><a href="https://github.com/acornjs/acorn/blob/master/acorn-walk/LICENSE">MIT license</a>.</p><p>You are welcome to</p>
<p><a href="https://github.com/acornjs/acorn/issues">report bugs</a> or create pull</p>
<p>requests on <a href="https://github.com/acornjs/acorn">github</a>. For questions</p>
<p>and discussion, please use the</p>
<p><a href="https://discuss.ternjs.net">Tern discussion forum</a>.</p><h3>Installation</h3>
<p>The easiest way to install acorn is from <a href="https://www.npmjs.com/"><code>npm</code></a>:</p>
<pre><code>
npm install acorn-walk
</code></pre><p>Alternately, you can download the source and build acorn yourself:</p>
<pre><code>
git clone https://github.com/acornjs/acorn.git
cd acorn
npm install
</code></pre><h3>Interface</h3>
<p>An algorithm for recursing through a syntax tree is stored as an</p>
<p>object, with a property for each tree node type holding a function</p>
<p>that will recurse through such a node. There are several ways to run</p>
<p>such a walker.</p><p><strong>simple</strong><code>(node, visitors, base, state)</code> does a 'simple' walk over a</p>
<p>tree. <code>node</code> should be the AST node to walk, and <code>visitors</code> an object</p>
<p>with properties whose names correspond to node types in the ESTree</p>
<p>spec. The properties should contain</p>
<p>functions that will be called with the node object and, if applicable</p>
<p>the state at that point. The last two arguments are optional. <code>base</code></p>
<p>is a walker algorithm, and <code>state</code> is a start state. The default</p>
<p>walker will simply visit all statements and expressions and not</p>
<p>produce a meaningful state. (An example of a use of state is to track</p>
<p>scope at each point in the tree.)</p><pre><code>
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})
}
})
</code></pre><p><strong>ancestor</strong><code>(node, visitors, base, state)</code> does a 'simple' walk over</p>
<p>a tree, building up an array of ancestor nodes (including the current node)</p>
<p>and passing the array to the callbacks as a third parameter.</p><pre><code>
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))
}
})
</code></pre><p><strong>recursive</strong><code>(node, state, functions, base)</code> does a 'recursive'</p>
<p>walk, where the walker functions are responsible for continuing the</p>
<p>walk on the child nodes of their target node. <code>state</code> is the start</p>
<p>state, and <code>functions</code> should contain an object that maps node types</p>
<p>to walker functions. Such functions are called with <code>(node, state, c)</code></p>
<p>arguments, and can cause the walk to continue on a sub-node by calling</p>
<p>the <code>c</code> argument on it with <code>(node, state)</code> arguments. The optional</p>
<p><code>base</code> argument provides the fallback walker functions for node types</p>
<p>that aren't handled in the <code>functions</code> object. If not given, the</p>
<p>default walkers will be used.</p><p><strong>make</strong><code>(functions, base)</code> builds a new walker object by using the</p>
<p>walker functions in <code>functions</code> and filling in the missing ones by</p>
<p>taking defaults from <code>base</code>.</p><p><strong>full</strong><code>(node, callback, base, state)</code> does a 'full' walk over a</p>
<p>tree, calling the callback with the arguments (node, state, type) for</p>
<p>each node</p><p><strong>fullAncestor</strong><code>(node, callback, base, state)</code> does a 'full' walk</p>
<p>over a tree, building up an array of ancestor nodes (including the</p>
<p>current node) and passing the array to the callbacks as a third</p>
<p>parameter.</p><pre><code>
const acorn = require("acorn")
const walk
这个参数你们一般设多少?评论区说说。
</code></pre>