Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2390 - fix incorrect formatting in jsx-wrap-multilines #2392

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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}]
}]
});