配置解析器(已弃用)

你可以使用自定义解析器将 JavaScript 代码转换为抽象语法树,供 ESLint 评估。如果你的代码与 ESLint 的默认解析器 Espree 不兼容,你可能想要添加自定义解析器。

¥You can use custom parsers to convert JavaScript code into an abstract syntax tree for ESLint to evaluate. You might want to add a custom parser if your code isn’t compatible with ESLint’s default parser, Espree.

配置自定义解析器

¥Configure a Custom Parser

默认情况下,ESLint 使用 埃斯普雷 作为其解析器。如果解析器满足以下要求,你可以选择在配置文件中指定应使用不同的解析器:

¥By default, ESLint uses Espree as its parser. You can optionally specify that a different parser should be used in your configuration file if the parser meets the following requirements:

  1. 它必须是可从使用解析器的配置文件中加载的 Node 模块。通常,这意味着你应该使用 npm 单独安装解析器包。

    ¥It must be a Node module loadable from the config file where the parser is used. Usually, this means you should install the parser package separately using npm.

  2. 它必须符合 解析器接口

    ¥It must conform to the parser interface.

请注意,即使具有这些兼容性,也不能保证外部解析器可以与 ESLint 正确配合使用。ESLint 不会修复与其他解析器不兼容相关的错误。

¥Note that even with these compatibilities, there are no guarantees that an external parser works correctly with ESLint. ESLint does not fix bugs related to incompatibilities with other parsers.

要指示要用作解析器的 npm 模块,请在 .eslintrc 文件中使用 parser 选项指定它。例如,以下内容指定使用 Esprima 而不是 Espree:

¥To indicate the npm module to use as your parser, specify it using the parser option in your .eslintrc file. For example, the following specifies to use Esprima instead of Espree:

{
	"parser": "esprima",
	"rules": {
		"semi": "error"
	}
}

以下解析器与 ESLint 兼容:

¥The following parsers are compatible with ESLint:

请注意,使用自定义解析器时,默认情况下,parserOptions 配置属性仍然是 ESLint 正常运行所必需的,以便与 ECMAScript 5 中没有的功能配合使用。解析器都传递了 parserOptions,可能会或可能不会使用它们来确定要启用哪些功能。

¥Note that when using a custom parser, the parserOptions configuration property is still required for ESLint to work properly with features not in ECMAScript 5 by default. Parsers are all passed parserOptions and may or may not use them to determine which features to enable.