Skip to content

Commit 9c1e652

Browse files
committed
[Fix] jsx-wrap-multilines: fix crash with declarations that are on a new line after =
Fixes #2875.
1 parent d873bdb commit 9c1e652

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
99
* [`jsx-key`]: added `checkKeyMustBeforeSpread` option for new jsx transform ([#2835][] @morlay)
1010
* [`jsx-newline`]: add new rule ([#2693][] @jzabala)
1111
* [`jsx-no-constructed-context-values`]: add new rule which checks when the value passed to a Context Provider will cause needless rerenders ([#2763][] @dylanOshima)
12+
* [`jsx-wrap-multilines`]: fix crash with `declaration`s that are on a new line after `=` ([#2875][] @ljharb)
1213

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

22+
[#2871]: https://github.com/yannickcr/eslint-plugin-react/issues/2875
2123
[#2871]: https://github.com/yannickcr/eslint-plugin-react/issues/2871
2224
[#2870]: https://github.com/yannickcr/eslint-plugin-react/issues/2870
2325
[#2869]: https://github.com/yannickcr/eslint-plugin-react/issues/2869

lib/rules/jsx-wrap-multilines.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,15 @@ module.exports = {
157157
if (!isParenthesised(node)) {
158158
const tokenBefore = sourceCode.getTokenBefore(node, {includeComments: true});
159159
const tokenAfter = sourceCode.getTokenAfter(node, {includeComments: true});
160-
if (tokenBefore.loc.end.line < node.loc.start.line) {
160+
const start = node.loc.start;
161+
if (tokenBefore.loc.end.line < start.line) {
161162
// Strip newline after operator if parens newline is specified
162163
report(
163164
node,
164165
MISSING_PARENS,
165166
(fixer) => fixer.replaceTextRange(
166167
[tokenBefore.range[0], tokenAfter && (tokenAfter.value === ';' || tokenAfter.value === '}') ? tokenAfter.range[0] : node.range[1]],
167-
`${trimTokenBeforeNewline(node, tokenBefore)}(\n${' '.repeat(node.loc.start.column)}${sourceCode.getText(node)}\n${' '.repeat(node.loc.start.column - 2)})`
168+
`${trimTokenBeforeNewline(node, tokenBefore)}(\n${start.column > 0 ? ' '.repeat(start.column) : ''}${sourceCode.getText(node)}\n${start.column > 0 ? ' '.repeat(start.column - 2) : ''})`
168169
)
169170
);
170171
} else {

tests/lib/rules/jsx-wrap-multilines.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,5 +1250,25 @@ ruleTester.run('jsx-wrap-multilines', rule, {
12501250
output: ARROW_WITH_LOGICAL_AUTOFIX,
12511251
options: [{logical: 'parens-new-line'}],
12521252
errors: [{message: MISSING_PARENS}]
1253+
}, {
1254+
code: [
1255+
'import React from \'react\';',
1256+
'',
1257+
'const A =',
1258+
'<div>',
1259+
' B',
1260+
'</div>;'
1261+
].join('\n'),
1262+
output: [
1263+
'import React from \'react\';',
1264+
'',
1265+
'const A = (',
1266+
'<div>',
1267+
' B',
1268+
'</div>',
1269+
');'
1270+
].join('\n'),
1271+
options: [{declaration: 'parens-new-line'}],
1272+
errors: [{message: MISSING_PARENS}]
12531273
}]
12541274
});

0 commit comments

Comments
 (0)