Skip to content

Commit

Permalink
Merge pull request #1576 from sharmilajesupaul/shar--remove-newline-b…
Browse files Browse the repository at this point in the history
…efore-paren

Potential autofix improvement for jsx-wrap-mulitlines
  • Loading branch information
ljharb committed Nov 30, 2017
2 parents c072c89 + 6a41c60 commit b801624
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
35 changes: 28 additions & 7 deletions lib/rules/jsx-wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,21 @@ module.exports = {
return node.loc.start.line !== node.loc.end.line;
}

function report(node, message, fixerFn) {
function report(node, message, fix) {
context.report({
node: node,
message: message,
fix: fixerFn
node,
message,
fix
});
}

function trimTokenBeforeNewline(node, tokenBefore) {
// if the token before the jsx is a bracket or curly brace
// we don't want a space between the opening parentheses and the multiline jsx
const isBracket = tokenBefore.value === '{' || tokenBefore.value === '[';
return `${tokenBefore.value.trim()}${isBracket ? '' : ' '}`;
}

function check(node, type) {
if (!node || node.type !== 'JSXElement') {
return;
Expand All @@ -125,7 +132,21 @@ module.exports = {

if (option === 'parens-new-line' && isMultilines(node)) {
if (!isParenthesised(node)) {
report(node, MISSING_PARENS, fixer => fixer.replaceText(node, `(\n${sourceCode.getText(node)}\n)`));
const tokenBefore = sourceCode.getTokenBefore(node, {includeComments: true});
const tokenAfter = sourceCode.getTokenAfter(node, {includeComments: true});
if (tokenBefore.loc.end.line < node.loc.start.line) {
// Strip newline after operator if parens newline is specified
report(
node,
MISSING_PARENS,
fixer => fixer.replaceTextRange(
[tokenBefore.range[0], tokenAfter.range[0]],
`${trimTokenBeforeNewline(node, tokenBefore)}(\n${sourceCode.getText(node)}\n)`
)
);
} 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`));
}
Expand Down Expand Up @@ -171,7 +192,7 @@ module.exports = {
}
},

'ArrowFunctionExpression:exit': function (node) {
'ArrowFunctionExpression:exit': function(node) {
const arrowBody = node.body;
const type = 'arrow';

Expand All @@ -195,7 +216,7 @@ module.exports = {
}
},

JSXAttribute: function (node) {
JSXAttribute: function(node) {
const type = 'prop';
if (isEnabled(type) && node.value && node.value.type === 'JSXExpressionContainer') {
check(node.value.expression, type);
Expand Down
27 changes: 23 additions & 4 deletions tests/lib/rules/jsx-wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ const LOGICAL_NO_PAREN = `
</div>
`;

const LOGICAL_PAREN_NEW_LINE_AUTOFIX = `
<div>
{foo && (
<div>
<p>Hello World</p>
</div>
)}
</div>
`;

const LOGICAL_PAREN_NEW_LINE = `
<div>
{foo && (
Expand Down Expand Up @@ -291,6 +301,16 @@ const ATTR_PAREN_NEW_LINE = `
</div>
`;

const ATTR_PAREN_NEW_LINE_AUTOFIX = `
<div prop={(
<div>
<p>Hello</p>
</div>
)}>
<p>Hello</p>
</div>
`;

function addNewLineSymbols(code) {
return code.replace(/\(</g, '(\n<').replace(/>\)/g, '>\n)');
}
Expand Down Expand Up @@ -640,7 +660,7 @@ ruleTester.run('jsx-wrap-multilines', rule, {
errors: [{message: PARENS_NEW_LINES}]
}, {
code: LOGICAL_NO_PAREN,
output: addNewLineSymbols(LOGICAL_PAREN),
output: LOGICAL_PAREN_NEW_LINE_AUTOFIX,
options: [{logical: 'parens-new-line'}],
errors: [{message: MISSING_PARENS}]
}, {
Expand All @@ -650,9 +670,8 @@ ruleTester.run('jsx-wrap-multilines', rule, {
errors: [{message: PARENS_NEW_LINES}]
}, {
code: ATTR_NO_PAREN,
output: addNewLineSymbols(ATTR_PAREN),
output: ATTR_PAREN_NEW_LINE_AUTOFIX,
options: [{prop: 'parens-new-line'}],
errors: [{message: MISSING_PARENS}]
}
]
}]
});

0 comments on commit b801624

Please sign in to comment.