Skip to content

Commit

Permalink
Merge pull request #1984 from MrHen/master
Browse files Browse the repository at this point in the history
jsx-wrap-multilines now catches single missing newlines
  • Loading branch information
ljharb committed Oct 31, 2018
2 parents 2e60d0e + 82e4f41 commit 3008e85
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 6 deletions.
45 changes: 39 additions & 6 deletions lib/rules/jsx-wrap-multilines.js
Expand Up @@ -94,13 +94,32 @@ module.exports = {
nextToken.value === ')' && nextToken.range[0] >= node.range[1];
}

function needsNewLines(node) {
function needsOpeningNewLine(node) {
const previousToken = sourceCode.getTokenBefore(node);

if (!isParenthesised(node)) {
return false;
}

if (previousToken.loc.end.line === node.loc.start.line) {
return true;
}

return false;
}

function needsClosingNewLine(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 (node.loc.end.line === nextToken.loc.end.line) {
return true;
}

return false;
}

function isMultilines(node) {
Expand Down Expand Up @@ -150,8 +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 => fixer.replaceText(node, `\n${sourceCode.getText(node)}\n`));
} 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);
});
}
}
}
}
Expand Down
60 changes: 60 additions & 0 deletions tests/lib/rules/jsx-wrap-multilines.js
Expand Up @@ -86,6 +86,56 @@ const RETURN_PAREN_NEW_LINE = `
});
`;

const RETURN_PAREN_NEW_LINE_OPENING = `
var Hello = createReactClass({
render: function() {
return (
<div>
<p>Hello {this.props.name}</p>
</div>);
}
});
`;

const RETURN_PAREN_NEW_LINE_OPENING_FIXED = `
var Hello = createReactClass({
render: function() {
return (
<div>
<p>Hello {this.props.name}</p>
</div>
);
}
});
`;

const RETURN_PAREN_NEW_LINE_CLOSING = `
var Hello = createReactClass({
render: function() {
return (<div>
<p>Hello {this.props.name}</p>
</div>
);
}
});
`;

const RETURN_PAREN_NEW_LINE_CLOSING_FIXED = `
var Hello = createReactClass({
render: function() {
return (
<div>
<p>Hello {this.props.name}</p>
</div>
);
}
});
`;

const RETURN_PAREN_NEW_LINE_FRAGMENT = `
var Hello = createReactClass({
render: function() {
Expand Down Expand Up @@ -912,6 +962,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: RETURN_PAREN_NEW_LINE_OPENING_FIXED,
options: [{return: 'parens-new-line'}],
errors: [{message: PARENS_NEW_LINES}]
}, {
code: RETURN_PAREN_NEW_LINE_CLOSING,
output: RETURN_PAREN_NEW_LINE_CLOSING_FIXED,
options: [{return: 'parens-new-line'}],
errors: [{message: PARENS_NEW_LINES}]
}, {
code: RETURN_PAREN_FRAGMENT,
parser: 'babel-eslint',
Expand Down

0 comments on commit 3008e85

Please sign in to comment.