Skip to content

Commit

Permalink
[Fix] jsx-wrap-multilines: avoid crash when no trailing newline
Browse files Browse the repository at this point in the history
Fixes #2100.
  • Loading branch information
ljharb committed Jan 1, 2019
1 parent 146d8d1 commit 695e534
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rules/jsx-wrap-multilines.js
Expand Up @@ -162,7 +162,7 @@ module.exports = {
node,
MISSING_PARENS,
fixer => fixer.replaceTextRange(
[tokenBefore.range[0], tokenAfter.range[0]],
[tokenBefore.range[0], tokenAfter ? tokenAfter.range[0] : node.range[1]],
`${trimTokenBeforeNewline(node, tokenBefore)}(\n${sourceCode.getText(node)}\n)`
)
);
Expand Down
29 changes: 29 additions & 0 deletions tests/lib/rules/jsx-wrap-multilines.js
Expand Up @@ -26,6 +26,16 @@ const parserOptions = {
const MISSING_PARENS = 'Missing parentheses around multilines JSX';
const PARENS_NEW_LINES = 'Parentheses around JSX should be on separate lines';

const OPTIONS_ALL_NEW_LINES = {
declaration: 'parens-new-line',
assignment: 'parens-new-line',
return: 'parens-new-line',
arrow: 'parens-new-line',
condition: 'parens-new-line',
logical: 'parens-new-line',
prop: 'parens-new-line',
};

const RETURN_SINGLE_LINE = `
var Hello = createReactClass({
render: function() {
Expand Down Expand Up @@ -550,6 +560,19 @@ const ATTR_PAREN_NEW_LINE_AUTOFIX_FRAGMENT = `
</div>
`;

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

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

function addNewLineSymbols(code) {
return code.replace(/\(</g, '(\n<').replace(/>\)/g, '>\n)');
}
Expand Down Expand Up @@ -1157,5 +1180,11 @@ ruleTester.run('jsx-wrap-multilines', rule, {
output: ATTR_PAREN_NEW_LINE_AUTOFIX_FRAGMENT,
options: [{prop: 'parens-new-line'}],
errors: [{message: MISSING_PARENS}]
},
{
code: SFC_NO_PARENS_NO_NEWLINE,
output: SFC_NO_PARENS_AUTOFIX,
options: [OPTIONS_ALL_NEW_LINES],
errors: [{message: MISSING_PARENS}]
}]
});

1 comment on commit 695e534

@mjhost
Copy link

@mjhost mjhost commented on 695e534 Jan 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was having issues with the tests and had no clue that node.range[1] could be used, thank you so much!

Please sign in to comment.