Skip to content

Commit 3a237b0

Browse files
committed
[Refactor] jsx-sort-props: make the map to an object instead of an array
1 parent dc62a8c commit 3a237b0

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

lib/rules/jsx-sort-props.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ function isReservedPropName(name, list) {
4747
}
4848

4949
let attributeMap;
50-
// attributeMap = [endrange, true||false if comment in between nodes exists, it needs to be sorted to end]
50+
// attributeMap = { end: endrange, hasComment: true||false if comment in between nodes exists, it needs to be sorted to end }
5151

5252
function shouldSortToEnd(node) {
5353
const attr = attributeMap.get(node);
54-
return !!attr && !!attr[1];
54+
return !!attr && !!attr.hasComment;
5555
}
5656

5757
function contextCompare(a, b, options) {
@@ -170,35 +170,35 @@ function getGroupsOfSortableAttributes(attributes, context) {
170170
}
171171
if (!attrIsSpread) {
172172
if (comment.length === 0) {
173-
attributeMap.set(attribute, [attribute.range[1], false]);
173+
attributeMap.set(attribute, { end: attribute.range[1], hasComment: false });
174174
addtoSortableAttributeGroups(attribute);
175175
} else {
176176
const firstComment = comment[0];
177177
const commentline = firstComment.loc.start.line;
178178
if (comment.length === 1) {
179179
if (attributeline + 1 === commentline && nextAttribute) {
180-
attributeMap.set(attribute, [nextAttribute.range[1], true]);
180+
attributeMap.set(attribute, { end: nextAttribute.range[1], hasComment: true });
181181
addtoSortableAttributeGroups(attribute);
182182
i += 1;
183183
} else if (attributeline === commentline) {
184184
if (firstComment.type === 'Block' && nextAttribute) {
185-
attributeMap.set(attribute, [nextAttribute.range[1], true]);
185+
attributeMap.set(attribute, { end: nextAttribute.range[1], hasComment: true });
186186
i += 1;
187187
} else if (firstComment.type === 'Block') {
188-
attributeMap.set(attribute, [firstComment.range[1], true]);
188+
attributeMap.set(attribute, { end: firstComment.range[1], hasComment: true });
189189
} else {
190-
attributeMap.set(attribute, [firstComment.range[1], false]);
190+
attributeMap.set(attribute, { end: firstComment.range[1], hasComment: false });
191191
}
192192
addtoSortableAttributeGroups(attribute);
193193
}
194194
} else if (comment.length > 1 && attributeline + 1 === comment[1].loc.start.line && nextAttribute) {
195195
const commentNextAttribute = sourceCode.getCommentsAfter(nextAttribute);
196-
attributeMap.set(attribute, [nextAttribute.range[1], true]);
196+
attributeMap.set(attribute, { end: nextAttribute.range[1], hasComment: true });
197197
if (
198198
commentNextAttribute.length === 1
199199
&& nextAttribute.loc.start.line === commentNextAttribute[0].loc.start.line
200200
) {
201-
attributeMap.set(attribute, [commentNextAttribute[0].range[1], true]);
201+
attributeMap.set(attribute, { end: commentNextAttribute[0].range[1], hasComment: true });
202202
}
203203
addtoSortableAttributeGroups(attribute);
204204
i += 1;
@@ -248,10 +248,9 @@ function generateFixerFunction(node, context, reservedList) {
248248
sortableAttributeGroups.forEach((sortableGroup, ii) => {
249249
sortableGroup.forEach((attr, jj) => {
250250
const sortedAttr = sortedAttributeGroups[ii][jj];
251-
const sortedAttrText = source.substring(sortedAttr.range[0], attributeMap.get(sortedAttr)[0]);
252-
const attrrangeEnd = attributeMap.get(attr)[0];
251+
const sortedAttrText = source.substring(sortedAttr.range[0], attributeMap.get(sortedAttr).end);
253252
fixers.push({
254-
range: [attr.range[0], attrrangeEnd],
253+
range: [attr.range[0], attributeMap.get(attr).end],
255254
text: sortedAttrText,
256255
});
257256
});

0 commit comments

Comments
 (0)