@@ -26,6 +26,24 @@ git clone https://github.com/acornjs/acorn.git
2626cd acorn
2727npm install
2828```
29+ ## Importing acorn
30+
31+ ESM as well as CommonJS is supported for all 3: ` acorn ` , ` acorn-walk ` and ` acorn-loose ` .
32+
33+ ESM example for ` acorn ` :
34+
35+ ``` js
36+ import * as acorn from " acorn"
37+ ```
38+
39+ CommonJS example for ` acorn ` :
40+
41+ ``` js
42+ let acorn = require (" acorn" )
43+ ```
44+
45+ ESM is preferred, as it allows better editor auto-completions by offering TypeScript support.
46+ For this reason, following examples will use ESM imports.
2947
3048## Interface
3149
@@ -36,8 +54,8 @@ syntax tree object as specified by the [ESTree
3654spec] ( https://github.com/estree/estree ) .
3755
3856``` javascript
39- let acorn = require ( " acorn" );
40- console .log (acorn .parse (" 1 + 1" , {ecmaVersion: 2020 }));
57+ import * as acorn from " acorn"
58+ console .log (acorn .parse (" 1 + 1" , {ecmaVersion: 2020 }))
4159```
4260
4361When encountering a syntax error, the parser will raise a
@@ -61,11 +79,12 @@ required):
6179 implemented through plugins.
6280
6381- ** sourceType** : Indicate the mode the code should be parsed in. Can be
64- either ` "script" ` or ` "module " ` . This influences global strict mode
82+ either ` "script" ` , ` "module" ` or ` "commonjs " ` . This influences global strict mode
6583 and parsing of ` import ` and ` export ` declarations.
6684
6785 ** NOTE** : If set to ` "module" ` , then static ` import ` / ` export ` syntax
68- will be valid, even if ` ecmaVersion ` is less than 6.
86+ will be valid, even if ` ecmaVersion ` is less than 6. If set to ` "commonjs" ` ,
87+ it is the same as ` "script" ` except that the top-level scope behaves like a function.
6988
7089- ** onInsertedSemicolon** : If given a callback, that callback will be
7190 called whenever a missing semicolon is inserted by the parser. The
@@ -97,7 +116,7 @@ required):
97116 for ` ecmaVersion ` 2022 and later, ` false ` for lower versions.
98117 Setting this option to ` true ` allows to have top-level ` await `
99118 expressions. They are still not allowed in non-` async ` functions,
100- though.
119+ though. Setting this option to ` true ` is not allowed when ` sourceType: "commonjs" ` .
101120
102121- ** allowSuperOutsideMethod** : By default, ` super ` outside a method
103122 raises an error. Set this to ` true ` to accept such code.
@@ -217,7 +236,7 @@ for (let token of acorn.tokenizer(str)) {
217236}
218237
219238// transform code to array of tokens:
220- var tokens = [... acorn .tokenizer (str)];
239+ var tokens = [... acorn .tokenizer (str)]
221240```
222241
223242** tokTypes** holds an object mapping names to the token type objects
@@ -238,10 +257,10 @@ on the extended version of the class. To extend a parser with plugins,
238257you can use its static ` extend ` method.
239258
240259``` javascript
241- var acorn = require (" acorn" );
242- var jsx = require (" acorn-jsx" );
243- var JSXParser = acorn .Parser .extend (jsx ());
244- JSXParser .parse (" foo(<bar/>)" , {ecmaVersion: 2020 });
260+ var acorn = require (" acorn" )
261+ var jsx = require (" acorn-jsx" )
262+ var JSXParser = acorn .Parser .extend (jsx ())
263+ JSXParser .parse (" foo(<bar/>)" , {ecmaVersion: 2020 })
245264```
246265
247266The ` extend ` method takes any number of plugin values, and returns a
0 commit comments