Skip to content

Commit

Permalink
Use explicit fixes for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHen committed Sep 20, 2018
1 parent 51f9c6a commit 82e4f41
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
35 changes: 27 additions & 8 deletions lib/rules/jsx-wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
});
}
}
}
}
Expand Down
32 changes: 29 additions & 3 deletions tests/lib/rules/jsx-wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ const RETURN_PAREN_NEW_LINE_OPENING = `
});
`;

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() {
Expand All @@ -110,6 +123,19 @@ const RETURN_PAREN_NEW_LINE_CLOSING = `
});
`;

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 @@ -525,7 +551,7 @@ const ATTR_PAREN_NEW_LINE_AUTOFIX_FRAGMENT = `
`;

function addNewLineSymbols(code) {
return code.replace(/\((\s*)</g, '($1\n<').replace(/>(\s*)\)/g, '>\n$1)');
return code.replace(/\(</g, '(\n<').replace(/>\)/g, '>\n)');
}

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -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}]
}, {
Expand Down

0 comments on commit 82e4f41

Please sign in to comment.