Skip to content

Commit 223aa32

Browse files
committed
[Fix] jsx-no-leaked-render: preserve RHS parens for multiline jsx elements while fixing
1 parent dd6e05c commit 223aa32

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

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 = Array(rightSideTextLastLine.search(/\S/)).fill(' ').join('');
90+
const indentSpacesClose = Array(rightSideTextLastLine.search(/\S/) - 2).fill(' ').join('');
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

+70
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+
},
888+
{
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+
{
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+
}],
887957
}
888958
)),
889959
});

0 commit comments

Comments
 (0)