Extend useSortedInterfaceMembers to object type literals #11535
finnan444
started this conversation in
Rule suggestion
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
useSortedInterfaceMembersonly matchesTsInterfaceDeclaration, so codebases that describe their public contracts as type aliases get nothing out of it:I couldn't find a documented reason for the limitation. Type aliases don't come up in #7553 or #8164, and there's no open issue about it, so it looks incidental rather than deliberate.
The implementation side is small.
TsObjectTypeis'{' members: TsTypeMemberList '}',TsInterfaceDeclarationholds the sameTsTypeMemberList, and those are the only two nodes in the grammar that hold one. Sorting, the sortable/non-sortable split for call/construct/index signatures, comment preservation and thepartitionByNewLinesectioning all work on the list alone, so they carry over unchanged.What changes is the query node: either
Ast<TsTypeMemberList>or adeclare_node_union!of the interface andTsObjectType. I'd go with the union, since querying the list directly moves the diagnostic range from the interface node to the member list and churns the existing snapshots for no reason.Two things I'd rather not decide on my own.
The name.
useSortedInterfaceMembersstops describing what the rule does. As I see it: keep the name and clarify the docs, rename touseSortedTypeMemberswith the old name as a deprecated alias, or add a separate rule for type literals. I'd pick the first as the least invasive, though it does leave a name that lies a bit.The scope. Only
type X = { ... }at the top level, or everyTsObjectType, including nested ones, function parameters, type arguments and intersections likeA & { ... }? I lean towards everyTsObjectType: it's uniform, and it's actually less code, because restricting to top-level aliases needs an extra ancestor check. The trade-off is a noticeably wider autofix surface.TsMappedTypeis a separate node either way, so{ [K in T]: V }stays untouched.If both get settled, I'll send the PR against
next, since that's where #10424 landed and the rule there already has thepartitionByNewLineplumbing.Disclosure: this write-up was drafted with Claude Code; the grammar and implementation details in it were checked against the code.
All reactions