Skip to content

Commit

Permalink
fix incorrect formatting in jsx-wrap-multilines
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmoyopenroot committed Aug 27, 2019
1 parent 97a9f39 commit 64db1a5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 13 deletions.
6 changes: 3 additions & 3 deletions lib/rules/jsx-wrap-multilines.js
Expand Up @@ -163,8 +163,8 @@ module.exports = {
node,
MISSING_PARENS,
fixer => fixer.replaceTextRange(
[tokenBefore.range[0], tokenAfter ? tokenAfter.range[0] : node.range[1]],
`${trimTokenBeforeNewline(node, tokenBefore)}(\n${sourceCode.getText(node)}\n)`
[tokenBefore.range[0], tokenAfter && (tokenAfter.value === ';' || tokenAfter.value === '}') ? tokenAfter.range[0] : node.range[1]],
`${trimTokenBeforeNewline(node, tokenBefore)}(\n${' '.repeat(node.loc.start.column)}${sourceCode.getText(node)}\n${' '.repeat(node.loc.start.column - 2)})`
)
);
} else {
Expand Down Expand Up @@ -229,7 +229,7 @@ module.exports = {
}
},

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

Expand Down
81 changes: 71 additions & 10 deletions tests/lib/rules/jsx-wrap-multilines.js
Expand Up @@ -464,20 +464,20 @@ const LOGICAL_NO_PAREN_FRAGMENT = `
const LOGICAL_PAREN_NEW_LINE_AUTOFIX = `
<div>
{foo && (
<div>
<div>
<p>Hello World</p>
</div>
)}
)}
</div>
`;

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

Expand Down Expand Up @@ -545,20 +545,20 @@ const ATTR_PAREN_NEW_LINE = `

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

const ATTR_PAREN_NEW_LINE_AUTOFIX_FRAGMENT = `
<div prop={(
<>
<>
<p>Hello</p>
</>
)}>
)}>
<p>Hello</p>
</div>
`;
Expand All @@ -571,10 +571,53 @@ export default () =>

const SFC_NO_PARENS_AUTOFIX = `
export default () => (
<div>
<div>
with newline without parentheses eslint crashes
</div>
)`;
)`;

const ARROW_WITH_EXPORT = `
const Component = () =>
<div>
<p>Some text</p>
</div>
export { Component as default }
`;

const ARROW_WITH_EXPORT_AUTOFIX = `
const Component = () => (
<div>
<p>Some text</p>
</div>
)
export { Component as default }
`;

const ARROW_WITH_LOGICAL = `
const Component = props => (
<div>
{true &&
<div>
<p>Some text</p>
</div>
}
</div>
)
`;

const ARROW_WITH_LOGICAL_AUTOFIX = `
const Component = props => (
<div>
{true && (
<div>
<p>Some text</p>
</div>
)}
</div>
)
`;

function addNewLineSymbols(code) {
return code.replace(/\(</g, '(\n<').replace(/>\)/g, '>\n)');
Expand Down Expand Up @@ -1189,5 +1232,23 @@ ruleTester.run('jsx-wrap-multilines', rule, {
output: SFC_NO_PARENS_AUTOFIX,
options: [OPTIONS_ALL_NEW_LINES],
errors: [{message: MISSING_PARENS}]
}, {
code: ARROW_WITH_EXPORT,
output: ARROW_WITH_EXPORT_AUTOFIX,
options: [{
declaration: 'parens-new-line',
assignment: 'parens-new-line',
return: 'parens-new-line',
arrow: 'parens-new-line',
condition: 'parens-new-line',
logical: 'ignore',
prop: 'ignore'
}],
errors: [{message: MISSING_PARENS}]
}, {
code: ARROW_WITH_LOGICAL,
output: ARROW_WITH_LOGICAL_AUTOFIX,
options: [{logical: 'parens-new-line'}],
errors: [{message: MISSING_PARENS}]
}]
});

0 comments on commit 64db1a5

Please sign in to comment.