核心概念

此页面包含 ESLint 的一些核心概念的高级概述。

¥This page contains a high-level overview of some of the core concepts of ESLint.

什么是 ESLint?

¥What is ESLint?

ESLint 是一个可配置的 JavaScript 检查器。它可以帮助你发现并修复 JavaScript 代码中的问题。问题可以是任何东西,从潜在的运行时错误,到不遵循最佳实践,再到风格问题。

¥ESLint is a configurable JavaScript linter. It helps you find and fix problems in your JavaScript code. Problems can be anything from potential runtime bugs, to not following best practices, to styling issues.

规则

¥Rules

规则是 ESLint 的核心构建块。规则验证你的代码是否满足特定期望,以及如果不满足该期望该怎么办。规则还可以包含特定于该规则的其他配置选项。

¥Rules are the core building block of ESLint. A rule validates if your code meets a certain expectation, and what to do if it does not meet that expectation. Rules can also contain additional configuration options specific to that rule.

例如,semi 规则允许你指定 JavaScript 语句是否应以分号 (;) 结尾。你可以将规则设置为始终需要分号或要求语句从不以分号结尾。

¥For example, the semi rule lets you specify whether or not JavaScript statements should end with a semicolon (;). You can set the rule to either always require semicolons or require that a statement never ends with a semicolon.

ESLint 包含数百个你可以使用的内置规则。你还可以创建自定义规则或使用其他人通过 插件 创建的规则。

¥ESLint contains hundreds of built-in rules that you can use. You can also create custom rules or use rules that others have created with plugins.

有关详细信息,请参阅 规则

¥For more information, refer to Rules.

规则修复

¥Rule Fixes

规则可以选择为他们发现的违规行为提供修复。修复安全地纠正违规行为,而无需更改应用逻辑。

¥Rules may optionally provide fixes for violations that they find. Fixes safely correct the violation without changing application logic.

修复程序可以通过 --fix 命令行选项 和编辑器扩展自动应用。

¥Fixes may be applied automatically with the --fix command line option and via editor extensions.

可能提供修复的规则在 规则 中用 🔧 标记。

¥Rules that may provide fixes are marked with 🔧 in Rules.

规则建议

¥Rule Suggestions

除了提供修复之外或代替提供修复之外,规则还可以选择提供建议。建议与修复有两个不同之处:

¥Rules may optionally provide suggestions in addition to or instead of providing fixes. Suggestions differ from fixes in two ways:

  1. 建议可能会更改应用逻辑,因此无法自动应用。

    ¥Suggestions may change application logic and so cannot be automatically applied.

  2. 建议无法通过 ESLint CLI 应用,只能通过编辑器集成使用。

    ¥Suggestions cannot be applied through the ESLint CLI and are only available through editor integrations.

可能提供建议的规则在 规则 中用💡标记。

¥Rules that may provide suggestions are marked with 💡 in Rules.

配置文件

¥Configuration Files

ESLint 配置文件是你在项目中放置 ESLint 配置的地方。你可以包含内置规则、你希望它们如何执行、具有自定义规则的插件、可共享配置、你希望规则应用到哪些文件等等。

¥An ESLint configuration file is a place where you put the configuration for ESLint in your project. You can include built-in rules, how you want them enforced, plugins with custom rules, shareable configurations, which files you want rules to apply to, and more.

有关详细信息,请参阅 配置文件

¥For more information, refer to Configuration Files.

可共享的配置

¥Shareable Configurations

可共享配置是通过 npm 共享的 ESLint 配置。

¥Shareable configurations are ESLint configurations that are shared via npm.

通常可共享配置用于使用 ESLint 的内置规则来强制执行风格指南。例如,可共享配置 eslint-config-airbnb-base 实现了流行的 Airbnb JavaScript 风格指南。

¥Often shareable configurations are used to enforce style guides using ESLint’s built-in rules. For example the sharable configuration eslint-config-airbnb-base implements the popular Airbnb JavaScript style guide.

有关详细信息,请参阅 使用可共享的配置包

¥For more information, refer to Using a shareable configuration package.

插件

¥Plugins

ESLint 插件是一个 npm 模块,可以包含一组 ESLint 规则、配置、处理器和环境。插件通常包含自定义规则。插件可用于强制执行风格指南并支持 JavaScript 扩展(如 TypeScript)、库(如 React)和框架(Angular)。

¥An ESLint plugin is an npm module that can contain a set of ESLint rules, configurations, processors, and environments. Often plugins include custom rules. Plugins can be used to enforce a style guide and support JavaScript extensions (like TypeScript), libraries (like React), and frameworks (Angular).

插件的一个流行用例是强制执行框架的最佳实践。例如,@angular-eslint/eslint-plugin 包含使用 Angular 框架的最佳实践。

¥A popular use case for plugins is to enforce best practices for a framework. For example, @angular-eslint/eslint-plugin contains best practices for using the Angular framework.

有关详细信息,请参阅 配置插件

¥For more information, refer to Configure Plugins.

解析器

¥Parsers

ESLint 解析器将代码转换为 ESLint 可以评估的抽象语法树。默认情况下,ESLint 使用内置的 Espree 解析器,它与标准的 JavaScript 运行时和版本兼容。

¥An ESLint parser converts code into an abstract syntax tree that ESLint can evaluate. By default, ESLint uses the built-in Espree parser, which is compatible with standard JavaScript runtimes and versions.

自定义解析器让 ESLint 解析非标准的 JavaScript 语法。自定义解析器通常包含在可共享配置或插件中,因此你不必直接使用它们。

¥Custom parsers let ESLint parse non-standard JavaScript syntax. Often custom parsers are included as part of shareable configurations or plugins, so you don’t have to use them directly.

例如,@typescript-eslint/parser 是一个包含在 typescript-eslint 项目中的自定义解析器,它可以让 ESLint 解析 TypeScript 代码。

¥For example, @typescript-eslint/parser is a custom parser included in the typescript-eslint project that lets ESLint parse TypeScript code.

自定义处理器

¥Custom Processors

ESLint 处理器从其他类型的文件中提取 JavaScript 代码,然后让 ESLint 对 JavaScript 代码进行检查。或者,你可以使用处理器在使用 ESLint 解析 JavaScript 代码之前对其进行操作。

¥An ESLint processor extracts JavaScript code from other kinds of files, then lets ESLint lint the JavaScript code. Alternatively, you can use a processor to manipulate JavaScript code before parsing it with ESLint.

例如,eslint-plugin-markdown 包含一个自定义处理器,可让你在 Markdown 代码块内对 JavaScript 代码进行检查。

¥For example, eslint-plugin-markdown contains a custom processor that lets you lint JavaScript code inside of Markdown code blocks.

格式化器

¥Formatters

ESLint 格式化程序控制检查结果在 CLI 中的外观。

¥An ESLint formatter controls the appearance of the linting results in the CLI.

有关详细信息,请参阅 格式化器

¥For more information, refer to Formatters.

集成

¥Integrations

使 ESLint 如此有用的工具之一是围绕它的集成生态系统。例如,许多代码编辑器都有 ESLint 扩展,可以在你工作时向你显示文件中代码的 ESLint 结果,这样你就无需使用 ESLint CLI 来查看检查结果。

¥One of the things that makes ESLint such a useful tool is the ecosystem of integrations that surrounds it. For example, many code editors have ESLint extensions that show you the ESLint results of your code in the file as you work so that you don’t need to use the ESLint CLI to see linting results.

有关详细信息,请参阅 集成

¥For more information, refer to Integrations.

CLI 和 Node.js API

¥CLI & Node.js API

ESLint CLI 是一个命令行接口,可让你从终端执行检查。CLI 有多种选项可以传递给它的命令。

¥The ESLint CLI is a command line interface that lets you execute linting from the terminal. The CLI has a variety of options that you can pass to its commands.

ESLint Node.js API 允许你以编程方式从 Node.js 代码中使用 ESLint。该 API 在开发与 ESLint 相关的插件、集成和其他工具时很有用。

¥The ESLint Node.js API lets you use ESLint programmatically from Node.js code. The API is useful when developing plugins, integrations, and other tools related to ESLint.

除非你以某种方式扩展 ESLint,否则你应该使用 CLI。

¥Unless you are extending ESLint in some way, you should use the CLI.

有关详细信息,请参阅 命令行接口Node.js API

¥For more information, refer to Command Line Interface and Node.js API.