From e5b9f3543fd7aa3c50099f50f98e8b185679712f Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 6 Nov 2021 22:50:28 -0700 Subject: [PATCH] [Fix] `no-invalid-html-attribute`: avoid crash on spread props Fixes #3126 --- CHANGELOG.md | 6 ++++-- lib/rules/no-invalid-html-attribute.js | 2 +- tests/lib/rules/no-invalid-html-attribute.js | 8 ++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a13a5eaf80..673f91b623 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,12 +22,14 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel * [`prop-types`], `propTypes`: add forwardRef<>, ForwardRefRenderFunction<> prop-types ([#3112] @vedadeepta) * [`no-typos`]: prevent a crash when using private methods (@ljharb) * [`destructuring-assignment`], component detection: improve component detection ([#3122] @vedadeepta) +* [`no-invalid-html-attribute`]: avoid crash on spread props ([#3126] @ljharb) ### Changed * [Tests] test on the new babel eslint parser ([#3113] @ljharb) -* [Docs] [`jsx-no-target-blank`]: adjust options description ([#3214] @gebsh) +* [Docs] [`jsx-no-target-blank`]: adjust options description ([#3124] @gebsh) -[#3214]: https://github.com/yannickcr/eslint-plugin-react/pull/3214 +[#3126]: https://github.com/yannickcr/eslint-plugin-react/issue/3126 +[#3124]: https://github.com/yannickcr/eslint-plugin-react/pull/3124 [#3122]: https://github.com/yannickcr/eslint-plugin-react/pull/3122 [#3113]: https://github.com/yannickcr/eslint-plugin-react/pull/3113 [#3112]: https://github.com/yannickcr/eslint-plugin-react/pull/3112 diff --git a/lib/rules/no-invalid-html-attribute.js b/lib/rules/no-invalid-html-attribute.js index c49d31209e..06eeb5b53c 100644 --- a/lib/rules/no-invalid-html-attribute.js +++ b/lib/rules/no-invalid-html-attribute.js @@ -416,7 +416,7 @@ function checkCreateProps(context, node, attribute) { } for (const prop of propsArg.properties) { - if (prop.key.type !== 'Identifier') { + if (!prop.key || prop.key.type !== 'Identifier') { // eslint-disable-next-line no-continue continue; // cannot check computed keys } diff --git a/tests/lib/rules/no-invalid-html-attribute.js b/tests/lib/rules/no-invalid-html-attribute.js index ed03b1fe92..6920ba7239 100644 --- a/tests/lib/rules/no-invalid-html-attribute.js +++ b/tests/lib/rules/no-invalid-html-attribute.js @@ -203,6 +203,14 @@ ruleTester.run('no-invalid-html-attribute', rule, { { code: '' }, { code: '' }, { code: 'React.createElement("Foo", { rel: true })' }, + { + code: ` + React.createElement('a', { + ...rest, + href: to, + }) + `, + }, ], invalid: [ {