配置插件
你可以通过多种不同的方式使用插件扩展 ESLint。插件可以包括:
🌐 You can extend ESLint with plugins in a variety of different ways. Plugins can include:
- 自定义规则来验证你的代码是否满足特定期望,以及如果不满足该期望该怎么办。
- 自定义配置。请查阅插件文档了解如何使用这些配置的详细信息。
- 自定义处理器从其他类型的文件中提取 JavaScript 代码或在 linting 之前预处理代码。
配置插件
🌐 Configure Plugins
ESLint 支持使用第三方插件。插件只是符合 ESLint 可识别的特定接口的对象。
🌐 ESLint supports the use of third-party plugins. Plugins are simply objects that conform to a specific interface that ESLint recognizes.
要在配置文件中配置插件,请使用 plugins 键,该键包含一个对象,其属性表示插件命名空间,值等于插件对象。
🌐 To configure plugins inside of a configuration file, use the plugins key, which contains an object with properties representing plugin namespaces and values equal to the plugin object.
// eslint.config.js
import example from "eslint-plugin-example";
import { defineConfig } from "eslint/config";
export default defineConfig([
{
plugins: {
example,
},
rules: {
"example/rule1": "warn",
},
},
]);
配置局部插件
🌐 Configure a Local Plugin
插件不需要发布到 npm 就可以在 ESLint 中使用。你也可以像下面的例子一样直接从文件加载插件:
🌐 Plugins don’t need to be published to npm for use with ESLint. You can also load plugins directly from a file, as in this example:
// eslint.config.js
import local from "./my-local-plugin.js";
import { defineConfig } from "eslint/config";
export default defineConfig([
{
plugins: {
local,
},
rules: {
"local/rule1": "warn",
},
},
]);
这里使用了命名空间 local,但你也可以使用任何你想要的名称。
🌐 Here, the namespace local is used, but you can also use any name you’d like instead.
配置虚拟插件
🌐 Configure a Virtual Plugin
插件定义几乎可以直接在你的配置中创建。例如,假设你有一个规则包含在名为 my-rule.js 的文件中,并且你想在配置中启用它。你可以定义一个虚拟插件来实现,如下例所示:
🌐 Plugin definitions can be created virtually directly in your config. For example, suppose you have a rule contained in a file called my-rule.js that you’d like to enable in your config. You can define a virtual plugin to do so, as in this example:
// eslint.config.js
import myRule from "./rules/my-rule.js";
import { defineConfig } from "eslint/config";
export default defineConfig([
{
plugins: {
local: {
rules: {
"my-rule": myRule,
},
},
},
rules: {
"local/my-rule": "warn",
},
},
]);
在这里,命名空间 local 用于定义一个虚拟插件。然后规则 myRule 在虚拟插件的 rules 对象内部被分配了名称 my-rule。(完整的插件格式请参见 创建插件。)然后你可以将规则引用为 local/my-rule 来进行配置。
🌐 Here, the namespace local is used to define a virtual plugin. The rule myRule is then assigned a name of my-rule inside of the virtual plugin’s rules object. (See Create Plugins for the complete format of a plugin.) You can then reference the rule as local/my-rule to configure it.
使用插件规则
🌐 Use Plugin Rules
你可以使用插件中包含的特定规则。为此,请在配置对象中使用 plugins 键指定插件。plugin 键的值是一个对象,其中插件的名称是属性名称,值是插件对象本身。示例如下:
🌐 You can use specific rules included in a plugin. To do this, specify the plugin
in a configuration object using the plugins key. The value for the plugin key
is an object where the name of the plugin is the property name and the value is the plugin object itself. Here’s an example:
// eslint.config.js
import jsdoc from "eslint-plugin-jsdoc";
import { defineConfig } from "eslint/config";
export default defineConfig([
{
files: ["**/*.js"],
plugins: {
jsdoc: jsdoc,
},
rules: {
"jsdoc/require-description": "error",
"jsdoc/check-values": "error",
},
},
]);
在此配置中,JSDoc 插件被定义为名称为 jsdoc。每个规则名称中的前缀 jsdoc/ 表示该规则来自具有该名称的插件,而不是来自 ESLint 本身。
🌐 In this configuration, the JSDoc plugin is defined to have the name jsdoc. The prefix jsdoc/ in each rule name indicates that the rule is coming from the plugin with that name rather than from ESLint itself.
因为插件的名称和插件对象都是 jsdoc,你也可以将配置简化为这样:
🌐 Because the name of the plugin and the plugin object are both jsdoc, you can also shorten the configuration to this:
import jsdoc from "eslint-plugin-jsdoc";
import { defineConfig } from "eslint/config";
export default defineConfig([
{
files: ["**/*.js"],
plugins: {
jsdoc,
},
rules: {
"jsdoc/require-description": "error",
"jsdoc/check-values": "error",
},
},
]);
虽然这是最常见的约定,但你不需要使用插件规定的相同名称。你可以指定任何你喜欢的前缀,例如:
🌐 While this is the most common convention, you don’t need to use the same name that the plugin prescribes. You can specify any prefix that you’d like, such as:
import jsdoc from "eslint-plugin-jsdoc";
import { defineConfig } from "eslint/config";
export default defineConfig([
{
files: ["**/*.js"],
plugins: {
jsd: jsdoc,
},
rules: {
"jsd/require-description": "error",
"jsd/check-values": "error",
},
},
]);
此配置对象使用 jsd 作为前缀插件,而不是 jsdoc。
🌐 This configuration object uses jsd as the prefix plugin instead of jsdoc.
指定处理器
🌐 Specify a Processor
插件可以提供处理器。处理器可以从其他类型的文件中提取 JavaScript 代码,然后让 ESLint 对这些 JavaScript 代码进行检查。或者,处理器可以在预处理过程中转换 JavaScript 代码。
🌐 Plugins may provide processors. Processors can extract JavaScript code from other kinds of files, then let ESLint lint the JavaScript code. Alternatively, processors can convert JavaScript code during preprocessing.
要在配置文件中指定处理器,请使用 processor 键,并使用 namespace/processor-name 格式分配处理器名称。例如,以下示例将 @eslint/markdown 的处理器用于 *.md 文件。
🌐 To specify processors in a configuration file, use the processor key and assign the name of processor in the format namespace/processor-name. For example, the following uses the processor from @eslint/markdown for *.md files.
// eslint.config.js
import markdown from "@eslint/markdown";
import { defineConfig } from "eslint/config";
export default defineConfig([
{
files: ["**/*.md"],
plugins: {
markdown,
},
processor: "markdown/markdown",
},
]);
处理器可能会创建命名的代码块,如 0.js 和 1.js。ESLint 将这样的命名代码块视为原始文件的子文件。你可以通过额外的配置对象为命名代码块指定附加配置。例如,下面的示例在 Markdown 文件中禁用了以 .js 结尾的命名代码块的 strict 规则。
🌐 Processors may make named code blocks such as 0.js and 1.js. ESLint handles such a named code block as a child file of the original file. You can specify additional configurations for named code blocks with additional config objects. For example, the following disables the strict rule for the named code blocks which end with .js in markdown files.
// eslint.config.js
import markdown from "@eslint/markdown";
import { defineConfig } from "eslint/config";
export default defineConfig([
// applies to all JavaScript files
{
rules: {
strict: "error",
},
},
// applies to Markdown files
{
files: ["**/*.md"],
plugins: {
markdown,
},
processor: "markdown/markdown",
},
// applies only to JavaScript blocks inside of Markdown files
{
files: ["**/*.md/*.js"],
rules: {
strict: "off",
},
},
]);
ESLint 仅在命名代码块是 JavaScript 文件或它们与配置对象中的 files 条目匹配时才会进行检查。如果你想要检查非 JavaScript 的命名代码块,请务必添加一个具有匹配 files 条目的配置对象。还要注意,全局忽略 也适用于命名代码块。
🌐 ESLint only lints named code blocks when they are JavaScript files or if they match a files entry in a config object. Be sure to add a config object with a matching files entry if you want to lint non-JavaScript named code blocks. Also note that global ignores apply to named code blocks as well.
// eslint.config.js
import markdown from "@eslint/markdown";
import { defineConfig } from "eslint/config";
export default defineConfig([
// applies to Markdown files
{
files: ["**/*.md"],
plugins: {
markdown,
},
processor: "markdown/markdown",
},
// applies to all .jsx files, including jsx blocks inside of Markdown files
{
files: ["**/*.jsx"],
languageOptions: {
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
},
// ignore jsx blocks inside of test.md files
{
ignores: ["**/test.md/*.jsx"],
},
]);
指定语言
🌐 Specify a Language
插件可以提供语言。语言允许 ESLint 对除 JavaScript 之外的编程语言进行检查。要在配置文件中指定语言,请使用 language 键,并以 namespace/language-name 格式分配语言名称。例如,下面的示例为 *.json 文件使用来自 @eslint/json 的 json/jsonc 语言。
🌐 Plugins may provide languages. Languages allow ESLint to lint programming languages besides JavaScript. To specify a language in a configuration file, use the language key and assign the name of language in the format namespace/language-name. For example, the following uses the json/jsonc language from @eslint/json for *.json files.
// eslint.config.js
import json from "@eslint/json";
import { defineConfig } from "eslint/config";
export default defineConfig([
{
files: ["**/*.json"],
plugins: {
json,
},
language: "json/jsonc",
},
]);
常见问题
🌐 Common Problems