eslint-flat-config-utils

  • Version 2.1.4
  • Published
  • 50.3 kB
  • 1 dependency
  • MIT license

Install

npm i eslint-flat-config-utils
yarn add eslint-flat-config-utils
pnpm add eslint-flat-config-utils

Overview

Utils for managing and manipulating ESLint flat config arrays

Index

Variables

variable DEFAULT_PLUGIN_CONFLICTS_ERROR

const DEFAULT_PLUGIN_CONFLICTS_ERROR: string;

    variable pipe

    const pipe: <
    T extends Linter.Config = Linter.Config,
    ConfigNames extends string = never
    >(
    ...configs: ResolvableFlatConfig<Linter.Config extends T ? T : Linter.Config>[]
    ) => FlatConfigComposer<Linter.Config extends T ? T : Linter.Config, ConfigNames>;
    • Deprecated

      Renamed to composer.

    Functions

    function composer

    composer: <
    T extends Linter.Config = Linter.Config,
    ConfigNames extends string = never
    >(
    ...configs: ResolvableFlatConfig<Linter.Config extends T ? T : Linter.Config>[]
    ) => FlatConfigComposer<Linter.Config extends T ? T : Linter.Config, ConfigNames>;
    • Create a chainable composer object that makes manipulating ESLint flat config easier.

      It extends Promise, so that you can directly await or export it to eslint.config.mjs

      // eslint.config.mjs
      import { composer } from 'eslint-flat-config-utils'
      export default composer(
      {
      plugins: {},
      rules: {},
      }
      // ...some configs, accepts same arguments as `concat`
      )
      .append(
      // appends more configs at the end, accepts same arguments as `concat`
      )
      .prepend(
      // prepends more configs at the beginning, accepts same arguments as `concat`
      )
      .insertAfter(
      'config-name', // specify the name of the target config, or index
      // insert more configs after the target, accepts same arguments as `concat`
      )
      .renamePlugins({
      // rename plugins
      'old-name': 'new-name',
      // for example, rename `n` from `eslint-plugin-n` to more a explicit prefix `node`
      'n': 'node'
      // applies to all plugins and rules in the configs
      })
      .override(
      'config-name', // specify the name of the target config, or index
      {
      // merge with the target config
      rules: {
      'no-console': 'off'
      },
      }
      )
      // And you an directly return the composer object to `eslint.config.mjs`

    function concat

    concat: <T extends Linter.Config = Linter.Config>(
    ...configs: Awaitable<T | T[]>[]
    ) => Promise<T[]>;
    • Concat multiple flat configs into a single flat config array.

      It also resolves promises and flattens the result.

      Example 1

      import { concat } from 'eslint-flat-config-utils'
      import eslint from '@eslint/js'
      import stylistic from '@stylistic/eslint-plugin'
      export default concat(
      eslint,
      stylistic.configs.customize(),
      { rules: { 'no-console': 'off' } },
      // ...
      )

    function defineFlatConfig

    defineFlatConfig: <T extends Linter.Config = Linter.Config>(config: T) => T;
    • A function that returns the config as-is, useful for providing type hints.

    function disableRuleFixes

    disableRuleFixes: (rule: Rule.RuleModule) => Rule.RuleModule;
    • Hijack into a rule's context.report to disable fixes.

    function extend

    extend: (
    configs: Awaitable<Linter.Config[]>,
    relativePath: string
    ) => Promise<Linter.Config[]>;
    • Extend another flat configs and rename globs paths.

      Example 1

      import { extend } from 'eslint-flat-config-utils'
      export default [
      ...await extend(
      // configs to extend
      import('./other-configs/eslint.config.js').then(m => m.default),
      // relative directory path
      'other-configs/',
      ),
      ]

    function hijackPluginRule

    hijackPluginRule: (
    plugin: ESLint.Plugin,
    name: string,
    factory: (rule: Rule.RuleModule) => Rule.RuleModule
    ) => ESLint.Plugin;
    • Replace a rule in a plugin with given factory.

    function mergeConfigs

    mergeConfigs: <T extends Linter.Config = Linter.Config>(...configs: T[]) => T;
    • Merge multiple flat configs into a single flat config.

      Note there is no guarantee that the result works the same as the original configs.

    function parseRuleId

    parseRuleId: (ruleId: string) => { plugin: string | null; rule: string };

      function renamePluginsInConfigs

      renamePluginsInConfigs: <T extends Linter.Config = Linter.Config>(
      configs: T[],
      map: Record<string, string>
      ) => T[];
      • Rename plugin names a flat configs array

        Example 1

        import { renamePluginsInConfigs } from 'eslint-flat-config-utils'
        import someConfigs from './some-configs'
        export default renamePluginsInConfigs(someConfigs, {
        '@typescript-eslint': 'ts',
        'import-x': 'import',
        })

      function renamePluginsInRules

      renamePluginsInRules: (
      rules: Record<string, any>,
      map: Record<string, string>
      ) => Record<string, any>;
      • Rename plugin prefixes in a rule object. Accepts a map of prefixes to rename.

        Example 1

        import { renamePluginsInRules } from 'eslint-flat-config-utils'
        export default [{
        rules: renamePluginsInRules(
        {
        '@typescript-eslint/indent': 'error'
        },
        { '@typescript-eslint': 'ts' }
        )
        }]

      Classes

      class FlatConfigComposer

      class FlatConfigComposer<
      T extends object = Linter.Config,
      ConfigNames extends string = keyof DefaultConfigNamesMap
      > extends Promise<T[]> {}
      • The underlying impolementation of composer().

      constructor

      constructor(...configs: ResolvableFlatConfig<T>[]);

        method append

        append: (...items: ResolvableFlatConfig<T>[]) => this;
        • Append configs to the end of the current configs array.

        method catch

        catch: (onRejected: (reason: any) => any) => Promise<any>;

          method clone

          clone: () => FlatConfigComposer<T>;
          • Clone the composer object.

          method disableRulesFix

          disableRulesFix: (ruleIds: string[], options?: DisableFixesOptions) => this;
          • Hijack into plugins to disable fixes for specific rules.

            Note this mutates the plugin object, use with caution.

            Example 1

            const config = await composer(...)
            .disableRulesFix([
            'unused-imports/no-unused-imports',
            'vitest/no-only-tests'
            ])

          method finally

          finally: (onFinally: () => any) => Promise<T[]>;

            method insertAfter

            insertAfter: (
            nameOrIndex: StringLiteralUnion<ConfigNames, string | number>,
            ...items: ResolvableFlatConfig<T>[]
            ) => this;
            • Insert configs after a specific config.

            method insertBefore

            insertBefore: (
            nameOrIndex: StringLiteralUnion<ConfigNames, string | number>,
            ...items: ResolvableFlatConfig<T>[]
            ) => this;
            • Insert configs before a specific config.

            method onResolved

            onResolved: (callback: (configs: T[]) => Awaitable<T[] | void>) => this;
            • Hook when all configs are resolved but before returning the final configs.

              You can modify the final configs here.

            method override

            override: (
            nameOrIndex: StringLiteralUnion<ConfigNames, string | number>,
            config: T | ((config: T) => Awaitable<T>)
            ) => this;
            • Provide overrides to a specific config.

              It will be merged with the original config, or provide a custom function to replace the config entirely.

            method overrideRules

            overrideRules: (rules: NullableObject<GetRuleRecordFromConfig<T>>) => this;
            • Override rules and it's options in **all configs**.

              Pass null as the value to remove the rule.

              Example 1

              composer
              .overrideRules({
              'no-console': 'off',
              'no-unused-vars': ['error', { vars: 'all', args: 'after-used' }],
              // remove the rule from all configs
              'no-undef': null,
              })

            method overrides

            overrides: (
            overrides: Partial<
            Record<
            StringLiteralUnion<ConfigNames, string | number>,
            T | ((config: T) => Awaitable<T>)
            >
            >
            ) => this;
            • Provide overrides to multiple configs as an object map.

              Same as calling override multiple times.

            method prepend

            prepend: (...items: ResolvableFlatConfig<T>[]) => this;
            • Prepend configs to the beginning of the current configs array.

            method remove

            remove: (nameOrIndex: ConfigNames | string | number) => this;
            • Remove a specific config by name or index.

            method removePlugins

            removePlugins: (...names: string[]) => this;
            • Remove plugins by name and all the rules referenced by them.

              Example 1

              composer
              .removePlugins(
              'node'
              )

              The plugins: { node } and rules: { 'node/xxx': 'error' } will be removed from all configs.

            method removeRules

            removeRules: (
            ...rules: StringLiteralUnion<
            FilterType<keyof GetRuleRecordFromConfig<T>, string>,
            string
            >[]
            ) => this;
            • Remove rules from **all configs**.

              Example 1

              composer
              .removeRules(
              'no-console',
              'no-unused-vars'
              )

            method renamePlugins

            renamePlugins: (renames: Record<string, string>) => this;
            • Set plugin renames, like n -> node, import-x -> import, etc.

              This will runs after all config items are resolved. Applies to plugins and rules.

            method replace

            replace: (
            nameOrIndex: StringLiteralUnion<ConfigNames, string | number>,
            ...items: ResolvableFlatConfig<T>[]
            ) => this;
            • Replace a specific config by name or index.

              The original config will be removed and replaced with the new one.

            method setPluginConflictsError

            setPluginConflictsError: {
            (warning?: string | PluginConflictsError): this;
            (
            pluginName: string,
            warning: string | PluginConflictsError<Linter.Config>
            ): this;
            };
            • Set a custom warning message for plugins conflicts.

              The error message can be a string or a function that returns a string.

              Error message accepts template strings: - {{pluginName}}: the name of the plugin that has conflicts - {{configName1}}: the name of the first config that uses the plugin - {{configName2}}: the name of the second config that uses the plugin - {{configNames}}: a list of config names that uses the plugin

              When only one argument is provided, it will be used as the default error message.

            method then

            then: (
            onFulfilled: (value: T[]) => any,
            onRejected?: (reason: any) => any
            ) => Promise<any>;

              method toConfigs

              toConfigs: () => Promise<T[]>;
              • Resolve the pipeline and return the final configs.

                This returns a promise. Calling .then() has the same effect.

              class FlatConfigPipeline

              class FlatConfigPipeline<
              T extends object = Linter.Config,
              ConfigNames extends string = string
              > extends FlatConfigComposer<T, ConfigNames> {}
              • Deprecated

                Renamed to FlatConfigComposer.

              Interfaces

              interface DefaultConfigNamesMap

              interface DefaultConfigNamesMap {}
              • Default config names map. Used for type augmentation.

                Example 1

                declare module 'eslint-flat-config-utils' {
                interface DefaultConfigNamesMap {
                'my-custom-config': true
                }
                }

              interface DisableFixesOptions

              interface DisableFixesOptions {}

                property builtinRules

                builtinRules?:
                | Map<string, Rule.RuleModule>
                | (() => Awaitable<Map<string, Rule.RuleModule>>);

                  interface FlatConfigItem

                  interface FlatConfigItem extends Linter.Config {}
                  • Alias to Linter.Config

                    Deprecated

                  Type Aliases

                  type Arrayable

                  type Arrayable<T> = T | T[];
                  • A type that can be an array or a single item.

                  type Awaitable

                  type Awaitable<T> = T | Promise<T>;
                  • A type that can be awaited. Promise or T.

                  type FilterType

                  type FilterType<T, F> = T extends F ? T : never;

                    type GetRuleRecordFromConfig

                    type GetRuleRecordFromConfig<T> = T extends {
                    rules?: infer R;
                    }
                    ? R
                    : Linter.RulesRecord;

                      type NullableObject

                      type NullableObject<T> = {
                      [K in keyof T]?: T[K] | null | undefined;
                      };

                        type PluginConflictsError

                        type PluginConflictsError<T extends Linter.Config = Linter.Config> = (
                        pluginName: string,
                        configs: T[]
                        ) => string;

                          type ResolvableFlatConfig

                          type ResolvableFlatConfig<T extends Linter.Config = Linter.Config> =
                          | Awaitable<Arrayable<T | false | undefined | null>>
                          | Awaitable<(Linter.Config | false | undefined | null)[]>
                          | FlatConfigComposer<any>;
                          • Awaitable array of ESLint flat configs or a composer object.

                          type StringLiteralUnion

                          type StringLiteralUnion<T extends U, U = string> = T | (U & Nothing);
                          • type StringLiteralUnion<'foo'> = 'foo' | string This has auto completion whereas 'foo' | string doesn't Adapted from https://github.com/microsoft/TypeScript/issues/29729

                          Package Files (1)

                          Dependencies (1)

                          Dev Dependencies (17)

                          Peer Dependencies (0)

                          No peer dependencies.

                          Badge

                          To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                          You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/eslint-flat-config-utils.

                          • Markdown
                            [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/eslint-flat-config-utils)
                          • HTML
                            <a href="https://www.jsdocs.io/package/eslint-flat-config-utils"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>