From bba0015d606ae2a71d428d3be9521ffba4147131 Mon Sep 17 00:00:00 2001 From: Stefan Buck Date: Mon, 16 Dec 2019 20:27:39 +0100 Subject: [PATCH] [Fix] `jsx-indent`: Fix indent handling for closing parentheses Fixes #618. --- lib/rules/jsx-indent.js | 14 +++++++- tests/lib/rules/jsx-indent.js | 66 +++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/lib/rules/jsx-indent.js b/lib/rules/jsx-indent.js index a7c4de1a37..005da5e989 100644 --- a/lib/rules/jsx-indent.js +++ b/lib/rules/jsx-indent.js @@ -395,7 +395,19 @@ module.exports = { checkNodesIndent(node, parentNodeIndent + indentSize); }, Literal: handleLiteral, - JSXText: handleLiteral + JSXText: handleLiteral, + ReturnStatement(node) { + if (!node.parent) { + return; + } + + const openingIndent = getNodeIndent(node); + const closingIndent = getNodeIndent(node, true); + + if (closingIndent !== openingIndent) { + report(node, openingIndent, closingIndent); + } + } }; } }; diff --git a/tests/lib/rules/jsx-indent.js b/tests/lib/rules/jsx-indent.js index 5969c1a3aa..af312f0131 100644 --- a/tests/lib/rules/jsx-indent.js +++ b/tests/lib/rules/jsx-indent.js @@ -997,6 +997,26 @@ const Component = () => ( 'const b = `b\nb`;', '}' ].join('\n') + }, { + code: [ + 'function App() {', + ' return (', + ' ', + ' );', + '}' + ].join('\n'), + options: [2], + parserOptions + }, { + code: [ + 'function App() {', + ' return ', + ' ', + ' ;', + '}' + ].join('\n'), + options: [2], + parserOptions }], invalid: [{ @@ -1102,7 +1122,10 @@ const Component = () => ( '}' ].join('\n'), options: [2], - errors: [{message: 'Expected indentation of 2 space characters but found 9.'}] + errors: [ + {message: 'Expected indentation of 2 space characters but found 9.'}, + {message: 'Expected indentation of 2 space characters but found 9.'} + ] }, { code: [ 'function App() {', @@ -1119,7 +1142,10 @@ const Component = () => ( '}' ].join('\n'), options: [2], - errors: [{message: 'Expected indentation of 2 space characters but found 4.'}] + errors: [ + {message: 'Expected indentation of 2 space characters but found 4.'}, + {message: 'Expected indentation of 2 space characters but found 4.'} + ] }, { code: [ 'function App() {', @@ -2026,5 +2052,41 @@ const Component = () => ( errors: [ {message: 'Expected indentation of 4 space characters but found 0.'} ] + }, { + code: [ + 'function App() {', + ' return (', + ' ', + ' );', + '}' + ].join('\n'), + output: [ + 'function App() {', + ' return (', + ' ', + ' );', + '}' + ].join('\n'), + options: [2], + parserOptions, + errors: [{message: 'Expected indentation of 2 space characters but found 4.'}] + }, { + code: [ + 'function App() {', + ' return (', + ' ', + ');', + '}' + ].join('\n'), + output: [ + 'function App() {', + ' return (', + ' ', + ' );', + '}' + ].join('\n'), + options: [2], + parserOptions, + errors: [{message: 'Expected indentation of 2 space characters but found 0.'}] }] });