Skip to content

Commit 3008e85

Browse files
authored
Merge pull request #1984 from MrHen/master
jsx-wrap-multilines now catches single missing newlines
2 parents 2e60d0e + 82e4f41 commit 3008e85

File tree

2 files changed

+99
-6
lines changed

2 files changed

+99
-6
lines changed

lib/rules/jsx-wrap-multilines.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,32 @@ 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+
100+
if (!isParenthesised(node)) {
101+
return false;
102+
}
103+
104+
if (previousToken.loc.end.line === node.loc.start.line) {
105+
return true;
106+
}
107+
108+
return false;
109+
}
110+
111+
function needsClosingNewLine(node) {
99112
const nextToken = sourceCode.getTokenAfter(node);
100113

101-
return isParenthesised(node) &&
102-
previousToken.loc.end.line === node.loc.start.line &&
103-
node.loc.end.line === nextToken.loc.end.line;
114+
if (!isParenthesised(node)) {
115+
return false;
116+
}
117+
118+
if (node.loc.end.line === nextToken.loc.end.line) {
119+
return true;
120+
}
121+
122+
return false;
104123
}
105124

106125
function isMultilines(node) {
@@ -150,8 +169,22 @@ module.exports = {
150169
} else {
151170
report(node, MISSING_PARENS, fixer => fixer.replaceText(node, `(\n${sourceCode.getText(node)}\n)`));
152171
}
153-
} else if (needsNewLines(node)) {
154-
report(node, PARENS_NEW_LINES, fixer => fixer.replaceText(node, `\n${sourceCode.getText(node)}\n`));
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+
}
155188
}
156189
}
157190
}

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,56 @@ 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_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+
114+
const RETURN_PAREN_NEW_LINE_CLOSING = `
115+
var Hello = createReactClass({
116+
render: function() {
117+
return (<div>
118+
<p>Hello {this.props.name}</p>
119+
</div>
120+
121+
);
122+
}
123+
});
124+
`;
125+
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+
89139
const RETURN_PAREN_NEW_LINE_FRAGMENT = `
90140
var Hello = createReactClass({
91141
render: function() {
@@ -912,6 +962,16 @@ ruleTester.run('jsx-wrap-multilines', rule, {
912962
output: addNewLineSymbols(RETURN_PAREN),
913963
options: [{return: 'parens-new-line'}],
914964
errors: [{message: PARENS_NEW_LINES}]
965+
}, {
966+
code: RETURN_PAREN_NEW_LINE_OPENING,
967+
output: RETURN_PAREN_NEW_LINE_OPENING_FIXED,
968+
options: [{return: 'parens-new-line'}],
969+
errors: [{message: PARENS_NEW_LINES}]
970+
}, {
971+
code: RETURN_PAREN_NEW_LINE_CLOSING,
972+
output: RETURN_PAREN_NEW_LINE_CLOSING_FIXED,
973+
options: [{return: 'parens-new-line'}],
974+
errors: [{message: PARENS_NEW_LINES}]
915975
}, {
916976
code: RETURN_PAREN_FRAGMENT,
917977
parser: 'babel-eslint',

0 commit comments

Comments
 (0)