From a4bf7dad8530222f1feb48683d0987ad29a3f4ba Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 23 Sep 2021 10:56:15 -0700 Subject: [PATCH] [Fix] `no-namespace`: fix crash on non-string React.createElement name Fixes #3082 --- CHANGELOG.md | 5 +++++ lib/rules/no-namespace.js | 4 ++-- tests/lib/rules/no-namespace.js | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2d440c808..05cbc962ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ## Unreleased +### Fixed +* [`no-namespace`]: fix crash on non-string React.createElement name ([#3082] @ljharb) + +[#3082]: https://github.com/yannickcr/eslint-plugin-react/pull/3082 + ## [7.26.0] - 2021.09.20 ### Added diff --git a/lib/rules/no-namespace.js b/lib/rules/no-namespace.js index 18a257592d..9b86427001 100644 --- a/lib/rules/no-namespace.js +++ b/lib/rules/no-namespace.js @@ -40,7 +40,7 @@ module.exports = { CallExpression(node) { if (isCreateElement(node, context) && node.arguments.length > 0 && node.arguments[0].type === 'Literal') { const name = node.arguments[0].value; - if (name.indexOf(':') === -1) return undefined; + if (!name || name.indexOf(':') === -1) return undefined; report(context, messages.noNamespace, 'noNamespace', { node, data: { @@ -51,7 +51,7 @@ module.exports = { }, JSXOpeningElement(node) { const name = elementType(node); - if (name.indexOf(':') === -1) return undefined; + if (!name || name.indexOf(':') === -1) return undefined; report(context, messages.noNamespace, 'noNamespace', { node, data: { diff --git a/tests/lib/rules/no-namespace.js b/tests/lib/rules/no-namespace.js index c3f6b3ef5d..bdad6b7888 100644 --- a/tests/lib/rules/no-namespace.js +++ b/tests/lib/rules/no-namespace.js @@ -74,6 +74,8 @@ ruleTester.run('no-namespace', rule, { code: '' }, { code: 'React.createElement("Object.TestComponent")' + }, { + code: 'React.createElement(null)' }], invalid: [{