Unverified Commit e9a8ee50 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Update codemod ESLint template to Flat config (#84919)

Stacked on https://github.com/vercel/next.js/pull/84874

### Why?

As `eslint-config-next` supports Flat config since PR
https://github.com/vercel/next.js/pull/84874, Release
[v16.0.0-canary.6](https://github.com/vercel/next.js/releases/tag/v16.0.0-canary.6),
update codemod to use Flat config. Since `eslint-config-next` version
and this codemod's version will match, no need to gate the version, as
by the time this change is released, the same `eslint-config-next`
version will support Flat config.

### How?

Updated to handle the three cases:

- Legacy config
- Flat config with FlatCompat (Which we used to recommend for v15)
- Flat config with no FlatCompat (Which means the app does not use
`eslint-config-next`)

For the Legacy config, ESLint provides a [Migration
tool](https://eslint.org/docs/latest/use/configure/migration-guide), so
use that to convert to Flat config. It will include FlatCompat, so the
rest of the process is the same as Flat config with FlatCompat.

For the Flat config with FlatCompat, the codemod will remove FlatCompat
and map the `eslint-config-next` imports directly to the export.

For the Flat config with no FlatCompat, the codemod will import
`eslint-config-next` and map the imports directly to the export.

Tested on
[shadcn/ui](https://github.com/shadcn-ui/ui/blob/cb769b7059b50b5dca033a6eb57bb8ecde69fc87/.eslintrc.json)
and other large legacy ESLint configs.
parent c525e139
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"react": "^18.3.0",
"react-dom": "^18.3.0",
"next": "15.0.0"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19"
}
}
\ No newline at end of file
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "eslint ."
},
"dependencies": {
"react": "^18.3.0",
"react-dom": "^18.3.0",
"next": "15.0.0"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"eslint": "^9",
"eslint-config-next": "15.0.0",
"@eslint/eslintrc": "^3"
}
}
\ No newline at end of file
module.exports = [
{
rules: {
"quotes": ["error", "double"],
"indent": ["error", 2]
}
}
]
\ No newline at end of file
{
"name": "complex-scripts-app",
"version": "1.0.0",
"scripts": {
"lint": "eslint --fix src pages",
"lint:strict": "eslint --max-warnings 0 .",
"lint:ci": "eslint --quiet --output-file lint-results.json .",
"precommit": "eslint --fix . && npm test",
"test": "jest && eslint .",
"complex": "npm run build && eslint . 2>/dev/null",
"pipe": "eslint . | tee lint.log",
"redirect": "eslint . > output.txt 2>&1",
"multi": "eslint .; next build; eslint --fix ."
},
"dependencies": {
"next": "15.0.0"
},
"devDependencies": {
"eslint": "^9",
"eslint-config-next": "15.0.0",
"@eslint/eslintrc": "^3"
}
}
\ No newline at end of file
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"react": "^18.3.0",
"react-dom": "^18.3.0",
"next": "15.0.0"
},
"devDependencies": {
"eslint": "^8.0.0",
"eslint-config-next": "15.0.0"
}
}
\ No newline at end of file
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "eslint ."
},
"dependencies": {
"react": "^18.3.0",
"react-dom": "^18.3.0",
"next": "15.0.0"
},
"devDependencies": {
"eslint": "^8.0.0",
"eslint-config-next": "15.0.0",
"@eslint/eslintrc": "^3"
}
}
\ No newline at end of file
export default [
{
rules: {
"no-console": "warn",
"semi": ["error", "always"]
}
},
{
files: ["**/*.test.js"],
rules: {
"no-console": "off"
}
}
]
\ No newline at end of file
{
"name": "app-with-flat-config",
"version": "1.0.0",
"scripts": {
"lint": "next lint",
"lint:fix": "next lint --fix"
},
"dependencies": {
"next": "15.0.0"
}
}
\ No newline at end of file
{
"name": "app-with-flat-config",
"version": "1.0.0",
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint --fix ."
},
"dependencies": {
"next": "15.0.0"
},
"devDependencies": {
"eslint": "^9",
"eslint-config-next": "15.0.0",
"@eslint/eslintrc": "^3"
}
}
\ No newline at end of file
export default [
{
ignores: ["dist/**", "coverage/**"]
},
{
rules: {
"no-unused-vars": "warn"
}
}
]
\ No newline at end of file
import { dirname } from 'path'
import { fileURLToPath } from 'url'
import { FlatCompat } from '@eslint/eslintrc'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const compat = new FlatCompat({
baseDirectory: __dirname,
})
const eslintConfig = [
...compat.extends('next/core-web-vitals', 'next/typescript'),
{
ignores: [
'node_modules/**',
'.next/**',
'out/**',
'build/**',
'next-env.d.ts',
],
},
]
export default eslintConfig
{ {
"name": "complex-scripts-app",
"version": "1.0.0",
"scripts": { "scripts": {
"lint": "next lint --fix --dir src --dir pages", "lint": "next lint --fix --dir src --dir pages",
"lint:strict": "next lint --strict", "lint:strict": "next lint --strict",
...@@ -13,6 +11,16 @@ ...@@ -13,6 +11,16 @@
"multi": "next lint; next build; next lint --fix" "multi": "next lint; next build; next lint --fix"
}, },
"dependencies": { "dependencies": {
"next": "15.0.0" "react": "^19",
"react-dom": "^19",
"next": "^16"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"eslint": "^9",
"eslint-config-next": "^16"
} }
} }
\ No newline at end of file
import { defineConfig } from 'eslint/config'
import foo from 'foo'
import bar from 'bar'
const eslintConfig = defineConfig([
foo,
bar,
{
ignores: [
'node_modules/**',
'.next/**',
'out/**',
'build/**',
'next-env.d.ts',
],
},
])
export default eslintConfig
{
"scripts": {
"lint": "next lint --fix --dir src --dir pages",
"lint:strict": "next lint --strict",
"lint:ci": "next lint --quiet --output-file lint-results.json",
"precommit": "next lint --fix && npm test",
"test": "jest && next lint",
"complex": "npm run build && next lint --dir . --ext .js,.jsx,.ts,.tsx 2>/dev/null",
"pipe": "next lint | tee lint.log",
"redirect": "next lint > output.txt 2>&1",
"multi": "next lint; next build; next lint --fix"
},
"dependencies": {
"react": "^19",
"react-dom": "^19",
"next": "^16"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"eslint": "^9",
"eslint-config-next": "^16"
}
}
node_modules/**
.next/**
out/**
build/**
next-env.d.ts
**/*.md
\ No newline at end of file
{
"$schema": "https://json.schemastore.org/eslintrc",
"root": true,
"extends": [
"next/core-web-vitals",
"turbo",
"prettier",
"plugin:tailwindcss/recommended"
],
"plugins": ["tailwindcss"],
"ignorePatterns": ["**/fixtures/**"],
"rules": {
"@next/next/no-html-link-for-pages": "off",
"tailwindcss/no-custom-classname": "off",
"tailwindcss/classnames-order": "error"
},
"settings": {
"tailwindcss": {
"callees": ["cn", "cva"],
"config": "tailwind.config.cjs"
},
"next": {
"rootDir": ["apps/*/"]
}
},
"overrides": [
{
"files": ["*.ts", "*.tsx"],
"parser": "@typescript-eslint/parser"
}
]
}
\ No newline at end of file
{
"scripts": {
"lint": "next lint --fix --dir src --dir pages",
"lint:strict": "next lint --strict",
"lint:ci": "next lint --quiet --output-file lint-results.json",
"precommit": "next lint --fix && npm test",
"test": "jest && next lint",
"complex": "npm run build && next lint --dir . --ext .js,.jsx,.ts,.tsx 2>/dev/null",
"pipe": "next lint | tee lint.log",
"redirect": "next lint > output.txt 2>&1",
"multi": "next lint; next build; next lint --fix"
},
"dependencies": {
"react": "^19",
"react-dom": "^19",
"next": "^16"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"eslint": "^9",
"eslint-config-next": "^16"
}
}
{
"name": "app-without-scripts",
"version": "1.0.0",
"dependencies": {
"next": "15.0.0"
}
}
\ No newline at end of file
{
"name": "app-without-scripts",
"version": "1.0.0",
"scripts": {},
"dependencies": {
"next": "15.0.0"
},
"devDependencies": {
"eslint": "^9",
"eslint-config-next": "15.0.0",
"@eslint/eslintrc": "^3"
}
}
\ No newline at end of file
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