核心概念

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

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

什么是 ESLint?

¥What is ESLint?

ESLint 是一个可配置的 JavaScript linter。它可以帮助你发现并修复 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 包含数百条可供你使用的内置规则。你还可以创建自定义规则或使用其他人通过 plugins 创建的规则。

¥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 使用内置的 埃斯普雷 解析器,它与标准 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/parsertypescript-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 代码之前使用处理器来操作 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 中 linting 结果的外观。

¥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 来查看 linting 结果。

¥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 是一个命令行接口,可让你从终端执行 linting。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.