Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle jsxspreadattribute in inline-script-id eslint rule #32421

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions packages/eslint-plugin-next/lib/rules/inline-script-id.js
Expand Up @@ -27,15 +27,22 @@ module.exports = {
return
}

const attributes = node.openingElement.attributes
const attributeNames = new Set()
node.openingElement.attributes.forEach((attribute) => {
if (attribute.type === 'JSXAttribute') {
attributeNames.add(attribute.name.name)
} else if (attribute.type === 'JSXSpreadAttribute') {
attribute.argument.properties.forEach((property) => {
attributeNames.add(property.key.name)
})
}
})

if (
node.children.length > 0 ||
attributes.some(
(attribute) => attribute.name.name === 'dangerouslySetInnerHTML'
)
attributeNames.has('dangerouslySetInnerHTML')
) {
if (!attributes.some((attribute) => attribute.name.name === 'id')) {
if (!attributeNames.has('id')) {
context.report({
node,
message:
Expand Down
22 changes: 22 additions & 0 deletions test/unit/eslint-plugin-next/inline-script-id.test.ts
Expand Up @@ -76,6 +76,28 @@ ruleTester.run('inline-script-id', rule, {
)
}`,
},
{
code: `import Script from 'next/script';

export default function TestPage() {
return (
<Script {...{ strategy: "lazyOnload" }} id={"test-script"}>
{\`console.log('Hello world');\`}
</Script>
)
}`,
},
{
code: `import Script from 'next/script';

export default function TestPage() {
return (
<Script {...{ strategy: "lazyOnload", id: "test-script" }}>
{\`console.log('Hello world');\`}
</Script>
)
}`,
},
],
invalid: [
{
Expand Down