组合配置

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

export default [
    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 Config Object to a Subset of Files

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

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

// eslint.config.js
import js from "@eslint/js";

export default [
    {
        ...js.configs.recommended,
        files: ["**/src/safe/*.js"]
    }
];

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

¥Here, the js.configs.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, you can use the array spread operator to insert the items from that array into your exported array. Here’s an example:

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

export default [
    ...exampleConfigs,

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

此处,首先应用 exampleConfigs 可共享配置,然后另一个配置对象添加 no-unused-vars 所需的配置。

¥Here, the exampleConfigs shareable configuration is applied first and then another configuration object adds the desired configuration for no-unused-vars.

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

¥Apply a Config Array to a Subset of Files

你可以使用 map() 方法将 files 键添加到每个配置对象,从而将配置数组应用于文件的子集。例如:

¥You can apply a config array to just a subset of files by using the map() method to add a files key to each config object. For example:

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

export default [
    ...exampleConfigs.map(config => ({
        ...config,
        files: ["**/src/safe/*.js"]
    })),

    // your modifications
    {
        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".

ESLint 中文网
粤ICP备13048890号