From b70cc3388b62f8e2bc9dde52fbdf1f4b6dd72c7d Mon Sep 17 00:00:00 2001 From: Yannick Croissant Date: Sun, 9 Oct 2016 14:34:06 +0200 Subject: [PATCH] Fix jsx-indent in multi-line function calls (fixes #895) --- lib/rules/jsx-indent.js | 10 +++++++--- tests/lib/rules/jsx-indent.js | 36 ++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/lib/rules/jsx-indent.js b/lib/rules/jsx-indent.js index 8590a99b48..0f18fc6ffd 100644 --- a/lib/rules/jsx-indent.js +++ b/lib/rules/jsx-indent.js @@ -191,11 +191,15 @@ module.exports = { return { JSXOpeningElement: function(node) { - if (!node.parent || !node.parent.parent) { + var prevToken = sourceCode.getTokenBefore(node); + if (!prevToken) { return; } - var parentElementIndent = getNodeIndent(node.parent.parent); - var indent = node.parent.parent.loc.start.line === node.loc.start.line ? 0 : indentSize; + if (prevToken.type === 'JSXText') { + prevToken = sourceCode.getNodeByRangeIndex(prevToken.start).parent; + } + var parentElementIndent = getNodeIndent(prevToken); + var indent = prevToken.loc.start.line === node.loc.start.line ? 0 : indentSize; checkNodesIndent(node, parentElementIndent + indent); }, JSXClosingElement: function(node) { diff --git a/tests/lib/rules/jsx-indent.js b/tests/lib/rules/jsx-indent.js index b70f3fe103..31dd39947d 100644 --- a/tests/lib/rules/jsx-indent.js +++ b/tests/lib/rules/jsx-indent.js @@ -99,6 +99,41 @@ ruleTester.run('jsx-indent', rule, { ].join('\n'), options: [2], parserOptions: parserOptions + }, { + code: [ + 'it(\n', + ' (\n', + '
\n', + ' \n', + '
\n', + ' )\n', + ')' + ].join('\n'), + parserOptions: parserOptions, + options: [2] + }, { + code: [ + '(\n', + '
\n', + ' \n', + '
\n', + ')' + ].join('\n'), + parserOptions: parserOptions, + options: [2] + }, { + code: ` + it('passes', () => assertPasses( + foo('bar'), + (
+ + + +
), + 'baz' + ));`, + parserOptions: parserOptions, + options: [2] }], invalid: [{ @@ -289,4 +324,3 @@ ruleTester.run('jsx-indent', rule, { ] }*/] }); -