Skip to content

[rust-compiler] Handle TS cast wrappers as assignment targets#36492

Merged
poteto merged 2 commits into
facebook:pr-36173from
poteto:rust-compiler-ts-cast-assignment-target
May 20, 2026
Merged

[rust-compiler] Handle TS cast wrappers as assignment targets#36492
poteto merged 2 commits into
facebook:pr-36173from
poteto:rust-compiler-ts-cast-assignment-target

Conversation

@poteto
Copy link
Copy Markdown
Collaborator

@poteto poteto commented May 19, 2026

A TS cast used as an assignment target, e.g. (obj.x as number) = 1, is a
valid Babel LVal (TSAsExpression, TSSatisfiesExpression,
TSNonNullExpression, TSTypeAssertion). The Rust port had no PatternLike
variants for these, so the NAPI AST JSON deserializer hard-failed the whole
file before compilation (Failed to parse AST JSON: unknown variant TSAsExpression). The reference TS compiler instead emits a graceful
FindContextIdentifiers Todo and skips just that function.

This adds the four variants to PatternLike, mirroring the existing
MemberExpression-as-LVal precedent, and makes find_context_identifiers
record the byte-identical Todo the reference emits. Every other PatternLike
match gets a minimal non-recording exhaustive arm; the AST visitor still
recurses into the inner expression so scope/identifier analysis is preserved.
No Unknown catch-all (the branch deliberately removed those).

The first commit snapshots the broken behavior: the fixture baseline records
the unexpected serde failure. The second commit applies the fix and flips the
baseline to the Todo bailout. After the fix the fixture passes under both
yarn snap and yarn snap --rust, and the rest of yarn snap --rust is
unchanged (no regression).

poteto added 2 commits May 19, 2026 14:10
…get failure

A TS cast used as an assignment target, e.g. `(obj.x as number) = 1`,
is a valid Babel LVal. The Rust port currently hard-fails at AST JSON
deserialization because PatternLike has no variant for the TS cast
wrappers. This fixture snapshots that broken behavior: the baseline
records the unexpected `Failed to parse AST JSON: unknown variant
TSAsExpression` error. The TS reference instead emits a graceful
FindContextIdentifiers Todo; the next commit makes Rust match it.
Babel's LVal includes TSAsExpression, TSSatisfiesExpression,
TSNonNullExpression and TSTypeAssertion (e.g. `(x as T) = ...`).
PatternLike had no variants for these, so the NAPI AST JSON
deserializer hard-failed before compilation.

Add the four variants to PatternLike, mirroring the existing
MemberExpression-as-LVal precedent. find_context_identifiers now
records the same graceful FindContextIdentifiers Todo the TS
reference emits for these targets; all other PatternLike matches get
minimal non-recording exhaustive arms (the visitor still recurses
into the inner expression).

The fixture baseline flips from the unexpected serde failure to the
Todo bailout, and now passes under both `yarn snap` and
`yarn snap --rust`.
@meta-cla meta-cla Bot added the CLA Signed label May 19, 2026
@poteto poteto merged commit 9a3f985 into facebook:pr-36173 May 20, 2026
21 of 26 checks passed
@poteto poteto deleted the rust-compiler-ts-cast-assignment-target branch May 20, 2026 16:18
poteto added a commit that referenced this pull request May 20, 2026
…ion test (#36499)

## What

`cargo test --workspace` (and `cargo test -p react_compiler_ast`)
currently fails to compile on `pr-36173`:

```
error[E0004]: non-exhaustive patterns: `&mut PatternLike::TSAsExpression(_)`,
  `&mut PatternLike::TSSatisfiesExpression(_)`, `&mut PatternLike::TSNonNullExpression(_)`
  and 1 more not covered
   --> crates/react_compiler_ast/tests/scope_resolution.rs:711:11
```

The `visit_pat` helper in the `scope_resolution` integration test was
not updated when #36492 added four TS cast-wrapper variants to
`PatternLike` (`TSAsExpression`, `TSSatisfiesExpression`,
`TSNonNullExpression`, `TSTypeAssertion`) to support TS cast wrappers as
assignment targets.

This PR adds those four arms to the test helper, mirroring exactly how
`visit_expr` in the same file already handles these same wrapper types:
descend into the inner `Expression` with `visit_expr`, and visit the
type annotation JSON (where present) with `visit_json`.

## Why

The Rust unit-test suite is currently zero-signal — `cargo test
--workspace` cannot even compile, let alone run. This change restores
it.

## Test plan

- [x] `cargo test --workspace` compiles cleanly
- [x] `cargo test --workspace` → 56 passed, 0 failed, 0 ignored
- [x] No changes to non-test code; isolated to one test helper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant