Skip to content

Commit 8785c16

Browse files
committed
[Fix] no-namespace: avoid crash on non-string createElement values
Fixes #3085
1 parent a8ecd54 commit 8785c16

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
77

88
### Fixed
99
* [`no-namespace`]: fix crash on non-string React.createElement name ([#3082] @ljharb)
10+
* [`no-namespace`]: avoid crash on non-string createElement values ([#3085] @ljharb)
1011

1112
### Changed
1213
* [Docs] [`jsx-max-props-per-line`]: fix options example ([#3083] @MrRaiter)
1314

15+
[#3085]: https://github.com/yannickcr/eslint-plugin-react/issue/3085
1416
[#3083]: https://github.com/yannickcr/eslint-plugin-react/pull/3083
1517
[#3082]: https://github.com/yannickcr/eslint-plugin-react/pull/3082
1618

lib/rules/no-namespace.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module.exports = {
4040
CallExpression(node) {
4141
if (isCreateElement(node, context) && node.arguments.length > 0 && node.arguments[0].type === 'Literal') {
4242
const name = node.arguments[0].value;
43-
if (!name || name.indexOf(':') === -1) return undefined;
43+
if (typeof name !== 'string' || name.indexOf(':') === -1) return undefined;
4444
report(context, messages.noNamespace, 'noNamespace', {
4545
node,
4646
data: {
@@ -51,7 +51,7 @@ module.exports = {
5151
},
5252
JSXOpeningElement(node) {
5353
const name = elementType(node);
54-
if (!name || name.indexOf(':') === -1) return undefined;
54+
if (typeof name !== 'string' || name.indexOf(':') === -1) return undefined;
5555
report(context, messages.noNamespace, 'noNamespace', {
5656
node,
5757
data: {

tests/lib/rules/no-namespace.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ ruleTester.run('no-namespace', rule, {
7676
code: 'React.createElement("Object.TestComponent")'
7777
}, {
7878
code: 'React.createElement(null)'
79+
}, {
80+
code: 'React.createElement(true)'
81+
}, {
82+
code: 'React.createElement({})'
7983
}],
8084

8185
invalid: [{

0 commit comments

Comments
 (0)