Repository navigation
feat(universal): support @{ } code blocks at JSX child position - #1313
Merged
Merged
Conversation
aleclarson
marked this pull request as ready for review
September 24, 2026 19:21
6 of 7 tasks
2 of 5 tasks
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
@{ … }code blocks at JSX child position, matching the DOMchildSlotcontract: empty blocks drop, render-only blocks merge into the parent template, and setup-bearing blocks run inside a persistent child scope at their authored sibling position.{() => @{ … }}(the explicit scoped-child spelling) lowers to the same form, keeping the arrow's parameters.@{ … }inside@if/@for/@switch/@trybodies — previously emitted as a raw statement that made esrap throwNot implemented: JSXCodeBlock— now compiles through the same path.Why
@{ … }at child position works on the DOM renderer but was rejected on universal renderers: direct children hitunsupported template node JSXCodeBlock, and blocks inside control-flow bodies leaked into generated code and crashed the printer. The universal runtime already had the required primitive —materializeScopedclaims a persistent child owner at a stable identity path and executes a thunk inside it (what@if/@switch/@for/@tryarms use) — so this is a small lowering + descriptor addition, not new machinery.Changes
src/universal-core.ts: newUNIVERSAL_BLOCKkind,UniversalBlockValue,universalBlock(body)helper, and amaterializeValuedispatch branch that mounts the body viamaterializeScopedat[...path, 'block']. The check sits after all existing$$kindbranches so common control-flow dispatch pays nothing for the new kind.UniversalRenderablegainsUniversalBlockValue.src/compiler/compile-universal.js:compileChildAsthandlesJSXCodeBlockand{() => @{…}}via newcompileCodeBlockAst, which reusescompileBlockValueAstfor the setup+render thunk;isTemplateNodeand thecompileBlockValueAsttemplate partition includeJSXCodeBlock; the partition also unwrapsExpressionStatement-wrapped JSX-producing nodes (the parser's shape insideBlockStatementbodies);universalBlockis allocated/imported/registered inregionHelpersfor both the module and region-lowering paths.src/compiler/compile.js:transformUniversalParallelUsetreatsuniversalBlock's argument 0 as a thunk souse()inside block setup gets the same parallel-use treatment as@ifarms.tests/universal-code-block.test.ts+_fixtures/universal-code-block.object.tsrx— sibling ordering, empty/render-only collapse, hook-state persistence across parent re-renders, host-instance retention,{() => @{…}},@for/@ifbody blocks, and effect create/cleanup on the block scope.Validation
pnpm format:files <changed files>— cleanpnpm sync— clean tree afterwardsuniversal-code-block.test.ts(7 tests × dev+prod) — fails pre-fix withunsupported template node JSXCodeBlock, passes post-fixuniversal-*.test.ts+tests/compiler/— 3,423 passing;code-block-child.test.ts(DOM parity suite) greenpnpm test(repo-wide) — not run; scoped runs above cover the changed pathspnpm typecheck(repo-wide) — not run; scopedtypecheck:fileson the changedsrc/files is clean. Note: test fixtures intests/are excluded from the repo tsconfig; scoped tsrx-tsc reports the same pre-existing diagnostics for existing*.object.tsrxfixtures as for the new one.compiler/volar.test.tsandcompiler/typescript-text-inference.test.tshit their 5s/15s timeouts on this machine — reproduced on the cleanupstream/mainbase (7 failures there), so unrelated.Risk / follow-ups
universalBlockexport joins the renderer ABI (octane/universal,octane/universal/native, and any third-party universal renderer module). Additive only: existing compiled output is unaffected, and modules compiled after this change need a runtime carrying the helper — normal for the renderer-module import pattern.implicit:Nresets per owner render pass), the same model@ifarms already use. Conditional hooks in block setup are supported because the owner persists per position.{(s) => @{…}}passed to a component) compiles the block body into auniversalValuethe consumer materializes in its own scope; parameters bind lexically through the thunk.$$kindcheck inmaterializeValueplus one small descriptor object + thunk closure per block per render, same shape asuniversalIf. Not benchmarked; happy to run a benchmark if wanted.Provenance
agent-authored)Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.Note
Medium Risk
Adds compiler lowering and a new renderer ABI helper (
universalBlock) that affects hook/effect scoping for child-position blocks; changes are additive but touch universal materialization and generated module imports.Overview
Universal renderers now compile
@{ … }at JSX child position, aligning with the DOMchildSlotbehavior: empty blocks are omitted, render-only blocks fold into the parent template, and blocks with setup lower to a newuniversalBlockdescriptor that runs viamaterializeScopedso hook state and effects live in a persistent child scope at the authored sibling position.The universal compiler gains
compileCodeBlockAst, treatsJSXCodeBlock(including inside@if/@forbodies viaExpressionStatementunwrapping), and lowers{() => @{ … }}the same way while preserving arrow parameters.compile.jsapplies parallel-use()thunk handling touniversalBlock. Regression tests cover ordering, scope persistence, control-flow bodies, and effect cleanup.Reviewed by Cursor Bugbot for commit cd6b42f. Bugbot is set up for automated code reviews on this repo. Configure here.