From bbdeb7dc1dfd5e2c8a911a874811b6393ca2088b Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Thu, 3 Sep 2026 03:35:10 -0700 Subject: [PATCH] Inline the last duplicate Platform.select key (#58249) 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 https://github.com/react/react-native/issues/1889](https://github.com/react/metro/pull/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. Pull Request resolved: https://github.com/react/react-native/pull/58249 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. Reviewed By: christophpurrer Differential Revision: D118499490 Pulled By: vzaidman fbshipit-source-id: 3bd58c44c70415c57ed02266a5af2eea242471e4 --- .../src/__tests__/inline-platform-plugin-test.js | 4 ++++ .../react-native-babel-preset/src/inline-platform-plugin.js | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/react-native-babel-preset/src/__tests__/inline-platform-plugin-test.js b/packages/react-native-babel-preset/src/__tests__/inline-platform-plugin-test.js index 92bb4aac073..2e182e5f763 100644 --- a/packages/react-native-babel-preset/src/__tests__/inline-platform-plugin-test.js +++ b/packages/react-native-babel-preset/src/__tests__/inline-platform-plugin-test.js @@ -479,6 +479,10 @@ describe('Platform.select', () => { expect(select('{ios() { return 1; }}')).toContain('function'); }); + test('uses the last definition of a duplicate key', () => { + expect(select('{ios: 1, ios: 2}')).toContain('const value=2'); + }); + test('does not inline computed keys', () => { expect(select('{[key]: 1, default: 2}')).toContain('Platform.select'); }); diff --git a/packages/react-native-babel-preset/src/inline-platform-plugin.js b/packages/react-native-babel-preset/src/inline-platform-plugin.js index c818ff3331d..f70a24b3e95 100644 --- a/packages/react-native-babel-preset/src/inline-platform-plugin.js +++ b/packages/react-native-babel-preset/src/inline-platform-plugin.js @@ -430,7 +430,9 @@ module.exports = function inlinePlatformPlugin( key /*: string */, fallback /*: () => Node */, ) /*: Node */ { - for (const property of objectExpression.properties) { + // Object literal evaluation keeps the last definition of a duplicate key. + for (let i = objectExpression.properties.length - 1; i >= 0; i--) { + const property = objectExpression.properties[i]; if (!t.isObjectProperty(property) && !t.isObjectMethod(property)) { continue; }