Skip to content

Commit c1cb519

Browse files
antfuhyoban
andauthored
feat(keep-sorted): support destructuring assignment (#11)
Co-authored-by: Stephen Zhou <[email protected]>
1 parent 24e5ed5 commit c1cb519

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/commands/keep-sorted.test.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ run(
1010
'apple',
1111
'bar',
1212
'foo',
13-
]`,
13+
]`
14+
,
1415
// Object property
1516
{
1617
code: $`
@@ -130,6 +131,7 @@ run(
130131
errors: ['command-fix'],
131132
},
132133
{
134+
description: 'Function arguments',
133135
code: $`
134136
function foo() {
135137
// @keep-sorted
@@ -170,6 +172,24 @@ run(
170172
}`,
171173
errors: ['command-fix'],
172174
},
175+
{
176+
description: 'Export statement without trailing comma',
177+
code: $`
178+
// @keep-sorted
179+
export {
180+
foo,
181+
bar,
182+
apple
183+
}`,
184+
output: $`
185+
// @keep-sorted
186+
export {
187+
apple,
188+
bar,
189+
foo,
190+
}`,
191+
errors: ['command-fix'],
192+
},
173193
{
174194
description: 'Sort array of objects',
175195
code: $`
@@ -206,6 +226,52 @@ run(
206226
]`,
207227
errors: ['command-error'],
208228
},
229+
{
230+
description: 'Destructuring assignment',
231+
code: $`
232+
// @keep-sorted
233+
const { foo, bar, apple } = obj`,
234+
output: $`
235+
// @keep-sorted
236+
const { apple, bar, foo, } = obj`,
237+
errors: ['command-fix'],
238+
},
239+
{
240+
description: 'Destructuring assignment multiple lines',
241+
code: $`
242+
// @keep-sorted
243+
const {
244+
foo,
245+
bar,
246+
apple,
247+
} = obj`,
248+
output: $`
249+
// @keep-sorted
250+
const {
251+
apple,
252+
bar,
253+
foo,
254+
} = obj`,
255+
errors: ['command-fix'],
256+
},
257+
{
258+
description: 'Destructuring assignment multiple lines without trailing comma',
259+
code: $`
260+
// @keep-sorted
261+
const {
262+
foo,
263+
bar,
264+
apple
265+
} = obj`,
266+
output: $`
267+
// @keep-sorted
268+
const {
269+
apple,
270+
bar,
271+
foo,
272+
} = obj`,
273+
errors: ['command-fix'],
274+
},
209275
{
210276
description: 'Block comment',
211277
code: $`

src/commands/keep-sorted.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const keepSorted: Command = {
2424

2525
const node = ctx.findNodeBelow(
2626
'ObjectExpression',
27+
'ObjectPattern',
2728
'ArrayExpression',
2829
'TSInterfaceBody',
2930
'TSTypeLiteral',
@@ -53,6 +54,18 @@ export const keepSorted: Command = {
5354
},
5455
)
5556
}
57+
else if (node.type === 'ObjectPattern') {
58+
sort(
59+
ctx,
60+
node,
61+
node.properties,
62+
(prop) => {
63+
if (prop.type === 'Property')
64+
return getString(prop.key)
65+
return null
66+
},
67+
)
68+
}
5669
else if (node.type === 'ArrayExpression') {
5770
return sort(
5871
ctx,

0 commit comments

Comments
 (0)