Architecture

dependency graph

At a high level, there are a few key parts to 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.

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.

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 Linter object.

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

This object's responsibilities include:

This object may not:

The Linter object

The main method of the Linter 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.

Once the AST is available, estraverse is used to traverse the AST from top to bottom. At each node, the Linter 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.

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.

These objects' responsibilities are:

These objects may not:

关注前端布道师,专注分享前端最新技术、大厂面试题、聊点程序员轶事、职场感情,做前端技术的传播者.

关注 前端布道师

热爱前端开发,专注分享前端最新技术、大厂面试题、聊点程序员轶事、职场感悟,做前端技术的传播者

LeetCode题解 | 每日一题