扩展 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 配置。例如,如果你使用像 React 这样的前端 JavaScript 库或像 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 组件生命周期方法以及在渲染动态列表时要求使用 key 属性等。
🌐 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 Configurations.
自定义格式化程序
🌐 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 的方式,用于支持对代码中新语言特性或自定义语法的检查。解析器负责将代码转换为 ESLint 可以分析和检查的抽象语法树 (AST)。
🌐 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 以对 TypeScript 代码进行 lint 检查。
🌐 For example, the custom parser @typescript-eslint/parser extends ESLint to lint TypeScript code.
自定义解析器也可以包含在插件中。
🌐 Custom parsers can be also included in a plugin.
要了解有关创建自定义解析器的更多信息,请参阅 自定义解析器。
🌐 To learn more about creating a custom parser, refer to Custom Parsers.