Commit b87344e
fix(babel-preset): preserve Platform.select initializers (#58350)
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(),
});
```
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 a regression test and ran existing tests, linter and formatter.
Reviewed By: GijsWeterings
Differential Revision: D119065685
Pulled By: vzaidman1 parent fea820f commit b87344e
2 files changed
Lines changed: 27 additions & 5 deletions
File tree
- packages/react-native-babel-preset/src
- __tests__
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
483 | 483 | | |
484 | 484 | | |
485 | 485 | | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
486 | 496 | | |
487 | 497 | | |
488 | 498 | | |
| |||
Lines changed: 17 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
520 | 520 | | |
521 | 521 | | |
522 | 522 | | |
523 | | - | |
524 | | - | |
525 | | - | |
526 | | - | |
527 | | - | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
528 | 526 | | |
529 | 527 | | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
530 | 542 | | |
531 | 543 | | |
532 | 544 | | |
| |||
0 commit comments