Unverified Commit 4bee3817 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

[Breaking] Flat config as default in `@next/eslint-plugin-next` (#83763)

> [!NOTE]
> This is not a breaking change for `eslint-config-next` as they are
modified to use the legacy. Therefore, CNA templates or current users
that use `eslint-config-next` won't be affected.

### Why?

This PR uses v16 as an opportunity to make the Flat config the default
for `@next/eslint-plugin-next`. Since ESLint v10 will drop the Legacy
config, it is recommended for the plugins to migrate the default config
to Flat. This follows the recommended [plugin migration guide to flat
config](https://eslint.org/docs/latest/extend/plugin-migration-flat-config)
from ESLint.

Along with the migration, it uses the types from ESLint to let users
have correct type safety when using the plugin.

Fixes https://github.com/vercel/next.js/issues/81695
Fixes https://github.com/vercel/next.js/issues/81083
Fixes https://github.com/vercel/next.js/issues/82967
Closes NEXT-4663
parent 721def6b
module.exports = { module.exports = {
extends: [require.resolve('.'), 'plugin:@next/next/core-web-vitals'], extends: [require.resolve('.'), 'plugin:@next/next/core-web-vitals-legacy'],
} }
...@@ -56,7 +56,7 @@ module.exports = { ...@@ -56,7 +56,7 @@ module.exports = {
extends: [ extends: [
'plugin:react/recommended', 'plugin:react/recommended',
'plugin:react-hooks/recommended', 'plugin:react-hooks/recommended',
'plugin:@next/next/recommended', 'plugin:@next/next/recommended-legacy',
], ],
plugins: ['import', 'react', 'jsx-a11y'], plugins: ['import', 'react', 'jsx-a11y'],
rules: { rules: {
......
...@@ -16,12 +16,13 @@ ...@@ -16,12 +16,13 @@
"fast-glob": "3.3.1" "fast-glob": "3.3.1"
}, },
"devDependencies": { "devDependencies": {
"eslint": "8.56.0" "eslint": "9.12.0",
"@types/eslint": "9.6.1"
}, },
"scripts": { "scripts": {
"dev": "pnpm build", "dev": "pnpm build",
"build": "swc -d dist src && pnpm types", "build": "swc -d dist src && pnpm types",
"types": "tsc src/index.ts --skipLibCheck --declaration --emitDeclarationOnly --declarationDir dist", "types": "tsc src/index.ts --skipLibCheck --declaration --emitDeclarationOnly --esModuleInterop --declarationDir dist",
"prepublishOnly": "cd ../../ && turbo run build" "prepublishOnly": "cd ../../ && turbo run build"
} }
} }
import type { Linter, Rule } from 'eslint'
import googleFontDisplay from './rules/google-font-display'
import googleFontPreconnect from './rules/google-font-preconnect'
import inlineScriptId from './rules/inline-script-id'
import nextScriptForGa from './rules/next-script-for-ga'
import noAssignModuleVariable from './rules/no-assign-module-variable'
import noAsyncClientComponent from './rules/no-async-client-component'
import noBeforeInteractiveScriptOutsideDocument from './rules/no-before-interactive-script-outside-document'
import noCssTags from './rules/no-css-tags'
import noDocumentImportInPage from './rules/no-document-import-in-page'
import noDuplicateHead from './rules/no-duplicate-head'
import noHeadElement from './rules/no-head-element'
import noHeadImportInDocument from './rules/no-head-import-in-document'
import noHtmlLinkForPages from './rules/no-html-link-for-pages'
import noImgElement from './rules/no-img-element'
import noPageCustomFont from './rules/no-page-custom-font'
import noScriptComponentInHead from './rules/no-script-component-in-head'
import noStyledJsxInDocument from './rules/no-styled-jsx-in-document'
import noSyncScripts from './rules/no-sync-scripts'
import noTitleInDocumentHead from './rules/no-title-in-document-head'
import noTypos from './rules/no-typos'
import noUnwantedPolyfillio from './rules/no-unwanted-polyfillio'
const recommendedRules = { const recommendedRules = {
// warnings // warnings
'@next/next/google-font-display': 'warn', '@next/next/google-font-display': 'warn',
...@@ -22,72 +46,61 @@ const recommendedRules = { ...@@ -22,72 +46,61 @@ const recommendedRules = {
'@next/next/no-duplicate-head': 'error', '@next/next/no-duplicate-head': 'error',
'@next/next/no-head-import-in-document': 'error', '@next/next/no-head-import-in-document': 'error',
'@next/next/no-script-component-in-head': 'error', '@next/next/no-script-component-in-head': 'error',
} } satisfies Linter.RulesRecord
const coreWebVitalsRules = { const coreWebVitalsRules = {
'@next/next/no-html-link-for-pages': 'error', '@next/next/no-html-link-for-pages': 'error',
'@next/next/no-sync-scripts': 'error', '@next/next/no-sync-scripts': 'error',
} } satisfies Linter.RulesRecord
const plugin = { const plugin = {
meta: {
name: '@next/eslint-plugin-next',
},
rules: { rules: {
'google-font-display': 'google-font-display': googleFontDisplay,
require('./rules/google-font-display') as typeof import('./rules/google-font-display'), 'google-font-preconnect': googleFontPreconnect,
'google-font-preconnect': 'inline-script-id': inlineScriptId,
require('./rules/google-font-preconnect') as typeof import('./rules/google-font-preconnect'), 'next-script-for-ga': nextScriptForGa,
'inline-script-id': 'no-assign-module-variable': noAssignModuleVariable,
require('./rules/inline-script-id') as typeof import('./rules/inline-script-id'), 'no-async-client-component': noAsyncClientComponent,
'next-script-for-ga':
require('./rules/next-script-for-ga') as typeof import('./rules/next-script-for-ga'),
'no-assign-module-variable':
require('./rules/no-assign-module-variable') as typeof import('./rules/no-assign-module-variable'),
'no-async-client-component':
require('./rules/no-async-client-component') as typeof import('./rules/no-async-client-component'),
'no-before-interactive-script-outside-document': 'no-before-interactive-script-outside-document':
require('./rules/no-before-interactive-script-outside-document') as typeof import('./rules/no-before-interactive-script-outside-document'), noBeforeInteractiveScriptOutsideDocument,
'no-css-tags': 'no-css-tags': noCssTags,
require('./rules/no-css-tags') as typeof import('./rules/no-css-tags'), 'no-document-import-in-page': noDocumentImportInPage,
'no-document-import-in-page': 'no-duplicate-head': noDuplicateHead,
require('./rules/no-document-import-in-page') as typeof import('./rules/no-document-import-in-page'), 'no-head-element': noHeadElement,
'no-duplicate-head': 'no-head-import-in-document': noHeadImportInDocument,
require('./rules/no-duplicate-head') as typeof import('./rules/no-duplicate-head'), 'no-html-link-for-pages': noHtmlLinkForPages,
'no-head-element': 'no-img-element': noImgElement,
require('./rules/no-head-element') as typeof import('./rules/no-head-element'), 'no-page-custom-font': noPageCustomFont,
'no-head-import-in-document': 'no-script-component-in-head': noScriptComponentInHead,
require('./rules/no-head-import-in-document') as typeof import('./rules/no-head-import-in-document'), 'no-styled-jsx-in-document': noStyledJsxInDocument,
'no-html-link-for-pages': 'no-sync-scripts': noSyncScripts,
require('./rules/no-html-link-for-pages') as typeof import('./rules/no-html-link-for-pages'), 'no-title-in-document-head': noTitleInDocumentHead,
'no-img-element': 'no-typos': noTypos,
require('./rules/no-img-element') as typeof import('./rules/no-img-element'), 'no-unwanted-polyfillio': noUnwantedPolyfillio,
'no-page-custom-font': } satisfies Record<string, Rule.RuleModule>,
require('./rules/no-page-custom-font') as typeof import('./rules/no-page-custom-font'), configs: {} as ESLintPluginConfigs,
'no-script-component-in-head': }
require('./rules/no-script-component-in-head') as typeof import('./rules/no-script-component-in-head'),
'no-styled-jsx-in-document': type ESLintPluginConfigs = {
require('./rules/no-styled-jsx-in-document') as typeof import('./rules/no-styled-jsx-in-document'), 'recommended-legacy': Linter.LegacyConfig
'no-sync-scripts': 'core-web-vitals-legacy': Linter.LegacyConfig
require('./rules/no-sync-scripts') as typeof import('./rules/no-sync-scripts'), recommended: Linter.Config
'no-title-in-document-head': 'core-web-vitals': Linter.Config
require('./rules/no-title-in-document-head') as typeof import('./rules/no-title-in-document-head'),
'no-typos':
require('./rules/no-typos') as typeof import('./rules/no-typos'),
'no-unwanted-polyfillio':
require('./rules/no-unwanted-polyfillio') as typeof import('./rules/no-unwanted-polyfillio'),
},
configs: {
recommended: {
plugins: ['@next/next'],
rules: recommendedRules,
},
'core-web-vitals': {
plugins: ['@next/next'],
extends: ['plugin:@next/next/recommended'],
rules: coreWebVitalsRules,
},
},
} }
const flatConfig = { Object.assign(plugin.configs, {
'recommended-legacy': {
plugins: ['@next/next'],
rules: recommendedRules,
},
'core-web-vitals-legacy': {
plugins: ['@next/next'],
extends: ['plugin:@next/next/recommended-legacy'],
rules: coreWebVitalsRules,
},
recommended: { recommended: {
name: 'next/recommended', name: 'next/recommended',
plugins: { plugins: {
...@@ -95,7 +108,7 @@ const flatConfig = { ...@@ -95,7 +108,7 @@ const flatConfig = {
}, },
rules: recommendedRules, rules: recommendedRules,
}, },
coreWebVitals: { 'core-web-vitals': {
name: 'next/core-web-vitals', name: 'next/core-web-vitals',
plugins: { plugins: {
'@next/next': plugin, '@next/next': plugin,
...@@ -105,8 +118,7 @@ const flatConfig = { ...@@ -105,8 +118,7 @@ const flatConfig = {
...coreWebVitalsRules, ...coreWebVitalsRules,
}, },
}, },
} } satisfies ESLintPluginConfigs)
export default plugin export default plugin
export const { rules, configs } = plugin export const { rules, configs } = plugin
export { flatConfig }
...@@ -3,7 +3,7 @@ import NodeAttributes from '../utils/node-attributes' ...@@ -3,7 +3,7 @@ import NodeAttributes from '../utils/node-attributes'
const url = 'https://nextjs.org/docs/messages/google-font-display' const url = 'https://nextjs.org/docs/messages/google-font-display'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: 'Enforce font-display behavior with Google Fonts.', description: 'Enforce font-display behavior with Google Fonts.',
......
...@@ -3,7 +3,7 @@ import NodeAttributes from '../utils/node-attributes' ...@@ -3,7 +3,7 @@ import NodeAttributes from '../utils/node-attributes'
const url = 'https://nextjs.org/docs/messages/google-font-preconnect' const url = 'https://nextjs.org/docs/messages/google-font-preconnect'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: 'Ensure `preconnect` is used with Google Fonts.', description: 'Ensure `preconnect` is used with Google Fonts.',
......
...@@ -2,7 +2,7 @@ import { defineRule } from '../utils/define-rule' ...@@ -2,7 +2,7 @@ import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/inline-script-id' const url = 'https://nextjs.org/docs/messages/inline-script-id'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: description:
......
...@@ -19,7 +19,7 @@ const containsStr = (str, strList) => { ...@@ -19,7 +19,7 @@ const containsStr = (str, strList) => {
return strList.some((s) => str.includes(s)) return strList.some((s) => str.includes(s))
} }
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description, description,
......
import { defineRule } from '../utils/define-rule' import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/no-assign-module-variable' const url = 'https://nextjs.org/docs/messages/no-assign-module-variable'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: 'Prevent assignment to the `module` variable.', description: 'Prevent assignment to the `module` variable.',
......
...@@ -8,7 +8,7 @@ function isCapitalized(str: string): boolean { ...@@ -8,7 +8,7 @@ function isCapitalized(str: string): boolean {
return /[A-Z]/.test(str?.[0]) return /[A-Z]/.test(str?.[0])
} }
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description, description,
......
...@@ -7,7 +7,7 @@ const url = ...@@ -7,7 +7,7 @@ const url =
const convertToCorrectSeparator = (str: string) => const convertToCorrectSeparator = (str: string) =>
str.replace(/[\\/]/g, path.sep) str.replace(/[\\/]/g, path.sep)
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: description:
......
import { defineRule } from '../utils/define-rule' import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/no-css-tags' const url = 'https://nextjs.org/docs/messages/no-css-tags'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: 'Prevent manual stylesheet tags.', description: 'Prevent manual stylesheet tags.',
......
...@@ -3,7 +3,7 @@ import * as path from 'path' ...@@ -3,7 +3,7 @@ import * as path from 'path'
const url = 'https://nextjs.org/docs/messages/no-document-import-in-page' const url = 'https://nextjs.org/docs/messages/no-document-import-in-page'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: description:
......
import { defineRule } from '../utils/define-rule' import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/no-duplicate-head' const url = 'https://nextjs.org/docs/messages/no-duplicate-head'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: description:
......
import path = require('path') import path from 'path'
import { defineRule } from '../utils/define-rule' import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/no-head-element' const url = 'https://nextjs.org/docs/messages/no-head-element'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: 'Prevent usage of `<head>` element.', description: 'Prevent usage of `<head>` element.',
......
...@@ -3,7 +3,7 @@ import * as path from 'path' ...@@ -3,7 +3,7 @@ import * as path from 'path'
const url = 'https://nextjs.org/docs/messages/no-head-import-in-document' const url = 'https://nextjs.org/docs/messages/no-head-import-in-document'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: 'Prevent usage of `next/head` in `pages/_document.js`.', description: 'Prevent usage of `next/head` in `pages/_document.js`.',
......
...@@ -37,7 +37,7 @@ const cachedGetUrlFromAppDirectory = memoize(getUrlFromAppDirectory) ...@@ -37,7 +37,7 @@ const cachedGetUrlFromAppDirectory = memoize(getUrlFromAppDirectory)
const url = 'https://nextjs.org/docs/messages/no-html-link-for-pages' const url = 'https://nextjs.org/docs/messages/no-html-link-for-pages'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: description:
......
import path = require('path') import path from 'path'
import { defineRule } from '../utils/define-rule' import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/no-img-element' const url = 'https://nextjs.org/docs/messages/no-img-element'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: description:
......
...@@ -9,7 +9,7 @@ function isIdentifierMatch(id1, id2) { ...@@ -9,7 +9,7 @@ function isIdentifierMatch(id1, id2) {
return (id1 === null && id2 === null) || (id1 && id2 && id1.name === id2.name) return (id1 === null && id2 === null) || (id1 && id2 && id1.name === id2.name)
} }
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: 'Prevent page-only custom fonts.', description: 'Prevent page-only custom fonts.',
......
import { defineRule } from '../utils/define-rule' import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/no-script-component-in-head' const url = 'https://nextjs.org/docs/messages/no-script-component-in-head'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: 'Prevent usage of `next/script` in `next/head` component.', description: 'Prevent usage of `next/script` in `next/head` component.',
......
...@@ -3,7 +3,7 @@ import * as path from 'path' ...@@ -3,7 +3,7 @@ import * as path from 'path'
const url = 'https://nextjs.org/docs/messages/no-styled-jsx-in-document' const url = 'https://nextjs.org/docs/messages/no-styled-jsx-in-document'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: 'Prevent usage of `styled-jsx` in `pages/_document.js`.', description: 'Prevent usage of `styled-jsx` in `pages/_document.js`.',
......
...@@ -2,7 +2,7 @@ import { defineRule } from '../utils/define-rule' ...@@ -2,7 +2,7 @@ import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/no-sync-scripts' const url = 'https://nextjs.org/docs/messages/no-sync-scripts'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: 'Prevent synchronous scripts.', description: 'Prevent synchronous scripts.',
......
import { defineRule } from '../utils/define-rule' import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/no-title-in-document-head' const url = 'https://nextjs.org/docs/messages/no-title-in-document-head'
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: description:
......
...@@ -41,7 +41,7 @@ function minDistance(a, b) { ...@@ -41,7 +41,7 @@ function minDistance(a, b) {
} }
/* eslint-disable eslint-plugin/require-meta-docs-url */ /* eslint-disable eslint-plugin/require-meta-docs-url */
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: 'Prevent common typos in Next.js data fetching functions.', description: 'Prevent common typos in Next.js data fetching functions.',
......
...@@ -73,7 +73,7 @@ const url = 'https://nextjs.org/docs/messages/no-unwanted-polyfillio' ...@@ -73,7 +73,7 @@ const url = 'https://nextjs.org/docs/messages/no-unwanted-polyfillio'
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Rule Definition // Rule Definition
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
export = defineRule({ export default defineRule({
meta: { meta: {
docs: { docs: {
description: 'Prevent duplicate polyfills from Polyfill.io.', description: 'Prevent duplicate polyfills from Polyfill.io.',
......
{ {
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
"target": "es2019" "target": "es2019",
"esModuleInterop": true
}, },
"include": ["src/**/*"], "include": ["src/**/*"],
"exclude": ["node_modules"] "exclude": ["node_modules"]
......
This diff is collapsed.
{ {
"extends": "plugin:@next/next/core-web-vitals", "extends": "plugin:@next/next/core-web-vitals-legacy",
"root": true, "root": true,
"parserOptions": { "parserOptions": {
"sourceType": "module", "sourceType": "module",
......
{ {
"extends": "plugin:@next/next/recommended", "extends": "plugin:@next/next/recommended-legacy",
"root": true, "root": true,
"parserOptions": { "parserOptions": {
"sourceType": "module", "sourceType": "module",
......
...@@ -14,6 +14,36 @@ describe('@next/eslint-plugin-next index', () => { ...@@ -14,6 +14,36 @@ describe('@next/eslint-plugin-next index', () => {
expect(index.rules).toContainAllKeys(rules) expect(index.rules).toContainAllKeys(rules)
}) })
it('should have meta information', () => {
expect(index.meta).toBeDefined()
expect(index.meta.name).toBe('@next/eslint-plugin-next')
})
it('should have proper flat config structure for recommended', () => {
const config = index.configs.recommended
expect(config.name).toBe('next/recommended')
expect(config.rules).toBeDefined()
})
it('should have proper flat config structure for core-web-vitals', () => {
const config = index.configs['core-web-vitals']
expect(config.name).toBe('next/core-web-vitals')
expect(config.rules).toBeDefined()
})
it('should have legacy recommended config', () => {
const config = index.configs['recommended-legacy']
expect(config.plugins).toContain('@next/next')
expect(config.rules).toBeDefined()
})
it('should have legacy core-web-vitals config', () => {
const config = index.configs['core-web-vitals-legacy']
expect(config.plugins).toContain('@next/next')
expect(config.extends).toContain('plugin:@next/next/recommended-legacy')
expect(config.rules).toBeDefined()
})
rulePaths.forEach((rulePath) => { rulePaths.forEach((rulePath) => {
let rule = require(rulePath) let rule = require(rulePath)
rule = rule.default ?? rule rule = rule.default ?? rule
......
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