mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
e1b8c00d0f
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.6 KiB
TypeScript
125 lines
2.6 KiB
TypeScript
import { RuleTester } from 'eslint'
|
|
import { rules } from '@next/eslint-plugin-next'
|
|
|
|
const NextESLintRule = rules['no-document-import-in-page']
|
|
|
|
const tests = {
|
|
valid: [
|
|
{
|
|
code: `import Document from "next/document"
|
|
|
|
export default class MyDocument extends Document {
|
|
render() {
|
|
return (
|
|
<Html>
|
|
</Html>
|
|
);
|
|
}
|
|
}
|
|
`,
|
|
filename: 'pages/_document.js',
|
|
},
|
|
{
|
|
code: `import Document from "next/document"
|
|
|
|
export default class MyDocument extends Document {
|
|
render() {
|
|
return (
|
|
<Html>
|
|
</Html>
|
|
);
|
|
}
|
|
}
|
|
`,
|
|
filename: 'pages/_document.page.tsx',
|
|
},
|
|
{
|
|
code: `import NDocument from "next/document"
|
|
|
|
export default class Document extends NDocument {
|
|
render() {
|
|
return (
|
|
<Html>
|
|
</Html>
|
|
);
|
|
}
|
|
}
|
|
`,
|
|
filename: 'pages/_document/index.js',
|
|
},
|
|
{
|
|
code: `import NDocument from "next/document"
|
|
|
|
export default class Document extends NDocument {
|
|
render() {
|
|
return (
|
|
<Html>
|
|
</Html>
|
|
);
|
|
}
|
|
}
|
|
`,
|
|
filename: 'pages/_document/index.tsx',
|
|
},
|
|
{
|
|
code: `import Document from "next/document"
|
|
|
|
export default class MyDocument extends Document {
|
|
render() {
|
|
return (
|
|
<Html>
|
|
</Html>
|
|
);
|
|
}
|
|
}
|
|
`,
|
|
filename: 'pagesapp/src/pages/_document.js',
|
|
},
|
|
],
|
|
invalid: [
|
|
{
|
|
code: `import Document from "next/document"
|
|
|
|
export const Test = () => <p>Test</p>
|
|
`,
|
|
filename: 'components/test.js',
|
|
errors: [
|
|
{
|
|
message:
|
|
'`<Document />` from `next/document` should not be imported outside of `pages/_document.js`. See: https://nextjs.org/docs/messages/no-document-import-in-page',
|
|
type: 'ImportDeclaration',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: `import Document from "next/document"
|
|
|
|
export const Test = () => <p>Test</p>
|
|
`,
|
|
filename: 'pages/test.js',
|
|
errors: [
|
|
{
|
|
message:
|
|
'`<Document />` from `next/document` should not be imported outside of `pages/_document.js`. See: https://nextjs.org/docs/messages/no-document-import-in-page',
|
|
type: 'ImportDeclaration',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
}
|
|
|
|
describe('no-document-import-in-page', () => {
|
|
new RuleTester({
|
|
languageOptions: {
|
|
ecmaVersion: 2018,
|
|
sourceType: 'module',
|
|
parserOptions: {
|
|
ecmaFeatures: {
|
|
modules: true,
|
|
jsx: true,
|
|
},
|
|
},
|
|
},
|
|
}).run('eslint', NextESLintRule, tests)
|
|
})
|