Skip to content

Commit

Permalink
fix: handle jsxspreadattribute in inline-script-id eslint rule (verce…
Browse files Browse the repository at this point in the history
…l#32421)

fixes vercel#32178

the `inline-script-id` eslint rule crashed when encountering a `JSXSpreadAttribute`. this pr fixes that, and also handles `id` being passed via the spreaded object.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] ~~Integration~~ Unit tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
  • Loading branch information
stefanprobst authored and natew committed Feb 16, 2022
1 parent 9cc5027 commit 267eb5c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
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

0 comments on commit 267eb5c

Please sign in to comment.