Skip to content

fix: sort ALL_CAPS keys before TitleCase and lowercase in destructure - #338

Open
0x5t4l1n wants to merge 1 commit into
mthadley:masterfrom
0x5t4l1n:fix/allcaps-sort-order
Open

fix: sort ALL_CAPS keys before TitleCase and lowercase in destructure#338
0x5t4l1n wants to merge 1 commit into
mthadley:masterfrom
0x5t4l1n:fix/allcaps-sort-order

Conversation

@0x5t4l1n

@0x5t4l1n 0x5t4l1n commented Aug 2, 2026

Copy link
Copy Markdown

Summary

Fixes #300 — ALL_CAPS keys (e.g. FOO, MAX_VALUE) now sort before TitleCase keys (e.g. Bar), which sort before lowercase keys (e.g. baz).

Root cause: createSorter() delegated entirely to naturalCompare. In ASCII, B (66) < F (70), so Bar sorted before FOO. The fix adds a capGroup() helper that buckets names into three tiers before falling through to naturalCompare.

Before:

const { Bar, FOO, baz } = obj; // no lint error

After:

const { FOO, Bar, baz } = obj; // correct order, no lint error
const { Bar, FOO, baz } = obj; // lint error: FOO should be before Bar

Works correctly with both caseSensitive: true (default) and caseSensitive: false.

Test plan

  • Run existing test suite: npm test
  • Add a test case for { FOO, Bar, baz } ordering in tests/lib/rules/sort-destructure-keys.js

Resolves mthadley#300. naturalCompare alone sorts by ASCII value, placing
capitalised keys (B=66) before all-caps ones that start lower (F=70).
Add a capGroup() function inside createSorter() that buckets names into
ALL_CAPS (0) → TitleCase (1) → lowercase (2) before falling through to
the existing naturalCompare, ensuring e.g. { FOO, Bar, baz } is the
canonical order regardless of caseSensitive setting.
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.

All capped keys should be at the top.

1 participant