What is espree?
Espree is an actively-maintained JavaScript parsing library used to parse ECMAScript (JavaScript) code. It is built on top of Acorn, a high-performance, tiny JavaScript parser, and it adheres to the ECMAScript standard. Espree is often used in the context of development tools and frameworks to analyze and understand JavaScript code structure or to enable code transformation.
What are espree's main functionalities?
Parsing JavaScript code to an Abstract Syntax Tree (AST)
This feature allows developers to parse a string of JavaScript code into an AST, which can then be used for various static analysis tasks.
const espree = require('espree');
const ast = espree.parse('let x = 5;');
Parsing with specific ECMAScript version
Espree can parse code according to a specified ECMAScript version, allowing developers to work with features from different stages of JavaScript evolution.
const espree = require('espree');
const ast = espree.parse('let x = 5;', { ecmaVersion: 2020 });
Parsing with source type module
Espree can parse code written in module format, which includes the use of `import` and `export` statements.
const espree = require('espree');
const ast = espree.parse('export var x = 5;', { sourceType: 'module' });
Other packages similar to espree
acorn
Acorn is a small, fast, JavaScript-based JavaScript parser. Espree is based on Acorn, but Espree provides additional support for experimental ECMAScript features and ESLint-specific extensions.
esprima
Esprima is a high performance, standard-compliant ECMAScript parser. It is similar to Espree in its parsing capabilities but differs in its API and the fact that it does not extend Acorn.
Espree
Espree is an actively-maintained fork Esprima, a high performance,
standard-compliant ECMAScript
parser written in ECMAScript (also popularly known as
JavaScript).
Features
- Full support for ECMAScript 5.1 (ECMA-262)
- Sensible syntax tree format compatible with Mozilla
Parser AST
- Optional tracking of syntax node location (index-based and line-column)
- Heavily tested (> 650 unit tests) with full code coverage
Usage
Install:
npm i espree --save
And in your Node.js code:
var espree = require("espree");
var ast = espree.parse(code);
There is a second argument to parse()
that allows you to specify various options:
var espree = require("espree");
var ast = espree.parse(code, {
range: true,
loc: true,
comments: true,
attachComment: true,
tokens: true,
tolerant: true,
ecmaFeatures: {
blockBindings: true,
regexYFlag: true,
regexUFlag: true,
binaryLiterals: true,
octalLiterals: true,
forOf: false,
objectLiteralComputedProperties: false,
objectLiteralShorthandMethods: false,
objectLiteralShorthandProperties: false
}
});
Plans
Espree starts as a fork of Esprima v1.2.2, the last stable published released of Esprima before work on ECMAScript 6 began. Espree's first version is therefore v1.2.2 and is 100% compatible with Esprima v1.2.2 as drop-in replacement. The version number will be incremented based on semantic versioning as features and bug fixes are added.
The immediate plans are:
- Move away from giant files and move towards small, modular files that are easier to manage.
- Move towards CommonJS for all files and use browserify to create browser bundles.
- Support ECMAScript version filtering, allowing users to specify which version the parser should work in (similar to Acorn's
ecmaVersion
property). - Add tests to track comment attachment.
- Add well-thought-out features that are useful for tools developers.
- Add full support for ECMAScript 6.
- Add optional parsing of JSX.
Esprima Compatibility Going Forward
The primary goal is to produce the exact same AST structure as Esprima and Acorn, and that takes precedence over anything else. (The AST structure being the SpiderMonkey Parser 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.
Frequent and Incremental Releases
Espree will not do giant releases. Releases will happen periodically as changes are made and incremental releases will be made towards larger goals. For instance, we will not have one big release for ECMAScript 6 support. Instead, we will implement ECMAScript 6, piece-by-piece, hiding those pieces behind an ecmaFeatures
property that allows you to opt-in to use those features.
Contributing
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 3-clause license.
Build Commands
npm test
- run all linting and testsnpm run lint
- run all lintingnpm run browserify
- creates a version of Espree that is usable in a browser
Frequently Asked Questions
Why are you forking Esprima?
ESLint has been relying on Esprima as its parser from the beginning. While that was fine when the JavaScript language was evolving slowly, the pace of development has increased dramatically and Esprima has fallen behind. ESLint, like many other tools reliant on Esprima, has been stuck in using new JavaScript language features until Esprima updates, and that has caused our users frustration.
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.
Have you tried working with Esprima?
Yes. Since the start of ESLint, we've regularly filed bugs and feature requests with Esprima. Unfortunately, we've been unable to make much progress towards getting our needs addressed.
Why don't you just use Facebook's Esprima fork?
esprima-fb
is Facebook's Esprima fork that features JSX and Flow type annotations. We tried working with esprima-fb
in our evaluation of how to support ECMAScript 6 and JSX in ESLint. Unfortunately, we were hampered by bugs that were part of Esprima (not necessarily Facebook's code). Since esprima-fb
tracks the Esprima Harmony branch, that means we still were unable to get fixes or features we needed in a timely manner.
Why don't you just use Acorn?
Acorn is a great JavaScript parser that produces an AST that is compatible with Esprima. Unfortunately, ESLint relies on more than just the AST to do its job. It relies on Esprima's tokens and comment attachment features to get a complete picture of the source code. We investigated switching to Acorn, but the inconsistencies between Esprima and Acorn created too much work for a project like ESLint.
We expect there are other tools like ESLint that rely on more than just the AST produced by Esprima, and so a drop-in replacement will help those projects as well as ESLint.
What ECMAScript 6 features do you support?
Please see the tracking issue for the most up-to-date information.