Skip to content

[compiler] Add Intl formatter types and fix moduleTypeProvider fallback#617

Open
everettbu wants to merge 1 commit into
mainfrom
pr35906
Open

[compiler] Add Intl formatter types and fix moduleTypeProvider fallback#617
everettbu wants to merge 1 commit into
mainfrom
pr35906

Conversation

@everettbu

Copy link
Copy Markdown

Mirror of facebook/react#35906
Original author: josephsavona


Add type definitions for all Intl formatter objects (DateTimeFormat,
NumberFormat, Collator, PluralRules, ListFormat, RelativeTimeFormat,
Segmenter, DisplayNames) so the compiler understands that formatter
instances are immutable and their methods only read arguments.

Without these types, new Intl.DateTimeFormat().format(date) would
conservatively assume the format call captures date into the
formatter, creating an unnecessary dependency.

Also fix #resolveModuleType to always fall back to
defaultModuleTypeProvider when a custom moduleTypeProvider returns
null, so that tools like the snap runner that set their own provider
still get the default module types (react-hook-form, tanstack, etc.).

Add type definitions for all Intl formatter objects (DateTimeFormat,
NumberFormat, Collator, PluralRules, ListFormat, RelativeTimeFormat,
Segmenter, DisplayNames) so the compiler understands that formatter
instances are immutable and their methods only read arguments.

Without these types, `new Intl.DateTimeFormat().format(date)` would
conservatively assume the format call captures `date` into the
formatter, creating an unnecessary dependency.

Also fix `#resolveModuleType` to always fall back to
`defaultModuleTypeProvider` when a custom `moduleTypeProvider` returns
null, so that tools like the snap runner that set their own provider
still get the default module types (react-hook-form, tanstack, etc.).
@everettbu everettbu added CLA Signed React Core Team Opened by a member of the React Core Team labels Feb 25, 2026
@greptile-apps

greptile-apps Bot commented Feb 25, 2026

Copy link
Copy Markdown

Greptile Summary

This PR adds type definitions for all 8 Intl formatter objects (DateTimeFormat, NumberFormat, Collator, PluralRules, ListFormat, RelativeTimeFormat, Segmenter, DisplayNames) to the React Compiler's type system, and fixes a fallback bug in moduleTypeProvider resolution.

  • Intl type definitions: Each formatter is modeled as a Frozen (immutable) object whose methods only Read their arguments, preventing the compiler from conservatively assuming that calls like formatter.format(date) capture their arguments into the formatter instance. Method return types correctly distinguish between primitives (format, compare, select, of) and mutable objects (formatToParts, formatRangeToParts, resolvedOptions).
  • moduleTypeProvider fallback fix: Previously, when a custom moduleTypeProvider was configured, it completely replaced the defaultModuleTypeProvider. If the custom provider returned null for a module, the default provider was never consulted. The fix chains the two: custom provider is tried first, and if it returns null, the default provider is used as a fallback. This ensures tools (like the snap runner) that set their own provider still get built-in module type definitions for libraries like react-hook-form and tanstack.
  • Test coverage: Includes a fixture test for Intl.DateTimeFormat demonstrating the compiler correctly memoizes the formatter instance and computes format(date) per render.

Confidence Score: 5/5

  • This PR is safe to merge — it adds well-structured type definitions following established patterns and fixes a clear fallback bug with correct logic.
  • Both changes are straightforward and correct. The Intl type definitions follow the exact same patterns used for existing built-ins (Map, Set, Array) and accurately model the immutable nature of Intl formatter instances. The moduleTypeProvider fix correctly chains custom → default fallback using nullish coalescing. A test fixture validates the key behavior.
  • No files require special attention.

Important Files Changed

Filename Overview
compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts Fixes moduleTypeProvider fallback so custom providers that return null still fall through to defaultModuleTypeProvider for built-in module types.
compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts Adds Intl as a typed global with constructors for all 8 Intl formatter types (DateTimeFormat, NumberFormat, Collator, PluralRules, ListFormat, RelativeTimeFormat, Segmenter, DisplayNames), each returning Frozen instances.
compiler/packages/babel-plugin-react-compiler/src/HIR/ObjectShape.ts Defines built-in IDs and instance shapes for all 8 Intl formatter types with correct method signatures (format, formatToParts, resolvedOptions, etc.) that only read their arguments.
compiler/packages/babel-plugin-react-compiler/src/tests/fixtures/compiler/global-types/intl-datetimeformat.expect.md Expected compiler output showing Intl.DateTimeFormat correctly memoized as a sentinel-guarded singleton, with format(date) computed per render.
compiler/packages/babel-plugin-react-compiler/src/tests/fixtures/compiler/global-types/intl-datetimeformat.ts New test fixture exercising Intl.DateTimeFormat construction and format() calls in a React component.

Last reviewed commit: 49bbeb9

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

5 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants