Architecture

At a high level, there are a few key parts to ESLint:

ESLint 有几个关键部分:

The cli object

The cli object is the API for the command line interface. Literally, the bin/eslint.js file simply passes arguments to the cli object and then sets process.exitCode to the returned exit code.

cli对象是命令行接口的 API。事实上,bin/eslint.js 文件只是简单的将参数传递给 cli 对象,然后设置 process.exitCode,并返回退出码。

The main method is cli.execute(), which accepts an array of strings that represent the command line options (as if process.argv were passed without the first two arguments). If you want to run ESLint from inside of another program and have it act like the CLI, then cli is the object to use.

它的主要方法是 cli.execute() ,该方法接收一个代表命令行选项的字符串数组(就像传递少了前两个参数的 process.argv一样)。如果你想在另一程序里像 CLI 一样运行 ESLint,那么 cli 就是你要用到的对象。

This object’s responsibilities include:

这个对象的职责包括:

This object may not:

这个对象不能:

The CLIEngine object

The CLIEngine type represents the core functionality of the CLI except that it reads nothing from the command line and doesn’t output anything by default. Instead, it accepts many (but not all) of the arguments that are passed into the CLI. It reads both configuration and source files as well as managing the environment that is passed into the eslint object.

CLIEngine 类型代表 CLI 的核心功能,除了在默认情况下它不从命令行读取任何内容,也不输出任何内容。相反,它接受很多(不是全部)传递给 CLI 的参数。它读取配置文件和源码文件,同时也管理传递到 eslint 对象的环境。

The main method of the CLIEngine is executeOnFiles(), which accepts an array of file and directory names to run the linter on.

CLIEngine 的主要方法是 executeOnFiles(),接收一个要被检查的文件和目录名的数组。

This object’s responsibilities include:

这个对象的职责包括:

This object may not:

这个对象不能:

The eslint object

The main method of the eslint object is verify() and accepts two arguments: the source text to verify and a configuration object (the baked configuration of the given configuration file plus command line options). The method first parses the given text with espree (or whatever the configured parser is) and retrieves the AST. The AST is produced with both line/column and range locations which are useful for reporting location of issues and retrieving the source text related to an AST node, respectively.

eslint 对象的主要方法是 verify(),接收两个参数:要验证的源码文本和一个配置对象(通过准备好的配置文件加命令行操作会生成配置)。该方法首先使用 espree(或配置的解析器) 解析获取的文本,检索 AST。AST 用来产生行/列和范围的位置,对报告问题的位置和检索与 AST 节点有关的源文本很有帮助。

Once the AST is available, estraverse is used to traverse the AST from top to bottom. At each node, the eslint object emits an event that has the same name as the node type (i.e., “Identifier”, “WithStatement”, etc.). On the way back up the subtree, an event is emitted with the AST type name and suffixed with “:exit”, such as “Identifier:exit” - this allows rules to take action both on the way down and on the way up in the traversal. Each event is emitted with the appropriate AST node available.

一旦AST是可用的,estraverse 被用来从上到下遍历 AST。在每个节点,eslint对象触发与该节点类型同名的一个事件(即 “Identifier”,”WithStatement” 等)。在回退到子树上时,一个带有 AST 类型名称和 “:exit” 后缀的事件被触发,比如 “Identifier:exit” - 这允许规则在正向和逆向遍历开始起作用。每个事件在恰当的 AST 节点可用时触发。

This object’s responsibilities include:

这个对象的职责包括:

This object may not:

这个对象不能:

Rules

Individual rules are the most specialized part of the ESLint architecture. Rules can do very little, they are simply a set of instructions executed against an AST that is provided. They do get some context information passed in, but the primary responsibility of a rule is to inspect the AST and report warnings.

独特的规则是 ESLint 架构中最专业的部分。规则能做的很少,它们只是对提供的 AST 执行的一组指令。它们获取以下传入的上下文信息,但主要职责是检查 AST,报告警告。

These objects’ responsibilities are:

这个对象的职责包括:

These objects may not:

这个对象不能: