rsnext/packages/eslint-plugin-next/lib/rules/inline-script-id.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

module.exports = {
meta: {
docs: {
description:
'next/script components with inline content must specify an `id` attribute.',
recommended: true,
2021-10-16 08:16:41 +02:00
url: 'https://nextjs.org/docs/messages/inline-script-id',
},
},
create: function (context) {
let nextScriptImportName = null
return {
ImportDeclaration(node) {
if (node.source.value === 'next/script') {
nextScriptImportName = node.specifiers[0].local.name
}
},
JSXElement(node) {
if (nextScriptImportName == null) return
if (
node.openingElement &&
node.openingElement.name &&
node.openingElement.name.name !== nextScriptImportName
) {
return
}
const attributes = node.openingElement.attributes
if (
node.children.length > 0 ||
attributes.some(
(attribute) => attribute.name.name === 'dangerouslySetInnerHTML'
)
) {
if (!attributes.some((attribute) => attribute.name.name === 'id')) {
context.report({
node,
message:
'next/script components with inline content must specify an `id` attribute. See: https://nextjs.org/docs/messages/inline-script-id',
})
}
}
},
}
},
}