| | |
| | | ## Interface |
| | | |
| | | **parse**`(input, options)` is the main interface to the library. The |
| | | `input` parameter is a string, `options` can be undefined or an object |
| | | setting some of the options listed below. The return value will be an |
| | | abstract syntax tree object as specified by the [ESTree |
| | | `input` parameter is a string, `options` must be an object setting |
| | | some of the options listed below. The return value will be an abstract |
| | | syntax tree object as specified by the [ESTree |
| | | spec](https://github.com/estree/estree). |
| | | |
| | | ```javascript |
| | | let acorn = require("acorn"); |
| | | console.log(acorn.parse("1 + 1")); |
| | | console.log(acorn.parse("1 + 1", {ecmaVersion: 2020})); |
| | | ``` |
| | | |
| | | When encountering a syntax error, the parser will raise a |
| | |
| | | error occurred, and a `loc` object that contains a `{line, column}` |
| | | object referring to that same position. |
| | | |
| | | Options can be provided by passing a second argument, which should be |
| | | an object containing any of these fields: |
| | | Options are provided by in a second argument, which should be an |
| | | object containing any of these fields (only `ecmaVersion` is |
| | | required): |
| | | |
| | | - **ecmaVersion**: Indicates the ECMAScript version to parse. Must be |
| | | either 3, 5, 6 (2015), 7 (2016), 8 (2017), 9 (2018) or 10 (2019, partial |
| | | support). This influences support for strict mode, the set of |
| | | reserved words, and support for new syntax features. Default is 9. |
| | | either 3, 5, 6 (or 2015), 7 (2016), 8 (2017), 9 (2018), 10 (2019), |
| | | 11 (2020), 12 (2021), 13 (2022, partial support) |
| | | or `"latest"` (the latest the library supports). This influences |
| | | support for strict mode, the set of reserved words, and support |
| | | for new syntax features. |
| | | |
| | | **NOTE**: Only 'stage 4' (finalized) ECMAScript features are being |
| | | implemented by Acorn. Other proposed new features can be implemented |
| | | through plugins. |
| | | implemented by Acorn. Other proposed new features must be |
| | | implemented through plugins. |
| | | |
| | | - **sourceType**: Indicate the mode the code should be parsed in. Can be |
| | | either `"script"` or `"module"`. This influences global strict mode |
| | |
| | | |
| | | - **allowImportExportEverywhere**: By default, `import` and `export` |
| | | declarations can only appear at a program's top level. Setting this |
| | | option to `true` allows them anywhere where a statement is allowed. |
| | | |
| | | - **allowAwaitOutsideFunction**: By default, `await` expressions can |
| | | only appear inside `async` functions. Setting this option to |
| | | option to `true` allows them anywhere where a statement is allowed, |
| | | and also allows `import.meta` expressions to appear in scripts |
| | | (when `sourceType` is not `"module"`). |
| | | |
| | | - **allowAwaitOutsideFunction**: If `false`, `await` expressions can |
| | | only appear inside `async` functions. Defaults to `true` for |
| | | `ecmaVersion` 2022 and later, `false` for lower versions. Setting this option to |
| | | `true` allows to have top-level `await` expressions. They are |
| | | still not allowed in non-`async` functions, though. |
| | | |
| | | - **allowSuperOutsideMethod**: By default, `super` outside a method |
| | | raises an error. Set this to `true` to accept such code. |
| | | |
| | | - **allowHashBang**: When this is enabled (off by default), if the |
| | | code starts with the characters `#!` (as in a shellscript), the |
| | |
| | | var acorn = require("acorn"); |
| | | var jsx = require("acorn-jsx"); |
| | | var JSXParser = acorn.Parser.extend(jsx()); |
| | | JSXParser.parse("foo(<bar/>)"); |
| | | JSXParser.parse("foo(<bar/>)", {ecmaVersion: 2020}); |
| | | ``` |
| | | |
| | | The `extend` method takes any number of plugin values, and returns a |
| | |
| | | - `--allow-hash-bang`: If the code starts with the characters #! (as |
| | | in a shellscript), the first line will be treated as a comment. |
| | | |
| | | - `--allow-await-outside-function`: Allows top-level `await` expressions. |
| | | See the `allowAwaitOutsideFunction` option for more information. |
| | | |
| | | - `--compact`: No whitespace is used in the AST output. |
| | | |
| | | - `--silent`: Do not output the AST, just return the exit status. |
| | |
| | | ## Existing plugins |
| | | |
| | | - [`acorn-jsx`](https://github.com/RReverser/acorn-jsx): Parse [Facebook JSX syntax extensions](https://github.com/facebook/jsx) |
| | | |
| | | Plugins for ECMAScript proposals: |
| | | |
| | | - [`acorn-stage3`](https://github.com/acornjs/acorn-stage3): Parse most stage 3 proposals, bundling: |
| | | - [`acorn-class-fields`](https://github.com/acornjs/acorn-class-fields): Parse [class fields proposal](https://github.com/tc39/proposal-class-fields) |
| | | - [`acorn-import-meta`](https://github.com/acornjs/acorn-import-meta): Parse [import.meta proposal](https://github.com/tc39/proposal-import-meta) |
| | | - [`acorn-numeric-separator`](https://github.com/acornjs/acorn-numeric-separator): Parse [numeric separator proposal](https://github.com/tc39/proposal-numeric-separator) |
| | | - [`acorn-private-methods`](https://github.com/acornjs/acorn-private-methods): parse [private methods, getters and setters proposal](https://github.com/tc39/proposal-private-methods)n |