mirror of
https://github.com/react/react-native.git
synced 2026-09-28 13:23:09 +08:00
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