核心概念
本页包含 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 中标有 🔧。
🌐 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:
- 建议可能会更改应用逻辑,因此无法自动应用。
- 建议无法通过 ESLint CLI 应用,只能通过编辑器集成使用。
可能提供建议的规则在 Rules 中用 💡 标记。
🌐 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 shareable configuration eslint-config-airbnb-base implements the popular Airbnb JavaScript style guide.
有关更多信息,请参阅 使用可共享的配置包。
🌐 For more information, refer to Use 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 languages. 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 (like 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 代码之前,使用处理器来操作 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/markdown 包含一个自定义处理器,可以让你在 Markdown 代码块中对 JavaScript 代码进行 lint 检查。
🌐 For example, @eslint/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.
有关更多信息,请参阅 Formatters。
🌐 For more information, refer to Formatters.
集成
🌐 Integrations
使 ESLint 如此有用的一个原因是围绕它的集成生态系统。例如,许多代码编辑器都有 ESLint 扩展,可以在你编辑文件时显示代码的 ESLint 结果,这样你就不需要使用 ESLint CLI 来查看 lint 结果。
🌐 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.