Preserve Platform.select initializer side effects (#1903) - #1929
Open
vzaidman wants to merge 1 commit into
Open
Conversation
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
Contributor
|
@vzaidman has exported this pull request. If you are a Meta employee, you can view the originating Diff in D119919582. |
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:
Metro 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.
findPropertyalso clones the selectedObjectMethodbefore callingt.toExpression, sincetoExpressionmutates the node in place and bailing out on an impure sibling would otherwise leave the original AST mutated (e.g.ios() {}becomingfunction () {}), producing invalid output.Changelog:
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