扩展 ESLint 的方法

ESLint 是高度插件化和可配置的。你可以通过多种方式扩展 ESLint 的功能。

¥ESLint is highly pluggable and configurable. There are a variety of ways that you can extend ESLint’s functionality.

本页解释了扩展 ESLint 的方法,以及这些扩展如何组合在一起。

¥This page explains the ways to extend ESLint, and how these extensions all fit together.

插件

¥Plugins

插件允许你将自己的 ESLint 自定义规则和自定义处理器添加到项目中。你可以将插件发布为 npm 模块。

¥Plugins let you add your own ESLint custom rules and custom processors to a project. You can publish a plugin as an npm module.

插件很有用,因为你的项目可能需要一些未包含在核心 eslint 包中的 ESLint 配置。例如,如果你使用的是前端 JavaScript 库(如 React)或框架(如 Vue),这些工具的某些功能需要 ESLint 核心规则范围之外的自定义规则。

¥Plugins are useful because your project may require some ESLint configuration that isn’t included in the core eslint package. For example, if you’re using a frontend JavaScript library like React or framework like Vue, these tools have some features that require custom rules outside the scope of the ESLint core rules.

通常,插件与 ESLint 配置配对,将插件中的一组功能应用于项目。你也可以在插件中包含配置。

¥Often a plugin is paired with a configuration for ESLint that applies a set of features from the plugin to a project. You can include configurations in a plugin as well.

例如,eslint-plugin-react 是一个 ESLint 插件,其中包含专门针对 React 项目的规则。这些规则包括强制 React 组件生命周期方法的一致使用以及在渲染动态列表时要求使用关键属性等内容。

¥For example, eslint-plugin-react is an ESLint plugin that includes rules specifically for React projects. The rules include things like enforcing consistent usage of React component lifecycle methods and requiring the use of key props when rendering dynamic lists.

要了解有关创建可包含在插件中的扩展的更多信息,请参阅以下文档:

¥To learn more about creating the extensions you can include in a plugin, refer to the following documentation:

要了解有关将这些扩展打包到插件中的更多信息,请参阅 插件

¥To learn more about bundling these extensions into a plugin, refer to Plugins.

可共享的配置

¥Shareable Configs

ESLint 可共享配置是你可以在项目中使用的 ESLint 预定义配置。它们将规则和其他配置打包在一个 npm 包中。你可以放入配置文件中的任何内容都可以放入可共享的配置中。

¥ESLint shareable configs are pre-defined configurations for ESLint that you can use in your projects. They bundle rules and other configuration together in an npm package. Anything that you can put in a configuration file can be put in a shareable config.

你可以独立发布可共享配置或作为插件的一部分。

¥You can either publish a shareable config independently or as part of a plugin.

例如,一个流行的可共享配置是 eslint-config-airbnb,其中除了一些 解析器选项 之外还包含各种规则。这是一套 ESLint 规则,旨在匹配 Airbnb JavaScript 风格指南 使用的风格指南。通过使用 eslint-config-airbnb 可共享配置,你可以在你的项目中自动执行 Airbnb 风格指南,而无需手动配置每条规则。

¥For example, a popular shareable config is eslint-config-airbnb, which contains a variety of rules in addition to some parser options. This is a set of rules for ESLint that is designed to match the style guide used by the Airbnb JavaScript style guide. By using the eslint-config-airbnb shareable config, you can automatically enforce the Airbnb style guide in your project without having to manually configure each rule.

要了解有关创建可共享配置的更多信息,请参阅 共享配置

¥To learn more about creating a shareable config, refer to Share Configuration.

自定义格式化程序

¥Custom Formatters

自定义格式化程序采用 ESLint linting 结果并以你定义的格式输出结果。自定义格式化程序允许你以最适合你需要的格式显示 linting 结果,无论是特定文件格式、特定显示样式还是针对特定工具优化的格式。如果 内置格式化器 不满足你的用例,你只需要创建自定义格式化程序。

¥Custom formatters take ESLint linting results and output the results in a format that you define. Custom formatters let you display linting results in a format that best fits your needs, whether that’s in a specific file format, a certain display style, or a format optimized for a particular tool. You only need to create a custom formatter if the built-in formatters don’t serve your use case.

例如,自定义格式化程序 eslint-formatter-gitlab 可用于在 GitLab 代码质量报告中显示 ESLint 结果。

¥For example, the custom formatter eslint-formatter-gitlab can be used to display ESLint results in GitLab code quality reports.

要了解有关创建自定义格式化程序的更多信息,请参阅 自定义格式化程序

¥To learn more about creating a custom formatter, refer to Custom Formatters.

自定义解析器

¥Custom Parsers

ESLint 自定义解析器是一种扩展 ESLint 以支持代码中新语言功能或自定义语法的 linting 的方法。解析器负责获取你的代码并将其转换为抽象语法树 (AST),然后 ESLint 可以对其进行分析和 lint。

¥ESLint custom parsers are a way to extend ESLint to support the linting of new language features or custom syntax in your code. A parser is responsible for taking your code and transforming it into an abstract syntax tree (AST) that ESLint can then analyze and lint.

ESLint 附带一个内置的 JavaScript 解析器 (Espree),但自定义解析器允许你对其他语言进行 lint 或扩展内置解析器的 linting 功能。

¥ESLint ships with a built-in JavaScript parser (Espree), but custom parsers allow you to lint other languages or to extend the linting capabilities of the built-in parser.

例如,自定义解析器 @typescript-eslint/parser 将 ESLint 扩展为 lint TypeScript 代码。

¥For example, the custom parser @typescript-eslint/parser extends ESLint to lint TypeScript code.

与其他扩展类型不同,自定义解析器不能包含在插件中。

¥Custom parsers cannot be included in a plugin, unlike the other extension types.

要了解有关创建自定义解析器的更多信息,请参阅 自定义解析器

¥To learn more about creating a custom parser, refer to Custom Parsers.

ESLint 中文网
粤ICP备13048890号