feat(keep-sorted): support as const expressions - #48
Merged
Conversation
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
approved these changes
Jul 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 constwithas constconstwithas const(no export)export constwithsatisfies🔗 Linked issue
N/A — this is a straightforward enhancement discovered while using the tool.
🧭 Context
The
keep-sortedcommand already supportssatisfiesexpressions viaTSSatisfiesExpression, but did not recognizeas constassertions (TSAsExpression). This meant that object literals and arrays withas constwere silently skipped during sorting, which is inconsistent and unexpected for users.Added
TSAsExpressionto the node types recognized bykeep-sorted, including unwrapping logic to reach the underlyingObjectExpressionorArrayExpression.📚 Description
TypeScript's
as constassertion is commonly used to create deeply readonly literal types (e.g.,const STATUS = { active: 'active' } as const). Previously,keep-sortedwould not sort these becauseTSAsExpressionwas not included in the allowed node types for variable declarations or in the unwrap logic.This PR:
TSAsExpressiontofindNodeBelow— allows the command to locate sorted blocks insideexport const x = { ... } as const.TSAsExpressionto the variable declaration guard — prevents the node from being discarded when extracted fromdec.init.TSAsExpressionto the unwrap logic — unwrapsas constto access the innerObjectExpressionorArrayExpressionfor sorting, mirroring the existingTSSatisfiesExpressionbehavior.Tests are added for three previously uncovered scenarios to ensure robustness.