配置插件
你可以通过各种不同的方式使用插件扩展 ESLint。插件可以包括:
¥You can extend ESLint with plugins in a variety of different ways. Plugins can include:
-
自定义规则来验证你的代码是否满足特定期望,以及如果不满足该期望该怎么办。
¥Custom rules to validate if your code meets a certain expectation, and what to do if it does not meet that expectation.
-
自定义配置。有关如何使用这些配置的详细信息,请参阅插件的文档。
¥Custom configurations. Please refer to the plugin’s documentation for details on how to use these configurations.
-
自定义处理器从其他类型的文件中提取 JavaScript 代码或在 linting 之前预处理代码。
¥Custom processors to extract JavaScript code from other kinds of files or preprocess code before 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";
export default [
{
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";
export default [
{
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";
export default [
{
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";
export default [
{
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";
export default [
{
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";
export default [
{
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 代码进行 lint。或者,处理器可以在预处理期间转换 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";
export default [
{
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";
export default [
// 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"
}
}
];
当命名代码块是 JavaScript 文件或与配置对象中的 files
条目匹配时,ESLint 仅对命名代码块进行 lint 分析。如果你想要检查非 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";
export default [
// 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 以外的编程语言进行 lint。要在 配置文件 中指定语言,请使用 language
键并以 namespace/language-name
格式分配语言名称。例如,以下内容使用 @eslint/json
中的 json/jsonc
语言来处理 *.json
文件。
¥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";
export default [
{
files: ["**/*.json"],
plugins: {
json
},
language: "json/jsonc"
}
];
常见问题
¥Common Problems