Skip to content

Commit

Permalink
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
@@ -107,19 +107,30 @@ const generateFixerFunction = (node, context, reservedList) => {

return function(fixer) {
const fixers = [];
let source = sourceCode.getText();

// Replace each unsorted attribute with the sorted one.
sortableAttributeGroups.forEach((sortableGroup, ii) => {
sortableGroup.forEach((attr, jj) => {
const sortedAttr = sortedAttributeGroups[ii][jj];
const sortedAttrText = sourceCode.getText(sortedAttr);
fixers.push(
fixer.replaceTextRange([attr.range[0], attr.range[1]], sortedAttrText)
);
fixers.push({
range: [attr.range[0], attr.range[1]],
text: sortedAttrText
});
});
});

return fixers;
fixers.sort((a, b) => a.range[0] < b.range[0]);

const rangeStart = fixers[fixers.length - 1].range[0];
const rangeEnd = fixers[0].range[1];

fixers.forEach(fix => {
source = `${source.substr(0, fix.range[0])}${fix.text}${source.substr(fix.range[1])}`;
});

return fixer.replaceTextRange([rangeStart, rangeEnd], source.substr(rangeStart, rangeEnd - rangeStart));
};
};

0 comments on commit 677e1bd

Please sign in to comment.