Skip to content

Latest commit

 

History

History

README.md

@eslint/config-helpers

Description

Helper utilities for creating ESLint configuration.

Installation

For Node.js and compatible runtimes:

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

For Deno:

deno add @eslint/config-helpers

Usage

defineConfig()

The defineConfig() function allows you to specify an ESLint configuration with full type checking and additional capabilities, such as extends. Here's an example:

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

export default defineConfig([
	{
		files: ["src/**/*.js"],
		plugins: { js },
		extends: ["js/recommended"],
		rules: {
			"no-var": "error",
			"prefer-const": "error",
		},
	},
	{
		files: ["test/**/*.js"],
		rules: {
			"no-console": "off",
		},
	},
]);

globalIgnores()

The globalIgnores() function allows you to specify patterns for files and directories that should be globally ignored by ESLint. This is useful for excluding files that you don't want to lint, such as build directories or third-party libraries. Here's an example:

// eslint.config.js
import { defineConfig, globalIgnores } from "@eslint/config-helpers";

export default defineConfig([
	{
		files: ["src/**/*.js"],
		rules: {
			"no-var": "error",
			"prefer-const": "error",
		},
	},
	globalIgnores(["node_modules/", "dist/", "coverage/"]),
]);

includeIgnoreFile()

The includeIgnoreFile() function reads a file with gitignore-style patterns (such as a .gitignore) and returns a config object with the patterns converted to a global ignores object. Pass the absolute path to the ignore file as the first argument:

// eslint.config.js

import { defineConfig, includeIgnoreFile } from "@eslint/config-helpers";
import path from "node:path";

const ignorePath = path.join(import.meta.dirname, ".gitignore");

export default defineConfig([
	includeIgnoreFile(ignorePath, {
		gitignoreResolution: true,
	}),
	// ...
]);

Options

The second argument is an optional options object:

  • gitignoreResolution (boolean): Controls how ignore patterns are interpreted.
    • false (default) — patterns are resolved relative to the location of the configuration file.
    • true — patterns are resolved relative to the location of the ignore file, matching the behavior of .gitignore files.
  • name (string): A custom name for the resulting config object.

For backwards compatibility with includeIgnoreFile() from @eslint/compat, passing a string instead of an object as the second argument is treated as equivalent to providing a value for name.

Multiple files

You can also pass an array of absolute paths to include multiple ignore files at once. In this case an array of config objects is returned:

// eslint.config.js

import { defineConfig, includeIgnoreFile } from "@eslint/config-helpers";
import path from "node:path";

export default defineConfig([
	includeIgnoreFile(
		[
			path.join(import.meta.dirname, ".gitignore"),
			path.join(import.meta.dirname, "packages/lib/.gitignore"),
		],
		{ gitignoreResolution: true },
	),
	// ...
]);

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 Syntax Depot Icons8 Discord GitBook HeroCoders Citadel AI

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