Skip to content

Commit

Permalink
[Fix] jsx-max-depth: avoid a crash
Browse files Browse the repository at this point in the history
Fixes #2102.
  • Loading branch information
ljharb committed Jan 1, 2019
1 parent dfeeb81 commit 146d8d1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/rules/jsx-max-depth.js
Expand Up @@ -89,7 +89,7 @@ module.exports = {

return jsxUtil.isJSX(writeExpr)
&& writeExpr
|| writeExpr.type === 'Identifier'
|| (writeExpr && writeExpr.type === 'Identifier')
&& findJSXElementOrFragment(variables, writeExpr.name);
}
}
Expand All @@ -103,7 +103,7 @@ module.exports = {

function checkDescendant(baseDepth, children) {
baseDepth++;
children.forEach(node => {
(children || []).forEach(node => {
if (!hasJSX(node)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/util/jsx.js
Expand Up @@ -31,7 +31,7 @@ function isDOMComponent(node) {
* @returns {boolean} Whether or not the node if a JSX element or fragment.
*/
function isJSX(node) {
return ['JSXElement', 'JSXFragment'].indexOf(node.type) >= 0;
return node && ['JSXElement', 'JSXFragment'].indexOf(node.type) >= 0;
}

module.exports = {
Expand Down
9 changes: 9 additions & 0 deletions tests/lib/rules/jsx-max-depth.js
Expand Up @@ -94,6 +94,15 @@ ruleTester.run('jsx-max-depth', rule, {
</tbody>
`,
options: [{max: 2}]
}, {
code: [
'const Example = props => {',
' for (let i = 0; i < length; i++) {',
' return <Text key={i} />;',
' }',
'};'
].join('\n'),
options: [{max: 1}]
}],

invalid: [{
Expand Down

0 comments on commit 146d8d1

Please sign in to comment.