Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ESLint Plugin] Handles edge case for no-import-document-in-page rule #28261

Merged
merged 5 commits into from Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,3 @@
const path = require('path')

module.exports = {
meta: {
docs: {
Expand All @@ -15,15 +13,10 @@ module.exports = {
return
}

const page = context.getFilename().split('pages')[1]
if (!page) {
return
}
const { name, dir } = path.parse(page)
if (
name.startsWith('_document') ||
(dir === '/_document' && name === 'index')
) {
const paths = context.getFilename().split('pages')
const page = paths[paths.length - 1]

if (!page || page.startsWith('/_document')) {
return
}

Expand Down
29 changes: 24 additions & 5 deletions test/unit/eslint-plugin-next/no-document-import-in-page.test.ts
Expand Up @@ -84,21 +84,40 @@ ruleTester.run('no-document-import-in-page', rule, {
`,
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:
'next/document should not be imported outside of pages/_document.js. See https://nextjs.org/docs/messages/no-document-import-in-page.',
type: 'ImportDeclaration',
},
],
},
],
invalid: [
{
code: `import Document from "next/document"

export const Test = () => (
<p>Test</p>
)
export const Test = () => <p>Test</p>
`,
filename: 'pages/test.js',
errors: [
Expand Down