配置插件(已弃用)

你可以通过各种不同的方式使用插件扩展 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.

  • 自定义环境。

    ¥Custom environments.

  • 自定义处理器从其他类型的文件中提取 JavaScript 代码或在 linting 之前预处理代码。

    ¥Custom processors to extract JavaScript code from other kinds of files or preprocess code before linting.

配置插件

¥Configure Plugins

ESLint 支持使用第三方插件。在使用插件之前,你必须使用 npm 安装它。

¥ESLint supports the use of third-party plugins. Before using a plugin, you have to install it using npm.

要在配置文件中配置插件,请使用 plugins 键,其中包含插件名称列表。可以从插件名称中省略 eslint-plugin- 前缀。

¥To configure plugins inside of a configuration file, use the plugins key, which contains a list of plugin names. The eslint-plugin- prefix can be omitted from the plugin name.

{
	"plugins": ["plugin1", "eslint-plugin-plugin2"]
}

以及 YAML 中的内容:

¥And in YAML:

---
plugins:
    - plugin1
    - eslint-plugin-plugin2

注释:

¥Notes:

  1. 插件是相对于配置文件解析的。换句话说,ESLint 会像用户通过在配置文件中运行 require('eslint-plugin-pluginname') 一样加载插件。

    ¥Plugins are resolved relative to the config file. In other words, ESLint loads the plugin as a user would obtain by running require('eslint-plugin-pluginname') in the config file.

  2. 基本配置中的插件(由 extends 设置加载)相对于派生的配置文件。例如,如果 ./.eslintrcextends: ["foo"]eslint-config-fooplugins: ["bar"],则 ESLint 会从 ./node_modules/(而不是 ./node_modules/eslint-config-foo/node_modules/)或祖级目录中找到 eslint-plugin-bar。因此,配置文件和基本配置中的每个插件都被唯一地解析。

    ¥Plugins in the base configuration (loaded by extends setting) are relative to the derived config file. For example, if ./.eslintrc has extends: ["foo"] and the eslint-config-foo has plugins: ["bar"], ESLint finds the eslint-plugin-bar from ./node_modules/ (rather than ./node_modules/eslint-config-foo/node_modules/) or ancestor directories. Thus every plugin in the config file and base configurations is resolved uniquely.

命名约定

¥Naming convention

包含插件

¥Include a plugin

对于非范围和范围包,都可以省略 eslint-plugin- 前缀。

¥The eslint-plugin- prefix can be omitted for both non-scoped and scoped packages.

非作用域包:

¥A non-scoped package:

{
    // ...
    "plugins": [
        "jquery", // means eslint-plugin-jquery
    ]
    // ...
}

作用域包:

¥A scoped package:

{
    // ...
    "plugins": [
        "@jquery/jquery", // means @jquery/eslint-plugin-jquery
        "@foobar" // means @foobar/eslint-plugin
    ]
    // ...
}

使用插件

¥Use a plugin

插件中定义的规则、环境和配置必须按照以下约定引用:

¥Rules, environments, and configurations defined in plugins must be referenced with the following convention:

  • eslint-plugin-foofoo/a-rule

  • @foo/eslint-plugin@foo/a-config

  • @foo/eslint-plugin-bar@foo/bar/a-environment

例如:

¥For example:

{
    // ...
    "plugins": [
        "jquery",   // eslint-plugin-jquery
        "@foo/foo", // @foo/eslint-plugin-foo
        "@bar"      // @bar/eslint-plugin
    ],
    "extends": [
        "plugin:@foo/foo/recommended",
        "plugin:@bar/recommended"
    ],
    "rules": {
        "jquery/a-rule": "error",
        "@foo/foo/some-rule": "error",
        "@bar/another-rule": "error"
    },
    "env": {
        "jquery/jquery": true,
        "@foo/foo/env-foo": true,
        "@bar/env-bar": true,
    }
    // ...
}

指定处理器

¥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 键以及用斜线连接的插件名称和处理器名称的字符串。例如,以下内容启用插件 a-plugin 提供的处理器 a-processor

¥To specify processors in a configuration file, use the processor key with the concatenated string of a plugin name and a processor name by a slash. For example, the following enables the processor a-processor that the plugin a-plugin provided:

{
	"plugins": ["a-plugin"],
	"processor": "a-plugin/a-processor"
}

要为特定类型的文件指定处理器,请使用 overrides 键和 processor 键的组合。例如,以下内容将处理器 a-plugin/markdown 用于 *.md 文件。

¥To specify processors for specific kinds of files, use the combination of the overrides key and the processor key. For example, the following uses the processor a-plugin/markdown for *.md files.

{
	"plugins": ["a-plugin"],
	"overrides": [
		{
			"files": ["*.md"],
			"processor": "a-plugin/markdown"
		}
	]
}

处理器可以生成命名代码块,例如 0.js1.js。ESLint 将这样的命名代码块作为原始文件的子文件处理。你可以在配置的 overrides 部分中为命名代码块指定其他配置。例如,以下对 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 in the overrides section of the config. For example, the following disables the strict rule for the named code blocks which end with .js in markdown files.

{
	"plugins": ["a-plugin"],
	"overrides": [
		{
			"files": ["*.md"],
			"processor": "a-plugin/markdown"
		},
		{
			"files": ["**/*.md/*.js"],
			"rules": {
				"strict": "off"
			}
		}
	]
}

ESLint 检查命名代码块的文件路径,然后如果任何 overrides 条目与文件路径不匹配,则忽略这些代码块。如果要对除 *.js 之外的命名代码块进行 lint,请务必添加 overrides 条目。

¥ESLint checks the file path of named code blocks then ignores those if any overrides entry didn’t match the file path. Be sure to add an overrides entry if you want to lint named code blocks other than *.js.