Skip to content

Commit dc62a8c

Browse files
ROSSROSALESljharb
authored andcommitted
[Fix] jsx-sort-props: avoid a crash
Fixes #3371.
1 parent 3d58253 commit dc62a8c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/rules/jsx-sort-props.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,11 @@ function getGroupsOfSortableAttributes(attributes, context) {
181181
addtoSortableAttributeGroups(attribute);
182182
i += 1;
183183
} else if (attributeline === commentline) {
184-
if (firstComment.type === 'Block') {
184+
if (firstComment.type === 'Block' && nextAttribute) {
185185
attributeMap.set(attribute, [nextAttribute.range[1], true]);
186186
i += 1;
187+
} else if (firstComment.type === 'Block') {
188+
attributeMap.set(attribute, [firstComment.range[1], true]);
187189
} else {
188190
attributeMap.set(attribute, [firstComment.range[1], false]);
189191
}

tests/lib/rules/jsx-sort-props.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,32 @@ ruleTester.run('jsx-sort-props', rule, {
10471047
line: 2,
10481048
},
10491049
],
1050+
} : [],
1051+
semver.satisfies(eslintPkg.version, '> 3') ? {
1052+
code: `
1053+
<ReactJson src={rowResult} name="data" collapsed={4} collapseStringsAfterLength={60} onEdit={onEdit} /* onDelete={onEdit} */ />
1054+
`,
1055+
output: `
1056+
<ReactJson collapseStringsAfterLength={60} collapsed={4} name="data" src={rowResult} onEdit={onEdit} /* onDelete={onEdit} */ />
1057+
`,
1058+
errors: [
1059+
{
1060+
messageId: 'sortPropsByAlpha',
1061+
line: 2,
1062+
},
1063+
{
1064+
messageId: 'sortPropsByAlpha',
1065+
line: 2,
1066+
},
1067+
{
1068+
messageId: 'sortPropsByAlpha',
1069+
line: 2,
1070+
},
1071+
{
1072+
messageId: 'sortPropsByAlpha',
1073+
line: 2,
1074+
},
1075+
],
10501076
} : []
10511077
)),
10521078
});

0 commit comments

Comments
 (0)