UNPKG

8.15 kBMarkdownView Raw
1[![npm version](https://img.shields.io/npm/v/espree.svg)](https://www.npmjs.com/package/espree)
2[![Build Status](https://travis-ci.org/eslint/espree.svg?branch=master)](https://travis-ci.org/eslint/espree)
3[![npm downloads](https://img.shields.io/npm/dm/espree.svg)](https://www.npmjs.com/package/espree)
4[![Bountysource](https://www.bountysource.com/badge/tracker?tracker_id=9348450)](https://www.bountysource.com/trackers/9348450-eslint?utm_source=9348450&utm_medium=shield&utm_campaign=TRACKER_BADGE)
5
6# Espree
7
8Espree started out as a fork of [Esprima](http://esprima.org) v1.2.2, the last stable published released of Esprima before work on ECMAScript 6 began. Espree is now built on top of [Acorn](https://github.com/ternjs/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.
9
10## Usage
11
12Install:
13
14```
15npm i espree --save
16```
17
18And in your Node.js code:
19
20```javascript
21var espree = require("espree");
22
23var ast = espree.parse(code);
24```
25
26There is a second argument to `parse()` that allows you to specify various options:
27
28```javascript
29var espree = require("espree");
30
31// Optional second options argument with the following default settings
32var ast = espree.parse(code, {
33
34 // attach range information to each node
35 range: false,
36
37 // attach line/column location information to each node
38 loc: false,
39
40 // create a top-level comments array containing all comments
41 comment: false,
42
43 // attach comments to the closest relevant node as leadingComments and trailingComments
44 attachComment: false,
45
46 // create a top-level tokens array containing all tokens
47 tokens: false,
48
49 // Set to 3, 5 (default), 6, 7, 8, or 9 to specify the version of ECMAScript syntax you want to use.
50 // You can also set to 2015 (same as 6), 2016 (same as 7), 2017 (same as 8), or 2018 (same as 9) to use the year-based naming.
51 ecmaVersion: 5,
52
53 // specify which type of script you're parsing ("script" or "module")
54 sourceType: "script",
55
56 // specify additional language features
57 ecmaFeatures: {
58
59 // enable JSX parsing
60 jsx: false,
61
62 // enable return in global scope
63 globalReturn: false,
64
65 // enable implied strict mode (if ecmaVersion >= 5)
66 impliedStrict: false,
67
68 // allow experimental object rest/spread
69 experimentalObjectRestSpread: false
70 }
71});
72```
73
74## Esprima Compatibility Going Forward
75
76The 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](https://github.com/estree/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.
77
78Espree may also deviate from Esprima in the interface it exposes.
79
80## Contributing
81
82Issues and pull requests will be triaged and responded to as quickly as possible. We operate under the [ESLint Contributor Guidelines](http://eslint.org/docs/developer-guide/contributing), so please be sure to read them before contributing. If you're not sure where to dig in, check out the [issues](https://github.com/eslint/espree/issues).
83
84Espree is licensed under a permissive BSD 2-clause license.
85
86## Build Commands
87
88* `npm test` - run all linting and tests
89* `npm run lint` - run all linting
90* `npm run browserify` - creates a version of Espree that is usable in a browser
91
92## Differences from Espree 2.x
93
94* The `tokenize()` method does not use `ecmaFeatures`. Any string will be tokenized completely based on ECMAScript 6 semantics.
95* Trailing whitespace no longer is counted as part of a node.
96* `let` and `const` declarations are no longer parsed by default. You must opt-in using `ecmaFeatures.blockBindings`.
97* The `esparse` and `esvalidate` binary scripts have been removed.
98* There is no `tolerant` option. We will investigate adding this back in the future.
99
100## Known Incompatibilities
101
102In 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.
103
104### Esprima 1.2.2
105
106* 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.
107* Espree does not parse `let` and `const` declarations by default.
108* Error messages returned for parsing errors are different.
109* There are two addition properties on every node and token: `start` and `end`. These represent the same data as `range` and are used internally by Acorn.
110
111### Esprima 2.x
112
113* 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.
114
115## Frequently Asked Questions
116
117### Why another parser
118
119[ESLint](http://eslint.org) had 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 increased dramatically and Esprima had fallen behind. ESLint, like many other tools reliant on Esprima, has been stuck in using new JavaScript language features until Esprima updates, and that caused our users frustration.
120
121We 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.
122
123With 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.
124
125### Have you tried working with Esprima?
126
127Yes. 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.
128
129### Why don't you just use Acorn?
130
131Acorn 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.
132
133We are building on top of Acorn, however, so that we can contribute back and help make Acorn even better.
134
135### What ECMAScript 6 features do you support?
136
137All of them.
138
139### What ECMAScript 7/2016 features do you support?
140
141There is only one ECMAScript 2016 syntax change: the exponentiation operator. Espree supports this.
142
143### What ECMAScript 2017 features do you support?
144
145There are two ECMAScript 2017 syntax changes: `async` functions, and trailing commas in function declarations and calls. Espree supports both of them.
146
147### What ECMAScript 2018 features do you support?
148
149Because ECMAScript 2018 is still under development, we are implementing features as they are finalized. Currently, Espree supports:
150
151* Invalid escape sequences in tagged template literals
152
153### How do you determine which experimental features to support?
154
155In general, we do not support experimental JavaScript features. We may make exceptions from time to time depending on the maturity of the features.