Skip to content

Preserve Platform.select initializer side effects - #1903

Closed
mfkrause wants to merge 1 commit into
react:mainfrom
mfkrause:fix/platform-select-side-effects
Closed

Preserve Platform.select initializer side effects#1903
mfkrause wants to merge 1 commit into
react:mainfrom
mfkrause:fix/platform-select-side-effects

Conversation

@mfkrause

@mfkrause mfkrause commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Metro 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:

Platform.select({
  ios: selected(),
  android: discarded(),
});

JavaScript would run both side effects, selected() and discarded(). The plugin's current behavior removes discarded however.

This fix preserves both side effects by testing for purity.

Changelog:

 - **[Fix]**: Preserve side effects from discarded `Platform.select` property initializers

Test plan

Added a regression test, and ensured existing tests, linter and formatter pass.

@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 Sep 4, 2026
@mfkrause mfkrause changed the title fix(metro-transform-plugins): preserve Platform.select initializers Preserve Platform.select initializer side effects Sep 4, 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 Sep 4, 2026
@vzaidman

vzaidman commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

why is the logic slightly different from react/react-native#58350 ?

edit: De Morgan's Rule

@meta-codesync

meta-codesync Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

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

@meta-codesync meta-codesync Bot closed this in d5d4e95 Sep 9, 2026
@meta-codesync meta-codesync Bot added the Merged label Sep 9, 2026
@meta-codesync

meta-codesync Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

@vzaidman merged this pull request in d5d4e95.

@facebook-github-tools

Copy link
Copy Markdown

This pull request has been reverted by 4c9b1cb.

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 Reverted 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.

2 participants