An Esprima-compatible JavaScript parser built on Acorn
npm install @geosolutions/espree
npm i espree --save
`
And in your Node.js code:
`javascript
var espree = require("espree");
var ast = espree.parse(code);
`
There is a second argument to parse() that allows you to specify various options:
`javascript
var espree = require("espree");
var ast = espree.parse(code, {
// attach range information to each node
range: true,
// attach line/column location information to each node
loc: true,
// create a top-level comments array containing all comments
comment: true,
// attach comments to the closest relevant node as leadingComments and
// trailingComments
attachComment: true,
// create a top-level tokens array containing all tokens
tokens: true,
// specify the language version (3, 5, 6, or 7, default is 5)
ecmaVersion: 5,
// specify which type of script you're parsing (script or module, default is script)
sourceType: "script",
// specify additional language features
ecmaFeatures: {
// enable JSX parsing
jsx: true,
// enable return in global scope
globalReturn: true,
// enable implied strict mode (if ecmaVersion >= 5)
impliedStrict: true,
// allow experimental object rest/spread
experimentalObjectRestSpread: true
}
});
`
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.
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 2-clause license.
Build Commands
* 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
* The 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.
* let and const declarations are no longer parsed by default. You must opt-in using ecmaFeatures.blockBindings.
* The esparse and esvalidate binary scripts have been removed.
* There is no tolerant option. We will investigate adding this back in the future.
Known Incompatibilities
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.
$3
* 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 let and const declarations by default.
* Error messages returned for parsing errors are different.
* 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.