Commit Graph

2 Commits

Author SHA1 Message Date
Boshen f13df97da3 fix(react_compiler): drop stray empty statement from catch bindings (#24133)
## Root cause

A `try`/`catch` with a catch parameter lowers the catch binding to a
`DeclareLocal` (and a `StoreLocal` in the handler) with
`InstructionKind::Catch`. In `emit_store` these codegen to an **empty
statement** — a faithful port of the fork's `emitStore` (`case
InstructionKind.Catch: return t.emptyStatement()`) — because the catch
parameter itself is emitted separately, from the `Try` terminal's
`handler_binding`, so the declaration has no textual form.

oxc's other instruction paths drop such empty statements
(`ox_codegen_instruction_nullable` filters `EmptyStatement` -> `None`),
but the `StoreLocal`/`DeclareLocal`/`DeclareContext`/`Destructure` path
returned the result of `ox_codegen_store_or_declare` **directly**, so
the catch declaration's empty statement was pushed into the block. It
surfaced as a stray `;`:

```js
bb0: {
  x = [];
  ;          // <- stray, from the catch `DeclareLocal`
  try { ... } catch (t1) { ... }
}
```

and, when the `try` was optimized away entirely, as a bare `;` in the
function body (`let x; ; return 42;`). The reference goldens
(`*.expect.md`) never contain such a `;`.

## Fix


`crates/oxc_react_compiler/src/react_compiler_reactive_scopes/codegen_reactive_function.rs`:

1. **Drop the empty statement** from the store/declare path in
`ox_codegen_instruction_nullable`, consistent with the other instruction
paths.
2. Removing it turned a labeled block that wrapped only a `try` into a
single-statement block, which tripped the labeled-block single-statement
**unwrap** and dropped the braces (`bb0: try {}` instead of `bb0: { try
{} catch {} }`). Restrict the unwrap so a labeled `try` keeps its braces
— matching the reference output, which always brace-wraps a labeled
`try` (the catch declaration originally padded the block to two
statements). Other single statements (`if`/`for`/`switch`/`while`,
nested labels such as `bb1: bb2: switch`) still unwrap as before.

## Fixtures changed

20 snapshots updated, each removing exactly one stray `;` (braces and
everything else preserved). ~29 `try`/`catch` fixtures now match the
fork golden at the compiled-component level. The change also cleaned two
non-`try-catch`-named fixtures that embed a `try`
(`repro-unreachable-code-early-return-in-useMemo`,
`error.bug-invariant-local-or-context-references`). The whole-suite diff
vs `main` is 20 files, all `-1` line; no snapshot regressed.

## Remaining work (out of scope, pre-existing)

The still-diverging `try`/`catch` fixtures differ only by codegen
formatting / error handling unrelated to the stray `;`:

- Prettier-style formatting: inline vs multi-line object/array literals
and `FIXTURE_ENTRYPOINT` params, multi-line vs inline JSX
(`try-catch-multiple-value-blocks`, `try-catch-within-object-method*`),
directive-comment placement (`propagate-scope-deps-hir-fork__*`,
`repro-nested-try-catch-in-usememo`).
- `finally` clauses and invalid-JSX-in-`catch` fixtures (`error.todo*`,
`invalid-jsx-*`, `fault-tolerance__error.try-finally-*`) where oxc's
error/`## Error` behavior diverges from the fork.
2026-07-04 10:45:11 +08:00
Boshen 6efce65746 test(react_compiler): snapshot the upstream fixture corpus (#24101)
## Summary

`oxc_react_compiler` had no committed test over the upstream React
Compiler fixture corpus — the fixtures lived only in the
`oxc-project/forked-react-compiler` checkout, exercised out-of-tree.
That gap let real regressions slip past the 15 in-crate integration
tests. This PR closes it.

- **`crates/oxc_react_compiler/fixtures/`** — 1734 fixture *input* files
(`.js`/`.ts`/`.tsx`/`.jsx`/`.mjs`), copied from the fork's
`babel-plugin-react-compiler/src/__tests__/fixtures/compiler` tree with
subdirectories preserved. Flow fixtures (the `@flow`-pragma ones) are
excluded — oxc has no Flow parser. The upstream `.expect.md` goldens are
also not copied; insta is the golden here.
- **`crates/oxc_react_compiler/tests/snapshot.rs`** — an `insta::glob!`
test that, per fixture, derives `PluginOptions`/`EnvironmentConfig` from
the first-line `// @directive` pragmas (base `compilationMode=all` /
`panicThreshold=all_errors`, matching the upstream `snap` runner),
parses → builds semantic → runs `transform`, and snapshots the compiled
output plus diagnostics. Unrecognised directives are ignored, as
upstream does; camelCase flag directives map to `EnvironmentConfig`
fields via `convert_case`.
- **`crates/oxc_react_compiler/tests/snapshots/`** — 1734 committed
`.snap` goldens (oxc's own output).
- **`.typos.toml`** — excludes the fixtures dir (the upstream fixtures
contain intentional typos; `**/*.snap` was already excluded).

These snapshots are oxc's own golden output, so `cargo test` (and CI)
now catches any change in compiler behaviour across the corpus.
Regenerate after an intended change with `cargo insta accept`.

## Verification

- `cargo test -p oxc_react_compiler` passes — the 1734-fixture snapshot
test plus the existing 15 integration tests; also green under
`--all-features` (matches the cross-target CI job).
- clippy `-D warnings`, `cargo fmt`, and `typos` clean.
- No fixture panics; 1336 compiled / 398 unchanged / 335 with
diagnostics.
- The `error.*` fixtures capture the exact diagnostics — including the
"Cannot reassign variable after render" error that a recent scoping
change had silently suppressed, confirming the suite catches real
regressions.

---

AI usage disclosure: authored with Claude Code and reviewed before
submission.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-07-03 16:49:53 +08:00