Index

组合配置

在许多情况下,你不会从头编写 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

如果你从另一个模块导入一个 配置对象,你可以通过创建一个带有 files 键的新对象,并使用 extends 键将导入对象的其余属性合并进来,从而仅将其应用于文件的子集。例如:

🌐 If you are importing a config object from another module, you can apply it 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 imported object. For example:

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

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

这里,"js/recommended" 预定义配置首先应用于匹配 "**/*.js" 模式的文件,然后为 no-unused-vars 添加所需的配置。

🌐 Here, the "js/recommended" predefined configuration is applied to files that match the pattern "**/*.js" first and then adds the desired configuration for no-unused-vars.

应用配置数组

🌐 Apply a Config Array

如果你正在从另一个模块导入配置数组,可以通过使用 extends 键将配置数组(配置对象数组)仅应用于文件的子集。例如:

🌐 If you are importing a config array from another module, you can apply a config array (an array of configuration objects) 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: ["**/*.js"],
		extends: [exampleConfigs],
		rules: {
			"no-unused-vars": "warn",
		},
	},
]);

在这里,exampleConfigs 可共享配置应用于匹配模式的文件 “**/*.js" 首先,然后另一个配置对象为 no-unused-vars 添加所需的配置。”

🌐 Here, the exampleConfigs shareable configuration is applied to files that match the pattern "**/*.js" first and then another configuration object adds the desired configuration for no-unused-vars.