Skip to content

Commit

Permalink
[Fix] jsx-sort-props: avoid a crash
Browse files Browse the repository at this point in the history
Fixes #3371.
  • Loading branch information
ROSSROSALES authored and ljharb committed Aug 30, 2022
1 parent 3d58253 commit dc62a8c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ function getGroupsOfSortableAttributes(attributes, context) {
addtoSortableAttributeGroups(attribute);
i += 1;
} else if (attributeline === commentline) {
if (firstComment.type === 'Block') {
if (firstComment.type === 'Block' && nextAttribute) {
attributeMap.set(attribute, [nextAttribute.range[1], true]);
i += 1;
} else if (firstComment.type === 'Block') {
attributeMap.set(attribute, [firstComment.range[1], true]);
} else {
attributeMap.set(attribute, [firstComment.range[1], false]);
}
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,32 @@ ruleTester.run('jsx-sort-props', rule, {
line: 2,
},
],
} : [],
semver.satisfies(eslintPkg.version, '> 3') ? {
code: `
<ReactJson src={rowResult} name="data" collapsed={4} collapseStringsAfterLength={60} onEdit={onEdit} /* onDelete={onEdit} */ />
`,
output: `
<ReactJson collapseStringsAfterLength={60} collapsed={4} name="data" src={rowResult} onEdit={onEdit} /* onDelete={onEdit} */ />
`,
errors: [
{
messageId: 'sortPropsByAlpha',
line: 2,
},
{
messageId: 'sortPropsByAlpha',
line: 2,
},
{
messageId: 'sortPropsByAlpha',
line: 2,
},
{
messageId: 'sortPropsByAlpha',
line: 2,
},
],
} : []
)),
});

0 comments on commit dc62a8c

Please sign in to comment.