diff --git a/lib/rules/jsx-max-depth.js b/lib/rules/jsx-max-depth.js index 149d4c7368..7c64f93b64 100644 --- a/lib/rules/jsx-max-depth.js +++ b/lib/rules/jsx-max-depth.js @@ -89,7 +89,7 @@ module.exports = { return jsxUtil.isJSX(writeExpr) && writeExpr - || writeExpr.type === 'Identifier' + || (writeExpr && writeExpr.type === 'Identifier') && findJSXElementOrFragment(variables, writeExpr.name); } } @@ -103,7 +103,7 @@ module.exports = { function checkDescendant(baseDepth, children) { baseDepth++; - children.forEach(node => { + (children || []).forEach(node => { if (!hasJSX(node)) { return; } diff --git a/lib/util/jsx.js b/lib/util/jsx.js index a1484ef8bb..69849df733 100644 --- a/lib/util/jsx.js +++ b/lib/util/jsx.js @@ -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 = { diff --git a/tests/lib/rules/jsx-max-depth.js b/tests/lib/rules/jsx-max-depth.js index 62b557e3da..1072f5dd21 100644 --- a/tests/lib/rules/jsx-max-depth.js +++ b/tests/lib/rules/jsx-max-depth.js @@ -94,6 +94,15 @@ ruleTester.run('jsx-max-depth', rule, { `, options: [{max: 2}] + }, { + code: [ + 'const Example = props => {', + ' for (let i = 0; i < length; i++) {', + ' return ;', + ' }', + '};' + ].join('\n'), + options: [{max: 1}] }], invalid: [{