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