Unverified Commit 0f786e04 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

[eslint-config] [Breaking] Use Flat config by default (#84874)

> [!WARNING]
> v16 will not support backwards compatibility of legacy config. The
users can either migrate to flat config via `@next/codemod
next-lint-to-eslint-cli` or stay at v15.

### Why?

`eslint-config-next` is the recommended way to use the ESLint
configuration for Next.js, but it hasn't correctly supported the Flat
config. This PR makes `eslint-config-next` a Flat config by default, and
the users can use it by:

```js
import coreWebVitals from "eslint-config-next/core-web-vitals";
import typescript from "eslint-config-next/typescript";

const eslintConfig = [
  ...coreWebVitals,
  ...typescript,
  { ignores: [/* ...ignores to override default ignores */] },
];

export default eslintConfig;
```

Double-checked the usage at
https://github.com/vercel/next.js/tree/ba0db6f1c0256ac7a2941abaadc41f8e47c654a6/examples/with-supabase

### How?

- Converted to Flat config format
- Removed `@rushstack/eslint-patch` hack for peerDeps
- Replaced `@typescript-eslint/` packages to `typescript-eslint`
  - https://github.com/vercel/next.js/pull/84874#discussion_r2432032100
- Replaced `env` property with `languageOptions.globals`
  - https://github.com/vercel/next.js/pull/84874#discussion_r2432061040

### Follow up

- Update Codemod
- Update CNA templates
- Update Docs https://github.com/vercel/next.js/pull/84781
parent 3ae7ace0
......@@ -13,18 +13,17 @@
],
"dependencies": {
"@next/eslint-plugin-next": "16.0.0-canary.5",
"@rushstack/eslint-patch": "^1.10.3",
"@typescript-eslint/eslint-plugin": "^5.4.2 || ^6.0.0 || ^7.0.0 || ^8.0.0",
"@typescript-eslint/parser": "^5.4.2 || ^6.0.0 || ^7.0.0 || ^8.0.0",
"eslint-import-resolver-node": "^0.3.6",
"eslint-import-resolver-typescript": "^3.5.2",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-import": "^2.32.0",
"eslint-plugin-jsx-a11y": "^6.10.0",
"eslint-plugin-react": "^7.37.0",
"eslint-plugin-react-hooks": "^7.0.0"
"eslint-plugin-react-hooks": "^7.0.0",
"globals": "16.4.0",
"typescript-eslint": "^8.46.0"
},
"peerDependencies": {
"eslint": "^7.23.0 || ^8.0.0 || ^9.0.0",
"eslint": ">=9.0.0",
"typescript": ">=3.3.1"
},
"peerDependenciesMeta": {
......@@ -34,6 +33,7 @@
},
"devDependencies": {
"@types/eslint": "9.6.1",
"@types/eslint-plugin-jsx-a11y": "6.10.1",
"typescript": "5.9.2"
},
"scripts": {
......
import type { Linter } from 'eslint'
import nextPlugin from '@next/eslint-plugin-next'
import baseConfig from './index'
const config: Linter.LegacyConfig = {
extends: [require.resolve('.'), 'plugin:@next/next/core-web-vitals-legacy'],
}
const config: Linter.Config[] = [
...baseConfig,
nextPlugin.configs['core-web-vitals'],
]
// Use `export =` instead of `export default` for ESLint parser compatibility.
// ESLint expects parser modules to be directly importable as CommonJS modules (module.exports).
......
import type { Linter } from 'eslint'
import module from 'module'
/*
* @rushstack/eslint-patch is used to include plugins as dev
* dependencies instead of imposing them as peer dependencies
*
* https://www.npmjs.com/package/@rushstack/eslint-patch
*/
const keptPaths: string[] = []
const sortedPaths: string[] = []
const cwd = process.cwd().replace(/\\/g, '/')
const originalPaths = require.resolve.paths('eslint-plugin-import') || []
// plugins
import next from '@next/eslint-plugin-next'
import react from 'eslint-plugin-react'
import reactHooks from 'eslint-plugin-react-hooks'
import tsEslint from 'typescript-eslint'
// import * as ... for plugins without default export
import * as importPlugin from 'eslint-plugin-import'
import * as jsxA11yPlugin from 'eslint-plugin-jsx-a11y'
// eslint throws a conflict error when plugins resolve to different
// locations, since we want to lock our dependencies by default
// but also need to allow using user dependencies this updates
// our resolve paths to first check the cwd and iterate to
// eslint-config-next's dependencies if needed
// utils
import globals from 'globals'
import eslintParser from './parser'
for (let i = originalPaths.length - 1; i >= 0; i--) {
const currentPath = originalPaths[i]
if (currentPath.replace(/\\/g, '/').startsWith(cwd)) {
sortedPaths.push(currentPath)
} else {
keptPaths.unshift(currentPath)
}
}
// maintain order of node_modules outside of cwd
sortedPaths.push(...keptPaths)
const hookPropertyMap = new Map(
[
'@typescript-eslint/eslint-plugin',
'eslint-plugin-import',
'eslint-plugin-react',
'eslint-plugin-jsx-a11y',
].map((request) => [
request,
require.resolve(request, { paths: sortedPaths }),
])
)
const mod = module as any
const resolveFilename = mod._resolveFilename
mod._resolveFilename = function (
request: string,
parent: any,
isMain?: boolean,
options?: any
) {
const hookResolved = hookPropertyMap.get(request)
if (hookResolved) {
request = hookResolved
}
return resolveFilename.call(mod, request, parent, isMain, options)
}
// TODO: Will remove soon as we convert this to flat config.
// eslint-disable-next-line @next/internal/typechecked-require
require('@rushstack/eslint-patch/modern-module-resolution')
const config: Linter.LegacyConfig = {
extends: [
'plugin:react/recommended',
'plugin:react-hooks/recommended',
'plugin:@next/next/recommended-legacy',
],
plugins: ['import', 'react', 'jsx-a11y'],
rules: {
'import/no-anonymous-default-export': 'warn',
'react/no-unknown-property': 'off',
'react/react-in-jsx-scope': 'off',
'react/prop-types': 'off',
'jsx-a11y/alt-text': [
'warn',
{
elements: ['img'],
img: ['Image'],
},
],
'jsx-a11y/aria-props': 'warn',
'jsx-a11y/aria-proptypes': 'warn',
'jsx-a11y/aria-unsupported-elements': 'warn',
'jsx-a11y/role-has-required-aria-props': 'warn',
'jsx-a11y/role-supports-aria-props': 'warn',
'react/jsx-no-target-blank': 'off',
},
parser: './parser.js',
parserOptions: {
requireConfigFile: false,
sourceType: 'module',
allowImportExportEverywhere: true,
babelOptions: {
presets: ['next/babel'],
caller: {
// Eslint supports top level await when a parser for it is included. We enable the parser by default for Babel.
supportsTopLevelAwait: true,
},
const config: Linter.Config[] = [
{
name: 'next',
// Default files, users can overwrite this.
files: ['**/*.{js,jsx,mjs,ts,tsx,mts,cts}'],
plugins: {
react,
'react-hooks': reactHooks,
import: importPlugin,
'jsx-a11y': jsxA11yPlugin,
'@next/next': next,
},
},
overrides: [
{
files: ['**/*.ts?(x)'],
parser: '@typescript-eslint/parser',
languageOptions: {
parser: eslintParser,
parserOptions: {
requireConfigFile: false,
sourceType: 'module',
allowImportExportEverywhere: true,
// TODO: Is this needed?
babelOptions: {
presets: ['next/babel'],
caller: {
// Eslint supports top level await when a parser for it is included. We enable the parser by default for Babel.
supportsTopLevelAwait: true,
},
},
},
globals: {
...globals.browser,
...globals.node,
},
},
],
settings: {
react: {
version: 'detect',
settings: {
react: {
version: 'detect',
},
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.mts', '.cts', '.tsx', '.d.ts'],
},
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
typescript: {
alwaysTryTypes: true,
},
},
},
'import/parsers': {
[require.resolve('@typescript-eslint/parser')]: [
'.ts',
'.mts',
'.cts',
'.tsx',
'.d.ts',
rules: {
...react.configs.recommended.rules,
...reactHooks.configs.recommended.rules,
...next.configs.recommended.rules,
'import/no-anonymous-default-export': 'warn',
'react/no-unknown-property': 'off',
'react/react-in-jsx-scope': 'off',
'react/prop-types': 'off',
'jsx-a11y/alt-text': [
'warn',
{
elements: ['img'],
img: ['Image'],
},
],
'jsx-a11y/aria-props': 'warn',
'jsx-a11y/aria-proptypes': 'warn',
'jsx-a11y/aria-unsupported-elements': 'warn',
'jsx-a11y/role-has-required-aria-props': 'warn',
'jsx-a11y/role-supports-aria-props': 'warn',
'react/jsx-no-target-blank': 'off',
},
'import/resolver': {
[require.resolve('eslint-import-resolver-node')]: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
[require.resolve('eslint-import-resolver-typescript')]: {
alwaysTryTypes: true,
},
{
name: 'next/typescript',
// Default files, users can overwrite this.
files: ['**/*.ts', '**/*.tsx'],
plugins: {
'@typescript-eslint': tsEslint.plugin,
},
languageOptions: {
parser: tsEslint.parser,
parserOptions: {
sourceType: 'module',
},
},
},
env: {
browser: true,
node: true,
// Global ignores, users can add more `ignores` or overwrite this by `!<ignore>`.
{
ignores: [
// node_modules/ and .git/ are ignored by default.
// https://eslint.org/docs/latest/use/configure/configuration-files#globally-ignoring-files-with-ignores
'.next/**',
'out/**',
'build/**',
'next-env.d.ts',
],
},
}
]
// Use `export =` instead of `export default` for ESLint parser compatibility.
// ESLint expects parser modules to be directly importable as CommonJS modules (module.exports).
......
import type { Linter } from 'eslint'
const {
parse,
parseForESLint,
// No types for compiled modules.
// eslint-disable-next-line @next/internal/typechecked-require
} = require('next/dist/compiled/babel/eslint-parser')
const { version } = require('../package.json')
// @ts-expect-error - No types for compiled modules.
import { parse, parseForESLint } from 'next/dist/compiled/babel/eslint-parser'
import { version } from '../package.json'
const parser: Linter.Parser = {
parse,
......
import type { Linter } from 'eslint'
import tsEslint from 'typescript-eslint'
const config: Linter.LegacyConfig = {
extends: ['plugin:@typescript-eslint/recommended'],
rules: {
'@typescript-eslint/no-unused-vars': 1,
'@typescript-eslint/no-unused-expressions': 1,
const config: Linter.Config[] = [
...tsEslint.configs.recommended,
{
rules: {
'@typescript-eslint/no-unused-vars': 'warn',
'@typescript-eslint/no-unused-expressions': 'warn',
},
},
}
// Global ignores, users can add more `ignores` or overwrite this by `!<ignore>`.
{
ignores: [
// node_modules/ and .git/ are ignored by default.
// https://eslint.org/docs/latest/use/configure/configuration-files#globally-ignoring-files-with-ignores
'.next/**',
'out/**',
'build/**',
'next-env.d.ts',
],
},
]
export = config
This diff is collapsed.
......@@ -43,9 +43,9 @@ describe('eslint-config-next/core-web-vitals', () => {
"@",
"react",
"react-hooks:eslint-plugin-react-hooks@7.0.0",
"@next/next",
"import",
"jsx-a11y:eslint-plugin-jsx-a11y@6.10.2",
"@next/next:@next/eslint-plugin-next",
],
"rules": {
"@next/next/google-font-display": [
......
import path from 'path'
import { fileURLToPath } from 'url'
import { FlatCompat } from '@eslint/eslintrc'
import { defineConfig } from 'eslint/config'
import eslintCoreWebVitals from 'eslint-config-next/core-web-vitals'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const eslintConfig = defineConfig(eslintCoreWebVitals)
const compat = new FlatCompat({
baseDirectory: __dirname,
})
export default compat.extends('next/core-web-vitals')
export default eslintConfig
......@@ -44,9 +44,9 @@ describe('eslint-config-next', () => {
"@",
"react",
"react-hooks:eslint-plugin-react-hooks@7.0.0",
"@next/next",
"import",
"jsx-a11y:eslint-plugin-jsx-a11y@6.10.2",
"@next/next:@next/eslint-plugin-next",
],
"rules": {
"@next/next/google-font-display": [
......
import path from 'path'
import { fileURLToPath } from 'url'
import { FlatCompat } from '@eslint/eslintrc'
import { defineConfig } from 'eslint/config'
import eslintNext from 'eslint-config-next'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const eslintConfig = defineConfig(eslintNext)
const compat = new FlatCompat({
baseDirectory: __dirname,
})
export default compat.extends('next')
export default eslintConfig
import path from 'path'
import { fileURLToPath } from 'url'
import { FlatCompat } from '@eslint/eslintrc'
import { defineConfig } from 'eslint/config'
import eslintTypescript from 'eslint-config-next/typescript'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const eslintConfig = defineConfig(eslintTypescript)
const compat = new FlatCompat({
baseDirectory: __dirname,
})
export default compat.extends('next/typescript')
export default eslintConfig
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment