Skip to content

Commit

Permalink
fix(keep-sorted): unable to find first interface/declare (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddosakura authored Sep 26, 2024
1 parent fe18fcf commit 902960c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/commands/keep-sorted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ run(
]
`
,
// multi interfaces without export
$`
// @keep-sorted
interface A {
foo: number
}
// @keep-sorted
interface B {
foo: number
}
`
,
// multi declares without export
$`
// @keep-sorted
const arr1 = [
{ index: 0, name: 'foo' },
]
// @keep-sorted
const arr2 = [
{ index: 0, name: 'foo' },
]
`
,
// Object property
{
code: $`
Expand Down
21 changes: 21 additions & 0 deletions src/commands/keep-sorted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,29 @@ export const keepSorted: Command = {
'TSSatisfiesExpression',
) || ctx.findNodeBelow(
'ExportNamedDeclaration',
'TSInterfaceDeclaration',
'VariableDeclaration',
)

if (node?.type === 'TSInterfaceDeclaration') {
node = node.body
}

if (node?.type === 'VariableDeclaration') {
const dec = node.declarations[0]
if (!dec) {
node = undefined
}
else if (dec.id.type === 'ObjectPattern') {
node = dec.id
}
else {
node = Array.isArray(dec.init) ? dec.init[0] : dec.init
if (node && node.type !== 'ObjectExpression' && node.type !== 'ArrayExpression' && node.type !== 'TSSatisfiesExpression')
node = undefined
}
}

// Unwrap TSSatisfiesExpression
if (node?.type === 'TSSatisfiesExpression') {
if (node.expression.type !== 'ArrayExpression' && node.expression.type !== 'ObjectExpression') {
Expand Down

0 comments on commit 902960c

Please sign in to comment.