Skip to content

Commit

Permalink
[Fix] jsx-wrap-multilines: fix crash with declarations that are o…
Browse files Browse the repository at this point in the history
…n a new line after `=`

Fixes #2875.
  • Loading branch information
ljharb committed Dec 16, 2020
1 parent d873bdb commit 9c1e652
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`jsx-key`]: added `checkKeyMustBeforeSpread` option for new jsx transform ([#2835][] @morlay)
* [`jsx-newline`]: add new rule ([#2693][] @jzabala)
* [`jsx-no-constructed-context-values`]: add new rule which checks when the value passed to a Context Provider will cause needless rerenders ([#2763][] @dylanOshima)
* [`jsx-wrap-multilines`]: fix crash with `declaration`s that are on a new line after `=` ([#2875][] @ljharb)

### Fixed
* [`display-name`]/component detection: avoid a crash on anonymous components ([#2840][] @ljharb)
Expand All @@ -18,6 +19,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`no-typos`]: avoid crash with computed method name ([#2870][] @ljharb, @AriPerkkio)
* [`jsx-max-depth`]: avoid crash with childless jsx child ([#2869][] @ljharb, @AriPerkkio)

[#2871]: https://github.com/yannickcr/eslint-plugin-react/issues/2875
[#2871]: https://github.com/yannickcr/eslint-plugin-react/issues/2871
[#2870]: https://github.com/yannickcr/eslint-plugin-react/issues/2870
[#2869]: https://github.com/yannickcr/eslint-plugin-react/issues/2869
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/jsx-wrap-multilines.js
Expand Up @@ -157,14 +157,15 @@ module.exports = {
if (!isParenthesised(node)) {
const tokenBefore = sourceCode.getTokenBefore(node, {includeComments: true});
const tokenAfter = sourceCode.getTokenAfter(node, {includeComments: true});
if (tokenBefore.loc.end.line < node.loc.start.line) {
const start = node.loc.start;
if (tokenBefore.loc.end.line < start.line) {
// Strip newline after operator if parens newline is specified
report(
node,
MISSING_PARENS,
(fixer) => fixer.replaceTextRange(
[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)})`
`${trimTokenBeforeNewline(node, tokenBefore)}(\n${start.column > 0 ? ' '.repeat(start.column) : ''}${sourceCode.getText(node)}\n${start.column > 0 ? ' '.repeat(start.column - 2) : ''})`
)
);
} else {
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/jsx-wrap-multilines.js
Expand Up @@ -1250,5 +1250,25 @@ ruleTester.run('jsx-wrap-multilines', rule, {
output: ARROW_WITH_LOGICAL_AUTOFIX,
options: [{logical: 'parens-new-line'}],
errors: [{message: MISSING_PARENS}]
}, {
code: [
'import React from \'react\';',
'',
'const A =',
'<div>',
' B',
'</div>;'
].join('\n'),
output: [
'import React from \'react\';',
'',
'const A = (',
'<div>',
' B',
'</div>',
');'
].join('\n'),
options: [{declaration: 'parens-new-line'}],
errors: [{message: MISSING_PARENS}]
}]
});

0 comments on commit 9c1e652

Please sign in to comment.