@eslint/config-array

中文文档
0.23.5发布于 2026/04/08Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 5.2亿

General purpose glob-based configuration matching.

原始使用文档
文档目录19 节
  1. Description
  2. Installation
  3. Background
  4. Usage
  5. Specifying a Schema
  6. Config Arrays
  7. Config Functions
  8. Normalizing Config Arrays
  9. Getting Config for a File
  10. Determining Ignored Paths
  11. Caching Mechanisms
  12. Acknowledgements
  13. License
  14. Sponsors
  15. Platinum Sponsors
  16. Gold Sponsors
  17. Silver Sponsors
  18. Bronze Sponsors
  19. Technology Sponsors

Config Array

Description

A config array is a way of managing configurations that are based on glob pattern matching of filenames. Each config array contains the information needed to determine the correct configuration for any file based on the filename.

Note: This is a generic package that can be used outside of ESLint. It contains no ESLint-specific functionality.

Installation

For Node.js and compatible runtimes:

npm install @eslint/config-array
# or
yarn add @eslint/config-array
# or
pnpm install @eslint/config-array
# or
bun add @eslint/config-array

For Deno:

deno add @eslint/config-array

Background

The basic idea is that all configuration, including overrides, can be represented by a single array where each item in the array is a config object. Config objects appearing later in the array override config objects appearing earlier in the array. You can calculate a config for a given file by traversing all config objects in the array to find the ones that match the filename. Matching is done by specifying glob patterns in files and ignores properties on each config object. Here's an example:

export default [
    // match all JSON files
    {
        name: "JSON Handler",
        files: ["**/*.json"],
        handler: jsonHandler,
    },

    // match only package.json
    {
        name: "package.json Handler",
        files: ["package.json"],
        handler: packageJsonHandler,
    },
];

In this example, there are two config objects: the first matches all JSON files in all directories and the second matches just package.json in the base path directory (all the globs are evaluated as relative to a base path that can be specified). When you retrieve a configuration for foo.json, only the first config object matches so handler is equal to jsonHandler; when you retrieve a configuration for package.json, handler is equal to packageJsonHandler (because both config objects match, the second one wins).

Usage

First, import the ConfigArray constructor:

import { ConfigArray } from "@eslint/config-array";

// or using CommonJS

const { ConfigArray } = require("@eslint/config-array");

When you create a new instance of ConfigArray, you must pass in two arguments: an array of configs and an options object. The array of configs is most likely read in from a configuration file, so here's a typical example:

import path from "node:path";
import { pathToFileURL } from "node:url";
import { ConfigArray } from "@eslint/config-array";

const configFilename = path.resolve(process.cwd(), "my.config.js");
const { default: rawConfigs } = await import(pathToFileURL(configFilename));

const configs = new ConfigArray(rawConfigs, {
    // the path to match filenames from
    basePath: process.cwd(),

    // additional items in each config
    schema: mySchema,
});

This example reads in an object or array from my.config.js and passes it into the ConfigArray constructor as the first argument. The second argument is an object specifying the basePath (the directory in which my.config.js is found) and a schema to define the additional properties of a config object beyond files, ignores, basePath, and name.

Specifying a Schema

The schema option is required for you to use additional properties in config objects. The schema is an object that follows the format of an ObjectSchema. The schema specifies both validation and merge rules that the ConfigArray instance needs to combine configs when there are multiple matches. Here's an example:

const configFilename = path.resolve(process.cwd(), "my.config.js");
const { default: rawConfigs } = await import(pathToFileURL(configFilename));

const mySchema = {
    // define the handler key in configs
    handler: {
        required: true,
        merge(a, b) {
            return b ?? a;
        },
        validate(value) {
            if (typeof value !== "function") {
                throw new TypeError("Function expected.");
            }
        },
    },
};

const configs = new ConfigArray(rawConfigs, {
    // the path to match filenames from
    basePath: process.cwd(),

    // additional item schemas in each config
    schema: mySchema,

    // additional config types supported (default: [])
    extraConfigTypes: ["array", "function"],
});

Config Arrays

Config arrays can be multidimensional, so it's possible for a config array to contain another config array when extraConfigTypes contains "array", such as:

export default [
    // JS config
    {
        files: ["**/*.js"],
        handler: jsHandler,
    },

    // JSON configs
    [
        // match all JSON files
        {
            name: "JSON Handler",
            files: ["**/*.json"],
            handler: jsonHandler,
        },

        // match only package.json
        {
            name: "package.json Handler",
            files: ["package.json"],
            handler: packageJsonHandler,
        },
    ],

    // filename must match function
    {
        files: [filePath => filePath.endsWith(".md")],
        handler: markdownHandler,
    },

    // filename must match all patterns in subarray
    {
        files: [["*.test.*", "*.js"]],
        handler: jsTestHandler,
    },

    // filename must not match patterns beginning with !
    {
        name: "Non-JS files",
        files: ["!*.js"],
        settings: {
            js: false,
        },
    },

    // specific settings for files inside `src` directory
    {
        name: "Source files",
        basePath: "src",
        files: ["**/*"],
        settings: {
            source: true,
        },
    },
];

In this example, the array contains both config objects and a config array. When a config array is normalized (see details below), it is flattened so only config objects remain. However, the order of evaluation remains the same.

If the files array contains a function, then that function is called with the path of the file as it was passed in. The function is expected to return true if there is a match and false if not. (The ignores array can also contain functions.)

If the files array contains an item that is an array of strings and functions, then all patterns must match in order for the config to match. In the preceding examples, both *.test.* and *.js must match in order for the config object to be used.

If a pattern in the files array begins with ! then it excludes that pattern. In the preceding example, any filename that doesn't end with .js will automatically get a settings.js property set to false.

You can also specify an ignores key that will force files matching those patterns to not be included. If the ignores key is in a config object without any other keys, then those ignores will always be applied; otherwise those ignores act as exclusions. Here's an example:

export default [
    // Always ignored
    {
        ignores: ["**/.git/**", "**/node_modules/**"],
    },

    // .eslintrc.js file is ignored only when .js file matches
    {
        files: ["**/*.js"],
        ignores: [".eslintrc.js"],
        handler: jsHandler,
    },
];

You can use negated patterns in ignores to exclude a file that was already ignored, such as:

export default [
    // Ignore all JSON files except tsconfig.json
    {
        files: ["**/*"],
        ignores: ["**/*.json", "!tsconfig.json"],
    },
];

Config Functions

Config arrays can also include config functions when extraConfigTypes contains "function". A config function accepts a single parameter, context (defined by you), and must return either a config object or a config array (it cannot return another function). Config functions allow end users to execute code in the creation of appropriate config objects. Here's an example:

export default [
    // JS config
    {
        files: ["**/*.js"],
        handler: jsHandler,
    },

    // JSON configs
    function (context) {
        return [
            // match all JSON files
            {
                name: context.name + " JSON Handler",
                files: ["**/*.json"],
                handler: jsonHandler,
            },

            // match only package.json
            {
                name: context.name + " package.json Handler",
                files: ["package.json"],
                handler: packageJsonHandler,
            },
        ];
    },
];

When a config array is normalized, each function is executed and replaced in the config array with the return value.

Note: Config functions can also be async.

Normalizing Config Arrays

Once a config array has been created and loaded with all of the raw config data, it must be normalized before it can be used. The normalization process goes through and flattens the config array as well as executing all config functions to get their final values.

To normalize a config array, call the normalize() method and pass in a context object:

await configs.normalize({
    name: "MyApp",
});

The normalize() method returns a promise, so be sure to use the await operator. The config array instance is normalized in-place, so you don't need to create a new variable.

If you want to disallow async config functions, you can call normalizeSync() instead. This method is completely synchronous and does not require using the await operator as it does not return a promise:

configs.normalizeSync({
    name: "MyApp",
});

Important: Once a ConfigArray is normalized, it cannot be changed further. You can, however, create a new ConfigArray and pass in the normalized instance to create an unnormalized copy.

Getting Config for a File

To get the config for a file, use the getConfig() method on a normalized config array and pass in the filename to get a config for:

// pass in filename
const fileConfig = configs.getConfig(
    path.resolve(process.cwd(), "package.json"),
);

The config array always returns an object, even if there are no configs matching the given filename. You can then inspect the returned config object to determine how to proceed.

A few things to keep in mind:

  • If a filename is not an absolute path, it will be resolved relative to the base path directory.
  • The returned config object never has files, ignores, basePath, or name properties; the only properties on the object will be the other configuration options specified.
  • The config array caches configs, so subsequent calls to getConfig() with the same filename will return in a fast lookup rather than another calculation.
  • A config will only be generated if the filename matches an entry in a files key. A config will not be generated without matching a files key (configs without a files key are only applied when another config with a files key is applied; configs without files are never applied on their own). Any config with a files key entry that is * or ends with /** or /* will only be applied if another entry in the same files key matches or another config matches.

Determining Ignored Paths

You can determine if a file is ignored by using the isFileIgnored() method and passing in the path of any file, as in this example:

const ignored = configs.isFileIgnored("/foo/bar/baz.txt");

A file is considered ignored if any of the following is true:

  • Its parent directory is ignored. For example, if foo is in ignores, then foo/a.js is considered ignored.
  • It has an ancestor directory that is ignored. For example, if foo is in ignores, then foo/baz/a.js is considered ignored.
  • It matches an ignored file pattern. For example, if **/a.js is in ignores, then foo/a.js and foo/baz/a.js are considered ignored.
  • If it matches an entry in files and also in ignores. For example, if **/*.js is in files and **/a.js is in ignores, then foo/a.js and foo/baz/a.js are considered ignored.
  • The file is outside the basePath. If the basePath is /usr/me, then /foo/a.js is considered ignored.

For directories, use the isDirectoryIgnored() method and pass in the path of any directory, as in this example:

const ignored = configs.isDirectoryIgnored("/foo/bar/");

A directory is considered ignored if any of the following is true:

  • Its parent directory is ignored. For example, if foo is in ignores, then foo/baz is considered ignored.
  • It has an ancestor directory that is ignored. For example, if foo is in ignores, then foo/bar/baz/a.js is considered ignored.
  • It matches an ignored file pattern. For example, if **/a.js is in ignores, then foo/a.js and foo/baz/a.js are considered ignored.
  • If it matches an entry in files and also in ignores. For example, if **/*.js is in files and **/a.js is in ignores, then foo/a.js and foo/baz/a.js are considered ignored.
  • The file is outside the basePath. If the basePath is /usr/me, then /foo/a.js is considered ignored.

Important: A pattern such as foo/** means that foo and foo/ are not ignored whereas foo/bar is ignored. If you want to ignore foo and all of its subdirectories, use the pattern foo or foo/ in ignores.

Caching Mechanisms

Each ConfigArray aggressively caches configuration objects to avoid unnecessary work. This caching occurs in two ways:

  1. File-based Caching. For each filename that is passed into a method, the resulting config is cached against that filename so you're always guaranteed to get the same object returned from getConfig() whenever you pass the same filename in.
  2. Index-based Caching. Whenever a config is calculated, the config elements that were used to create the config are also cached. So if a given filename matches elements 1, 5, and 7, the resulting config is cached with a key of 1,5,7. That way, if another file is passed that matches the same config elements, the result is already known and doesn't have to be recalculated. That means two files that match all the same elements will return the same config from getConfig().

Acknowledgements

The design of this project was influenced by feedback on the ESLint RFC, and incorporates ideas from:

  • Teddy Katz (@not-an-aardvark)
  • Toru Nagashima (@mysticatea)
  • Kai Cataldo (@kaicataldo)

License

Apache 2.0

Sponsors

The following companies, organizations, and individuals support ESLint's ongoing maintenance and development. Become a Sponsor to get your logo on our READMEs and website.

Platinum Sponsors

Automattic

Gold Sponsors

Qlty Software

Silver Sponsors

Vite Liftoff StackBlitz

Bronze Sponsors

Cybozu SAP CrawlJobs aeriusventilation's Org Depot Icons8 Discord GitBook HeroCoders TestMu AI Open Source Office (Formerly LambdaTest)

Technology Sponsors

Technology sponsors allow us to use their products and services for free as part of a contribution to the open source ecosystem and our work.

Netlify Algolia 1Password

版本与发布记录

@eslint/config-array 共发布 20 个版本,首次发布于 2024/05/31;过去 12 个月发布了 8 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.23.5latest2026/04/086 个月前
0.23.42026/04/047 个月前
0.23.32026/03/078 个月前
0.21.2maintenance2026/03/078 个月前
0.23.22026/02/208 个月前
0.23.12026/02/078 个月前

关于 @eslint/config-array 的常见问题

@eslint/config-array 是什么?

General purpose glob-based configuration matching.

如何安装 @eslint/config-array?

在项目目录运行 npm install @eslint/config-array,或使用 pnpm add / yarn add / bun add。

@eslint/config-array 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@eslint/config-array 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@eslint/config-array 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^20.19.0 || ^22.13.0 || >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@eslint/config-array 可以商用吗?

@eslint/config-array 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@eslint/config-array 的下载量有多少?

根据 npm 官方下载统计,@eslint/config-array 在 2026/10/07 – 2026/11/05 这 30 天内被下载 517,948,465 次,日均约 17,264,949 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@eslint/config-array 还在维护吗?

最新版本 0.23.5 发布于 2026/04/08(6 个月前);至今共发布 20 个版本,其中过去 12 个月发布了 8 个;首次发布于 2024/05/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。