Skip to content

Commit

Permalink
[Fix] jsx-max-depth: avoid crash with childless jsx child
Browse files Browse the repository at this point in the history
Fixes #2869
  • Loading branch information
ljharb committed Dec 7, 2020
1 parent 4073d0e commit 7f41894
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,9 +16,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`jsx-no-target-blank`]: avoid a crash with a non-string literal ([#2851][] @ljharb)
* [`jsx-no-script-url`]: avoid crash with boolean `href` ([#2871][] @ljharb, @AriPerkkio)
* [`no-typos`]: avoid crash with computed method name ([#2870][] @ljharb, @AriPerkkio)
* [`jsx-max-depth`]: avoid crash with childless jsx child ([#2869][] @ljharb, @AriPerkkio)

[#2871]: https://github.com/yannickcr/eslint-plugin-react/issues/2871
[#2870]: https://github.com/yannickcr/eslint-plugin-react/issues/2870
[#2869]: https://github.com/yannickcr/eslint-plugin-react/issues/2869
[#2851]: https://github.com/yannickcr/eslint-plugin-react/issues/2851
[#2843]: https://github.com/yannickcr/eslint-plugin-react/pull/2843
[#2840]: https://github.com/yannickcr/eslint-plugin-react/issues/2840
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-max-depth.js
Expand Up @@ -52,7 +52,7 @@ module.exports = {
function isLeaf(node) {
const children = node.children;

return !children.length || !children.some(hasJSX);
return !children || children.length === 0 || !children.some(hasJSX);
}

function getDepth(node) {
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/jsx-max-depth.js
Expand Up @@ -107,6 +107,13 @@ ruleTester.run('jsx-max-depth', rule, {
'};'
].join('\n'),
options: [{max: 1}]
}, {
code: [
'export function MyComponent() {',
' const A = <>{<div />}</>;',
' return <div>{A}</div>;',
'}'
].join('\n')
}],

invalid: [{
Expand Down

0 comments on commit 7f41894

Please sign in to comment.