Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function getGroupsOfSortableAttributes(attributes, context) {
} else if (firstComment.type === 'Block') {
attributeMap.set(attribute, { end: firstComment.range[1], hasComment: true });
} else {
attributeMap.set(attribute, { end: firstComment.range[1], hasComment: false });
attributeMap.set(attribute, { end: firstComment.range[1], hasComment: false, hasTrailingLineComment: true });
}
addtoSortableAttributeGroups(attribute);
}
Expand Down Expand Up @@ -274,6 +274,16 @@ function generateFixerFunction(node, context, reservedList) {
const sortedAttributeGroups = sortableAttributeGroups
.slice(0)
.map((group) => toSorted(group, (a, b) => contextCompare(a, b, options)));
const hasUnsafeTrailingLineCommentFix = sortableAttributeGroups.some(
(sortableGroup, groupIndex) => sortableGroup.some((attribute, attributeIndex) => {
const sortedAttribute = sortedAttributeGroups[groupIndex][attributeIndex];
const attr = attributeMap.get(sortedAttribute);
return attr && attr.hasTrailingLineComment && attribute.loc.end.line === node.loc.end.line;
})
);
if (hasUnsafeTrailingLineCommentFix) {
return null;
}

return function fixFunction(fixer) {
const fixers = [];
Expand Down Expand Up @@ -364,10 +374,15 @@ function reportNodeAttribute(nodeAttribute, errorType, node, context, reservedLi

reportedNodeAttributes.set(nodeAttribute, errors);

report(context, messages[errorType], errorType, {
const fix = generateFixerFunction(node, context, reservedList);
const reportConfig = {
node: nodeAttribute.name,
fix: generateFixerFunction(node, context, reservedList),
});
};
if (fix) {
reportConfig.fix = fix;
}

report(context, messages[errorType], errorType, reportConfig);
}

/** @type {import('eslint').Rule.RuleModule} */
Expand Down
40 changes: 40 additions & 0 deletions tests/lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,46 @@ ruleTester.run('jsx-sort-props', rule, {
},
],
} : [],
semver.satisfies(eslintPkg.version, '> 3') ? {
code: `
<div
onClick={() => console.log()} // Comment
className="flex"
>
<span>Problematic Component</span>
</div>
`,
output: `
<div
className="flex"
onClick={() => console.log()} // Comment
>
<span>Problematic Component</span>
</div>
`,
errors: [
{
messageId: 'sortPropsByAlpha',
line: 4,
},
],
} : [],
semver.satisfies(eslintPkg.version, '> 3') ? {
code: `
<div
onClick={() => console.log()} // Comment
id="root">
<span>Problematic Component</span>
</div>
`,
output: null,
errors: [
{
messageId: 'sortPropsByAlpha',
line: 4,
},
],
} : [],
{
code: `
<Page
Expand Down
Loading