Skip to content

Commit 51f9c6a

Browse files
committed
jsx-wrap-multilines now catches single missing newlines (#1965)
1 parent 60b5642 commit 51f9c6a

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

lib/rules/jsx-wrap-multilines.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,19 @@ module.exports = {
9898
const previousToken = sourceCode.getTokenBefore(node);
9999
const nextToken = sourceCode.getTokenAfter(node);
100100

101-
return isParenthesised(node) &&
102-
previousToken.loc.end.line === node.loc.start.line &&
103-
node.loc.end.line === nextToken.loc.end.line;
101+
if (!isParenthesised(node)) {
102+
return false;
103+
}
104+
105+
if (previousToken.loc.end.line === node.loc.start.line) {
106+
return true;
107+
}
108+
109+
if (node.loc.end.line === nextToken.loc.end.line) {
110+
return true;
111+
}
112+
113+
return false;
104114
}
105115

106116
function isMultilines(node) {
@@ -151,7 +161,11 @@ module.exports = {
151161
report(node, MISSING_PARENS, fixer => fixer.replaceText(node, `(\n${sourceCode.getText(node)}\n)`));
152162
}
153163
} else if (needsNewLines(node)) {
154-
report(node, PARENS_NEW_LINES, fixer => fixer.replaceText(node, `\n${sourceCode.getText(node)}\n`));
164+
report(node, PARENS_NEW_LINES, fixer => {
165+
const text = sourceCode.getText(node);
166+
const fixed = text.replace(/^\n?((.|\n)*?)\n?$/, '\n$1\n');
167+
return fixer.replaceText(node, fixed);
168+
});
155169
}
156170
}
157171
}

tests/lib/rules/jsx-wrap-multilines.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ const RETURN_PAREN_NEW_LINE = `
8686
});
8787
`;
8888

89+
const RETURN_PAREN_NEW_LINE_OPENING = `
90+
var Hello = createReactClass({
91+
render: function() {
92+
return (
93+
94+
<div>
95+
<p>Hello {this.props.name}</p>
96+
</div>);
97+
}
98+
});
99+
`;
100+
101+
const RETURN_PAREN_NEW_LINE_CLOSING = `
102+
var Hello = createReactClass({
103+
render: function() {
104+
return (<div>
105+
<p>Hello {this.props.name}</p>
106+
</div>
107+
108+
);
109+
}
110+
});
111+
`;
112+
89113
const RETURN_PAREN_NEW_LINE_FRAGMENT = `
90114
var Hello = createReactClass({
91115
render: function() {
@@ -501,7 +525,7 @@ const ATTR_PAREN_NEW_LINE_AUTOFIX_FRAGMENT = `
501525
`;
502526

503527
function addNewLineSymbols(code) {
504-
return code.replace(/\(</g, '(\n<').replace(/>\)/g, '>\n)');
528+
return code.replace(/\((\s*)</g, '($1\n<').replace(/>(\s*)\)/g, '>\n$1)');
505529
}
506530

507531
// ------------------------------------------------------------------------------
@@ -912,6 +936,16 @@ ruleTester.run('jsx-wrap-multilines', rule, {
912936
output: addNewLineSymbols(RETURN_PAREN),
913937
options: [{return: 'parens-new-line'}],
914938
errors: [{message: PARENS_NEW_LINES}]
939+
}, {
940+
code: RETURN_PAREN_NEW_LINE_OPENING,
941+
output: addNewLineSymbols(RETURN_PAREN_NEW_LINE_OPENING),
942+
options: [{return: 'parens-new-line'}],
943+
errors: [{message: PARENS_NEW_LINES}]
944+
}, {
945+
code: RETURN_PAREN_NEW_LINE_CLOSING,
946+
output: addNewLineSymbols(RETURN_PAREN_NEW_LINE_CLOSING),
947+
options: [{return: 'parens-new-line'}],
948+
errors: [{message: PARENS_NEW_LINES}]
915949
}, {
916950
code: RETURN_PAREN_FRAGMENT,
917951
parser: 'babel-eslint',

0 commit comments

Comments
 (0)