<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Acorn AST walker——模具行业从业者必备知识手册]]></title><description><![CDATA[<p dir="auto">&lt;h2&gt;Acorn AST walker&lt;/h2&gt;</p>
<p dir="auto">&lt;p&gt;An abstract syntax tree walker for the&lt;/p&gt;<br />
&lt;p&gt;&lt;a href="<a href="https://github.com/estree/estree" rel="nofollow ugc">https://github.com/estree/estree</a>"&gt;ESTree&lt;/a&gt; format.&lt;/p&gt;</p>
<p dir="auto">&lt;h3&gt;Community&lt;/h3&gt;</p>
<p dir="auto">&lt;p&gt;Acorn is open source software released under an&lt;/p&gt;<br />
&lt;p&gt;&lt;a href="<a href="https://github.com/acornjs/acorn/blob/master/acorn-walk/LICENSE" rel="nofollow ugc">https://github.com/acornjs/acorn/blob/master/acorn-walk/LICENSE</a>"&gt;MIT license&lt;/a&gt;.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;You are welcome to&lt;/p&gt;<br />
&lt;p&gt;&lt;a href="<a href="https://github.com/acornjs/acorn/issues" rel="nofollow ugc">https://github.com/acornjs/acorn/issues</a>"&gt;report bugs&lt;/a&gt; or create pull&lt;/p&gt;<br />
&lt;p&gt;requests on &lt;a href="<a href="https://github.com/acornjs/acorn" rel="nofollow ugc">https://github.com/acornjs/acorn</a>"&gt;github&lt;/a&gt;. For questions&lt;/p&gt;<br />
&lt;p&gt;and discussion, please use the&lt;/p&gt;<br />
&lt;p&gt;&lt;a href="<a href="https://discuss.ternjs.net" rel="nofollow ugc">https://discuss.ternjs.net</a>"&gt;Tern discussion forum&lt;/a&gt;.&lt;/p&gt;</p>
<p dir="auto">&lt;h3&gt;Installation&lt;/h3&gt;</p>
<p dir="auto">&lt;p&gt;The easiest way to install acorn is from &lt;a href="<a href="https://www.npmjs.com/" rel="nofollow ugc">https://www.npmjs.com/</a>"&gt;&lt;code&gt;npm&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
npm install acorn-walk<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;Alternately, you can download the source and build acorn yourself:&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
git clone <a href="https://github.com/acornjs/acorn.git" rel="nofollow ugc">https://github.com/acornjs/acorn.git</a><br />
cd acorn<br />
npm install<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;h3&gt;Interface&lt;/h3&gt;</p>
<p dir="auto">&lt;p&gt;An algorithm for recursing through a syntax tree is stored as an&lt;/p&gt;<br />
&lt;p&gt;object, with a property for each tree node type holding a function&lt;/p&gt;<br />
&lt;p&gt;that will recurse through such a node. There are several ways to run&lt;/p&gt;<br />
&lt;p&gt;such a walker.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;&lt;strong&gt;simple&lt;/strong&gt;&lt;code&gt;(node, visitors, base, state)&lt;/code&gt; does a 'simple' walk over a&lt;/p&gt;<br />
&lt;p&gt;tree. &lt;code&gt;node&lt;/code&gt; should be the AST node to walk, and &lt;code&gt;visitors&lt;/code&gt; an object&lt;/p&gt;<br />
&lt;p&gt;with properties whose names correspond to node types in the <a href="https://github.com/estree/estree" rel="nofollow ugc">ESTree&lt;/p&gt;<br />
&lt;p&gt;spec</a>. The properties should contain&lt;/p&gt;<br />
&lt;p&gt;functions that will be called with the node object and, if applicable&lt;/p&gt;<br />
&lt;p&gt;the state at that point. The last two arguments are optional. &lt;code&gt;base&lt;/code&gt;&lt;/p&gt;<br />
&lt;p&gt;is a walker algorithm, and &lt;code&gt;state&lt;/code&gt; is a start state. The default&lt;/p&gt;<br />
&lt;p&gt;walker will simply visit all statements and expressions and not&lt;/p&gt;<br />
&lt;p&gt;produce a meaningful state. (An example of a use of state is to track&lt;/p&gt;<br />
&lt;p&gt;scope at each point in the tree.)&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
const acorn = require("acorn")<br />
const walk = require("acorn-walk")</p>
<p dir="auto">walk.simple(acorn.parse("let x = 10"), {<br />
Literal(node) {<br />
console.log(<code>Found a literal: ${node.value}</code>)<br />
}<br />
})<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;&lt;strong&gt;ancestor&lt;/strong&gt;&lt;code&gt;(node, visitors, base, state)&lt;/code&gt; does a 'simple' walk over&lt;/p&gt;<br />
&lt;p&gt;a tree, building up an array of ancestor nodes (including the current node)&lt;/p&gt;<br />
&lt;p&gt;and passing the array to the callbacks as a third parameter.&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
const acorn = require("acorn")<br />
const walk = require("acorn-walk")</p>
<p dir="auto">walk.ancestor(acorn.parse("foo('hi')"), {<br />
Literal(_, ancestors) {<br />
console.log("This literal's ancestors are:", ancestors.map(n =&gt; n.type))<br />
}<br />
})<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;&lt;strong&gt;recursive&lt;/strong&gt;&lt;code&gt;(node, state, functions, base)&lt;/code&gt; does a 'recursive'&lt;/p&gt;<br />
&lt;p&gt;walk, where the walker functions are responsible for continuing the&lt;/p&gt;<br />
&lt;p&gt;walk on the child nodes of their target node. &lt;code&gt;state&lt;/code&gt; is the start&lt;/p&gt;<br />
&lt;p&gt;state, and &lt;code&gt;functions&lt;/code&gt; should contain an object that maps node types&lt;/p&gt;<br />
&lt;p&gt;to walker functions. Such functions are called with &lt;code&gt;(node, state, c)&lt;/code&gt;&lt;/p&gt;<br />
&lt;p&gt;arguments, and can cause the walk to continue on a sub-node by calling&lt;/p&gt;<br />
&lt;p&gt;the &lt;code&gt;c&lt;/code&gt; argument on it with &lt;code&gt;(node, state)&lt;/code&gt; arguments. The optional&lt;/p&gt;<br />
&lt;p&gt;&lt;code&gt;base&lt;/code&gt; argument provides the fallback walker functions for node types&lt;/p&gt;<br />
&lt;p&gt;that aren't handled in the &lt;code&gt;functions&lt;/code&gt; object. If not given, the&lt;/p&gt;<br />
&lt;p&gt;default walkers will be used.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;&lt;strong&gt;make&lt;/strong&gt;&lt;code&gt;(functions, base)&lt;/code&gt; builds a new walker object by using the&lt;/p&gt;<br />
&lt;p&gt;walker functions in &lt;code&gt;functions&lt;/code&gt; and filling in the missing ones by&lt;/p&gt;<br />
&lt;p&gt;taking defaults from &lt;code&gt;base&lt;/code&gt;.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;&lt;strong&gt;full&lt;/strong&gt;&lt;code&gt;(node, callback, base, state)&lt;/code&gt; does a 'full' walk over a&lt;/p&gt;<br />
&lt;p&gt;tree, calling the callback with the arguments (node, state, type) for&lt;/p&gt;<br />
&lt;p&gt;each node&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;&lt;strong&gt;fullAncestor&lt;/strong&gt;&lt;code&gt;(node, callback, base, state)&lt;/code&gt; does a 'full' walk&lt;/p&gt;<br />
&lt;p&gt;over a tree, building up an array of ancestor nodes (including the&lt;/p&gt;<br />
&lt;p&gt;current node) and passing the array to the callbacks as a third&lt;/p&gt;<br />
&lt;p&gt;parameter.&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
const acorn = require("acorn")<br />
const walk</p>
<hr />
<p dir="auto">这个参数你们一般设多少？评论区说说。<br />
&lt;/code&gt;&lt;/pre&gt;</p>
]]></description><link>https://forum.mj7.cn/topic/7171/acorn-ast-walker-模具行业从业者必备知识手册</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 11:45:57 GMT</lastBuildDate><atom:link href="https://forum.mj7.cn/topic/7171.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 19 Apr 2026 21:45:58 GMT</pubDate><ttl>60</ttl></channel></rss>