Skip to content

Commit 677e1bd

Browse files
committed
Fix jsx-sort-props auto fix with ESLint 3
1 parent 31e4f33 commit 677e1bd

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

lib/rules/jsx-sort-props.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,30 @@ const generateFixerFunction = (node, context, reservedList) => {
107107

108108
return function(fixer) {
109109
const fixers = [];
110+
let source = sourceCode.getText();
110111

111112
// Replace each unsorted attribute with the sorted one.
112113
sortableAttributeGroups.forEach((sortableGroup, ii) => {
113114
sortableGroup.forEach((attr, jj) => {
114115
const sortedAttr = sortedAttributeGroups[ii][jj];
115116
const sortedAttrText = sourceCode.getText(sortedAttr);
116-
fixers.push(
117-
fixer.replaceTextRange([attr.range[0], attr.range[1]], sortedAttrText)
118-
);
117+
fixers.push({
118+
range: [attr.range[0], attr.range[1]],
119+
text: sortedAttrText
120+
});
119121
});
120122
});
121123

122-
return fixers;
124+
fixers.sort((a, b) => a.range[0] < b.range[0]);
125+
126+
const rangeStart = fixers[fixers.length - 1].range[0];
127+
const rangeEnd = fixers[0].range[1];
128+
129+
fixers.forEach(fix => {
130+
source = `${source.substr(0, fix.range[0])}${fix.text}${source.substr(fix.range[1])}`;
131+
});
132+
133+
return fixer.replaceTextRange([rangeStart, rangeEnd], source.substr(rangeStart, rangeEnd - rangeStart));
123134
};
124135
};
125136

0 commit comments

Comments
 (0)