Skip to content

Preserve Platform.select initializer side effects (#1903) - #1929

Open
vzaidman wants to merge 1 commit into
mainfrom
export-D119919582
Open

Preserve Platform.select initializer side effects (#1903)#1929
vzaidman wants to merge 1 commit into
mainfrom
export-D119919582

Conversation

@vzaidman

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(),
});

OR

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. findProperty also clones the selected ObjectMethod before calling t.toExpression, since toExpression mutates the node in place and bailing out on an impure sibling would otherwise leave the original AST mutated (e.g. ios() {} becoming function () {}), producing invalid output.

Changelog:

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

Test Plan: Added regression tests, and ensured existing tests, linter and formatter pass. js1 test inline-plugin-test.js -- 64 passed. Verified the ObjectMethod bailout case emits the source unchanged instead of invalid JS.

Differential Revision: D119919582

Pulled By: vzaidman

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:

```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. `findProperty` also clones the selected `ObjectMethod` before calling `t.toExpression`, since `toExpression` mutates the node in place and bailing out on an impure sibling would otherwise leave the original AST mutated (e.g. `ios() {}` becoming `function () {}`), producing invalid output.

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


Test Plan: Added regression tests, and ensured existing tests, linter and formatter pass. js1 test inline-plugin-test.js -- 64 passed. Verified the ObjectMethod bailout case emits the source unchanged instead of invalid JS.

Differential Revision: D119919582

Pulled By: vzaidman
@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 14, 2026
@meta-codesync

meta-codesync Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

@vzaidman has exported this pull request. If you are a Meta employee, you can view the originating Diff in D119919582.

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. meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants