Skip to content

Commit 27b9c72

Browse files
committed
Reworked the Fix order
1 parent 1e162bd commit 27b9c72

File tree

4 files changed

+83
-16
lines changed

4 files changed

+83
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
## 5.1.0
4+
5+
- Reworked the `Fix order` suggestion so that applying it sorts the entire spread-bounded region around the reported key in a single step, instead of only swapping it with its immediate neighbour.
6+
37
## 5.0.0
48

59
### Major Changes

lib/rules/sort-keys-shorthand.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ module.exports = {
217217
prevName: null,
218218
numKeys: node.properties.length,
219219
isSameLine: isSameLine(node),
220-
lastNode: node,
221220
};
222221
},
223222

@@ -239,15 +238,13 @@ module.exports = {
239238

240239
const prevName = stack.prevName;
241240
const prevIsShorthand = stack.isShorthand;
242-
const lastNode = stack.lastNode;
243241
const numKeys = stack.numKeys;
244242

245243
const data = getPropertyData(node);
246244
const thisName = data && data.name;
247245
if (thisName !== null) {
248246
stack.prevName = thisName;
249247
stack.isShorthand = data.isShorthand;
250-
stack.lastNode = node;
251248
}
252249
if (
253250
prevName === null ||
@@ -287,10 +284,34 @@ module.exports = {
287284
desc: 'Fix order',
288285
fix(fixer) {
289286
const sourceCode = context.sourceCode;
290-
return fixer.replaceTextRange(
291-
[lastNode.range[0], node.range[1]],
292-
`${sourceCode.text.slice(node.range[0], node.range[1])},${sourceCode.text.slice(lastNode.range[0], lastNode.range[1])}`
293-
);
287+
const siblings = node.parent.properties;
288+
const idx = siblings.indexOf(node);
289+
290+
let start = idx;
291+
while (start > 0 && siblings[start - 1].type === 'Property') {
292+
start--;
293+
}
294+
let end = idx;
295+
while (end < siblings.length - 1 && siblings[end + 1].type === 'Property') {
296+
end++;
297+
}
298+
299+
const region = siblings.slice(start, end + 1);
300+
const sorted = region.slice().sort(function(a, b) {
301+
const ad = getPropertyData(a);
302+
const bd = getPropertyData(b);
303+
if (ad.name === bd.name && ad.isShorthand === bd.isShorthand) {
304+
return 0;
305+
}
306+
return isValidOrder(ad.name, bd.name, shorthand, ad.isShorthand, bd.isShorthand) ? -1 : 1;
307+
});
308+
309+
return region.map(function(prop, i) {
310+
return fixer.replaceTextRange(
311+
prop.range,
312+
sourceCode.text.slice(sorted[i].range[0], sorted[i].range[1])
313+
);
314+
});
294315
}
295316
}],
296317
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-sort-keys-shorthand",
3-
"version": "5.0.0",
3+
"version": "5.1.0",
44
"description": "eslint plugin which support sorting shorthand keys",
55
"main": "index.js",
66
"scripts": {

tests/lib/rules/sort-keys-shorthand.test.js

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,48 @@ ruleTester.run('sort-keys-shorthand', rule, {
482482
]
483483
},
484484

485+
// suggestion sorts the full region in one pass (not just an adjacent swap)
486+
{
487+
code: 'var obj = {a:1, d:2, c:3, b:4, e:5}',
488+
options: [],
489+
errors: [
490+
{
491+
message: "Expected object keys to be in ascending order. 'c' should be before 'd'.",
492+
suggestions: [{
493+
desc: 'Fix order',
494+
output: 'var obj = {a:1, b:4, c:3, d:2, e:5}'
495+
}]
496+
},
497+
{
498+
message: "Expected object keys to be in ascending order. 'b' should be before 'c'.",
499+
suggestions: [{
500+
desc: 'Fix order',
501+
output: 'var obj = {a:1, b:4, c:3, d:2, e:5}'
502+
}]
503+
}
504+
]
505+
},
506+
// region sort stays within spread-bounded groups
507+
{
508+
code: 'var obj = {d:1, a:2, ...x, f:3, e:4}',
509+
options: [],
510+
errors: [
511+
{
512+
message: "Expected object keys to be in ascending order. 'a' should be before 'd'.",
513+
suggestions: [{
514+
desc: 'Fix order',
515+
output: 'var obj = {a:2, d:1, ...x, f:3, e:4}'
516+
}]
517+
},
518+
{
519+
message: "Expected object keys to be in ascending order. 'e' should be before 'f'.",
520+
suggestions: [{
521+
desc: 'Fix order',
522+
output: 'var obj = {d:1, a:2, ...x, e:4, f:3}'
523+
}]
524+
}
525+
]
526+
},
485527
// not ignore properties not separated by spread properties
486528
{
487529
code: 'var obj = {...z, c:1, b:1}',
@@ -950,7 +992,7 @@ ruleTester.run('sort-keys-shorthand', rule, {
950992
message: "Expected object keys to be in descending order. 'a' should be before ''.",
951993
suggestions: [{
952994
desc: "Fix order",
953-
output: "var obj = {a:'2','':1} // desc"
995+
output: "var obj = {a:'2', '':1} // desc"
954996
}],
955997
}
956998
]
@@ -963,7 +1005,7 @@ ruleTester.run('sort-keys-shorthand', rule, {
9631005
message: "Expected object keys to be in descending order. 'a' should be before ''.",
9641006
suggestions: [{
9651007
desc: "Fix order",
966-
output: "var obj = {a:'2',[``]:1} // desc"
1008+
output: "var obj = {a:'2', [``]:1} // desc"
9671009
}],
9681010
}
9691011
]
@@ -1030,14 +1072,14 @@ ruleTester.run('sort-keys-shorthand', rule, {
10301072
message: "Expected object keys to be in descending order. '2' should be before '1'.",
10311073
suggestions: [{
10321074
desc: "Fix order",
1033-
output: "var obj = {2:4,1:1, A:3, '11':2}"
1075+
output: "var obj = {A:3, 2:4, '11':2, 1:1}"
10341076
}],
10351077
},
10361078
{
10371079
message: "Expected object keys to be in descending order. 'A' should be before '2'.",
10381080
suggestions: [{
10391081
desc: "Fix order",
1040-
output: "var obj = {1:1, A:3,2:4, '11':2}"
1082+
output: "var obj = {A:3, 2:4, '11':2, 1:1}"
10411083
}],
10421084
}
10431085
]
@@ -1132,14 +1174,14 @@ ruleTester.run('sort-keys-shorthand', rule, {
11321174
message: "Expected object keys to be in insensitive descending order. '2' should be before '1'.",
11331175
suggestions: [{
11341176
desc: "Fix order",
1135-
output: "var obj = {2:4,1:1, A:3, '11':2}"
1177+
output: "var obj = {A:3, 2:4, '11':2, 1:1}"
11361178
}],
11371179
},
11381180
{
11391181
message: "Expected object keys to be in insensitive descending order. 'A' should be before '2'.",
11401182
suggestions: [{
11411183
desc: "Fix order",
1142-
output: "var obj = {1:1, A:3,2:4, '11':2}"
1184+
output: "var obj = {A:3, 2:4, '11':2, 1:1}"
11431185
}],
11441186
}
11451187
]
@@ -1238,14 +1280,14 @@ ruleTester.run('sort-keys-shorthand', rule, {
12381280
message: "Expected object keys to be in natural descending order. '2' should be before '1'.",
12391281
suggestions: [{
12401282
desc: "Fix order",
1241-
output: "var obj = {2:4,1:1, A:3, '11':2}"
1283+
output: "var obj = {A:3, '11':2, 2:4, 1:1}"
12421284
}],
12431285
},
12441286
{
12451287
message: "Expected object keys to be in natural descending order. 'A' should be before '2'.",
12461288
suggestions: [{
12471289
desc: "Fix order",
1248-
output: "var obj = {1:1, A:3,2:4, '11':2}"
1290+
output: "var obj = {A:3, '11':2, 2:4, 1:1}"
12491291
}],
12501292
}
12511293
]

0 commit comments

Comments
 (0)