diff --git a/lib/rules/jsx-sort-props.js b/lib/rules/jsx-sort-props.js index a98d20ec19..7d2fc07668 100644 --- a/lib/rules/jsx-sort-props.js +++ b/lib/rules/jsx-sort-props.js @@ -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); } @@ -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 = []; @@ -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} */ diff --git a/tests/lib/rules/jsx-sort-props.js b/tests/lib/rules/jsx-sort-props.js index 38d598a10f..affddb3152 100644 --- a/tests/lib/rules/jsx-sort-props.js +++ b/tests/lib/rules/jsx-sort-props.js @@ -1127,6 +1127,46 @@ ruleTester.run('jsx-sort-props', rule, { }, ], } : [], + semver.satisfies(eslintPkg.version, '> 3') ? { + code: ` +