Skip to content

Commit 82e4f41

Browse files
committed
Use explicit fixes for tests
1 parent 51f9c6a commit 82e4f41

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

lib/rules/jsx-wrap-multilines.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ module.exports = {
9494
nextToken.value === ')' && nextToken.range[0] >= node.range[1];
9595
}
9696

97-
function needsNewLines(node) {
97+
function needsOpeningNewLine(node) {
9898
const previousToken = sourceCode.getTokenBefore(node);
99-
const nextToken = sourceCode.getTokenAfter(node);
10099

101100
if (!isParenthesised(node)) {
102101
return false;
@@ -106,6 +105,16 @@ module.exports = {
106105
return true;
107106
}
108107

108+
return false;
109+
}
110+
111+
function needsClosingNewLine(node) {
112+
const nextToken = sourceCode.getTokenAfter(node);
113+
114+
if (!isParenthesised(node)) {
115+
return false;
116+
}
117+
109118
if (node.loc.end.line === nextToken.loc.end.line) {
110119
return true;
111120
}
@@ -160,12 +169,22 @@ module.exports = {
160169
} else {
161170
report(node, MISSING_PARENS, fixer => fixer.replaceText(node, `(\n${sourceCode.getText(node)}\n)`));
162171
}
163-
} else if (needsNewLines(node)) {
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-
});
172+
} else {
173+
const needsOpening = needsOpeningNewLine(node);
174+
const needsClosing = needsClosingNewLine(node);
175+
if (needsOpening || needsClosing) {
176+
report(node, PARENS_NEW_LINES, fixer => {
177+
const text = sourceCode.getText(node);
178+
let fixed = text;
179+
if (needsOpening) {
180+
fixed = `\n${fixed}`;
181+
}
182+
if (needsClosing) {
183+
fixed = `${fixed}\n`;
184+
}
185+
return fixer.replaceText(node, fixed);
186+
});
187+
}
169188
}
170189
}
171190
}

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ const RETURN_PAREN_NEW_LINE_OPENING = `
9898
});
9999
`;
100100

101+
const RETURN_PAREN_NEW_LINE_OPENING_FIXED = `
102+
var Hello = createReactClass({
103+
render: function() {
104+
return (
105+
106+
<div>
107+
<p>Hello {this.props.name}</p>
108+
</div>
109+
);
110+
}
111+
});
112+
`;
113+
101114
const RETURN_PAREN_NEW_LINE_CLOSING = `
102115
var Hello = createReactClass({
103116
render: function() {
@@ -110,6 +123,19 @@ const RETURN_PAREN_NEW_LINE_CLOSING = `
110123
});
111124
`;
112125

126+
const RETURN_PAREN_NEW_LINE_CLOSING_FIXED = `
127+
var Hello = createReactClass({
128+
render: function() {
129+
return (
130+
<div>
131+
<p>Hello {this.props.name}</p>
132+
</div>
133+
134+
);
135+
}
136+
});
137+
`;
138+
113139
const RETURN_PAREN_NEW_LINE_FRAGMENT = `
114140
var Hello = createReactClass({
115141
render: function() {
@@ -525,7 +551,7 @@ const ATTR_PAREN_NEW_LINE_AUTOFIX_FRAGMENT = `
525551
`;
526552

527553
function addNewLineSymbols(code) {
528-
return code.replace(/\((\s*)</g, '($1\n<').replace(/>(\s*)\)/g, '>\n$1)');
554+
return code.replace(/\(</g, '(\n<').replace(/>\)/g, '>\n)');
529555
}
530556

531557
// ------------------------------------------------------------------------------
@@ -938,12 +964,12 @@ ruleTester.run('jsx-wrap-multilines', rule, {
938964
errors: [{message: PARENS_NEW_LINES}]
939965
}, {
940966
code: RETURN_PAREN_NEW_LINE_OPENING,
941-
output: addNewLineSymbols(RETURN_PAREN_NEW_LINE_OPENING),
967+
output: RETURN_PAREN_NEW_LINE_OPENING_FIXED,
942968
options: [{return: 'parens-new-line'}],
943969
errors: [{message: PARENS_NEW_LINES}]
944970
}, {
945971
code: RETURN_PAREN_NEW_LINE_CLOSING,
946-
output: addNewLineSymbols(RETURN_PAREN_NEW_LINE_CLOSING),
972+
output: RETURN_PAREN_NEW_LINE_CLOSING_FIXED,
947973
options: [{return: 'parens-new-line'}],
948974
errors: [{message: PARENS_NEW_LINES}]
949975
}, {

0 commit comments

Comments
 (0)