Skip to content

feat(@formatjs/intl-pluralrules): replace make-plural-compiler with native implementation and add BigInt support - #5910

Closed
longlho wants to merge 1 commit into
mainfrom
01-14-feat_formatjs_intl-pluralrules_remove_make-plural-compiler_add_support_for_huge_numbers
Closed

longlho wants to merge 1 commit into
mainfrom
01-14-feat_formatjs_intl-pluralrules_remove_make-plural-compiler_add_support_for_huge_numbers

Conversation

@longlho

@longlho longlho commented Jan 15, 2026 •

Copy link
Copy Markdown
Member

TL;DR

Removed make-plural-compiler dependency and implemented a custom plural rules compiler with improved BigInt support.

What changed?

  • Removed the make-plural-compiler dependency and its associated patch file
  • Created a new custom plural-rules-compiler.ts implementation that handles CLDR plural rules
  • Enhanced ToIntlMathematicalValue and ToNumber to properly support BigInt values
  • Fixed edge cases in ApplyUnsignedRoundingMode for handling boundary values
  • Updated GetOperands to handle very large integer values that exceed JavaScript's number limits
  • Added comprehensive tests for BigInt and large number handling
  • Updated Bazel dependencies to newer versions

How to test?

  1. Run the test suite with pnpm test to verify the new implementation works correctly
  2. Test with BigInt values:
    const pr = new Intl.PluralRules('en');
    console.log(pr.select(1n)); // "one"
    console.log(pr.select(2n)); // "other"
    
    const ordinal = new Intl.PluralRules('en', {type: 'ordinal'});
    console.log(ordinal.select(1n)); // "one"
    console.log(ordinal.select(2n)); // "two"
    console.log(ordinal.select(3n)); // "few"
  3. Test with very large numbers:
    console.log(pr.select(10000000000000n)); // "other"
    console.log(ordinal.select(10000000000001n)); // "one"

Why make this change?

The make-plural-compiler dependency had limitations in handling BigInt values and very large numbers. This change improves compatibility with the ECMAScript specification by properly supporting BigInt values in Intl.PluralRules.prototype.select() as required by ECMA-402. The custom implementation also provides better handling of edge cases and maintains precision for very large numbers that exceed JavaScript's safe integer limits.

longlho commented Jan 15, 2026 •

Copy link
Copy Markdown
Member Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • main - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has required the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@longlho longlho changed the title feat(@formatjs/intl-pluralrules): remove make-plural-compiler, add support for huge numbers feat(@formatjs/intl-pluralrules): remove make-plural-compiler, add support for bigint Jan 15, 2026
@longlho longlho changed the title feat(@formatjs/intl-pluralrules): remove make-plural-compiler, add support for bigint feat(@formatjs/intl-pluralrules): replace make-plural-compiler with native implementation and add BigInt support Jan 15, 2026
Comment on lines +377 to +378
['n', 'Math.abs(parseFloat(numStr))'],
['i', 'Math.floor(Math.abs(parseFloat(integerPart)))'],

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Critical precision loss for huge numbers in modulo operations

The operand i uses parseFloat(integerPart) which will lose precision for numbers exceeding Number.MAX_SAFE_INTEGER (9007199254740991). This breaks modulo operations like i % 10 or i % 100 for truly huge numbers.

For example, the string "900719925474099991" would be parsed to 900719925474099970 (due to floating-point rounding), causing i % 10 to incorrectly return 7 instead of 1. This breaks plural/ordinal rules that depend on the last digits.

Fix: For huge numbers, implement string-based modulo operations:

// For numbers exceeding MAX_SAFE_INTEGER, use string-based modulo
function safeMod(numStr: string, divisor: number): number {
  if (numStr.length > 15) { // Rough check for potentially unsafe numbers
    // String-based modulo for last digits
    if (divisor === 10) return parseInt(numStr.slice(-1), 10) || 0
    if (divisor === 100) return parseInt(numStr.slice(-2), 10) || 0
    if (divisor === 1000) return parseInt(numStr.slice(-3), 10) || 0
  }
  return Math.floor(Math.abs(parseFloat(numStr))) % divisor
}

Or document that the implementation only supports numbers up to MAX_SAFE_INTEGER despite the PR title claiming "huge numbers" support.

Spotted by Graphite Agent

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

@longlho
longlho force-pushed the 01-14-feat_formatjs_intl-pluralrules_remove_make-plural-compiler_add_support_for_huge_numbers branch from 519ab1f to e554504 Compare January 15, 2026 03:36
@graphite-app

graphite-app Bot commented Jan 15, 2026 •

Copy link
Copy Markdown

Merge activity

  • Jan 15, 3:57 AM UTC: longlho added this pull request to the Graphite merge queue.
  • Jan 15, 3:57 AM UTC: CI is running for this pull request on a draft pull request (#5912) due to your merge queue CI optimization settings.
  • Jan 15, 4:23 AM UTC: Merged by the Graphite merge queue via draft PR: #5912.

graphite-app Bot pushed a commit that referenced this pull request Jan 15, 2026
…ative implementation and add BigInt support (#5910)

### TL;DR

Removed `make-plural-compiler` dependency and implemented a custom plural rules compiler with improved BigInt support.

### What changed?

- Removed the `make-plural-compiler` dependency and its associated patch file
- Created a new custom `plural-rules-compiler.ts` implementation that handles CLDR plural rules
- Enhanced `ToIntlMathematicalValue` and `ToNumber` to properly support BigInt values
- Fixed edge cases in `ApplyUnsignedRoundingMode` for handling boundary values
- Updated `GetOperands` to handle very large integer values that exceed JavaScript's number limits
- Added comprehensive tests for BigInt and large number handling
- Updated Bazel dependencies to newer versions

### How to test?

1. Run the test suite with `pnpm test` to verify the new implementation works correctly
2. Test with BigInt values:
   ```js
   const pr = new Intl.PluralRules('en');
   console.log(pr.select(1n)); // "one"
   console.log(pr.select(2n)); // "other"

   const ordinal = new Intl.PluralRules('en', {type: 'ordinal'});
   console.log(ordinal.select(1n)); // "one"
   console.log(ordinal.select(2n)); // "two"
   console.log(ordinal.select(3n)); // "few"
   ```
3. Test with very large numbers:
   ```js
   console.log(pr.select(10000000000000n)); // "other"
   console.log(ordinal.select(10000000000001n)); // "one"
   ```

### Why make this change?

The `make-plural-compiler` dependency had limitations in handling BigInt values and very large numbers. This change improves compatibility with the ECMAScript specification by properly supporting BigInt values in `Intl.PluralRules.prototype.select()` as required by ECMA-402. The custom implementation also provides better handling of edge cases and maintains precision for very large numbers that exceed JavaScript's safe integer limits.
@graphite-app
graphite-app Bot changed the base branch from 01-14-test_formatjs_intl-displaynames_add_conformance_tests to graphite-base/5910 January 15, 2026 04:01
@longlho
longlho force-pushed the 01-14-feat_formatjs_intl-pluralrules_remove_make-plural-compiler_add_support_for_huge_numbers branch from e554504 to e933aa8 Compare January 15, 2026 04:13
@longlho
longlho force-pushed the graphite-base/5910 branch from 835e2c9 to 6f83aad Compare January 15, 2026 04:13
@longlho
longlho changed the base branch from graphite-base/5910 to main January 15, 2026 04:13
@graphite-app graphite-app Bot closed this Jan 15, 2026
@graphite-app
graphite-app Bot deleted the 01-14-feat_formatjs_intl-pluralrules_remove_make-plural-compiler_add_support_for_huge_numbers branch January 15, 2026 04:23
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