@geosolutions/espree

中文文档
3.1.8发布于 2019/12/03BSD-2-ClauseCommonJS近 30 天下载 7887

An Esprima-compatible JavaScript parser built on Acorn

原始使用文档
文档目录15 节
  1. Usage
  2. Esprima Compatibility Going Forward
  3. Contributing
  4. Build Commands
  5. Differences from Espree 2.x
  6. Known Incompatibilities
  7. Esprima 1.2.2
  8. Esprima 2.x
  9. Frequently Asked Questions
  10. Why another parser
  11. Have you tried working with Esprima?
  12. Why don't you just use Acorn?
  13. What ECMAScript 6 features do you support?
  14. What ECMAScript 7 features do you support?
  15. How do you determine which experimental features to support?

Fork info

This repo has been forked because we needed to add this fix We are going to publish the release branch on npm GeoSolutions registry

Espree

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.

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, {

    // 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.

Esprima 1.2.2

  • 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.

Esprima 2.x

  • 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

ESLint 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.

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?

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 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 features do you support?

There is only one ECMAScript 7 syntax change: the exponentiation operator. Espree supports this.

How do you determine which experimental features to support?

In general, we do not support experimental JavaScript features. We may make exceptions from time to time depending on the maturity of the features.

版本与发布记录

@geosolutions/espree 共发布 1 个版本,首次发布于 2019/12/03;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.8latest2019/12/036 年前

关于 @geosolutions/espree 的常见问题

@geosolutions/espree 是什么?

An Esprima-compatible JavaScript parser built on Acorn

如何安装 @geosolutions/espree?

在项目目录运行 npm install @geosolutions/espree,或使用 pnpm add / yarn add / bun add。

@geosolutions/espree 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/geosolutions__espree 是否存在。

@geosolutions/espree 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@geosolutions/espree 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=0.10.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@geosolutions/espree 可以商用吗?

@geosolutions/espree 使用 BSD-2-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@geosolutions/espree 的下载量有多少?

根据 npm 官方下载统计,@geosolutions/espree 在 2026/10/16 – 2026/11/14 这 30 天内被下载 7,887 次,日均约 263 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@geosolutions/espree 还在维护吗?

最新版本 3.1.8 发布于 2019/12/03(6 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/12/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。