Skip to content

Commit

Permalink
fix(keep-sorted): support satisfies, close #25
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 7, 2024
1 parent 3c02f93 commit 8556eab
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/commands/keep-sorted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,32 @@ run(
]
`,
},
{
description: 'With satisfies',
code: $`
// @keep-sorted
const a = {
foo,
bar,
apple
} satisfies Record<string, any>
`,
output: $`
// @keep-sorted
const a = {
apple,
bar,
foo,
} satisfies Record<string, any>
`,
errors: ['command-fix'],
},
{
description: 'With satisfies',
code: $`
// @keep-sorted
const a = bar satisfies Record<string, any>
`,
errors: ['command-error'],
},
)
14 changes: 13 additions & 1 deletion src/commands/keep-sorted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,27 @@ export const keepSorted: Command = {
return ctx.reportError(`Failed to parse options: ${optionsRaw}`)
}

const node = ctx.findNodeBelow(
let node = ctx.findNodeBelow(
'ObjectExpression',
'ObjectPattern',
'ArrayExpression',
'TSInterfaceBody',
'TSTypeLiteral',
'TSSatisfiesExpression',
) || ctx.findNodeBelow(
'ExportNamedDeclaration',
)

// Unwrap TSSatisfiesExpression
if (node?.type === 'TSSatisfiesExpression') {
if (node.expression.type !== 'ArrayExpression' && node.expression.type !== 'ObjectExpression') {
node = undefined
}
else {
node = node.expression
}
}

if (!node)
return ctx.reportError('Unable to find object/array/interface to sort')

Expand Down

0 comments on commit 8556eab

Please sign in to comment.