@barefootjs/jsx 0.26.4 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/dist/compiler.d.ts.map +1 -1
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +321 -104
  5. package/dist/ir-to-client-js/client-only-elision.d.ts +103 -0
  6. package/dist/ir-to-client-js/client-only-elision.d.ts.map +1 -0
  7. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  8. package/dist/ir-to-client-js/control-flow/shared.d.ts +1 -1
  9. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +54 -0
  10. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -0
  11. package/dist/ir-to-client-js/control-flow/stringify/inner-loop.d.ts.map +1 -1
  12. package/dist/ir-to-client-js/control-flow/stringify/insert.d.ts.map +1 -1
  13. package/dist/ir-to-client-js/control-flow/stringify/loop-child-arm.d.ts.map +1 -1
  14. package/dist/ir-to-client-js/control-flow/stringify/loop.d.ts +17 -20
  15. package/dist/ir-to-client-js/control-flow/stringify/loop.d.ts.map +1 -1
  16. package/dist/ir-to-client-js/control-flow/stringify/reactive-effects.d.ts +63 -4
  17. package/dist/ir-to-client-js/control-flow/stringify/reactive-effects.d.ts.map +1 -1
  18. package/dist/ir-to-client-js/control-flow.d.ts +1 -1
  19. package/dist/ir-to-client-js/element-refs.d.ts +8 -3
  20. package/dist/ir-to-client-js/element-refs.d.ts.map +1 -1
  21. package/dist/ir-to-client-js/emit-reactive.d.ts +8 -1
  22. package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
  23. package/dist/ir-to-client-js/html-template.d.ts +24 -0
  24. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  25. package/dist/ir-to-client-js/imports.d.ts +2 -2
  26. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  27. package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
  28. package/dist/ir-to-client-js/types.d.ts +9 -1
  29. package/dist/ir-to-client-js/types.d.ts.map +1 -1
  30. package/dist/ir-to-client-js/utils.d.ts +1 -1
  31. package/dist/types.d.ts +25 -2
  32. package/dist/types.d.ts.map +1 -1
  33. package/package.json +2 -2
  34. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +398 -671
  35. package/src/__tests__/child-components-in-map.test.ts +4 -4
  36. package/src/__tests__/client-js-generation.test.ts +19 -11
  37. package/src/__tests__/composite-branch-loop.test.ts +2 -2
  38. package/src/__tests__/cross-file-client-signal.test.ts +5 -1
  39. package/src/__tests__/early-return-scope-var-ref.test.ts +10 -11
  40. package/src/__tests__/inline-jsx-callback.test.ts +6 -4
  41. package/src/__tests__/ir-jsx-props.test.ts +6 -5
  42. package/src/__tests__/loop-fallback-wrap.test.ts +3 -4
  43. package/src/__tests__/module-level-signal.audit.test.ts +7 -2
  44. package/src/__tests__/nested-loop-conditional.test.ts +9 -7
  45. package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
  46. package/src/__tests__/preamble-region-patch.test.ts +18 -12
  47. package/src/__tests__/profile-cond-binding-ids.test.ts +3 -2
  48. package/src/__tests__/profile-loop-binding-ids.test.ts +4 -3
  49. package/src/__tests__/profile-nested-binding-ids.test.ts +11 -6
  50. package/src/__tests__/reactive-attrs-in-map.test.ts +6 -4
  51. package/src/__tests__/template-closure.test.ts +1 -1
  52. package/src/compiler.ts +11 -0
  53. package/src/index.ts +1 -0
  54. package/src/ir-to-client-js/client-only-elision.ts +273 -0
  55. package/src/ir-to-client-js/collect-elements.ts +8 -4
  56. package/src/ir-to-client-js/control-flow/shared.ts +1 -1
  57. package/src/ir-to-client-js/control-flow/stringify/branch-loop.ts +12 -4
  58. package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +68 -0
  59. package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +18 -9
  60. package/src/ir-to-client-js/control-flow/stringify/insert.ts +17 -11
  61. package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +35 -16
  62. package/src/ir-to-client-js/control-flow/stringify/loop.ts +48 -46
  63. package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +241 -31
  64. package/src/ir-to-client-js/control-flow.ts +1 -1
  65. package/src/ir-to-client-js/element-refs.ts +9 -11
  66. package/src/ir-to-client-js/emit-reactive.ts +50 -14
  67. package/src/ir-to-client-js/html-template.ts +29 -9
  68. package/src/ir-to-client-js/imports.ts +7 -4
  69. package/src/ir-to-client-js/reactivity.ts +3 -2
  70. package/src/ir-to-client-js/types.ts +10 -2
  71. package/src/ir-to-client-js/utils.ts +1 -1
  72. package/src/jsx-to-ir.ts +8 -7
  73. package/src/types.ts +25 -2
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Stringify LoopPlan variants to source lines.
3
3
  *
4
- * Output shapes (preserved byte-identical from the legacy emitter):
4
+ * Output shapes:
5
5
  *
6
6
  * PlainLoopPlan, no reactive effects (single-line renderItem):
7
7
  * <indent>mapArray(() => <arr>, <container>, <keyFn>, (<head>, <idx>, __existing) =>
@@ -12,13 +12,18 @@
12
12
  * <indent> <unwrap?>
13
13
  * <indent> <preamble?>
14
14
  * <indent> const __el = __existing ?? (() => { ... })()
15
- * <indent> <reactive effects via stringifyReactiveEffects>
15
+ * <indent> <reactive effects + preamble regions via stringifyReactiveEffects
16
+ * <indent> row-granularity (spec/slot-unification.md §3(c)): attrs, outer
17
+ * <indent> texts, and preamble regions merge into ONE createEffect per row
18
+ * <indent> in normal builds; profile-mode builds keep the legacy
19
+ * <indent> one-effect-per-slot shape (see that module's docstring)>
16
20
  * <indent> return __el
17
21
  * <indent>})
18
22
  *
19
23
  * StaticLoopPlan: two parallel forEach blocks (attrs / texts) — see
20
24
  * emitStaticLoop. The forEach-duplication noted in observation O-4 is
21
- * preserved bug-for-bug; PR 5+ collapses them.
25
+ * preserved bug-for-bug; PR 5+ collapses them. Left untouched by the
26
+ * row-granularity pass above — a different (forEach, not mapArray) shape.
22
27
  *
23
28
  * Indent convention for plain loops: top-level emission uses `' '` (2 sp);
24
29
  * passed in via `topIndent`.
@@ -31,8 +36,8 @@ import { emitTemplateCloneInline, emitLoopItemElementSetup, emitHoistedTemplateD
31
36
  import { buildSkeletonPathPlan, type SkeletonPathPlan } from './skeleton-paths.ts'
32
37
  import { stringifyComponentLoop } from './component-loop.ts'
33
38
  import { stringifyCompositeLoop } from './composite-loop.ts'
39
+ import { claimPlanLiteral, claimWriterVarName, type ClaimSlotSpec } from './claim-plan.ts'
34
40
  import type { LoopChildRefBinding, LoopPlan, PlainLoopPlan, StaticLoopPlan } from '../plan/types.ts'
35
- import type { PreambleRegionPlan } from '../plan/loop.ts'
36
41
 
37
42
  /**
38
43
  * Emit `(callback)(__rf)` for each ref on a per-item slot, looking up the
@@ -66,38 +71,16 @@ export function emitLoopChildRefs(
66
71
  }
67
72
 
68
73
  /**
69
- * Emit the region-patch effect for each preamble-patched region (#2389
70
- * `arr.map(t => { const cells = []; ...; return <tr>{cells}<td>{t.name}</td></tr> })`).
71
- * The effect re-runs the (loop-param-accessor-wrapped) preamble on every
72
- * reactive tick so it re-reads the current per-item signal, recomputes the
73
- * region's value, and past the FIRST run (which only records, trusting
74
- * the SSR/CSR mount-time content already in the DOM) — patches the DOM
75
- * range via `patchSlotRange(__el, 'sN', html)`. The marker lookup lives
76
- * inside `patchSlotRange`, so a row that never changes pays zero lookup
77
- * cost at mount/adoption.
78
- *
79
- * Non-empty `regions` forces the caller's multi-line renderItem layout (the
80
- * effect needs `__el` as a query root), mirroring `emitLoopChildRefs`.
74
+ * Preamble-patched regions (#2389 `arr.map(t => { const cells = []; ...;
75
+ * return <tr>{cells}<td>{t.name}</td></tr> })`) used to get their own
76
+ * per-region `createEffect` here. Row-granularity effects (perf,
77
+ * spec/slot-unification.md §3(c)/§8) merged that into the SAME single row
78
+ * effect `stringifyReactiveEffects` now emits for attrs/outer texts see
79
+ * that module's docstring. Both plain-loop-row call sites
80
+ * (`stringifyPlainLoop` below, `stringify/branch-loop.ts`'s `emitPlain`) pass
81
+ * `preambleRegions`/`mapPreambleWrapped` straight into
82
+ * `stringifyReactiveEffects` instead of calling a separate emitter.
81
83
  */
82
- export function emitPreambleRegionEffects(
83
- lines: string[],
84
- regions: readonly PreambleRegionPlan[],
85
- mapPreambleWrapped: string,
86
- opts: { indent: string; elVar: string },
87
- ): void {
88
- if (regions.length === 0) return
89
- const { indent, elVar } = opts
90
- for (const region of regions) {
91
- const v = varSlotId(region.slotId)
92
- lines.push(`${indent}{ let __last_${v}`)
93
- lines.push(`${indent}createEffect(() => {`)
94
- if (mapPreambleWrapped) lines.push(`${indent} ${mapPreambleWrapped}`)
95
- lines.push(`${indent} const __html_${v} = ${region.valueExpr}`)
96
- lines.push(`${indent} if (__last_${v} === undefined) { __last_${v} = __html_${v}; return }`)
97
- lines.push(`${indent} if (__html_${v} !== __last_${v}) { __last_${v} = __html_${v}; patchSlotRange(${elVar}, '${region.slotId}', __html_${v}) }`)
98
- lines.push(`${indent}}) }`)
99
- }
100
- }
101
84
 
102
85
  /**
103
86
  * Single dispatch over `LoopPlan` (#1253). Narrows on `plan.kind` and
@@ -254,33 +237,49 @@ export function stringifyPlainLoop(
254
237
  // `reactiveEffects.conditionals` is always empty here and needs no path
255
238
  // handling. `__p` is `null` on the hydration branch (`__existing` truthy)
256
239
  // since the skeleton's empty markers/omitted attrs don't describe the
257
- // SSR-rendered tree; every resolution falls back to qsa/$t there.
240
+ // SSR-rendered tree; every resolution falls back to qsa/$t there. Attr/ref
241
+ // slots still resolve through `__p` (unchanged by slot unification A3 —
242
+ // only CONTENT slots migrate to claim plans); text slots below embed the
243
+ // same skeleton path directly into their claim-plan entry, guarded by the
244
+ // same `__existing ? [] : […]` ternary so a hydrated row never tries a
245
+ // path the skeleton doesn't actually describe.
258
246
  let pathPlan: SkeletonPathPlan | null = null
259
247
  if (hoistedTpl && plan.skeletonPaths) {
260
248
  const elementSlotIds = [
261
249
  ...(reactiveEffects?.attrSlots.map(s => s.slotId) ?? []),
262
250
  ...childRefs.map(r => r.childSlotId),
263
251
  ]
264
- const textSlotIds = reactiveEffects?.outerTexts.map(t => t.slotId) ?? []
265
- if (elementSlotIds.length > 0 || textSlotIds.length > 0) {
266
- const built = buildSkeletonPathPlan(plan.skeletonPaths, '__el', { elementSlotIds, textSlotIds })
252
+ if (elementSlotIds.length > 0) {
253
+ const built = buildSkeletonPathPlan(plan.skeletonPaths, '__el', { elementSlotIds, textSlotIds: [] })
267
254
  if (built.arrayElems.length > 0) {
268
255
  pathPlan = built
269
256
  lines.push(`${bodyIndent}const __p = __existing ? null : [${built.arrayElems.join(', ')}]`)
270
257
  }
271
258
  }
272
259
  }
273
- if (reactiveEffects !== null) {
260
+ const textClaimPathExprs = new Map<string, string>()
261
+ if (hoistedTpl && plan.skeletonPaths) {
262
+ for (const text of reactiveEffects?.outerTexts ?? []) {
263
+ const path = plan.skeletonPaths.textMarkerPaths.get(text.slotId)
264
+ if (path) textClaimPathExprs.set(text.slotId, `__existing ? [] : [${path.join(', ')}]`)
265
+ }
266
+ }
267
+ if (reactiveEffects !== null || preambleRegions.length > 0) {
268
+ // Row-granularity effects (§3(c)): attrs, outer texts, AND preamble
269
+ // regions all merge into the SAME single row `createEffect` — pass
270
+ // `preambleRegions`/`mapPreambleWrapped` straight through instead of a
271
+ // separate emitter call (see `stringifyReactiveEffects`'s docstring).
274
272
  stringifyReactiveEffects(lines, reactiveEffects, {
275
273
  indent: bodyIndent,
276
274
  elVar: '__el',
277
275
  bodyIsMultiRoot,
278
276
  elementIndexBySlot: pathPlan?.elementIndexBySlot,
279
- textIndexBySlot: pathPlan?.textIndexBySlot,
277
+ textClaimPathExprs,
278
+ preambleRegions,
279
+ mapPreambleWrapped,
280
280
  })
281
281
  }
282
282
  emitLoopChildRefs(lines, childRefs, { indent: bodyIndent, elVar: '__el', bodyIsMultiRoot, elementIndexBySlot: pathPlan?.elementIndexBySlot })
283
- emitPreambleRegionEffects(lines, preambleRegions, mapPreambleWrapped, { indent: bodyIndent, elVar: '__el' })
284
283
  lines.push(`${bodyIndent}return __el`)
285
284
  lines.push(`${topIndent}}, '${markerId}'${loopBfId})`)
286
285
  }
@@ -433,10 +432,13 @@ export function stringifyStaticLoop(lines: string[], plan: StaticLoopPlan): void
433
432
  }
434
433
  lines.push(` }`)
435
434
  }
436
- for (const text of texts) {
437
- const vn = `__rt_${varSlotId(text.slotId)}`
438
- lines.push(` { const [${vn}] = $t(__iterEl, '${text.slotId}')`)
439
- lines.push(` if (${vn}) createEffect(() => { ${vn}.textContent = String(${text.expression}) }${profileBindingId(pc, text.slotId)}) }`)
435
+ if (texts.length > 0) {
436
+ const slots: ClaimSlotSpec[] = texts.map(t => ({ id: t.slotId, kind: 'text', path: [] }))
437
+ const writer = claimWriterVarName(slots, varSlotId)
438
+ lines.push(` const ${writer} = lazySlots(__iterEl, ${claimPlanLiteral(slots)})`)
439
+ for (const text of texts) {
440
+ lines.push(` createEffect(() => { ${writer}('${text.slotId}', String(${text.expression})) }${profileBindingId(pc, text.slotId)})`)
441
+ }
440
442
  }
441
443
  // Ref callbacks fire on every forEach iteration — initial mount and any
442
444
  // future array-change-driven re-iteration (#1244). For static arrays the
@@ -6,24 +6,58 @@
6
6
  * bodies (events, child component inits, inner loops, nested conditionals,
7
7
  * branch-scoped texts) flow through the per-arm stringifiers in
8
8
  * `loop-child-arm.ts` — no legacy passthrough remains.
9
+ *
10
+ * Row-granularity effects (perf, spec/slot-unification.md §3(c)/§8, the
11
+ * deferred A3b): for the "plain loop row, top-level or branch-scoped" shape
12
+ * — the only shape where `stringifyReactiveEffects` is called alongside
13
+ * `PreambleRegionPlan`s from the SAME renderItem scope (`stringify/loop.ts`'s
14
+ * `stringifyPlainLoop`, `stringify/branch-loop.ts`'s `emitPlain`) — reactive
15
+ * attrs, outer texts, and preamble regions all read the SAME per-item signal
16
+ * dependency, so consolidating them into ONE `createEffect` per row removes
17
+ * N-1 effect objects and subscription-list entries per row without changing
18
+ * the firing profile. Composite loops, component loops, and the anchored
19
+ * (whole-item-conditional) loop shape keep the legacy one-effect-per-slot
20
+ * emission — their reactiveEffects never carry preamble regions, and
21
+ * consolidating them wasn't part of this pass's mechanically-verified scope
22
+ * (see the call sites' docstrings).
23
+ *
24
+ * Profile mode (#1690) carries a `<Component>#binding:<slotId>` id per
25
+ * effect so the profiler attributes a re-run to its source binding — merging
26
+ * bindings into one effect would make every binding on the row share one id.
27
+ * Resolution: profile mode (`plan.profileComponentName` set) keeps the
28
+ * granular per-slot/per-attr effect emission (`emitAttrSlotsGranular` /
29
+ * `emitOuterTexts` below); normal builds get the consolidated row effect.
30
+ * Preamble regions never carried a per-region bfId even before this pass, so
31
+ * they always consolidate into one effect regardless of profile mode.
9
32
  */
10
33
 
11
34
  import { varSlotId, profileBindingId } from '../../utils.ts'
12
35
  import { emitAttrUpdate } from '../../emit-reactive.ts'
13
36
  import { stringifyLoopChildArm } from './loop-child-arm.ts'
37
+ import { claimPlanLiteral, claimWriterVarName, type ClaimSlotSpec } from './claim-plan.ts'
14
38
  import type {
15
39
  NestedConditionalPlan,
40
+ ReactiveAttrSlot,
16
41
  ReactiveEffectsPlan,
17
42
  ReactiveTextEffect,
18
43
  } from '../plan/reactive-effects.ts'
19
44
 
45
+ /** A resolved preamble-patched region (#2389) ready to merge into the row
46
+ * effect — same shape as `plan/loop.ts`'s `PreambleRegionPlan`, restated
47
+ * here so this module doesn't need a cross-import into the loop plan file
48
+ * for one two-field shape. */
49
+ export interface RowPreambleRegion {
50
+ slotId: string
51
+ valueExpr: string
52
+ }
53
+
20
54
  export interface StringifyReactiveEffectsOptions {
21
55
  /** Indent prefix for every emitted line. */
22
56
  indent: string
23
57
  /**
24
58
  * Element variable to attach effects to (e.g., `__el`, `__existing`,
25
59
  * `__csrEl`). The stringifier never inspects it — it is simply substituted
26
- * into the qsa() / $t() / insert() call shapes.
60
+ * into the qsa() / lazySlots() / insert() call shapes.
27
61
  */
28
62
  elVar: string
29
63
  /**
@@ -42,27 +76,88 @@ export interface StringifyReactiveEffectsOptions {
42
76
  * still resolves through the battle-tested runtime lookup.
43
77
  */
44
78
  elementIndexBySlot?: ReadonlyMap<string, number>
45
- /** Same as `elementIndexBySlot`, for text-marker slots (`$t`). */
46
- textIndexBySlot?: ReadonlyMap<string, number>
79
+ /**
80
+ * Rendered path EXPRESSION source (not a plain array — see
81
+ * `ClaimSlotSpec.pathExpr`) for outer text slots, keyed by slotId: perf,
82
+ * #2143's hoisted single-root loop skeleton
83
+ * (`SkeletonSlotPaths.textMarkerPaths`), guarded by `__existing` the same
84
+ * way `__p` is nulled on the hydration branch (the skeleton's simplified
85
+ * markup doesn't describe the real SSR-rendered tree). Only
86
+ * `stringifyPlainLoop`'s hoisted-skeleton path supplies these; every other
87
+ * caller omits it and each text slot's claim-plan entry gets `path: []`
88
+ * — A2's marker-scan fallback resolves it at claim time (sound, just
89
+ * slower), per `spec/slot-unification.md` §5-A3's explicit "cannot be
90
+ * statically pathed" allowance.
91
+ */
92
+ textClaimPathExprs?: ReadonlyMap<string, string>
93
+ /**
94
+ * Preamble-patched regions (#2389) to merge into the SAME row effect as
95
+ * this scope's reactive attrs/outer texts (row-granularity effects,
96
+ * §3(c)) — only supplied by the plain-loop-row call sites
97
+ * (`stringify/loop.ts`, `stringify/branch-loop.ts`) where
98
+ * `PlainLoopVariant.preambleRegions` exists on the plan. Every other
99
+ * caller omits this and gets the legacy per-slot emission untouched.
100
+ */
101
+ preambleRegions?: readonly RowPreambleRegion[]
102
+ /**
103
+ * The loop callback's pre-return preamble (already wrapped with the loop
104
+ * param accessor), re-run once per row-effect tick — BEFORE any preamble
105
+ * region read — so `preambleRegions`' `valueExpr`s see freshly recomputed
106
+ * locals. Only meaningful alongside `preambleRegions`.
107
+ */
108
+ mapPreambleWrapped?: string
47
109
  }
48
110
 
49
111
  export function stringifyReactiveEffects(
50
112
  lines: string[],
51
- plan: ReactiveEffectsPlan,
113
+ plan: ReactiveEffectsPlan | null,
52
114
  opts: StringifyReactiveEffectsOptions,
53
115
  ): void {
54
- const { indent, elVar, bodyIsMultiRoot, elementIndexBySlot, textIndexBySlot } = opts
116
+ const { indent, elVar, bodyIsMultiRoot, elementIndexBySlot, textClaimPathExprs, preambleRegions = [], mapPreambleWrapped } = opts
55
117
  const lookup = bodyIsMultiRoot ? 'qsaItem' : 'qsa'
56
- const pc = plan.profileComponentName
118
+ const pc = plan?.profileComponentName
57
119
  const bindingBfId = (slotId: string): string => profileBindingId(pc, slotId)
120
+ const attrSlots = plan?.attrSlots ?? []
121
+ const outerTexts = plan?.outerTexts ?? []
122
+ const conditionals = plan?.conditionals ?? []
123
+
124
+ if (pc) {
125
+ // Profile mode: preserve the granular per-slot/per-attr effect shape so
126
+ // every binding keeps its own bfId (see module docstring).
127
+ emitAttrSlotsGranular(lines, indent, elVar, lookup, attrSlots, elementIndexBySlot, bindingBfId)
128
+ emitOuterTexts(lines, indent, elVar, outerTexts, bindingBfId, textClaimPathExprs)
129
+ emitPreambleRegionsEffect(lines, indent, elVar, preambleRegions, mapPreambleWrapped)
130
+ } else {
131
+ emitConsolidatedRowEffect(
132
+ lines, indent, elVar, lookup,
133
+ attrSlots, outerTexts, elementIndexBySlot, textClaimPathExprs,
134
+ preambleRegions, mapPreambleWrapped,
135
+ )
136
+ }
137
+
138
+ // Reactive conditionals — each emits an insert(...) over `elVar` whose arm
139
+ // bodies dispatch through the per-arm stringifiers. Structurally distinct
140
+ // from the attrs/texts/regions merge above (its own per-branch disposable
141
+ // effects), so it's untouched by row-granularity consolidation.
142
+ for (const cond of conditionals) {
143
+ emitOuterConditional(lines, indent, elVar, cond, pc)
144
+ }
145
+ }
146
+
147
+ // --- profile-mode / legacy granular emission (one createEffect per slot-attr) ---
58
148
 
59
- // 1. Reactive attribute effects (one qsa per slot, then per-attr createEffect).
60
- for (const slot of plan.attrSlots) {
149
+ function emitAttrSlotsGranular(
150
+ lines: string[],
151
+ indent: string,
152
+ elVar: string,
153
+ lookup: string,
154
+ attrSlots: readonly ReactiveAttrSlot[],
155
+ elementIndexBySlot: ReadonlyMap<string, number> | undefined,
156
+ bindingBfId: (slotId: string) => string,
157
+ ): void {
158
+ for (const slot of attrSlots) {
61
159
  const varName = `__ra_${varSlotId(slot.slotId)}`
62
- const pIdx = elementIndexBySlot?.get(slot.slotId)
63
- const lookupExpr = pIdx !== undefined
64
- ? `__p ? __p[${pIdx}] : ${lookup}(${elVar}, '[bf="${slot.slotId}"]')`
65
- : `${lookup}(${elVar}, '[bf="${slot.slotId}"]')`
160
+ const lookupExpr = attrLookupExpr(slot.slotId, varName, elVar, lookup, elementIndexBySlot)
66
161
  lines.push(`${indent}{ const ${varName} = ${lookupExpr}`)
67
162
  lines.push(`${indent}if (${varName}) {`)
68
163
  for (const attr of slot.attrs) {
@@ -74,36 +169,151 @@ export function stringifyReactiveEffects(
74
169
  }
75
170
  lines.push(`${indent}} }`)
76
171
  }
172
+ }
173
+
174
+ /** ONE createEffect covering every preamble region — never carried a bfId
175
+ * before this pass (regions were never profiled per-binding), so this
176
+ * shape applies in both profile and non-profile mode. */
177
+ function emitPreambleRegionsEffect(
178
+ lines: string[],
179
+ indent: string,
180
+ elVar: string,
181
+ preambleRegions: readonly RowPreambleRegion[],
182
+ mapPreambleWrapped: string | undefined,
183
+ ): void {
184
+ if (preambleRegions.length === 0) return
185
+ const slots: ClaimSlotSpec[] = preambleRegions.map(r => ({ id: r.slotId, kind: 'markup', path: [] }))
186
+ const writer = claimWriterVarName(slots, varSlotId)
187
+ lines.push(`${indent}const ${writer} = lazySlots(${elVar}, ${claimPlanLiteral(slots)})`)
188
+ lines.push(`${indent}createEffect(() => {`)
189
+ if (mapPreambleWrapped) lines.push(`${indent} ${mapPreambleWrapped}`)
190
+ for (const region of preambleRegions) {
191
+ lines.push(`${indent} ${writer}('${region.slotId}', ${region.valueExpr})`)
192
+ }
193
+ lines.push(`${indent}})`)
194
+ }
195
+
196
+ function attrLookupExpr(
197
+ slotId: string,
198
+ varName: string,
199
+ elVar: string,
200
+ lookup: string,
201
+ elementIndexBySlot: ReadonlyMap<string, number> | undefined,
202
+ ): string {
203
+ const pIdx = elementIndexBySlot?.get(slotId)
204
+ return pIdx !== undefined
205
+ ? `__p ? __p[${pIdx}] : ${lookup}(${elVar}, '[bf="${slotId}"]')`
206
+ : `${lookup}(${elVar}, '[bf="${slotId}"]')`
207
+ }
208
+
209
+ // --- row-granularity consolidated emission (ONE createEffect per row) ---
210
+
211
+ /**
212
+ * Emit ONE `createEffect` covering: reactive attr writes (for every slot in
213
+ * `attrSlots`), outer text writes, and preamble-region writes — in that
214
+ * order, mirroring the legacy relative ordering (attrs, then texts, then
215
+ * regions). Attr-slot element lookups are resolved ONCE, outside the
216
+ * effect (identical to the legacy shape — only the WRITE moves inside the
217
+ * shared effect). Outer texts and preamble regions share ONE claimed-slot
218
+ * writer (mixed 'text'/'markup' kinds — `claim-slots.ts`'s `write()` already
219
+ * dispatches per-slot on `kind`), so a row pays for at most one `lazySlots`
220
+ * closure + one claim `Map` instead of one per category.
221
+ *
222
+ * Every attr's write statements are wrapped in their own `{ }` block (not
223
+ * just each slot's) — `emitAttrUpdate`'s 'value'-attr shape emits a bare
224
+ * `const __val = …` with no block of its own, and merging multiple attrs
225
+ * into one function body (previously each had its own arrow-function scope)
226
+ * would let two 'value' attrs in the same row effect collide on `__val`.
227
+ */
228
+ function emitConsolidatedRowEffect(
229
+ lines: string[],
230
+ indent: string,
231
+ elVar: string,
232
+ lookup: string,
233
+ attrSlots: readonly ReactiveAttrSlot[],
234
+ outerTexts: readonly ReactiveTextEffect[],
235
+ elementIndexBySlot: ReadonlyMap<string, number> | undefined,
236
+ textClaimPathExprs: ReadonlyMap<string, string> | undefined,
237
+ preambleRegions: readonly RowPreambleRegion[],
238
+ mapPreambleWrapped: string | undefined,
239
+ ): void {
240
+ if (attrSlots.length === 0 && outerTexts.length === 0 && preambleRegions.length === 0) return
241
+
242
+ // 1. Resolve every attr slot's element reference once, outside the effect.
243
+ for (const slot of attrSlots) {
244
+ const varName = `__ra_${varSlotId(slot.slotId)}`
245
+ lines.push(`${indent}const ${varName} = ${attrLookupExpr(slot.slotId, varName, elVar, lookup, elementIndexBySlot)}`)
246
+ }
77
247
 
78
- // 2. Outer text effects (slots NOT inside any conditional branch).
79
- for (const text of plan.outerTexts) {
80
- emitOuterText(lines, indent, elVar, text, bindingBfId(text.slotId), textIndexBySlot?.get(text.slotId))
248
+ // 2. ONE claimed-slot writer covering outer texts (kind 'text') and
249
+ // preamble regions (kind 'markup') mixed-kind plans are supported by
250
+ // claim-slots.ts's per-slot `kind` dispatch.
251
+ const claimSlots: ClaimSlotSpec[] = [
252
+ ...outerTexts.map((t): ClaimSlotSpec => ({ id: t.slotId, kind: 'text', path: [], pathExpr: textClaimPathExprs?.get(t.slotId) })),
253
+ ...preambleRegions.map((r): ClaimSlotSpec => ({ id: r.slotId, kind: 'markup', path: [] })),
254
+ ]
255
+ const writer = claimSlots.length > 0 ? claimWriterVarName(claimSlots, varSlotId) : null
256
+ if (writer) {
257
+ lines.push(`${indent}const ${writer} = lazySlots(${elVar}, ${claimPlanLiteral(claimSlots)})`)
81
258
  }
82
259
 
83
- // 3. Reactive conditionals each emits an insert(...) over `elVar` whose
84
- // arm bodies dispatch through the per-arm stringifiers.
85
- for (const cond of plan.conditionals) {
86
- emitOuterConditional(lines, indent, elVar, cond, pc)
260
+ // 3. The single row effect: attr writes, then (if any region needs it) the
261
+ // preamble re-run, then text writes, then region writes. The by-far
262
+ // most common shape no attrs, no regions, exactly one outer text —
263
+ // collapses to the SAME single-line `createEffect(() => { ... })` the
264
+ // legacy per-text emitter used (doc-examples snapshots pin this byte
265
+ // shape); anything busier (an attr present, multiple slots, or a
266
+ // preamble region) uses the multi-line block.
267
+ if (attrSlots.length === 0 && preambleRegions.length === 0 && outerTexts.length === 1) {
268
+ const text = outerTexts[0]
269
+ lines.push(`${indent}createEffect(() => { ${writer}('${text.slotId}', String(${text.wrappedExpression})) })`)
270
+ return
271
+ }
272
+ lines.push(`${indent}createEffect(() => {`)
273
+ for (const slot of attrSlots) {
274
+ const varName = `__ra_${varSlotId(slot.slotId)}`
275
+ lines.push(`${indent} if (${varName}) {`)
276
+ for (const attr of slot.attrs) {
277
+ lines.push(`${indent} {`)
278
+ for (const stmt of emitAttrUpdate(varName, attr.attrName, attr.wrappedExpression, attr.meta)) {
279
+ lines.push(`${indent} ${stmt}`)
280
+ }
281
+ lines.push(`${indent} }`)
282
+ }
283
+ lines.push(`${indent} }`)
284
+ }
285
+ if (preambleRegions.length > 0 && mapPreambleWrapped) {
286
+ lines.push(`${indent} ${mapPreambleWrapped}`)
87
287
  }
288
+ for (const text of outerTexts) {
289
+ lines.push(`${indent} ${writer}('${text.slotId}', String(${text.wrappedExpression}))`)
290
+ }
291
+ for (const region of preambleRegions) {
292
+ lines.push(`${indent} ${writer}('${region.slotId}', ${region.valueExpr})`)
293
+ }
294
+ lines.push(`${indent}})`)
88
295
  }
89
296
 
90
- function emitOuterText(
297
+ function emitOuterTexts(
91
298
  lines: string[],
92
299
  indent: string,
93
300
  elVar: string,
94
- text: ReactiveTextEffect,
95
- bfId: string = '',
96
- pIdx?: number,
301
+ texts: readonly ReactiveTextEffect[],
302
+ bindingBfId: (slotId: string) => string,
303
+ textClaimPathExprs?: ReadonlyMap<string, string>,
97
304
  ): void {
98
- const varName = `__rt_${varSlotId(text.slotId)}`
99
- if (pIdx !== undefined) {
100
- // Direct-path resolution (#2143): `__p` holds the marker Comment node;
101
- // `tAfter` mirrors `$t`'s create-if-absent Text-node semantics exactly.
102
- lines.push(`${indent}{ const ${varName} = __p ? tAfter(__p[${pIdx}]) : $t(${elVar}, '${text.slotId}')[0]`)
103
- } else {
104
- lines.push(`${indent}{ const [${varName}] = $t(${elVar}, '${text.slotId}')`)
305
+ if (texts.length === 0) return
306
+ const slots: ClaimSlotSpec[] = texts.map(t => ({
307
+ id: t.slotId,
308
+ kind: 'text',
309
+ path: [],
310
+ pathExpr: textClaimPathExprs?.get(t.slotId),
311
+ }))
312
+ const writer = claimWriterVarName(slots, varSlotId)
313
+ lines.push(`${indent}const ${writer} = lazySlots(${elVar}, ${claimPlanLiteral(slots)})`)
314
+ for (const text of texts) {
315
+ lines.push(`${indent}createEffect(() => { ${writer}('${text.slotId}', String(${text.wrappedExpression})) }${bindingBfId(text.slotId)})`)
105
316
  }
106
- lines.push(`${indent}if (${varName}) createEffect(() => { ${varName}.textContent = String(${text.wrappedExpression}) }${bfId}) }`)
107
317
  }
108
318
 
109
319
  function emitOuterConditional(
@@ -4,7 +4,7 @@
4
4
  * These three functions are called from `generate-init.ts` to emit the
5
5
  * client-side runtime calls that drive reactive conditionals
6
6
  * (`insert(...)`) and reactive loops (`mapArray(...)` /
7
- * `reconcileElements`) for a single component.
7
+ * `mapArrayAnchored(...)`) for a single component.
8
8
  *
9
9
  * Each entry point thinly wraps the Plan layer:
10
10
  *
@@ -2,13 +2,18 @@
2
2
  * Element ref declarations — `const [_s0, _s1] = $(…, 's0', 's1')`.
3
3
  *
4
4
  * Every element the init body needs to find at hydration time (events,
5
- * dynamic text, loops, refs, reactive attrs, reactive props, rest-attr
6
- * elements, child component slots) is emitted via one of three finders:
5
+ * loops, refs, reactive attrs, reactive props, rest-attr elements, child
6
+ * component slots) is emitted via one of two finders:
7
7
  *
8
8
  * `$(…)` — regular DOM elements (bf-*)
9
- * `$t(…)` — text comment markers for dynamic text (`<!--bf:sN-->`)
10
9
  * `$c(…)` — component slots (bf-s, component-rooted)
11
10
  *
11
+ * Dynamic text/JSX content slots (`<!--bf:sN-->` markers) don't need a ref
12
+ * here at all — they resolve through a claimed-slot writer built where
13
+ * they're used (`emitDynamicTextUpdates`/`emitClientOnlyExpressions` in
14
+ * `ir-to-client-js/emit-reactive.ts`, slot unification A3), not a
15
+ * pre-resolved `_sN` var.
16
+ *
12
17
  * Slots inside conditional branches are excluded here because
13
18
  * `insert()` bindEvents re-queries them when the branch activates.
14
19
  * Component slots take precedence over regular slots (#360): when a
@@ -26,7 +31,6 @@ import { varSlotId } from './utils.ts'
26
31
  */
27
32
  export function generateElementRefs(ctx: ClientJsContext): string {
28
33
  const regularSlots = new Set<string>()
29
- const textSlots = new Set<string>()
30
34
  const componentSlots = new Set<string>()
31
35
  const conditionalSlotIds = collectConditionalSlotIds(ctx)
32
36
 
@@ -35,11 +39,6 @@ export function generateElementRefs(ctx: ClientJsContext): string {
35
39
  regularSlots.add(elem.slotId)
36
40
  }
37
41
  }
38
- for (const elem of ctx.dynamicElements) {
39
- if (!elem.insideConditional) {
40
- textSlots.add(elem.slotId)
41
- }
42
- }
43
42
  for (const elem of ctx.conditionalElements) {
44
43
  regularSlots.add(elem.slotId)
45
44
  }
@@ -72,11 +71,10 @@ export function generateElementRefs(ctx: ClientJsContext): string {
72
71
  regularSlots.delete(slotId)
73
72
  }
74
73
 
75
- if (regularSlots.size === 0 && textSlots.size === 0 && componentSlots.size === 0) return ''
74
+ if (regularSlots.size === 0 && componentSlots.size === 0) return ''
76
75
 
77
76
  const refLines: string[] = []
78
77
  emitSlotRefs(refLines, [...regularSlots], '$')
79
- emitSlotRefs(refLines, [...textSlots], '$t')
80
78
  emitSlotRefs(refLines, [...componentSlots], '$c')
81
79
  return refLines.join('\n')
82
80
  }
@@ -9,6 +9,7 @@ import type { AttrMeta, IRMetadata } from '../types.ts'
9
9
  import { isBooleanAttr } from '../html-constants.ts'
10
10
  import type { ClientJsContext } from './types.ts'
11
11
  import { toHtmlAttrName, varSlotId, PROPS_PARAM } from './utils.ts'
12
+ import { claimPlanLiteral, claimWriterVarName, type ClaimSlotSpec } from './control-flow/stringify/claim-plan.ts'
12
13
  import { createTemplateAwareStringProtector } from './html-template.ts'
13
14
  import { datePlugin, DATE_METHODS } from '../date-lowering.ts'
14
15
  import { toLocaleDatePlugin, foldedArgToClientJs } from '../to-locale-date-lowering.ts'
@@ -303,24 +304,44 @@ export function emitDynamicTextUpdates(lines: string[], ctx: ClientJsContext): v
303
304
  const normalElems = elems.filter(e => !e.insideConditional)
304
305
 
305
306
  if (normalElems.length > 0 || conditionalElems.length > 0) {
306
- // Persistent slot trackers for non-conditional elements. `__bfText`
307
- // returns the node now occupying the slot; a JSX-valued expression
308
- // (`{themeLogo(id)}`) replaces the text node with a live element, so
309
- // the next reactive run must operate on that element, not the stale
310
- // text node (#1663). Primitive values keep the same text node.
311
- for (const elem of normalElems) {
312
- const v = varSlotId(elem.slotId)
313
- lines.push(` let __anchor_${v} = _${v}`)
314
- }
315
307
  const __textSlot = (normalElems[0] ?? conditionalElems[0])?.slotId
308
+
309
+ // Non-conditional elements: ONE claimed 'markup' writer covering every
310
+ // slot this expression feeds (slot unification A3, row-level claim at
311
+ // `__scope` — spec/slot-unification.md §3(a)/§5-A3). 'markup' (not
312
+ // 'text') because the value may be a live Node (e.g. a JSX-returning
313
+ // call like `{themeLogo(id)}`) — the writer splices a Node in by
314
+ // identity, exactly the contract `__bfText` used to provide (#1663).
315
+ let writer = ''
316
+ if (normalElems.length > 0) {
317
+ const slots: ClaimSlotSpec[] = normalElems.map(elem => ({ id: elem.slotId, kind: 'markup', path: [] }))
318
+ writer = claimWriterVarName(slots, varSlotId)
319
+ lines.push(` const ${writer} = lazySlots(__scope, ${claimPlanLiteral(slots)})`)
320
+ }
321
+
316
322
  lines.push(` createEffect(() => {`)
317
323
  if (normalElems.length > 0) {
318
324
  // Expression is always evaluated for non-conditional elements
319
325
  lines.push(` const __val = ${expr}`)
326
+ // `escapeTextOrNode`: `writeMarkup` inserts a string via
327
+ // `innerHTML =`, which — unlike the plain `Text.nodeValue =`
328
+ // assignment the initial SSR/CSR TEMPLATE's `escapeText(${expr})`
329
+ // was standing in for — DOES interpret HTML, so a raw un-escaped
330
+ // string here is an injection/byte-parity gap; a live Node (this
331
+ // expression may return one) must pass through untouched instead.
320
332
  for (const elem of normalElems) {
321
- const v = varSlotId(elem.slotId)
322
- lines.push(` __anchor_${v} = __bfText(__anchor_${v}, __val)`)
333
+ lines.push(` ${writer}('${elem.slotId}', escapeTextOrNode(__val))`)
323
334
  }
335
+ // Conditional elements: deferred from slot unification A3 — see
336
+ // `__bfText`'s docstring for why. `insert()` may swap the branch's
337
+ // DOM independently of this effect's own reruns, and a cached claim
338
+ // door's dedup state would go stale across that swap (unlike the
339
+ // 'text'-kind conditional cases elsewhere in the compiler, this
340
+ // slot's value may be a live Node, so it can't reuse the "just
341
+ // re-claim fresh every run" trick those use — a fresh 'markup'
342
+ // claim's dedup state always starts empty, so re-claiming per-run
343
+ // would throw away the unchanged-value skip on every single run).
344
+ // Keeps `$t` + `__bfText`.
324
345
  for (const elem of conditionalElems) {
325
346
  const v = varSlotId(elem.slotId)
326
347
  lines.push(` const [__el_${v}] = $t(__scope, '${elem.slotId}')`)
@@ -348,13 +369,28 @@ export function emitDynamicTextUpdates(lines: string[], ctx: ClientJsContext): v
348
369
  }
349
370
  }
350
371
 
351
- /** Emit createEffect blocks for client-only expressions using comment markers. */
372
+ /**
373
+ * Emit createEffect blocks for `@client` expressions. Slot unification A3:
374
+ * these are ordinary claimed 'text' slots now (never Node-valued — `@client`
375
+ * only ever evaluates a plain expression), one writer per slot at `__scope`,
376
+ * replacing `updateClientMarker`'s full-tree rescan + zero-width-space
377
+ * managed-node marker (no longer needed: a claimed Text node's identity is
378
+ * already known, so there's nothing to disambiguate from other content).
379
+ */
352
380
  export function emitClientOnlyExpressions(lines: string[], ctx: ClientJsContext): void {
353
381
  for (const elem of ctx.clientOnlyElements) {
382
+ // Slot unification Step B: `elem.elidedPath`, when present, was proven
383
+ // safe by `client-only-elision.ts` before this pass ran — use the real
384
+ // position-based path and skip the marker-scan fallback entirely.
385
+ const slots: ClaimSlotSpec[] = elem.elidedPath
386
+ ? [{ id: elem.slotId, kind: 'text', path: elem.elidedPath, markerless: true }]
387
+ : [{ id: elem.slotId, kind: 'text', path: [] }]
388
+ const writer = claimWriterVarName(slots, varSlotId)
354
389
  lines.push(` // @client: ${elem.slotId}`)
390
+ lines.push(` { const ${writer} = lazySlots(__scope, ${claimPlanLiteral(slots)})`)
355
391
  lines.push(` createEffect(() => {`)
356
- lines.push(` updateClientMarker(__scope, '${elem.slotId}', ${elem.expression})`)
357
- lines.push(` }${bindingIdArg(ctx, elem.slotId)})`)
392
+ lines.push(` ${writer}('${elem.slotId}', ${elem.expression})`)
393
+ lines.push(` }${bindingIdArg(ctx, elem.slotId)}) }`)
358
394
  lines.push('')
359
395
  }
360
396
  }