rsnext/packages/eslint-plugin-next/lib/rules/no-script-in-head.js
Jesse Jafa a28e775e88
[ESLint] Disallow <Script /> inside _document.js & <Script /> inside the next/head component (#27257)
## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. [Feature Request](https://github.com/vercel/next.js/discussions/26365)
- [x] Eslint unit ests added
- [x] Errors have helpful link attached, see `contributing.md`

Let me know if this looks good or something needs to be changed. I still need to add the error links and improve the eslint error messages.

I don't know if the CI runs the ESLint tests, but current all pass locally
2021-08-13 23:16:15 +00:00

51 lines
1.2 KiB
JavaScript

module.exports = {
meta: {
docs: {
description: 'Disallow using next/script inside the next/head component',
recommended: true,
},
},
create: function (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 shouldn't be used inside next/head. See: https://nextjs.org/docs/messages/no-script-in-head-component ",
})
}
},
}
},
}