Skip to content

feat(universal): support @{ } code blocks at JSX child position - #1313

Merged
trueadm merged 2 commits into
octanejs:mainfrom
aleclarson:feat/universal-jsx-code-block
Sep 24, 2026
Merged

trueadm merged 2 commits into
octanejs:mainfrom
aleclarson:feat/universal-jsx-code-block

Conversation

@aleclarson

@aleclarson aleclarson commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Universal renderers now support @{ … } code blocks at JSX child position, matching the DOM childSlot contract: 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/@try bodies — previously emitted as a raw statement that made esrap throw Not 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 hit unsupported 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 — materializeScoped claims a persistent child owner at a stable identity path and executes a thunk inside it (what @if/@switch/@for/@try arms use) — so this is a small lowering + descriptor addition, not new machinery.

Changes

  • src/universal-core.ts: new UNIVERSAL_BLOCK kind, UniversalBlockValue, universalBlock(body) helper, and a materializeValue dispatch branch that mounts the body via materializeScoped at [...path, 'block']. The check sits after all existing $$kind branches so common control-flow dispatch pays nothing for the new kind. UniversalRenderable gains UniversalBlockValue.
  • src/compiler/compile-universal.js: compileChildAst handles JSXCodeBlock and {() => @{…}} via new compileCodeBlockAst, which reuses compileBlockValueAst for the setup+render thunk; isTemplateNode and the compileBlockValueAst template partition include JSXCodeBlock; the partition also unwraps ExpressionStatement-wrapped JSX-producing nodes (the parser's shape inside BlockStatement bodies); universalBlock is allocated/imported/registered in regionHelpers for both the module and region-lowering paths.
  • src/compiler/compile.js: transformUniversalParallelUse treats universalBlock's argument 0 as a thunk so use() inside block setup gets the same parallel-use treatment as @if arms.
  • Regression coverage: 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/@if body blocks, and effect create/cleanup on the block scope.

Validation

  • pnpm format:files <changed files> — clean
  • pnpm sync — clean tree afterwards
  • targeted tests: universal-code-block.test.ts (7 tests × dev+prod) — fails pre-fix with unsupported template node JSXCodeBlock, passes post-fix
  • neighboring suites: all universal-*.test.ts + tests/compiler/ — 3,423 passing; code-block-child.test.ts (DOM parity suite) green
  • pnpm test (repo-wide) — not run; scoped runs above cover the changed paths
  • pnpm typecheck (repo-wide) — not run; scoped typecheck:files on the changed src/ files is clean. Note: test fixtures in tests/ are excluded from the repo tsconfig; scoped tsrx-tsc reports the same pre-existing diagnostics for existing *.object.tsrx fixtures as for the new one.
  • Pre-existing environment failures: compiler/volar.test.ts and compiler/typescript-text-inference.test.ts hit their 5s/15s timeouts on this machine — reproduced on the clean upstream/main base (7 failures there), so unrelated.

Risk / follow-ups

  • New universalBlock export 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.
  • Hooks inside block setup resolve as call-order slots inside the claimed child owner (implicit:N resets per owner render pass), the same model @if arms already use. Conditional hooks in block setup are supported because the owner persists per position.
  • Render-prop position ({(s) => @{…}} passed to a component) compiles the block body into a universalValue the consumer materializes in its own scope; parameters bind lexically through the thunk.
  • Benchmark note: no measurable hot-path cost expected — one trailing $$kind check in materializeValue plus one small descriptor object + thunk closure per block per render, same shape as universalIf. Not benchmarked; happy to run a benchmark if wanted.

Provenance

  • An agent produced this diff (agent-authored)

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with 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 DOM childSlot behavior: empty blocks are omitted, render-only blocks fold into the parent template, and blocks with setup lower to a new universalBlock descriptor that runs via materializeScoped so hook state and effects live in a persistent child scope at the authored sibling position.

The universal compiler gains compileCodeBlockAst, treats JSXCodeBlock (including inside @if/@for bodies via ExpressionStatement unwrapping), and lowers {() => @{ … }} the same way while preserving arrow parameters. compile.js applies parallel-use() thunk handling to universalBlock. 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.

@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent feat PR: new feature or capability labels Sep 24, 2026
@aleclarson
aleclarson marked this pull request as ready for review September 24, 2026 19:21
@trueadm
trueadm merged commit b9b0bc4 into octanejs:main Sep 24, 2026
47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent feat PR: new feature or capability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants