Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/commands/keep-sorted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,70 @@ run(
`,
errors: ['command-fix'],
},
{
description: 'export const with as const',
code: $`
/// keep-sorted
export const statusMap = {
timeout: 'timeout',
rejected: 'rejected',
fulfilled: 'fulfilled',
pending: 'pending',
} as const;
`,
output: $`
/// keep-sorted
export const statusMap = {
fulfilled: 'fulfilled',
pending: 'pending',
rejected: 'rejected',
timeout: 'timeout',
} as const;
`,
errors: ['command-fix'],
},
{
description: 'const with as const (no export)',
code: $`
/// keep-sorted
const statusMap = {
timeout: 'timeout',
rejected: 'rejected',
fulfilled: 'fulfilled',
pending: 'pending',
} as const;
`,
output: $`
/// keep-sorted
const statusMap = {
fulfilled: 'fulfilled',
pending: 'pending',
rejected: 'rejected',
timeout: 'timeout',
} as const;
`,
errors: ['command-fix'],
},
{
description: 'export const with satisfies',
code: $`
/// keep-sorted
export const statusMap = {
timeout: 'timeout',
rejected: 'rejected',
fulfilled: 'fulfilled',
pending: 'pending',
} satisfies Record<string, string>;
`,
output: $`
/// keep-sorted
export const statusMap = {
fulfilled: 'fulfilled',
pending: 'pending',
rejected: 'rejected',
timeout: 'timeout',
} satisfies Record<string, string>;
`,
errors: ['command-fix'],
},
)
7 changes: 4 additions & 3 deletions src/commands/keep-sorted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const keepSorted: Command = {
'TSInterfaceBody',
'TSTypeLiteral',
'TSSatisfiesExpression',
'TSAsExpression',
) || ctx.findNodeBelow(
'ExportNamedDeclaration',
'TSInterfaceDeclaration',
Expand All @@ -49,13 +50,13 @@ export const keepSorted: Command = {
}
else {
node = Array.isArray(dec.init) ? dec.init[0] : dec.init
if (node && node.type !== 'ObjectExpression' && node.type !== 'ArrayExpression' && node.type !== 'TSSatisfiesExpression')
if (node && node.type !== 'ObjectExpression' && node.type !== 'ArrayExpression' && node.type !== 'TSSatisfiesExpression' && node.type !== 'TSAsExpression')
node = undefined
}
}

// Unwrap TSSatisfiesExpression
if (node?.type === 'TSSatisfiesExpression') {
// Unwrap TSSatisfiesExpression / TSAsExpression (e.g. `satisfies`, `as const`)
if (node?.type === 'TSSatisfiesExpression' || node?.type === 'TSAsExpression') {
if (node.expression.type !== 'ArrayExpression' && node.expression.type !== 'ObjectExpression') {
node = undefined
}
Expand Down
Loading