silverwolf-x/project / packages /eslint-plugin-next /src /rules /no-script-component-in-head.ts
silverwolf-x's picture
download
raw
1.35 kB
import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/no-script-component-in-head'
export default defineRule({
meta: {
docs: {
description: 'Prevent usage of `next/script` in `next/head` component.',
recommended: true,
url,
},
type: 'problem',
schema: [],
},
create(context) {
let isNextHead = null
return {
ImportDeclaration(node) {
if (node.source.value === 'next/head') {
isNextHead = node.source.value
}
if (node.source.value !== 'next/script') {
return
}
},
JSXElement(node) {
if (!isNextHead) {
return
}
if (
node.openingElement &&
node.openingElement.name &&
node.openingElement.name.name !== 'Head'
) {
return
}
const scriptTag = node.children.find(
(child) =>
child.openingElement &&
child.openingElement.name &&
child.openingElement.name.name === 'Script'
)
if (scriptTag) {
context.report({
node,
message: `\`next/script\` should not be used in \`next/head\` component. Move \`<Script />\` outside of \`<Head>\` instead. See: ${url}`,
})
}
},
}
},
})

Xet Storage Details

Size:
1.35 kB
·
Xet hash:
0d4f56fa5fb693e4bb92de1232c60c278d796a59cfac403ec8cb3a27043d55d8

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.