Skip to content

Preserve nested Platform write targets in Babel preset - #58252

Open
OskarEichler wants to merge 2 commits into
react:mainfrom
OskarEichler:codex/preserve-nested-platform-write-targets
Open

Preserve nested Platform write targets in Babel preset#58252
OskarEichler wants to merge 2 commits into
react:mainfrom
OskarEichler:codex/preserve-nested-platform-write-targets

Conversation

@OskarEichler

@OskarEichler OskarEichler commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Summary:

The React Native Babel preset replaces Platform.OS with a string literal even when the member expression is nested inside a destructuring assignment target. Direct and nested for...in / for...of targets are affected as well; direct loop targets can make Babel abort because a string literal is not a valid assignment target.

Walk the member expression's ancestors and preserve it whenever the containing expression is the left side of an assignment or loop, or the argument of an update/delete operation. This prevents invalid generated syntax and keeps write semantics intact. Metro has a parallel transform, so companion Metro PR #1890 applies the same guard there.

Changelog:

[GENERAL] [FIXED] - Preserve nested Platform.OS write targets in the Babel preset.

Test Plan:

  • Added regressions for array/object destructuring assignments and direct/nested for...in and for...of targets.
  • Exact baseline rewrites destructuring targets and fails Babel validation for a direct loop target; the fixed suite preserves every write target.
  • Full preset Jest passes: 4/4 suites, 112/112 tests, 16 snapshots.
  • Fresh Flow check reports 0 errors.
  • Targeted no-ignore ESLint, Prettier, and git diff --check pass.

No public API or intended read-transform behavior changes.

@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 Aug 30, 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 Aug 31, 2026
@OskarEichler

Copy link
Copy Markdown
Contributor Author

A second pass found that the ancestor walk treated every expression nested anywhere under an assignment LHS as a write. That incorrectly prevented inlining ordinary reads such as target[Platform.OS] = value and computed keys in destructuring targets. The walk now continues only through actual assignment-pattern containers and stops at regular expressions. Added regressions cover direct computed members, nested array targets, and computed object keys. The focused suite passes (53 tests / 11 snapshots), with targeted ESLint and Prettier checks passing.

@robhogan robhogan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

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