Skip to content

Add fixer for jsx/sort-props #1273

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

Merged
merged 2 commits into from
Aug 7, 2017
Merged
Changes from 1 commit
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
32 changes: 16 additions & 16 deletions lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function alphabeticalCompare(a, b, ignoreCase) {
function getGroupsOfSortableAttributes(attributes) {
const sortableAttributeGroups = [];
let groupCount = 0;
for (var i = 0; i < attributes.length; i++) {
for (let i = 0; i < attributes.length; i++) {
const lastAttr = attributes[i - 1];
// If we have no groups or if the last attribute was JSXSpreadAttribute
// then we start a new group. Append attributes to the group until we
Expand All @@ -77,30 +77,30 @@ function getGroupsOfSortableAttributes(attributes) {
return sortableAttributeGroups;
}

function generateFixerFunction(node, context) {
var sourceCode = context.getSourceCode();
var attributes = node.attributes.slice(0);
var configuration = context.options[0] || {};
var ignoreCase = configuration.ignoreCase || false;
const generateFixerFunction = (node, context) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(fwiw i think this was fine to leave as a normal function)

const sourceCode = context.getSourceCode();
const attributes = node.attributes.slice(0);
const configuration = context.options[0] || {};
const ignoreCase = configuration.ignoreCase || 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(function(group) {
return group.slice(0).sort(function(a, b) {
return alphabeticalCompare(propName(a), propName(b), ignoreCase);
});
});
const sortedAttributeGroups = sortableAttributeGroups.slice(0).map(group =>
group.slice(0).sort((a, b) =>
alphabeticalCompare(propName(a), propName(b), ignoreCase)
)
);

return function(fixer) {
const fixers = [];

// Replace each unsorted attribute with the sorted one.
sortableAttributeGroups.forEach(function(sortableGroup, ii) {
sortableGroup.forEach(function(attr, jj) {
var sortedAttr = sortedAttributeGroups[ii][jj];
var sortedAttrText = sourceCode.getText(sortedAttr);
sortableAttributeGroups.forEach((sortableGroup, ii) => {
sortableGroup.forEach((attr, jj) => {
const sortedAttr = sortedAttributeGroups[ii][jj];
const sortedAttrText = sourceCode.getText(sortedAttr);
fixers.push(
fixer.replaceTextRange([attr.start, attr.end], sortedAttrText)
);
Expand All @@ -109,7 +109,7 @@ function generateFixerFunction(node, context) {

return fixers;
};
}
};

/**
* Checks if the `reservedFirst` option is valid
Expand Down