Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure jsx-sort-props fixer respects callbacksLast #1992

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 16 additions & 6 deletions lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ function propNameCompare(a, b, options) {
if (options.reservedFirst) {
const aIsReserved = isReservedPropName(a, options.reservedList);
const bIsReserved = isReservedPropName(b, options.reservedList);
if ((aIsReserved && bIsReserved) || (!aIsReserved && !bIsReserved)) {
return a.localeCompare(b);
} else if (aIsReserved && !bIsReserved) {
if (aIsReserved && !bIsReserved) {
return -1;
} else if (!aIsReserved && bIsReserved) {
return 1;
}
}
if (options.callbacksLast) {
const aIsCallback = isCallbackPropName(a);
const bIsCallback = isCallbackPropName(b);
if (!aIsCallback && bIsCallback) {
return -1;
} else if (aIsCallback && !bIsCallback) {
return 1;
}
return 1;
}
return a.localeCompare(b);
}
Expand Down Expand Up @@ -80,14 +88,15 @@ const generateFixerFunction = (node, context, reservedList) => {
const configuration = context.options[0] || {};
const ignoreCase = configuration.ignoreCase || false;
const reservedFirst = configuration.reservedFirst || false;
const callbacksLast = configuration.reservedFirst || false;

// Sort props according to the context. Only supports ignoreCase.
// Since we cannot safely move JSXSpreadAttribute (due to potential variable overrides),
// we only consider groups of sortable attributes.
const sortableAttributeGroups = getGroupsOfSortableAttributes(attributes);
const sortedAttributeGroups = sortableAttributeGroups.slice(0).map(group =>
group.slice(0).sort((a, b) =>
propNameCompare(propName(a), propName(b), {ignoreCase, reservedFirst, reservedList})
propNameCompare(propName(a), propName(b), {ignoreCase, reservedFirst, reservedList, callbacksLast})
)
);

Expand Down Expand Up @@ -267,7 +276,8 @@ module.exports = {
// Encountered a non-callback prop after a callback prop
context.report({
node: memo,
message: 'Callbacks must be listed after all other props'
message: 'Callbacks must be listed after all other props',
fix: generateFixerFunction(node, context, reservedList)
});
return memo;
}
Expand Down