fix(babel-preset): preserve Platform.select initializers (#58350) - #58442
Open
vzaidman wants to merge 1 commit into
Open
fix(babel-preset): preserve Platform.select initializers (#58350)#58442vzaidman wants to merge 1 commit into
vzaidman wants to merge 1 commit into
Conversation
Summary:
The React Native Babel preset replaces `Platform.select({...})` with the selected property during production transforms. JavaScript evaluates every object property initializer before calling `Platform.select`, so this can silently remove side effects from non-selected properties.
For example:
```js
Platform.select({
ios: selected(),
android: discarded(),
});
```
OR
```js
Platform.select({
ios() {
selected()
},
android: discarded(),
});
```
JavaScript would run both side effects, `selected()` and `discarded()`. The plugin's current behavior removes `discarded` on iOS however.
This fix preserves both side effects by testing for purity.
## Changelog:
[GENERAL] [FIXED] - Preserve side effects from discarded Platform.select property initializers in the Babel preset.
Test Plan: Added regression tests and ran existing tests, linter and formatter. Verified the ObjectMethod bailout case emits the source unchanged instead of invalid JS.
Differential Revision: D119484692
Pulled By: vzaidman
|
@vzaidman has exported this pull request. If you are a Meta employee, you can view the originating Diff in D119484692. |
This was referenced Sep 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
The React Native Babel preset replaces
Platform.select({...})with the selected property during production transforms. JavaScript evaluates every object property initializer before callingPlatform.select, so this can silently remove side effects from non-selected properties.For example:
OR
JavaScript would run both side effects,
selected()anddiscarded(). The plugin's current behavior removesdiscardedon iOS however.This fix preserves both side effects by testing for purity.
Changelog:
[GENERAL] [FIXED] - Preserve side effects from discarded Platform.select property initializers in the Babel preset.
Test Plan: Added regression tests and ran existing tests, linter and formatter. Verified the ObjectMethod bailout case emits the source unchanged instead of invalid JS.
Differential Revision: D119484692
Pulled By: vzaidman