diff --git a/lib/rules/jsx-max-depth.js b/lib/rules/jsx-max-depth.js index b5b3723733..3c9ab3894f 100644 --- a/lib/rules/jsx-max-depth.js +++ b/lib/rules/jsx-max-depth.js @@ -103,12 +103,12 @@ module.exports = { } function checkDescendant(baseDepth, children) { + baseDepth++; children.forEach(node => { if (!hasJSX(node)) { return; } - baseDepth++; if (baseDepth > maxDepth) { report(node, baseDepth); } else if (!isLeaf(node)) { diff --git a/tests/lib/rules/jsx-max-depth.js b/tests/lib/rules/jsx-max-depth.js index 0b2ceb1a9c..85dd2cd95b 100644 --- a/tests/lib/rules/jsx-max-depth.js +++ b/tests/lib/rules/jsx-max-depth.js @@ -61,6 +61,19 @@ ruleTester.run('jsx-max-depth', rule, { }, { code: 'const foo = (x) =>
{x}
;', options: [{max: 2}] + }, { + code: ` + const x = ( + + 1 + 2 + + ); + + {x} + + `, + options: [{max: 2}] }], invalid: [{ @@ -122,5 +135,22 @@ ruleTester.run('jsx-max-depth', rule, { '' ].join('\n'), errors: [{message: 'Expected the depth of nested jsx elements to be <= 2, but found 3.'}] + }, { + code: ` + const x = ( + + 1 + 2 + + ); + + {x} + + `, + options: [{max: 1}], + errors: [ + {message: 'Expected the depth of nested jsx elements to be <= 1, but found 2.'}, + {message: 'Expected the depth of nested jsx elements to be <= 1, but found 2.'} + ] }] });