Skip to content

Commit 422ff33

Browse files
akulsr0ljharb
authored andcommitted
[Fix] jsx-no-leaked-render: preserve RHS parens for multiline jsx elements while fixing
1 parent dd6e05c commit 422ff33

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
88
### Added
99
* [`sort-prop-types`]: give errors on TS types ([#3615][] @akulsr0)
1010

11+
### Fixed
12+
* [`jsx-no-leaked-render`]: preserve RHS parens for multiline jsx elements while fixing ([#3623][] @akulsr0)
13+
14+
[#3623]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3623
1115
[#3615]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3615
1216

1317
## [7.33.2] - 2023.08.15

lib/rules/jsx-no-leaked-render.js

+9
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ function ruleFixer(context, fixStrategy, fixer, reportedNode, leftNode, rightNod
8282
if (rightNode.type === 'ConditionalExpression') {
8383
return fixer.replaceText(reportedNode, `${newText} && (${rightSideText})`);
8484
}
85+
if (rightNode.type === 'JSXElement') {
86+
const rightSideTextLines = rightSideText.split('\n');
87+
if (rightSideTextLines.length > 1) {
88+
const rightSideTextLastLine = rightSideTextLines[rightSideTextLines.length - 1];
89+
const indentSpacesStart = ' '.repeat(rightSideTextLastLine.search(/\S/));
90+
const indentSpacesClose = ' '.repeat(rightSideTextLastLine.search(/\S/) - 2);
91+
return fixer.replaceText(reportedNode, `${newText} && (\n${indentSpacesStart}${rightSideText}\n${indentSpacesClose})`);
92+
}
93+
}
8594
if (rightNode.type === 'Literal') {
8695
return null;
8796
}

tests/lib/rules/jsx-no-leaked-render.js

+71-1
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,76 @@ ruleTester.run('jsx-no-leaked-render', rule, {
884884
line: 3,
885885
column: 38,
886886
}],
887-
}
887+
},
888+
semver.satisfies(eslintPkg.version, '> 4') ? {
889+
code: `
890+
const MyComponent = () => {
891+
return (
892+
<>
893+
{someCondition && (
894+
<div>
895+
<p>hello</p>
896+
</div>
897+
)}
898+
</>
899+
)
900+
}
901+
`,
902+
output: `
903+
const MyComponent = () => {
904+
return (
905+
<>
906+
{!!someCondition && (
907+
<div>
908+
<p>hello</p>
909+
</div>
910+
)}
911+
</>
912+
)
913+
}
914+
`,
915+
options: [{ validStrategies: ['coerce', 'ternary'] }],
916+
errors: [{
917+
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
918+
line: 5,
919+
column: 16,
920+
}],
921+
} : [],
922+
semver.satisfies(eslintPkg.version, '> 4') ? {
923+
code: `
924+
const MyComponent = () => {
925+
return (
926+
<>
927+
{someCondition && (
928+
<SomeComponent
929+
prop1={val1}
930+
prop2={val2}
931+
/>
932+
)}
933+
</>
934+
)
935+
}
936+
`,
937+
output: `
938+
const MyComponent = () => {
939+
return (
940+
<>
941+
{!!someCondition && (
942+
<SomeComponent
943+
prop1={val1}
944+
prop2={val2}
945+
/>
946+
)}
947+
</>
948+
)
949+
}
950+
`,
951+
options: [{ validStrategies: ['coerce', 'ternary'] }],
952+
errors: [{
953+
message: 'Potential leaked value that might cause unintentionally rendered values or rendering crashes',
954+
line: 5,
955+
column: 16,
956+
}],
957+
} : []
888958
)),
889959
});

0 commit comments

Comments
 (0)