Skip to content

Inline the last duplicate Platform.select key - #58249

Closed
OskarEichler wants to merge 1 commit into
react:mainfrom
OskarEichler:codex/fix-inline-platform-duplicate-keys
Closed

Inline the last duplicate Platform.select key#58249
OskarEichler wants to merge 1 commit into
react:mainfrom
OskarEichler:codex/fix-inline-platform-duplicate-keys

Conversation

@OskarEichler

Copy link
Copy Markdown
Contributor

Summary:

The React Native Babel preset statically replaces Platform.select({...}) for the target platform. Its property scan currently stops at the first matching key, while JavaScript object-literal evaluation keeps the last duplicate definition. For example, Platform.select({ios: 1, ios: 2}) runs as 2 but the preset compiles it to 1.

Scan the already-validated static properties from the end so compiled output matches runtime semantics while preserving O(n), allocation-free lookup. Metro has a parallel transform that can run first; companion Metro PR #1889 applies the same correction so output remains transform-order independent.

Changelog:

[GENERAL] [FIXED] - Inline the last duplicate key from static Platform.select object literals.

Test Plan:

  • Added a focused preset regression; pristine main emits const value=1, while the fix emits const value=2.
  • Full preset Jest passes: 4/4 suites, 111/111 tests, 16 snapshots.
  • Fresh Flow check reports 0 errors.
  • Targeted no-ignore ESLint, Prettier, and git diff --check pass.

No behavior changes for object literals without duplicate static keys; no UI change.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 30, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Aug 30, 2026
@vzaidman

vzaidman commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Let's discuss this at react/metro#1889.

@meta-codesync

meta-codesync Bot commented Sep 2, 2026

Copy link
Copy Markdown

@vzaidman has imported this pull request. If you are a Meta employee, you can view this in D118499490.

@meta-codesync meta-codesync Bot closed this in bbdeb7d Sep 3, 2026
@meta-codesync meta-codesync Bot added the Merged This PR has been merged. label Sep 3, 2026
@meta-codesync

meta-codesync Bot commented Sep 3, 2026

Copy link
Copy Markdown

@vzaidman merged this pull request in bbdeb7d.

meta-codesync Bot pushed a commit to react/metro that referenced this pull request Sep 4, 2026
Summary:
Metro statically replaces `Platform.select({...})` for the target platform. Its property scan currently stops at the first matching key, while JavaScript object-literal evaluation keeps the last duplicate definition. For example, `Platform.select({ios: 1, ios: 2})` runs as `2` but Metro compiles it to `1`.

Scan the already-validated static properties from the end and return the first reverse match. This preserves O(n) behavior, avoids an unnecessary nullable temporary, and keeps compiled output consistent with runtime object semantics. The React Native Babel preset contains a parallel transform; companion [React Native PR #58249](react/react-native#58249) applies the same rule there.

Changelog:
```
 - **[Fix]**: Inline the last duplicate key from static `Platform.select` object literals
```

Pull Request resolved: #1889

Test Plan:
- Added a focused `metro-transform-plugins` regression; it fails on pristine main with `1` and passes with `2` after the fix.
- All package suites pass: 6/6 suites, 160/160 tests, 41 snapshots.
- Full Flow check reports no errors.
- Full monorepo build passes for all 17 packages.
- Targeted ESLint, Prettier, and `git diff --check` pass.

No behavior changes for object literals without duplicate static keys.

Reviewed By: GijsWeterings

Differential Revision: D118499357

Pulled By: vzaidman

fbshipit-source-id: 1036c78826c4e7d12a94baa8b8eeb1a04f9f8f60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants