From 51f9c6a6d26acfe6e9de86d169fe256723d553de Mon Sep 17 00:00:00 2001 From: Adam Babcock Date: Sun, 16 Sep 2018 15:40:13 -0500 Subject: [PATCH 1/2] jsx-wrap-multilines now catches single missing newlines (#1965) --- lib/rules/jsx-wrap-multilines.js | 22 +++++++++++++--- tests/lib/rules/jsx-wrap-multilines.js | 36 +++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/lib/rules/jsx-wrap-multilines.js b/lib/rules/jsx-wrap-multilines.js index 32d3d08113..4890716210 100644 --- a/lib/rules/jsx-wrap-multilines.js +++ b/lib/rules/jsx-wrap-multilines.js @@ -98,9 +98,19 @@ module.exports = { const previousToken = sourceCode.getTokenBefore(node); const nextToken = sourceCode.getTokenAfter(node); - return isParenthesised(node) && - previousToken.loc.end.line === node.loc.start.line && - node.loc.end.line === nextToken.loc.end.line; + if (!isParenthesised(node)) { + return false; + } + + if (previousToken.loc.end.line === node.loc.start.line) { + return true; + } + + if (node.loc.end.line === nextToken.loc.end.line) { + return true; + } + + return false; } function isMultilines(node) { @@ -151,7 +161,11 @@ module.exports = { report(node, MISSING_PARENS, fixer => fixer.replaceText(node, `(\n${sourceCode.getText(node)}\n)`)); } } else if (needsNewLines(node)) { - report(node, PARENS_NEW_LINES, fixer => fixer.replaceText(node, `\n${sourceCode.getText(node)}\n`)); + report(node, PARENS_NEW_LINES, fixer => { + const text = sourceCode.getText(node); + const fixed = text.replace(/^\n?((.|\n)*?)\n?$/, '\n$1\n'); + return fixer.replaceText(node, fixed); + }); } } } diff --git a/tests/lib/rules/jsx-wrap-multilines.js b/tests/lib/rules/jsx-wrap-multilines.js index 0351df1831..458d3dbdc4 100644 --- a/tests/lib/rules/jsx-wrap-multilines.js +++ b/tests/lib/rules/jsx-wrap-multilines.js @@ -86,6 +86,30 @@ const RETURN_PAREN_NEW_LINE = ` }); `; +const RETURN_PAREN_NEW_LINE_OPENING = ` + var Hello = createReactClass({ + render: function() { + return ( + +
+

Hello {this.props.name}

+
); + } + }); +`; + +const RETURN_PAREN_NEW_LINE_CLOSING = ` + var Hello = createReactClass({ + render: function() { + return (
+

Hello {this.props.name}

+
+ + ); + } + }); +`; + const RETURN_PAREN_NEW_LINE_FRAGMENT = ` var Hello = createReactClass({ render: function() { @@ -501,7 +525,7 @@ const ATTR_PAREN_NEW_LINE_AUTOFIX_FRAGMENT = ` `; function addNewLineSymbols(code) { - return code.replace(/\(\)/g, '>\n)'); + return code.replace(/\((\s*)(\s*)\)/g, '>\n$1)'); } // ------------------------------------------------------------------------------ @@ -912,6 +936,16 @@ ruleTester.run('jsx-wrap-multilines', rule, { output: addNewLineSymbols(RETURN_PAREN), options: [{return: 'parens-new-line'}], errors: [{message: PARENS_NEW_LINES}] + }, { + code: RETURN_PAREN_NEW_LINE_OPENING, + output: addNewLineSymbols(RETURN_PAREN_NEW_LINE_OPENING), + options: [{return: 'parens-new-line'}], + errors: [{message: PARENS_NEW_LINES}] + }, { + code: RETURN_PAREN_NEW_LINE_CLOSING, + output: addNewLineSymbols(RETURN_PAREN_NEW_LINE_CLOSING), + options: [{return: 'parens-new-line'}], + errors: [{message: PARENS_NEW_LINES}] }, { code: RETURN_PAREN_FRAGMENT, parser: 'babel-eslint', From 82e4f411658ecbc9ab0f95be850f1e47327fea3e Mon Sep 17 00:00:00 2001 From: Adam Babcock Date: Wed, 19 Sep 2018 21:38:48 -0500 Subject: [PATCH 2/2] Use explicit fixes for tests --- lib/rules/jsx-wrap-multilines.js | 35 ++++++++++++++++++++------ tests/lib/rules/jsx-wrap-multilines.js | 32 ++++++++++++++++++++--- 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/lib/rules/jsx-wrap-multilines.js b/lib/rules/jsx-wrap-multilines.js index 4890716210..de85d89ac3 100644 --- a/lib/rules/jsx-wrap-multilines.js +++ b/lib/rules/jsx-wrap-multilines.js @@ -94,9 +94,8 @@ module.exports = { nextToken.value === ')' && nextToken.range[0] >= node.range[1]; } - function needsNewLines(node) { + function needsOpeningNewLine(node) { const previousToken = sourceCode.getTokenBefore(node); - const nextToken = sourceCode.getTokenAfter(node); if (!isParenthesised(node)) { return false; @@ -106,6 +105,16 @@ module.exports = { return true; } + return false; + } + + function needsClosingNewLine(node) { + const nextToken = sourceCode.getTokenAfter(node); + + if (!isParenthesised(node)) { + return false; + } + if (node.loc.end.line === nextToken.loc.end.line) { return true; } @@ -160,12 +169,22 @@ module.exports = { } else { report(node, MISSING_PARENS, fixer => fixer.replaceText(node, `(\n${sourceCode.getText(node)}\n)`)); } - } else if (needsNewLines(node)) { - report(node, PARENS_NEW_LINES, fixer => { - const text = sourceCode.getText(node); - const fixed = text.replace(/^\n?((.|\n)*?)\n?$/, '\n$1\n'); - return fixer.replaceText(node, fixed); - }); + } else { + const needsOpening = needsOpeningNewLine(node); + const needsClosing = needsClosingNewLine(node); + if (needsOpening || needsClosing) { + report(node, PARENS_NEW_LINES, fixer => { + const text = sourceCode.getText(node); + let fixed = text; + if (needsOpening) { + fixed = `\n${fixed}`; + } + if (needsClosing) { + fixed = `${fixed}\n`; + } + return fixer.replaceText(node, fixed); + }); + } } } } diff --git a/tests/lib/rules/jsx-wrap-multilines.js b/tests/lib/rules/jsx-wrap-multilines.js index 458d3dbdc4..f0f6e0d9a6 100644 --- a/tests/lib/rules/jsx-wrap-multilines.js +++ b/tests/lib/rules/jsx-wrap-multilines.js @@ -98,6 +98,19 @@ const RETURN_PAREN_NEW_LINE_OPENING = ` }); `; +const RETURN_PAREN_NEW_LINE_OPENING_FIXED = ` + var Hello = createReactClass({ + render: function() { + return ( + +
+

Hello {this.props.name}

+
+); + } + }); +`; + const RETURN_PAREN_NEW_LINE_CLOSING = ` var Hello = createReactClass({ render: function() { @@ -110,6 +123,19 @@ const RETURN_PAREN_NEW_LINE_CLOSING = ` }); `; +const RETURN_PAREN_NEW_LINE_CLOSING_FIXED = ` + var Hello = createReactClass({ + render: function() { + return ( +
+

Hello {this.props.name}

+
+ + ); + } + }); +`; + const RETURN_PAREN_NEW_LINE_FRAGMENT = ` var Hello = createReactClass({ render: function() { @@ -525,7 +551,7 @@ const ATTR_PAREN_NEW_LINE_AUTOFIX_FRAGMENT = ` `; function addNewLineSymbols(code) { - return code.replace(/\((\s*)(\s*)\)/g, '>\n$1)'); + return code.replace(/\(\)/g, '>\n)'); } // ------------------------------------------------------------------------------ @@ -938,12 +964,12 @@ ruleTester.run('jsx-wrap-multilines', rule, { errors: [{message: PARENS_NEW_LINES}] }, { code: RETURN_PAREN_NEW_LINE_OPENING, - output: addNewLineSymbols(RETURN_PAREN_NEW_LINE_OPENING), + output: RETURN_PAREN_NEW_LINE_OPENING_FIXED, options: [{return: 'parens-new-line'}], errors: [{message: PARENS_NEW_LINES}] }, { code: RETURN_PAREN_NEW_LINE_CLOSING, - output: addNewLineSymbols(RETURN_PAREN_NEW_LINE_CLOSING), + output: RETURN_PAREN_NEW_LINE_CLOSING_FIXED, options: [{return: 'parens-new-line'}], errors: [{message: PARENS_NEW_LINES}] }, {