fix(compiler): make early-return guards reactive in component bodies#2991
Merged
viniciusdacal merged 1 commit intomainfrom Apr 23, 2026
Merged
fix(compiler): make early-return guards reactive in component bodies#2991viniciusdacal merged 1 commit intomainfrom
viniciusdacal merged 1 commit intomainfrom
Conversation
…2987] A component of the shape `if (cond) return <Loading/>; return <Ready/>;` stayed frozen at the guard branch because the body ran once at mount and never re-ran when `cond` flipped. This broke the documented loading-UX pattern (`query().loading` + early return). The compiler now detects the guard shape — N consecutive `if (cond) return <jsx>;` at the top of a component body, followed by a single trailing `return <jsx>;` — and rewrites the body to wrap the main return in a chain of `__conditional(() => cond, () => branch, () => fallback)` calls. The condition is re-evaluated reactively and the DOM swaps between branches without re-mounting the component. Guards with `else`, multi-statement blocks, or non-guard statements between guards are left alone for the per-return mount-frame wrapper to handle. Closes #2987. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
if (cond) return <jsx>; ... return <jsx>;guard shape in component bodies and rewrites the body so the main return is wrapped in a reactive__conditional(() => cond, () => branch, () => fallback)chain.if (data.loading) return <Loading/>; return <Ready/>;now swap to the ready branch whendata.loadingflips, instead of staying frozen at the guard forever.Public API Changes
No new or removed APIs. Behavior change only: the compiler's output structure for early-return guard components now uses
__conditionalso conditions are tracked reactively. This matches the documented loading-UX pattern.Test plan
native/vertz-compiler-core/src/reactive_guard_transformer.rscover simple/multi/braced guards, else branches (rejected), non-guard ifs (rejected), and non-guard components (unchanged).native/vertz-compiler/__tests__/reactive-guards.test.tsassert the emitted code has__conditional(() => cond, ...)and a single__flushMountFrame().packages/ui/src/__tests__/reactive-guard-runtime.test.tsmounts the compiler-emitted structure and verifies DOM swaps both directions, including nested guards.🤖 Generated with Claude Code