2021-05-10 23:28:06 +02:00
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 ]
2021-08-05 02:58:06 +02:00
if ( ! document ) {
2021-05-10 23:28:06 +02:00
return
}
2021-08-05 02:58:06 +02:00
const { name , dir } = path . parse ( document )
if (
name . startsWith ( '_document' ) ||
( dir === '/_document' && name === 'index' )
) {
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. ` ,
} )
}
2021-05-10 23:28:06 +02:00
} ,
}
} ,
}