mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
ESLint plugin defaults to flat config, and we also dropped the built-in lint check at https://github.com/vercel/next.js/pull/83136, so we can sunset the deprecated ESLint v8 testing.
125 lines
2.4 KiB
TypeScript
125 lines
2.4 KiB
TypeScript
import { RuleTester } from 'eslint'
|
|
import { rules } from '@next/eslint-plugin-next'
|
|
|
|
const NextESLintRule = rules['no-head-element']
|
|
|
|
const message =
|
|
'Do not use `<head>` element. Use `<Head />` from `next/head` instead. See: https://nextjs.org/docs/messages/no-head-element'
|
|
|
|
const tests = {
|
|
valid: [
|
|
{
|
|
code: `import Head from 'next/head';
|
|
|
|
export class MyComponent {
|
|
render() {
|
|
return (
|
|
<div>
|
|
<Head>
|
|
<title>My page title</title>
|
|
</Head>
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
`,
|
|
filename: 'pages/index.js',
|
|
},
|
|
{
|
|
code: `import Head from 'next/head';
|
|
|
|
export class MyComponent {
|
|
render() {
|
|
return (
|
|
<div>
|
|
<Head>
|
|
<title>My page title</title>
|
|
</Head>
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
`,
|
|
filename: 'pages/index.tsx',
|
|
},
|
|
{
|
|
code: `
|
|
export default function Layout({ children }) {
|
|
return (
|
|
<html>
|
|
<head>
|
|
<title>layout</title>
|
|
</head>
|
|
<body>{children}</body>
|
|
</html>
|
|
);
|
|
}
|
|
`,
|
|
filename: './app/layout.js',
|
|
},
|
|
],
|
|
invalid: [
|
|
{
|
|
code: `
|
|
export class MyComponent {
|
|
render() {
|
|
return (
|
|
<div>
|
|
<head>
|
|
<title>My page title</title>
|
|
</head>
|
|
</div>
|
|
);
|
|
}
|
|
}`,
|
|
filename: './pages/index.js',
|
|
errors: [
|
|
{
|
|
message,
|
|
type: 'JSXOpeningElement',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: `import Head from 'next/head';
|
|
|
|
export class MyComponent {
|
|
render() {
|
|
return (
|
|
<div>
|
|
<head>
|
|
<title>My page title</title>
|
|
</head>
|
|
<Head>
|
|
<title>My page title</title>
|
|
</Head>
|
|
</div>
|
|
);
|
|
}
|
|
}`,
|
|
filename: 'pages/index.ts',
|
|
errors: [
|
|
{
|
|
message,
|
|
type: 'JSXOpeningElement',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
}
|
|
|
|
describe('no-head-element', () => {
|
|
new RuleTester({
|
|
languageOptions: {
|
|
ecmaVersion: 2018,
|
|
sourceType: 'module',
|
|
parserOptions: {
|
|
ecmaFeatures: {
|
|
modules: true,
|
|
jsx: true,
|
|
},
|
|
},
|
|
},
|
|
}).run('eslint', NextESLintRule, tests)
|
|
})
|