|
|
import { defineRule } from '../utils/define-rule' |
|
|
|
|
|
const url = 'https://nextjs.org/docs/messages/inline-script-id' |
|
|
|
|
|
export = defineRule({ |
|
|
meta: { |
|
|
docs: { |
|
|
description: |
|
|
'Enforce `id` attribute on `next/script` components with inline content.', |
|
|
recommended: true, |
|
|
url, |
|
|
}, |
|
|
type: 'problem', |
|
|
schema: [], |
|
|
}, |
|
|
create(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 attributeNames = new Set() |
|
|
|
|
|
let hasNonCheckableSpreadAttribute = false |
|
|
node.openingElement.attributes.forEach((attribute) => { |
|
|
|
|
|
if (hasNonCheckableSpreadAttribute) return |
|
|
|
|
|
if (attribute.type === 'JSXAttribute') { |
|
|
attributeNames.add(attribute.name.name) |
|
|
} else if (attribute.type === 'JSXSpreadAttribute') { |
|
|
if (attribute.argument && attribute.argument.properties) { |
|
|
attribute.argument.properties.forEach((property) => { |
|
|
attributeNames.add(property.key.name) |
|
|
}) |
|
|
} else { |
|
|
|
|
|
hasNonCheckableSpreadAttribute = true |
|
|
} |
|
|
} |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
if (hasNonCheckableSpreadAttribute) return |
|
|
|
|
|
if ( |
|
|
node.children.length > 0 || |
|
|
attributeNames.has('dangerouslySetInnerHTML') |
|
|
) { |
|
|
if (!attributeNames.has('id')) { |
|
|
context.report({ |
|
|
node, |
|
|
message: `\`next/script\` components with inline content must specify an \`id\` attribute. See: ${url}`, |
|
|
}) |
|
|
} |
|
|
} |
|
|
}, |
|
|
} |
|
|
}, |
|
|
}) |
|
|
|