From 49afcd4871c1778b0380281cdf2cc8b060308382 Mon Sep 17 00:00:00 2001 From: stefanprobst Date: Thu, 3 Feb 2022 00:04:22 +0100 Subject: [PATCH] fix: handle jsxspreadattribute in inline-script-id eslint rule (#32421) fixes #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` --- .../lib/rules/inline-script-id.js | 17 +++++++++----- .../inline-script-id.test.ts | 22 +++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin-next/lib/rules/inline-script-id.js b/packages/eslint-plugin-next/lib/rules/inline-script-id.js index 099b12de8edaede..20322f913c22d47 100644 --- a/packages/eslint-plugin-next/lib/rules/inline-script-id.js +++ b/packages/eslint-plugin-next/lib/rules/inline-script-id.js @@ -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: diff --git a/test/unit/eslint-plugin-next/inline-script-id.test.ts b/test/unit/eslint-plugin-next/inline-script-id.test.ts index eb59af7744304c6..d584af36402ad75 100644 --- a/test/unit/eslint-plugin-next/inline-script-id.test.ts +++ b/test/unit/eslint-plugin-next/inline-script-id.test.ts @@ -76,6 +76,28 @@ ruleTester.run('inline-script-id', rule, { ) }`, }, + { + code: `import Script from 'next/script'; + + export default function TestPage() { + return ( + + ) + }`, + }, + { + code: `import Script from 'next/script'; + + export default function TestPage() { + return ( + + ) + }`, + }, ], invalid: [ {