rsnext/packages/eslint-plugin-next/lib/rules/no-head-import-in-document.js
Houssein Djirdeh 6d0150f02e
ESLint Plugin: Prevent bad imports of next/document and next/head (#24832)
Adds lint rules to the Next.js ESLint plugin to:

- Disallow importing `next/head` inside `pages/_document.js`
- Disallow importing `next/document` outside of `pages/_document.js`

Both rules will be surfaced as **errors** within the recommended config of the plugin.

Fixes #13712 #13958
2021-05-10 21:28:06 +00:00

29 lines
751 B
JavaScript

const path = require('path')
module.exports = {
meta: {
docs: {
description: 'Disallow importing next/head in pages/document.js',
recommended: true,
},
},
create: function (context) {
return {
ImportDeclaration(node) {
if (node.source.value !== 'next/head') {
return
}
const document = context.getFilename().split('pages')[1]
if (!document || path.parse(document).name !== '_document') {
return
}
context.report({
node,
message: `next/head should not be imported in pages${document}. Import Head from next/document instead. See https://nextjs.org/docs/messages/no-head-import-in-document.`,
})
},
}
},
}