Skip to content
Merged
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
28 changes: 19 additions & 9 deletions lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,21 @@ function isReservedPropName(name, list) {
return list.indexOf(name) >= 0;
}

function alphabeticalCompare(a, b, ignoreCase) {
if (ignoreCase) {
function propNameCompare(a, b, options) {
if (options.ignoreCase) {
a = a.toLowerCase();
b = b.toLowerCase();
}
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) {
return -1;
}
return 1;
}
return a.localeCompare(b);
}

Expand Down Expand Up @@ -77,19 +87,20 @@ function getGroupsOfSortableAttributes(attributes) {
return sortableAttributeGroups;
}

const generateFixerFunction = (node, context) => {
const generateFixerFunction = (node, context, reservedList) => {
const sourceCode = context.getSourceCode();
const attributes = node.attributes.slice(0);
const configuration = context.options[0] || {};
const ignoreCase = configuration.ignoreCase || false;
const reservedFirst = 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) =>
alphabeticalCompare(propName(a), propName(b), ignoreCase)
propNameCompare(propName(a), propName(b), {ignoreCase, reservedFirst, reservedList})
)
);

Expand Down Expand Up @@ -235,23 +246,22 @@ module.exports = {
const previousIsReserved = isReservedPropName(previousPropName, reservedList);
const currentIsReserved = isReservedPropName(currentPropName, reservedList);

if (previousIsReserved && currentIsReserved) {
if ((previousIsReserved && currentIsReserved) || (!previousIsReserved && !currentIsReserved)) {
if (!noSortAlphabetically && currentPropName < previousPropName) {
context.report({
node: decl,
message: 'Props should be sorted alphabetically',
fix: generateFixerFunction(node, context)
fix: generateFixerFunction(node, context, reservedList)
});
return memo;
}
return decl;
}
if (!previousIsReserved && currentIsReserved) {
context.report({
node: decl,
message: 'Reserved props must be listed before all other props'
message: 'Reserved props must be listed before all other props',
fix: generateFixerFunction(node, context, reservedList)
});
return memo;
}
return decl;
}
Expand Down
15 changes: 14 additions & 1 deletion tests/lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,21 @@ ruleTester.run('jsx-sort-props', rule, {
errors: [expectedError]
},
{
code: '<App dangerouslySetInnerHTML={{__html: "EPR"}} key={2} b />',
code: '<App key={2} b a />',
options: reservedFirstAsBooleanArgs,
output: '<App key={2} a b />',
errors: [expectedError]
},
{
code: '<App b a />',
options: reservedFirstAsBooleanArgs,
output: '<App a b />',
errors: [expectedError]
},
{
code: '<App dangerouslySetInnerHTML={{__html: "EPR"}} e key={2} b />',
options: reservedFirstAsBooleanArgs,
output: '<App key={2} b dangerouslySetInnerHTML={{__html: "EPR"}} e />',
errors: [expectedReservedFirstError]
},
{
Expand Down