Skip to content
Open
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
24 changes: 24 additions & 0 deletions src/commands/keep-sorted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,28 @@ run(
`,
errors: ['command-fix'],
},
{
description: 'Class property with array',
code: $`
class Foo {
/// keep-sorted
protected _tags: ItemTag[] = [
ItemTag.Foo,
ItemTag.Bar,
ItemTag.Apple,
];
}
`,
output: $`
class Foo {
/// keep-sorted
protected _tags: ItemTag[] = [
ItemTag.Apple,
ItemTag.Bar,
ItemTag.Foo,
];
}
`,
errors: ['command-fix'],
},
)
18 changes: 18 additions & 0 deletions src/commands/keep-sorted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const keepSorted: Command = {
'ExportNamedDeclaration',
'TSInterfaceDeclaration',
'VariableDeclaration',
'PropertyDefinition',
)

if (node?.type === 'TSInterfaceDeclaration') {
Expand All @@ -55,6 +56,14 @@ export const keepSorted: Command = {
}
}

if (node?.type === 'PropertyDefinition') {
const value = node.value
if (value?.type === 'ObjectExpression' || value?.type === 'ArrayExpression' || value?.type === 'TSSatisfiesExpression' || value?.type === 'TSAsExpression')
node = value
else
node = undefined
}

// Unwrap TSSatisfiesExpression / TSAsExpression (e.g. `satisfies`, `as const`)
if (node?.type === 'TSSatisfiesExpression' || node?.type === 'TSAsExpression') {
if (node.expression.type !== 'ArrayExpression' && node.expression.type !== 'ObjectExpression') {
Expand Down Expand Up @@ -307,5 +316,14 @@ function getString(node: Tree.Node): string | null {
return node.name
if (node.type === 'Literal')
return String(node.raw)
if (node.type === 'MemberExpression') {
const object = getString(node.object)
const property = node.computed
? (node.property.type === 'Literal' ? String(node.property.raw) : null)
: getString(node.property)
if (object == null || property == null)
return null
return `${object}.${property}`
}
return null
}
Loading