Espree started out as a fork of Esprima v1.2.2, the last stable published released of Esprima before work on ECMAScript 6 began. Espree is now built on top of Acorn, which has a modular architecture that allows extension of core functionality. The goal of Espree is to produce output that is similar to Esprima with a similar API so that it can be used in place of Esprima.
npm i espree
And in your Node.js code:
const espree = ;const ast = espree;
There is a second argument to
parse() that allows you to specify various options:
const espree = ;// Optional second options argument with the following default settingsconst ast = espree;
Esprima Compatibility Going Forward
The primary goal is to produce the exact same AST structure and tokens as Esprima, and that takes precedence over anything else. (The AST structure being the ESTree API with JSX extensions.) Separate from that, Espree may deviate from what Esprima outputs in terms of where and how comments are attached, as well as what additional information is available on AST nodes. That is to say, Espree may add more things to the AST nodes than Esprima does but the overall AST structure produced will be the same.
Espree may also deviate from Esprima in the interface it exposes.
Issues and pull requests will be triaged and responded to as quickly as possible. We operate under the ESLint Contributor Guidelines, so please be sure to read them before contributing. If you're not sure where to dig in, check out the issues.
Espree is licensed under a permissive BSD 2-clause license.
npm test- run all linting and tests
npm run lint- run all linting
npm run browserify- creates a version of Espree that is usable in a browser
Differences from Espree 2.x
tokenize()method does not use
ecmaFeatures. Any string will be tokenized completely based on ECMAScript 6 semantics.
- Trailing whitespace no longer is counted as part of a node.
constdeclarations are no longer parsed by default. You must opt-in by using an
esvalidatebinary scripts have been removed.
- There is no
tolerantoption. We will investigate adding this back in the future.
In an effort to help those wanting to transition from other parsers to Espree, the following is a list of noteworthy incompatibilities with other parsers. These are known differences that we do not intend to change.
- Esprima counts trailing whitespace as part of each AST node while Espree does not. In Espree, the end of a node is where the last token occurs.
- Espree does not parse
constdeclarations by default.
- Error messages returned for parsing errors are different.
- There are two addition properties on every node and token:
end. These represent the same data as
rangeand are used internally by Acorn.
- Esprima 2.x uses a different comment attachment algorithm that results in some comments being added in different places than Espree. The algorithm Espree uses is the same one used in Esprima 1.2.2.
Frequently Asked Questions
Why another parser
We decided the only way for us to move forward was to create our own parser, bringing us inline with JSHint and JSLint, and allowing us to keep implementing new features as we need them. We chose to fork Esprima instead of starting from scratch in order to move as quickly as possible with a compatible API.
With Espree 2.0.0, we are no longer a fork of Esprima but rather a translation layer between Acorn and Esprima syntax. This allows us to put work back into a community-supported parser (Acorn) that is continuing to grow and evolve while maintaining an Esprima-compatible parser for those utilities still built on Esprima.
Have you tried working with Esprima?
Yes. Since the start of ESLint, we've regularly filed bugs and feature requests with Esprima and will continue to do so. However, there are some different philosophies around how the projects work that need to be worked through. The initial goal was to have Espree track Esprima and eventually merge the two back together, but we ultimately decided that building on top of Acorn was a better choice due to Acorn's plugin support.
Why don't you just use Acorn?
We are building on top of Acorn, however, so that we can contribute back and help make Acorn even better.
What ECMAScript 6 features do you support?
All of them.
What ECMAScript 7/2016 features do you support?
There is only one ECMAScript 2016 syntax change: the exponentiation operator. Espree supports this.
What ECMAScript 2017 features do you support?
There are two ECMAScript 2017 syntax changes:
async functions, and trailing commas in function declarations and calls. Espree supports both of them.
What ECMAScript 2018 features do you support?
There are seven ECMAScript 2018 syntax changes:
- Invalid escape sequences in tagged template literals
- Rest/spread properties
- Async iteration
- RegExp named capture groups
- RegExp lookbehind assertions
- RegExp Unicode property escapes
Espree supports all of them.
What ECMAScript 2019 features do you support?
Because ECMAScript 2019 is still under development, we are implementing features as they are finalized. Currently, Espree supports:
- JSON superset (
\u2029in string literals)
How do you determine which experimental features to support?