Skip to content

feat(keep-sorted): support as const expressions - #48

Merged
sxzz merged 1 commit into
antfu:mainfrom
luo2430:feat/keep-sorted
Jul 14, 2026
Merged

feat(keep-sorted): support as const expressions#48
sxzz merged 1 commit into
antfu:mainfrom
luo2430:feat/keep-sorted

Conversation

@luo2430

@luo2430 luo2430 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Extend keep-sorted to handle TSAsExpression nodes, enabling sorting of objects and arrays annotated with as const (e.g. const x = { ... } as const).

Also add test coverage for:

  • export const with as const
  • const with as const (no export)
  • export const with satisfies

🔗 Linked issue

N/A — this is a straightforward enhancement discovered while using the tool.

🧭 Context

The keep-sorted command already supports satisfies expressions via TSSatisfiesExpression, but did not recognize as const assertions (TSAsExpression). This meant that object literals and arrays with as const were silently skipped during sorting, which is inconsistent and unexpected for users.

Added TSAsExpression to the node types recognized by keep-sorted, including unwrapping logic to reach the underlying ObjectExpression or ArrayExpression.

📚 Description

TypeScript's as const assertion is commonly used to create deeply readonly literal types (e.g., const STATUS = { active: 'active' } as const). Previously, keep-sorted would not sort these because TSAsExpression was not included in the allowed node types for variable declarations or in the unwrap logic.

This PR:

  1. Adds TSAsExpression to findNodeBelow — allows the command to locate sorted blocks inside export const x = { ... } as const.
  2. Adds TSAsExpression to the variable declaration guard — prevents the node from being discarded when extracted from dec.init.
  3. Adds TSAsExpression to the unwrap logic — unwraps as const to access the inner ObjectExpression or ArrayExpression for sorting, mirroring the existing TSSatisfiesExpression behavior.

Tests are added for three previously uncovered scenarios to ensure robustness.

Thanks for reviewing — let me know if you'd like any adjustments!
Also, thank you for building and maintaining this excellent ESLint plugin!

Extend keep-sorted to handle TSAsExpression nodes, enabling sorting
of objects and arrays annotated with `as const` (e.g. `const x = { ... } as const`).

Also add test coverage for:
- `export const` with `as const`
- `const` with `as const` (no export)
- `export const` with `satisfies`
@sxzz
sxzz merged commit 35909b9 into antfu:main Jul 14, 2026
5 of 8 checks passed
@luo2430
luo2430 deleted the feat/keep-sorted branch July 14, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants