组合配置

在许多情况下,你不会从头开始编写 ESLint 配置文件,而是将使用预定义和可共享配置的组合以及你自己的覆盖来为项目创建配置。本页介绍了一些可用于在配置文件中组合配置的模式。

¥In many cases, you won’t write an ESLint config file from scratch, but rather, you’ll use a combination of predefined and shareable configs along with your own overrides to create the config for your project. This page explains some of the patterns you can use to combine configs in your configuration file.

应用配置对象

¥Apply a Config Object

如果你从另一个模块导入对象,在大多数情况下,你可以直接将该对象插入到配置文件的导出数组中。例如,你可以通过导入 recommended 配置并在数组中使用它来使用 JavaScript 的推荐规则配置:

¥If you are importing an object from another module, in most cases, you can just insert the object directly into your config file’s exported array. For example, you can use the recommended rule configurations for JavaScript by importing the recommended config and using it in your array:

// eslint.config.js
import js from "@eslint/js";
import { defineConfig } from "eslint/config";

export default defineConfig([
	js.configs.recommended,
	{
		rules: {
			"no-unused-vars": "warn",
		},
	},
]);

在这里,首先应用 js.configs.recommended 预定义的配置,然后另一个配置对象为 no-unused-vars 添加所需的配置。

¥Here, the js.configs.recommended predefined configuration is applied first and then another configuration object adds the desired configuration for no-unused-vars.

将配置应用于文件子集

¥Apply a Configuration to a Subset of Files

你可以通过使用 files 键创建新对象并使用 extends 键合并配置对象中的其余属性,将配置对象应用于文件子集。例如:

¥You can apply a config object to just a subset of files by creating a new object with a files key and using the extends key to merge in the rest of the properties from the config object. For example:

// eslint.config.js
import js from "@eslint/js";
import { defineConfig } from "eslint/config";

export default defineConfig([
	{
		files: ["**/src/safe/*.js"],
		plugins: {
			js,
		},
		extends: ["js/recommended"],
	},
]);

此处,js/recommended 配置对象仅应用于与模式“**/src/safe/*.js".txt”匹配的文件。

¥Here, the js/recommended config object is applied only to files that match the pattern "**/src/safe/*.js".

应用配置数组

¥Apply a Config Array

如果要从另一个模块导入数组,请将数组直接插入到导出的数组中。这是一个例子:

¥If you are importing an array from another module, insert the array directly into your exported array. Here’s an example:

// eslint.config.js
import exampleConfigs from "eslint-config-example";
import { defineConfig } from "eslint/config";

export default defineConfig([
	// insert array directly
	exampleConfigs,

	// your modifications
	{
		rules: {
			"no-unused-vars": "warn",
		},
	},
]);

此处,首先应用 exampleConfigs 可共享配置,然后另一个配置对象添加 no-unused-vars 所需的配置。这相当于按顺序插入 exampleConfigs 的各个元素,例如:

¥Here, the exampleConfigs shareable configuration is applied first and then another configuration object adds the desired configuration for no-unused-vars. This is equivalent to inserting the individual elements of exampleConfigs in order, such as:

// eslint.config.js
import exampleConfigs from "eslint-config-example";
import { defineConfig } from "eslint/config";

export default defineConfig([
	// insert individual elements instead of an array
	exampleConfigs[0],
	exampleConfigs[1],
	exampleConfigs[2],

	// your modifications
	{
		rules: {
			"no-unused-vars": "warn",
		},
	},
]);

将配置数组应用于文件子集

¥Apply a Config Array to a Subset of Files

你可以使用 extends 键将配置数组应用于文件子集。例如:

¥You can apply a config array to just a subset of files by using the extends key. For example:

// eslint.config.js
import exampleConfigs from "eslint-config-example";
import { defineConfig } from "eslint/config";

export default defineConfig([
	{
		files: ["**/src/safe/*.js"],
		extends: [exampleConfigs],
		rules: {
			"no-unused-vars": "warn",
		},
	},
]);

这里,exampleConfigs 中的每个配置对象仅应用于与模式“**/src/safe/*.js".txt”匹配的文件。

¥Here, each config object in exampleConfigs is applied only to files that match the pattern "**/src/safe/*.js".