Skip to content

Commit

Permalink
[Fix] no-namespace: avoid crash on non-string createElement values
Browse files Browse the repository at this point in the history
Fixes #3085
  • Loading branch information
ljharb committed Sep 26, 2021
1 parent a8ecd54 commit 8785c16
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

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

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

[#3085]: https://github.com/yannickcr/eslint-plugin-react/issue/3085
[#3083]: https://github.com/yannickcr/eslint-plugin-react/pull/3083
[#3082]: https://github.com/yannickcr/eslint-plugin-react/pull/3082

Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || name.indexOf(':') === -1) return undefined;
if (typeof name !== 'string' || name.indexOf(':') === -1) return undefined;
report(context, messages.noNamespace, 'noNamespace', {
node,
data: {
Expand All @@ -51,7 +51,7 @@ module.exports = {
},
JSXOpeningElement(node) {
const name = elementType(node);
if (!name || name.indexOf(':') === -1) return undefined;
if (typeof name !== 'string' || name.indexOf(':') === -1) return undefined;
report(context, messages.noNamespace, 'noNamespace', {
node,
data: {
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/rules/no-namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ ruleTester.run('no-namespace', rule, {
code: 'React.createElement("Object.TestComponent")'
}, {
code: 'React.createElement(null)'
}, {
code: 'React.createElement(true)'
}, {
code: 'React.createElement({})'
}],

invalid: [{
Expand Down

0 comments on commit 8785c16

Please sign in to comment.