Skip to content

Commit 902960c

Browse files
authored
fix(keep-sorted): unable to find first interface/declare (#29)
1 parent fe18fcf commit 902960c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/commands/keep-sorted.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ run(
1313
]
1414
`
1515
,
16+
// multi interfaces without export
17+
$`
18+
// @keep-sorted
19+
interface A {
20+
foo: number
21+
}
22+
// @keep-sorted
23+
interface B {
24+
foo: number
25+
}
26+
`
27+
,
28+
// multi declares without export
29+
$`
30+
// @keep-sorted
31+
const arr1 = [
32+
{ index: 0, name: 'foo' },
33+
]
34+
// @keep-sorted
35+
const arr2 = [
36+
{ index: 0, name: 'foo' },
37+
]
38+
`
39+
,
1640
// Object property
1741
{
1842
code: $`

src/commands/keep-sorted.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,29 @@ export const keepSorted: Command = {
3131
'TSSatisfiesExpression',
3232
) || ctx.findNodeBelow(
3333
'ExportNamedDeclaration',
34+
'TSInterfaceDeclaration',
35+
'VariableDeclaration',
3436
)
3537

38+
if (node?.type === 'TSInterfaceDeclaration') {
39+
node = node.body
40+
}
41+
42+
if (node?.type === 'VariableDeclaration') {
43+
const dec = node.declarations[0]
44+
if (!dec) {
45+
node = undefined
46+
}
47+
else if (dec.id.type === 'ObjectPattern') {
48+
node = dec.id
49+
}
50+
else {
51+
node = Array.isArray(dec.init) ? dec.init[0] : dec.init
52+
if (node && node.type !== 'ObjectExpression' && node.type !== 'ArrayExpression' && node.type !== 'TSSatisfiesExpression')
53+
node = undefined
54+
}
55+
}
56+
3657
// Unwrap TSSatisfiesExpression
3758
if (node?.type === 'TSSatisfiesExpression') {
3859
if (node.expression.type !== 'ArrayExpression' && node.expression.type !== 'ObjectExpression') {

0 commit comments

Comments
 (0)