diff --git a/docs/how-to-guides/eslint-v10-migration.md b/docs/how-to-guides/eslint-v10-migration.md new file mode 100644 index 00000000000000..e0e619fb93e9f5 --- /dev/null +++ b/docs/how-to-guides/eslint-v10-migration.md @@ -0,0 +1,272 @@ +# Migrating to ESLint v10 with `@wordpress/eslint-plugin` + +`@wordpress/eslint-plugin` has been upgraded to support ESLint v10 with flat config. This guide covers how to migrate your project. + +## Upstream ESLint migration resources + +If you are also upgrading ESLint itself (e.g., coming from ESLint v8), the ESLint project publishes per-version migration guides that complement this document: + +- [Migrate to v9.x](https://eslint.org/docs/latest/use/migrate-to-9.0.0) — flat config as default, removal of legacy formatters, Node.js version requirements, and more. +- [Migrate to v10.x](https://eslint.org/docs/latest/use/migrate-to-10.0.0) — complete removal of eslintrc, removal of `/* eslint-env */` comments, and other breaking changes. + +If you are jumping straight from ESLint v8 to v10, you need to follow **both** upstream guides in order (v8 → v9 → v10), since v10 assumes you have already addressed the v9 breaking changes. + +## TL;DR — Breaking changes by ESLint version + +### v8 → v9 + +- Flat config (`eslint.config.*`) becomes the default, but `.eslintrc.*` is still supported as a fallback. +- Node.js `^18.18.0 || ^20.9.0 || >=21.1.0` is required. +- Several formatters moved out of core into separate packages. +- Some rules were removed or had their defaults changed (see the upstream [v9 migration guide](https://eslint.org/docs/latest/use/migrate-to-9.0.0)). + +### v9 → v10 + +- **`.eslintrc.*` is no longer supported at all** — flat config is mandatory. +- `/* eslint-env */` comments are no longer supported — configure globals via `languageOptions.globals`. +- The `--ignore-path` CLI flag was removed — use `ignores` in your flat config. +- Various rule default changes (see the upstream [v10 migration guide](https://eslint.org/docs/latest/use/migrate-to-10.0.0)). + +### `@wordpress/eslint-plugin` specific changes + +- **Flat config is the default.** The plugin now exports flat config arrays instead of eslintrc objects. +- **Minimum ESLint version** is `^9.0.0 || ^10.0.0`. +- **Rule prefix change:** `eslint-comments/*` → `@eslint-community/eslint-comments/*`. +- **TypeScript ESLint** upgraded from `@typescript-eslint/*` v6 to v8 via the unified `typescript-eslint` package. +- **`wp-scripts lint-js`** now defaults to flat config detection. The default config path changed from `config/.eslintrc.js` to `config/eslint.config.cjs`. + +## Migration steps + +### 1. Replace your config file + +Replace your `.eslintrc.*` file with `eslint.config.mjs`: + +```js +// OLD — .eslintrc.js +module.exports = { + extends: [ 'plugin:@wordpress/eslint-plugin/recommended' ], +}; + +// NEW — eslint.config.mjs +import wordpress from '@wordpress/eslint-plugin'; + +export default [ ...wordpress.configs.recommended ]; +``` + +### 2. Convert `overrides` to flat config objects + +In eslintrc, you used `overrides` to apply rules to specific files. In flat config, each override becomes a separate config object with a `files` pattern: + +```js +// OLD — .eslintrc.js +module.exports = { + extends: [ 'plugin:@wordpress/eslint-plugin/recommended' ], + overrides: [ + { + files: [ '*.test.js' ], + rules: { + 'no-console': 'off', + }, + }, + ], +}; + +// NEW — eslint.config.mjs +import wordpress from '@wordpress/eslint-plugin'; + +export default [ + ...wordpress.configs.recommended, + { + files: [ '**/*.test.js' ], + rules: { + 'no-console': 'off', + }, + }, +]; +``` + +Note that flat config `files` patterns are relative to the config file location, and globs like `*.test.js` only match the current directory. Use `**/*.test.js` to match recursively. + +### 3. Replace `env` with `languageOptions.globals` + +The `env` key is not supported in flat config. Use the [`globals`](https://www.npmjs.com/package/globals) package instead: + +```bash +npm install globals --save-dev +``` + +```js +// OLD — .eslintrc.js +module.exports = { + env: { + browser: true, + node: true, + }, +}; + +// NEW — eslint.config.mjs +import globals from 'globals'; + +export default [ + { + languageOptions: { + globals: { + ...globals.browser, + ...globals.node, + }, + }, + }, +]; +``` + +### 4. Move `.eslintignore` to config + +Delete your `.eslintignore` file and add the patterns to your config: + +```js +// eslint.config.mjs +import wordpress from '@wordpress/eslint-plugin'; + +export default [ + { + ignores: [ 'build/', 'vendor/', 'node_modules/' ], + }, + ...wordpress.configs.recommended, +]; +``` + +Note: the `ignores` object should come **before** other config objects to apply globally. + +### 5. Update inline comments + +#### Rule prefix changes + +`eslint-plugin-eslint-comments` has been replaced with `@eslint-community/eslint-plugin-eslint-comments`. Update any inline disable comments: + +```diff +- /* eslint-disable eslint-comments/no-unlimited-disable */ ++ /* eslint-disable @eslint-community/eslint-comments/no-unlimited-disable */ +``` + +To find all occurrences in your project: + +```bash +grep -r "eslint-comments/" --include="*.js" --include="*.ts" --include="*.jsx" --include="*.tsx" . +``` + +#### Remove `/* eslint-env */` comments + +ESLint v10 no longer supports `/* eslint-env */` comments. Remove them and configure globals in your config file instead (see step 3). + +```diff +- /* eslint-env browser */ +``` + +### 6. Convert `plugins` to direct imports + +If you used additional plugins, import them directly: + +```js +// OLD — .eslintrc.js +module.exports = { + plugins: [ 'my-plugin' ], + rules: { + 'my-plugin/some-rule': 'error', + }, +}; + +// NEW — eslint.config.mjs +import myPlugin from 'eslint-plugin-my-plugin'; + +export default [ + { + plugins: { + 'my-plugin': myPlugin, + }, + rules: { + 'my-plugin/some-rule': 'error', + }, + }, +]; +``` + +## For `wp-scripts lint-js` users + +### What changed + +- `wp-scripts lint-js` now detects `eslint.config.*` files first. If found, it uses flat config mode. +- If no flat config is found, it falls back to `.eslintrc.*` files. **This fallback is deprecated** and will be removed in a future version. +- The default config path shipped with `wp-scripts` changed from `config/.eslintrc.js` to `config/eslint.config.cjs`. +- The `--ignore-path` CLI flag was removed along with the eslintrc system. Use `ignores` arrays in your flat config instead. + +### Migrating a custom `wp-scripts` config + +If you were extending the default `wp-scripts` ESLint config: + +```js +// OLD — .eslintrc.js +const defaultConfig = require( '@wordpress/scripts/config/.eslintrc' ); + +module.exports = { + ...defaultConfig, + rules: { + ...defaultConfig.rules, + 'no-console': 'off', + }, +}; + +// NEW — eslint.config.cjs +const defaultConfig = require( '@wordpress/scripts/config/eslint.config.cjs' ); + +module.exports = [ + ...defaultConfig, + { + rules: { + 'no-console': 'off', + }, + }, +]; +``` + +## Temporary eslintrc compatibility wrapper + +If you are using ESLint v9 and are not ready to migrate to flat config, a compatibility wrapper is available: + +```js +// .eslintrc.js +const wordpress = require( '@wordpress/eslint-plugin/eslintrc' ); + +module.exports = wordpress.configs.recommended; +``` + +All config presets are available (e.g., `wordpress.configs.esnext`, `wordpress.configs[ 'recommended-with-formatting' ]`). + +> **This wrapper is deprecated** and will be removed in the next major version. ESLint v10 does not support `.eslintrc.*` files at all. We strongly recommend migrating to flat config. + +## TypeScript ESLint changes + +`@typescript-eslint/*` has been upgraded from v6 to v8 via the unified `typescript-eslint` package. Most rules remain the same, but some defaults may have changed. See the [typescript-eslint v8 announcement](https://typescript-eslint.io/blog/announcing-typescript-eslint-v8/) for details. + +If you were referencing `@typescript-eslint` rules directly in your config, they should continue to work without changes. + +## Troubleshooting + +### "ESLintrc configuration objects are no longer supported" + +You are running ESLint v10 with an `.eslintrc.*` file. ESLint v10 only supports flat config. Migrate your config to `eslint.config.mjs` following the steps above. + +### "Cannot find module '@wordpress/eslint-plugin/eslintrc'" + +The eslintrc wrapper requires `@wordpress/eslint-plugin` v25+. Update your dependency: + +```bash +npm install @wordpress/eslint-plugin@latest --save-dev +``` + +### "Definition for rule 'eslint-comments/...' was not found" + +The rule prefix changed to `@eslint-community/eslint-comments/*`. Update your inline comments and any custom rule configurations to use the new prefix. + +### Flat config not being detected by `wp-scripts` + +Ensure your config file is named `eslint.config.mjs`, `eslint.config.cjs`, or `eslint.config.js` in your project root. Other names or locations are not auto-detected. diff --git a/packages/eslint-plugin/CHANGELOG.md b/packages/eslint-plugin/CHANGELOG.md index a08fb39e1b9357..68235757074eab 100644 --- a/packages/eslint-plugin/CHANGELOG.md +++ b/packages/eslint-plugin/CHANGELOG.md @@ -6,16 +6,16 @@ ### Breaking Changes -- Upgraded to ESLint v10 with flat config format. The plugin now exports flat config arrays instead of eslintrc objects. Consumers must migrate from `.eslintrc.*` files to `eslint.config.mjs`. See the [migration guide](https://eslint.org/docs/latest/use/configure/migration-guide) for details. -- The minimum required ESLint version is now `^9.0.0 || ^10.0.0`. -- Upgraded `eslint-plugin-import` to v2.31+ (wrapped with `fixupPluginRules` for flat config compatibility). -- Upgraded `@typescript-eslint/*` from v6 to v8 (via the unified `typescript-eslint` package). -- Replaced `eslint-plugin-eslint-comments` with `@eslint-community/eslint-plugin-eslint-comments`. Rule prefixes changed from `eslint-comments/*` to `@eslint-community/eslint-comments/*`. +- Upgraded to ESLint v10 with flat config format. The plugin now exports flat config arrays instead of eslintrc objects. Consumers must migrate from `.eslintrc.*` files to `eslint.config.mjs`. See the [migration guide](https://eslint.org/docs/latest/use/configure/migration-guide) for details ([#76654](https://github.com/WordPress/gutenberg/pull/76654)). +- The minimum required ESLint version is now `^9.0.0 || ^10.0.0` ([#76654](https://github.com/WordPress/gutenberg/pull/76654)). +- Upgraded `eslint-plugin-import` to v2.31+ (wrapped with `fixupPluginRules` for flat config compatibility) ([#76654](https://github.com/WordPress/gutenberg/pull/76654)). +- Upgraded `@typescript-eslint/*` from v6 to v8 (via the unified `typescript-eslint` package) ([#76654](https://github.com/WordPress/gutenberg/pull/76654)). +- Replaced `eslint-plugin-eslint-comments` with `@eslint-community/eslint-plugin-eslint-comments`. Rule prefixes changed from `eslint-comments/*` to `@eslint-community/eslint-comments/*` ([#76654](https://github.com/WordPress/gutenberg/pull/76654)). ### New Features -- Added `@wordpress/eslint-plugin/eslintrc` entry point — a compatibility wrapper for ESLint v9 consumers still using `.eslintrc.*` files. This is deprecated and will be removed in a future major version. -- Configs are now exported as flat config arrays (e.g., `wordpress.configs.recommended` returns an array of config objects suitable for spreading into `eslint.config.mjs`). +- Added `@wordpress/eslint-plugin/eslintrc` entry point — a compatibility wrapper for ESLint v9 consumers still using `.eslintrc.*` files. This is deprecated and will be removed in a future major version ([#76654](https://github.com/WordPress/gutenberg/pull/76654)). +- Configs are now exported as flat config arrays (e.g., `wordpress.configs.recommended` returns an array of config objects suitable for spreading into `eslint.config.mjs`) ([#76654](https://github.com/WordPress/gutenberg/pull/76654)). ### Enhancements diff --git a/packages/eslint-plugin/README.md b/packages/eslint-plugin/README.md index 619ca7c59a5d0b..a612961855cd77 100644 --- a/packages/eslint-plugin/README.md +++ b/packages/eslint-plugin/README.md @@ -14,7 +14,11 @@ npm install @wordpress/eslint-plugin --save-dev ## Usage -### Flat config (ESLint v10+, recommended) +**Minimum ESLint version:** `^9.0.0 || ^10.0.0` + +> **Upgrading from an older version?** See the [ESLint v10 migration guide](https://github.com/WordPress/gutenberg/blob/HEAD/docs/how-to-guides/eslint-v10-migration.md) for a comprehensive walkthrough of the breaking changes, migration steps, and troubleshooting. + +### Flat config (ESLint v9+, recommended) Create an `eslint.config.mjs` file in your project root: @@ -41,7 +45,7 @@ export default [ Refer to the [ESLint flat config documentation](https://eslint.org/docs/latest/use/configure/configuration-files) for more information. -### Legacy eslintrc (ESLint v9, deprecated) +### Legacy eslintrc (ESLint v9 only, deprecated) If you are still using ESLint v9 with the legacy `.eslintrc.*` format, a compatibility wrapper is available: @@ -52,7 +56,9 @@ const wordpress = require( '@wordpress/eslint-plugin/eslintrc' ); module.exports = wordpress.configs.recommended; ``` -> **Note:** The eslintrc wrapper is deprecated and will be removed in a future major version. Please migrate to flat config. +All config presets are available through the wrapper (e.g., `wordpress.configs.esnext`, `wordpress.configs[ 'recommended-with-formatting' ]`). + +> **Note:** The eslintrc wrapper is deprecated and will be removed in the next major version. ESLint v10 does not support `.eslintrc.*` files at all. Please migrate to flat config. ### About the recommended preset @@ -151,8 +157,14 @@ If you are upgrading from a previous version that used `.eslintrc.*` files: 3. Convert `overrides` to separate config objects with `files` patterns. 4. Replace `env` with `languageOptions.globals` using the [`globals`](https://www.npmjs.com/package/globals) package. 5. Delete your `.eslintignore` file and move patterns into an `ignores` config object. +6. Update rule prefixes in inline comments: `eslint-comments/*` has been renamed to `@eslint-community/eslint-comments/*`. For example: + ```diff + - /* eslint-disable eslint-comments/no-unlimited-disable */ + + /* eslint-disable @eslint-community/eslint-comments/no-unlimited-disable */ + ``` +7. Remove any `/* eslint-env */` comments — they are no longer supported in ESLint v10. Use `languageOptions.globals` in your config instead. -See the [ESLint migration guide](https://eslint.org/docs/latest/use/configure/migration-guide) for full details. +For a comprehensive walkthrough with examples and troubleshooting, see the [Gutenberg ESLint v10 migration guide](https://github.com/WordPress/gutenberg/blob/HEAD/docs/how-to-guides/eslint-v10-migration.md). See also the [ESLint migration guide](https://eslint.org/docs/latest/use/configure/migration-guide) for general flat config details. ## Contributing to this package diff --git a/packages/scripts/CHANGELOG.md b/packages/scripts/CHANGELOG.md index 32c9be68778cb3..16b0ec98509f71 100644 --- a/packages/scripts/CHANGELOG.md +++ b/packages/scripts/CHANGELOG.md @@ -6,9 +6,9 @@ ### Breaking Changes -- The bundled `eslint` dependency has been upgraded from v8 to v10. -- The `lint-js` script now uses flat config (`eslint.config.*`) by default. Legacy `.eslintrc.*` files are still detected as a fallback, but this support is deprecated and will be removed in a future version. -- The default config shipped with `wp-scripts` has changed from `config/.eslintrc.js` to `config/eslint.config.cjs`. +- The bundled `eslint` dependency has been upgraded from v8 to v10 ([#76654](https://github.com/WordPress/gutenberg/pull/76654)). +- The `lint-js` script now uses flat config (`eslint.config.*`) by default. Legacy `.eslintrc.*` files are still detected as a fallback, but this support is deprecated and will be removed in a future version ([#76654](https://github.com/WordPress/gutenberg/pull/76654)). +- The default config shipped with `wp-scripts` has changed from `config/.eslintrc.js` to `config/eslint.config.cjs` ([#76654](https://github.com/WordPress/gutenberg/pull/76654)). ## 31.8.0 (2026-04-01) diff --git a/packages/scripts/README.md b/packages/scripts/README.md index e9c9f68492d433..6da35b66f5db11 100644 --- a/packages/scripts/README.md +++ b/packages/scripts/README.md @@ -246,6 +246,12 @@ By default, files located in `build`, `node_modules`, and `vendor` folders are i It uses [ESLint](https://eslint.org/) with the set of recommended rules defined in [@wordpress/eslint-plugin](https://www.npmjs.com/package/@wordpress/eslint-plugin) npm package. It detects flat config files (`eslint.config.*`) first, falling back to legacy `.eslintrc.*` files if present. You can override default rules with your own as described in the [ESLint configuration docs](https://eslint.org/docs/latest/use/configure/). Learn more in the [Advanced Usage](#advanced-usage) section. +**Breaking changes in ESLint v10:** + +- The default config path has changed from `config/.eslintrc.js` to `config/eslint.config.cjs`. If you were extending the default config, update your import path. +- The `--ignore-path` CLI flag was removed along with the eslintrc system. Use `ignores` arrays in your flat config instead. +- Legacy `.eslintrc.*` fallback detection is deprecated and will be removed in a future version. Migrate to `eslint.config.*` files. + ### `lint-pkg-json` Helps enforce standards for your `package.json` files.