Acorn工具 - 模具行业Web开发参考——模具行业从业者必备知识手册
-
<h2>Acorn</h2>
<p>A tiny, fast JavaScript parser written in JavaScript.</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/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>.</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
</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>Importing acorn</h3><p>ESM as well as CommonJS is supported for all 3: <code>acorn</code>, <code>acorn-walk</code> and <code>acorn-loose</code>.</p>
<p>ESM example for <code>acorn</code>:</p>
<pre><code>
import * as acorn from "acorn"
</code></pre><p>CommonJS example for <code>acorn</code>:</p>
<pre><code>
let acorn = require("acorn")
</code></pre><p>ESM is preferred, as it allows better editor auto-completions by offering TypeScript support.</p>
<p>For this reason, following examples will use ESM imports.</p><h3>Interface</h3>
<p><strong>parse</strong><code>(input, options)</code> is the main interface to the library. The</p>
<p><code>input</code> parameter is a string, <code>options</code> must be an object setting</p>
<p>some of the options listed below. The return value will be an abstract</p>
<p>syntax tree object as specified by the ESTree</p>
<p>spec.</p><pre><code>
import * as acorn from "acorn"
console.log(acorn.parse("1 + 1", {ecmaVersion: 2020}))
</code></pre><p>When encountering a syntax error, the parser will raise a</p>
<p><code>SyntaxError</code> object with a meaningful message. The error object will</p>
<p>have a <code>pos</code> property that indicates the string offset at which the</p>
<p>error occurred, and a <code>loc</code> object that contains a <code>{line, column}</code></p>
<p>object referring to that same position.</p><p>Options are provided by in a second argument, which should be an</p>
<p>object containing any of these fields (only <code>ecmaVersion</code> is</p>
<p>required):</p><ul>
<li><strong>ecmaVersion</strong>: Indicates the ECMAScript version to parse. Can be a</li>
</ul>
<p> number, either in year (<code>2022</code>) or plain version number (<code>6</code>) form,</p>
<p> or <code>"latest"</code> (the latest the library supports). This influences</p>
<p> support for strict mode, the set of reserved words, and support for</p>
<p> new syntax features.</p><p> <strong>NOTE</strong>: Only 'stage 4' (finalized) ECMAScript features are being</p>
<p> implemented by Acorn. Other proposed new features must be</p>
<p> implemented through plugins.</p><ul>
<li><strong>sourceType</strong>: Indicate the mode the code should be parsed in. Can be</li>
</ul>
<p> either <code>"script"</code>, <code>"module"</code> or <code>"commonjs"</code>. This influences global strict mode</p>
<p> and parsing of <code>import</code> and <code>export</code> declarations.</p><p> <strong>NOTE</strong>: If set to <code>"module"</code>, then static <code>import</code> / <code>export</code> syntax</p>
<p> will be valid, even if <code>ecmaVersion</code> is less than 6. If set to <code>"commonjs"</code>,</p>
<p> it is the same as <code>"script"</code> except that the top-level scope behaves like a function.</p><ul>
<li><strong>onInsertedSemicolon</strong>: If given a callback, that callback will be</li>
</ul>
<p> called whenever a missing semicolon is inserted by the parser. The</p>
<p> callback will be given the character offset of the point where the</p>
<p> semicolon is inserted as argument, and if <code>locations</code> is on, also a</p>
<p> <code>{line, column}</code> object representing this position.</p><ul>
<li><strong>onTrailingComma</strong>: Like <code>onInsertedSemicolon</code>, but for trailing</li>
</ul>
<p> commas.</p><ul>
<li><strong>allowReserved</strong>: If <code>false</code>, using a reserved word will generate</li>
</ul>
<p> an error. Defaults to <code>true</code> for <code>ecmaVersion</code> 3, <code>false</code> for higher</p>
<p> versions. When given the value <code>"never"</code>, reserved words and</p>
<p> keywords can also not be used as property names (as in Internet</p>
<p> Explorer's old parser).</p><p>-</p>
<hr>
<p>各位师傅觉得这个方案怎么样?有更好的做法吗?</p>