Skip to content

fix(codemode): honor localeCompare locales and collator options - #64935

Merged
rekram1-node merged 3 commits into
devfrom
locale-compare-options
Nov 19, 2026
Merged

rekram1-node merged 3 commits into
devfrom
locale-compare-options

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Summary

String.prototype.localeCompare in Code Mode ignored its locales and options arguments, so natural sorting and case-insensitive comparison quietly gave the wrong answer. It now hands the program's locales and collator options to the host's localeCompare, so results match Bun. Getters and toString objects in the options are read and converted first. With no locales or options, it runs the same call as before. No global Intl is added.

["a10", "a9", "a1"].sort((x, y) => x.localeCompare(y, undefined, { numeric: true }))
"a".localeCompare("A", undefined, { sensitivity: "base" })
"a".localeCompare("á", "en", { sensitivity: "base" })
"a,b".localeCompare("ab", "en", { ignorePunctuation: true })
Bun 1.4.4 Code Mode before Code Mode after
numeric sort a1, a9, a10 a1, a10, a9 a1, a9, a10
sensitivity base (a/A) 0 -1 0
sensitivity base (a/á) 0 -1 0
ignorePunctuation 0 -1 0

Invalid locale tags and option values throw catchable RangeErrors, and a non-string locale throws a TypeError, as in Bun. null locales or options throw a clear TypeError instead of the host's message, which would show the runtime's own source.

Testing

  • bun test test/parity.test.ts test/string-core-test262.test.ts (new parity tests check numeric, sensitivity, caseFirst, ignorePunctuation, a getter option, a toString option, a locale array, the sv locale, and the error cases; expected values were checked against bun -e)
  • bun typecheck in packages/codemode; bun run check at the root
  • Full packages/codemode suite: 1787 pass, 0 fail.

Visual

<svg xmlns="http://www.w3.org/2000/svg" width="240" height="72" viewBox="0 0 240 72" fill="currentColor" font-family="ui-monospace,monospace" font-size="14">
<style>
.t{animation:s 4s ease-in-out infinite}
.a{animation-name:p}
@keyframes s{0%,35%{transform:translateX(0)}50%,85%{transform:translateX(-70px)}100%{transform:translateX(0)}}
@keyframes p{0%,35%{transform:translateX(0)}50%,85%{transform:translateX(70px)}100%{transform:translateX(0)}}
@media (prefers-reduced-motion:reduce){.t{animation:none;transform:translateX(-70px)}.a{transform:translateX(70px)}}
</style>
<text x="12" y="16" font-size="11" opacity=".6">localeCompare(…, { numeric: true })</text>
<rect x="12" y="28" width="216" height="32" rx="6" fill="none" stroke="currentColor" opacity=".25"/>
<text x="30" y="49">a1</text>
<g class="t a"><text x="100" y="49">a10</text></g>
<g class="t"><text x="170" y="49" opacity=".85">a9</text></g>
</svg>

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found nothing that should block this. On the base, the numeric sort returned a1, a10, a9, sensitivity: "base" and ignorePunctuation returned -1, and invalid locales or options were silently accepted. With this change the results match the host collator (a1, a9, a10, 0, 0), and bad tags, option values and null throw catchable RangeError/TypeErrors.

Beyond your cases, I checked that getter and toString options are read in collator order, that array-like locale lists and huge lengths are handled, and that host namespaces passed as a locale or option go through the host's normal conversion and expose nothing. The parity and string test262 tests pass, the full packages/codemode suite passes (1787 tests), and tsc --noEmit passes.

The public API is unchanged: this is internal to the Code Mode language stdlib.

@rekram1-node

Copy link
Copy Markdown
Member Author

Security follow-up: locale lists over 100 entries (arrays and array-likes, checked before any element is read) and tags over 256 characters now throw a catchable RangeError before reaching host Intl, so one call can no longer stall the host event loop.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commits look sound and I found nothing that blocks a merge.

The locale list is now capped at 100 entries, checked before any element is read, and each tag at 256 characters, checked after objects in the list are converted to strings. Both limits live with the other interpreter limits in limits.ts.

The parity and string test262 tests pass, the full packages/codemode suite passes (1788 tests), and tsc --noEmit passes. I re-ran my earlier checks and the collator behaviour is unchanged. The new test checks the boundary on each side of both caps and that a too-long list is rejected without reading any element.

The public API is unchanged: this is internal to the Code Mode language runtime and stdlib.

@rekram1-node

Copy link
Copy Markdown
Member Author

Removed the size caps from this PR at the maintainer's request.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commit is a clean revert of the locale-cap commit, and I see no problem with it. The code is now the same as the version I first reviewed, which I had already verified.

Without the cap, a locale list is limited only by the interpreter's array-length limit and tags have no length cap. I timed both at this head: 100,000 locale tags took about 0.3 s and a 160,000-character tag about 0.1 s, so dropping the cap doesn't look risky.

The parity and string test262 tests pass (306) and tsc --noEmit passes. The public API is unchanged.

@rekram1-node
rekram1-node merged commit 277c9a4 into dev Nov 19, 2026
12 checks passed
@rekram1-node
rekram1-node deleted the locale-compare-options branch November 19, 2026 21:27
@amrkmn amrkmn mentioned this pull request Nov 23, 2026
1 task
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.

1 participant