@barefootjs/jsx 0.37.0 → 0.38.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 (128) hide show
  1. package/dist/adapters/env-signal.d.ts +0 -8
  2. package/dist/adapters/env-signal.d.ts.map +1 -1
  3. package/dist/adapters/interface.d.ts +12 -0
  4. package/dist/adapters/interface.d.ts.map +1 -1
  5. package/dist/analyzer.d.ts +40 -0
  6. package/dist/analyzer.d.ts.map +1 -1
  7. package/dist/builtin-lowering-plugins.d.ts.map +1 -1
  8. package/dist/compiler.d.ts.map +1 -1
  9. package/dist/component-root-scope-comment.d.ts +36 -0
  10. package/dist/component-root-scope-comment.d.ts.map +1 -0
  11. package/dist/errors.d.ts +3 -0
  12. package/dist/errors.d.ts.map +1 -1
  13. package/dist/event-handler-name.d.ts +13 -0
  14. package/dist/event-handler-name.d.ts.map +1 -0
  15. package/dist/expression-parser.d.ts +9 -0
  16. package/dist/expression-parser.d.ts.map +1 -1
  17. package/dist/format-date-refusal.d.ts +37 -0
  18. package/dist/format-date-refusal.d.ts.map +1 -0
  19. package/dist/index.d.ts +5 -3
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +2973 -2310
  22. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  23. package/dist/ir-to-client-js/control-flow/plan/build-branch-loop.d.ts.map +1 -1
  24. package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
  25. package/dist/ir-to-client-js/control-flow/plan/build-composite-loop.d.ts.map +1 -1
  26. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts +13 -2
  27. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  28. package/dist/ir-to-client-js/control-flow/plan/build-loop.d.ts.map +1 -1
  29. package/dist/ir-to-client-js/control-flow/plan/build-reactive-effects.d.ts.map +1 -1
  30. package/dist/ir-to-client-js/control-flow/plan/lazy-conditional.d.ts.map +1 -1
  31. package/dist/ir-to-client-js/control-flow/plan/lazy-row-eligibility.d.ts +1 -1
  32. package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts +9 -0
  33. package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts.map +1 -1
  34. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +57 -12
  35. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
  36. package/dist/ir-to-client-js/control-flow/shared.d.ts +44 -3
  37. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  38. package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts +7 -5
  39. package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
  40. package/dist/ir-to-client-js/control-flow/stringify/loop-child-arm.d.ts +9 -1
  41. package/dist/ir-to-client-js/control-flow/stringify/loop-child-arm.d.ts.map +1 -1
  42. package/dist/ir-to-client-js/emit-reactive.d.ts +43 -2
  43. package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
  44. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  45. package/dist/ir-to-client-js/reactivity.d.ts +16 -1
  46. package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
  47. package/dist/ir-to-client-js/source-map.d.ts.map +1 -1
  48. package/dist/ir-to-client-js/types.d.ts +2 -0
  49. package/dist/ir-to-client-js/types.d.ts.map +1 -1
  50. package/dist/ir-to-client-js/utils.d.ts +26 -0
  51. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  52. package/dist/jsx-to-ir.d.ts.map +1 -1
  53. package/dist/opaque-local-accessor.d.ts +27 -0
  54. package/dist/opaque-local-accessor.d.ts.map +1 -0
  55. package/dist/to-locale-date-lowering.d.ts +7 -4
  56. package/dist/to-locale-date-lowering.d.ts.map +1 -1
  57. package/dist/types.d.ts +53 -17
  58. package/dist/types.d.ts.map +1 -1
  59. package/package.json +2 -2
  60. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +724 -4564
  61. package/src/__tests__/binding-scope-ratchet.test.ts +21 -10
  62. package/src/__tests__/branch-local-in-raw-capture.test.ts +1 -1
  63. package/src/__tests__/branch-local-same-name-collision.test.ts +9 -9
  64. package/src/__tests__/client-js-generation.test.ts +175 -0
  65. package/src/__tests__/component-root-scope-comment.test.ts +106 -0
  66. package/src/__tests__/doc-examples.test.ts +12 -22
  67. package/src/__tests__/event-handler-name.test.ts +74 -0
  68. package/src/__tests__/flatmap-segments.test.ts +11 -9
  69. package/src/__tests__/format-date-refusal.test.ts +134 -0
  70. package/src/__tests__/fragment-wrapped-conditional-return-branch.test.ts +132 -0
  71. package/src/__tests__/hydrate-template.test.ts +9 -1
  72. package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +2 -2
  73. package/src/__tests__/issue-3009-loop-branch-ref.test.ts +80 -0
  74. package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +213 -0
  75. package/src/__tests__/issue-3113-init-fn-name-collision.test.ts +133 -0
  76. package/src/__tests__/jsx-return-type-assertion.test.ts +2 -2
  77. package/src/__tests__/lazy-row-eligibility.test.ts +5 -4
  78. package/src/__tests__/loop-row-child-children-reactive-attrs.test.ts +108 -0
  79. package/src/__tests__/loop-row-child-text-children-markers.test.ts +158 -0
  80. package/src/__tests__/opaque-local-accessor.test.ts +90 -0
  81. package/src/__tests__/profiler-e2e.test.ts +13 -8
  82. package/src/__tests__/ref-attr-absent-at-ssr.test.ts +472 -0
  83. package/src/__tests__/ssr-portal-ref-callback.test.ts +217 -0
  84. package/src/adapters/env-signal.ts +2 -21
  85. package/src/adapters/interface.ts +13 -0
  86. package/src/analyzer.ts +40 -28
  87. package/src/builtin-lowering-plugins.ts +0 -2
  88. package/src/compiler.ts +24 -0
  89. package/src/component-root-scope-comment.ts +59 -0
  90. package/src/errors.ts +44 -1
  91. package/src/event-handler-name.ts +14 -0
  92. package/src/expression-parser.ts +41 -17
  93. package/src/format-date-refusal.ts +266 -0
  94. package/src/index.ts +13 -3
  95. package/src/ir-to-client-js/child-components.ts +8 -3
  96. package/src/ir-to-client-js/collect-elements.ts +29 -13
  97. package/src/ir-to-client-js/control-flow/plan/build-branch-loop.ts +2 -4
  98. package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +67 -5
  99. package/src/ir-to-client-js/control-flow/plan/build-composite-loop.ts +3 -6
  100. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +31 -1
  101. package/src/ir-to-client-js/control-flow/plan/build-loop.ts +23 -11
  102. package/src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts +5 -0
  103. package/src/ir-to-client-js/control-flow/plan/lazy-conditional.ts +1 -0
  104. package/src/ir-to-client-js/control-flow/plan/lazy-row-eligibility.ts +3 -3
  105. package/src/ir-to-client-js/control-flow/plan/loop-child-arm.ts +11 -0
  106. package/src/ir-to-client-js/control-flow/plan/loop.ts +51 -10
  107. package/src/ir-to-client-js/control-flow/shared.ts +78 -5
  108. package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +25 -12
  109. package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +22 -1
  110. package/src/ir-to-client-js/emit-reactive.ts +140 -10
  111. package/src/ir-to-client-js/emit-registration.ts +2 -2
  112. package/src/ir-to-client-js/generate-init.ts +2 -2
  113. package/src/ir-to-client-js/html-template.ts +41 -19
  114. package/src/ir-to-client-js/index.ts +4 -3
  115. package/src/ir-to-client-js/reactivity.ts +28 -17
  116. package/src/ir-to-client-js/source-map.ts +2 -1
  117. package/src/ir-to-client-js/types.ts +2 -0
  118. package/src/ir-to-client-js/utils.ts +34 -0
  119. package/src/jsx-to-ir.ts +737 -49
  120. package/src/opaque-local-accessor.ts +44 -0
  121. package/src/rich-type-refusal.ts +1 -1
  122. package/src/to-locale-date-lowering.ts +7 -4
  123. package/src/types.ts +51 -15
  124. package/dist/format-date-lowering.d.ts +0 -30
  125. package/dist/format-date-lowering.d.ts.map +0 -1
  126. package/src/__tests__/create-selector.test.ts +0 -124
  127. package/src/__tests__/format-date-lowering.test.ts +0 -125
  128. package/src/format-date-lowering.ts +0 -55
@@ -8,6 +8,7 @@
8
8
 
9
9
  import type {
10
10
  ConditionalBranchEvent,
11
+ ConditionalBranchRef,
11
12
  LoopChildBranchSummary,
12
13
  LoopChildConditional,
13
14
  LoopChildEvent,
@@ -62,6 +63,7 @@ function wrapAttrValueExpression(value: AttrValue, wrap: (s: string) => string):
62
63
  }
63
64
  import { destructureLoopParam, loopKeyFn, buildCompSelector, nestedLoopIndexAlias } from '../shared.ts'
64
65
  import { BF_HOST, BF_AT } from '@barefootjs/shared'
66
+ import { isEventHandlerName } from '../../../event-handler-name.ts'
65
67
  import type {
66
68
  BranchChildComponentInit,
67
69
  BranchChildComponentInitsPlan,
@@ -71,6 +73,8 @@ import type {
71
73
  BranchInnerLoop,
72
74
  BranchInnerLoopText,
73
75
  BranchInnerLoopsPlan,
76
+ BranchRef,
77
+ BranchRefBindingsPlan,
74
78
  LoopChildArmPlan,
75
79
  LoopChildConditionalPlan,
76
80
  } from './loop-child-arm.ts'
@@ -118,6 +122,28 @@ export function buildBranchEventBindingsPlan(
118
122
  return slots
119
123
  }
120
124
 
125
+ export interface BuildBranchRefBindingsArgs {
126
+ refs: readonly ConditionalBranchRef[] | undefined
127
+ /** Loop-param wrap closure. Identity (`x => x`) when no loop param applies. */
128
+ wrap: (expr: string) => string
129
+ }
130
+
131
+ /**
132
+ * Pre-wrap each branch ref's `callback` via the supplied loop-param wrap
133
+ * closure (#3009). Mirrors `buildBranchEventBindingsPlan`, minus the
134
+ * per-slot grouping — a slot carries at most one ref.
135
+ */
136
+ export function buildBranchRefBindingsPlan(
137
+ args: BuildBranchRefBindingsArgs,
138
+ ): BranchRefBindingsPlan {
139
+ const { refs, wrap } = args
140
+ if (!refs || refs.length === 0) return []
141
+ return refs.map((ref): BranchRef => ({
142
+ slotId: ref.slotId,
143
+ wrappedCallback: wrap(ref.callback),
144
+ }))
145
+ }
146
+
121
147
  /** A loose shape for one child component inside a conditional branch IR. */
122
148
  export interface BranchComponentLike {
123
149
  name: string
@@ -149,7 +175,7 @@ export function buildBranchChildComponentInitsPlan(
149
175
  const propsEntries = comp.props
150
176
  .filter(p => p.name !== 'key')
151
177
  .map(p => {
152
- if (p.name.startsWith('on') && p.name.length > 2) {
178
+ if (isEventHandlerName(p.name)) {
153
179
  return `${quotePropName(p.name)}: ${wrap(attrValueToString(p.value) ?? 'undefined')}`
154
180
  }
155
181
  switch (p.value.kind) {
@@ -491,6 +517,10 @@ function buildLoopChildArmPlan(args: BuildLoopChildArmArgs): LoopChildArmPlan {
491
517
  events: branch.events,
492
518
  wrap,
493
519
  }),
520
+ refs: buildBranchRefBindingsPlan({
521
+ refs: branch.refs,
522
+ wrap,
523
+ }),
494
524
  childComponents: buildBranchChildComponentInitsPlan({
495
525
  components: branch.childComponents,
496
526
  wrap,
@@ -33,6 +33,7 @@ import {
33
33
  loopKeyFn,
34
34
  buildChildRefBindings,
35
35
  buildStaticChildRefBindings,
36
+ hasReactiveLoopBindings,
36
37
  } from '../shared.ts'
37
38
  import { buildLoopReactiveEffectsPlan } from './build-reactive-effects.ts'
38
39
  import { buildPlainRowCore } from './build-plain-row.ts'
@@ -112,9 +113,7 @@ export function buildPlainLoopPlan(
112
113
  profileComponentName?: string,
113
114
  lazyScope?: LazyRowScopeInfo,
114
115
  ): PlainLoopPlan {
115
- const hasReactive = elem.bindings.reactiveAttrs.length > 0
116
- || elem.bindings.reactiveTexts.length > 0
117
- || elem.bindings.conditionals.length > 0
116
+ const hasReactive = hasReactiveLoopBindings(elem.bindings)
118
117
 
119
118
  // flatMap descriptor mode: reconcile the FLATTENED leaves. The source
120
119
  // accessor runs the flatMap body (plain items — per-item signals don't
@@ -216,16 +215,29 @@ export function buildPlainLoopPlan(
216
215
  export function buildStaticLoopPlan(elem: TopLevelLoop, unsafeLocalNames: Set<string>, profileComponentName?: string): StaticLoopPlan {
217
216
  // Group reactive attrs by their child slot id, preserving the legacy
218
217
  // declaration-order Map-iteration semantics.
218
+ //
219
+ // #3143: this used to skip the whole loop (`if (!elem.childComponent)`)
220
+ // whenever the static array's row root is a child component
221
+ // (`<Chip>...</Chip>`, e.g. `opts.map(opt => <Chip>...)` for a plain
222
+ // array `opts`) — a migration-era artifact from when this builder was
223
+ // mechanically split out of the legacy `emitStaticArrayUpdates` emitter
224
+ // (#1253), not a deliberate exclusion: `texts` a few lines below has
225
+ // never had the equivalent guard, and the stringifier
226
+ // (`stringifyStaticLoop`) patches both through the exact same
227
+ // `qsa(__iterEl, '[bf="<slot>"]')` lookup — a descendant CSS query that
228
+ // reaches an attribute on an element forwarded as the child component's
229
+ // OWN `children` (`^`-prefixed slot id) just as readily as one on
230
+ // `__iterEl` itself. Dropping the guard lets the forwarded `<a>`'s
231
+ // `href`/`data-current` (this fixture) get the same reactive-attribute
232
+ // effect a plain-element static row's own attrs already get.
219
233
  const attrsBySlotMap = new Map<string, LoopChildReactiveAttr[]>()
220
- if (!elem.childComponent) {
221
- for (const attr of elem.bindings.reactiveAttrs) {
222
- let bucket = attrsBySlotMap.get(attr.childSlotId)
223
- if (!bucket) {
224
- bucket = []
225
- attrsBySlotMap.set(attr.childSlotId, bucket)
226
- }
227
- bucket.push(attr)
234
+ for (const attr of elem.bindings.reactiveAttrs) {
235
+ let bucket = attrsBySlotMap.get(attr.childSlotId)
236
+ if (!bucket) {
237
+ bucket = []
238
+ attrsBySlotMap.set(attr.childSlotId, bucket)
228
239
  }
240
+ bucket.push(attr)
229
241
  }
230
242
 
231
243
  const indexParam = elem.index || '__idx'
@@ -36,6 +36,7 @@ import {
36
36
  buildArmTextsPlan,
37
37
  buildBranchChildComponentInitsPlan,
38
38
  buildBranchEventBindingsPlan,
39
+ buildBranchRefBindingsPlan,
39
40
  buildBranchInnerLoopsPlan,
40
41
  buildLoopChildConditionalsPlan,
41
42
  } from './build-loop-child-arm.ts'
@@ -152,6 +153,10 @@ function buildOuterArm(
152
153
  wrap,
153
154
  profileComponentName,
154
155
  }),
156
+ refs: buildBranchRefBindingsPlan({
157
+ refs: branch.refs,
158
+ wrap,
159
+ }),
155
160
  childComponents: buildBranchChildComponentInitsPlan({
156
161
  components: branch.childComponents,
157
162
  wrap,
@@ -94,6 +94,7 @@ function wiringOn(branch: LoopChildBranchSummary): string[] {
94
94
  if (branch.innerLoops && branch.innerLoops.length > 0) found.push('an inner loop')
95
95
  if (branch.conditionals && branch.conditionals.length > 0) found.push('a nested conditional')
96
96
  if (branch.events && branch.events.length > 0) found.push('events')
97
+ if (branch.refs && branch.refs.length > 0) found.push('refs')
97
98
  if (branch.reactiveAttrs && branch.reactiveAttrs.length > 0) found.push('reactive attrs')
98
99
  if (branch.reactiveTexts && branch.reactiveTexts.length > 0) found.push('reactive text')
99
100
  return found
@@ -42,7 +42,7 @@
42
42
  * may be defined as getters over the parent's signals), or a name the
43
43
  * compiler cannot classify at all — is `opaque` and makes the loop
44
44
  * ineligible. Imports and component-local functions are opaque TOO
45
- * (`const isSelected = createSelector(selected)` is an ordinary local
45
+ * (`const isSelected = someFactory(selected)` is an ordinary local
46
46
  * whose CALL is the reactive read), so they refuse the loop as well —
47
47
  * the `inert` set names them only so the refusal can say which one.
48
48
  * Genuinely ignorable are the names that cannot carry a reactive read
@@ -471,8 +471,8 @@ export function classifyLazyBinding(args: {
471
471
  // Everything else is OPAQUE, and deliberately so — this is where the
472
472
  // conservative line has to sit, because a reactive accessor can hide
473
473
  // behind an ordinary-looking name:
474
- // - `const isSelected = createSelector(selected)` is a local `const`
475
- // whose CALL is reactive (`create-selector.test.ts`),
474
+ // - `const isSelected = someFactory(selected)` is a local `const`
475
+ // whose CALL is reactive,
476
476
  // - a local function's body can read a signal,
477
477
  // - a prop may be defined as a getter over the parent's signals,
478
478
  // - an imported name may be another module's `@client` module signal.
@@ -36,6 +36,16 @@ export interface BranchEventSlot {
36
36
  */
37
37
  export type BranchEventBindingsPlan = readonly BranchEventSlot[]
38
38
 
39
+ /** One imperative `ref` callback inside an arm body (#3009). Flat, not slot-grouped like events — a slot carries at most one ref. */
40
+ export interface BranchRef {
41
+ slotId: string
42
+ /** Already wrapped via wrapLoopParamAsAccessor at build time. */
43
+ wrappedCallback: string
44
+ }
45
+
46
+ /** Pre-built ref bindings for one arm of a loop-scoped conditional. Mirrors `BranchEventBindingsPlan` (#3009). */
47
+ export type BranchRefBindingsPlan = readonly BranchRef[]
48
+
39
49
  /**
40
50
  * One child component initialiser inside an arm body (qsa() + initChild for
41
51
  * SSR, or placeholder replacement + createComponent for CSR). The selector,
@@ -188,6 +198,7 @@ export interface LoopChildArmText {
188
198
  */
189
199
  export interface LoopChildArmPlan {
190
200
  events: BranchEventBindingsPlan
201
+ refs: BranchRefBindingsPlan
191
202
  childComponents: BranchChildComponentInitsPlan
192
203
  innerLoops: BranchInnerLoopsPlan
193
204
  nestedConditionals: readonly LoopChildConditionalPlan[]
@@ -192,12 +192,17 @@ interface PlainLoopVariant extends DynamicLoopCommon {
192
192
  * Loop body is a single child component (with or without nested child
193
193
  * components inside it).
194
194
  *
195
- * `nestedComps.length === 0` → simple two-line renderItem.
196
- * `nestedComps.length > 0` → SSR/CSR split that initialises both the
197
- * outer component and each nested child.
195
+ * `nestedComps.length === 0 && reactiveEffects === null` → simple two-line
196
+ * renderItem.
197
+ * otherwise → SSR/CSR split that initialises the outer
198
+ * component, each nested child, and any
199
+ * reactive effect on the outer component's
200
+ * own forwarded children / conditionals.
198
201
  *
199
- * `childConditionalEffects` is non-null when the body contains reactive
200
- * conditionals; same plan shape as plain/composite `reactiveEffects`.
202
+ * `reactiveEffects` is non-null when the row has a reactive attr/text on an
203
+ * element forwarded as the ROOT component's own `children` (#3143) or a
204
+ * reactive conditional in the body; same plan shape as plain/composite
205
+ * `reactiveEffects`.
201
206
  */
202
207
  interface ComponentLoopVariant extends DynamicLoopCommon {
203
208
  kind: 'component'
@@ -209,8 +214,15 @@ interface ComponentLoopVariant extends DynamicLoopCommon {
209
214
  keyExpr: string
210
215
  /** Nested child component initialisers; empty for the simple case. */
211
216
  nestedComps: NestedComponentInit[]
212
- /** Reactive-effects plan for `childConditionals` inside the loop body. */
213
- childConditionalEffects: ReactiveEffectsPlan | null
217
+ /**
218
+ * Reactive-effects plan for the loop ROOT component's own reactive attrs /
219
+ * texts / conditionals — i.e. bindings on elements forwarded as the root
220
+ * component's `children` (`<Chip><a href={...}>...</a></Chip>`, #3143),
221
+ * plus any reactive conditional in the row. Named to match
222
+ * `PlainLoopVariant.reactiveEffects` (not just `childConditionalEffects` —
223
+ * it used to carry only conditionals, before #3143 widened it).
224
+ */
225
+ reactiveEffects: ReactiveEffectsPlan | null
214
226
  /**
215
227
  * Profile-mode loop id (#1690, #1795 Phase 3) for the component loop's
216
228
  * `mapArray`. Undefined off → byte-identical (SR8).
@@ -320,11 +332,40 @@ export type ComponentLoopPlan = Extract<LoopPlan, { kind: 'component' }>
320
332
  export type CompositeLoopPlan = Extract<LoopPlan, { kind: 'composite' }>
321
333
  export type StaticLoopPlan = Extract<LoopPlan, { kind: 'static' }>
322
334
 
335
+ /**
336
+ * How a loop-row nested/forwarded child component's text-only `children`
337
+ * should be kept in sync with the loop's per-row signal (#3064).
338
+ *
339
+ * `'markers'` (preferred): one patch per `expression`-type child, each
340
+ * writing through its own SSR-emitted `<!--bf:^sN-->…<!--/-->` marker pair
341
+ * via the marker-aware `$t` primitive — the row's own text markers and
342
+ * static text siblings (e.g. the `"$"` in a `${amount}` cell) are left
343
+ * untouched, matching how a parent-owned text patches everywhere else.
344
+ *
345
+ * `'textContent'` (legacy fallback): the whole joined value is written with
346
+ * a single `.textContent =` assignment, discarding any marker/text
347
+ * structure in the child root. Used only when the children mix in a
348
+ * text-only CONDITIONAL (`isTextOnlyConditional`) — which patches through
349
+ * its own `insert()`, not a stable per-slot marker this scheme can target —
350
+ * or, defensively, if some `expression` child was never granted a slot id.
351
+ *
352
+ * Built by `buildChildrenTextEffect` and stringified by
353
+ * `stringifyChildrenTextEffect` (both `../shared.ts`) — defined here, not
354
+ * there, so `shared.ts` (which the whole `plan`/`stringify` tree feeds data
355
+ * through, per this file's one-way dependency note) doesn't need a type
356
+ * import pointing back at `plan/loop.ts`, mirroring how `shared.ts` already
357
+ * imports `LoopChildRefBinding`/`PreambleRegionPlan` FROM here.
358
+ */
359
+ export type NestedChildrenTextEffect =
360
+ | { readonly kind: 'markers'; readonly slotPatches: ReadonlyArray<{ readonly slotId: string; readonly wrappedExpr: string }> }
361
+ | { readonly kind: 'textContent'; readonly wrappedChildren: string }
362
+
323
363
  /**
324
364
  * One nested child component to initialise inside a renderItem body.
325
365
  * `childrenTextEffect` is non-null when the component's children are
326
366
  * text-equivalent AND reference the outer loop param — in that case the
327
- * stringifier emits a `createEffect` that updates the child's `textContent`.
367
+ * stringifier emits a reactive effect (marker-scoped where possible, #3064)
368
+ * that keeps the child's forwarded text in sync alongside `initChild`.
328
369
  */
329
370
  export interface NestedComponentInit {
330
371
  componentName: string
@@ -332,8 +373,8 @@ export interface NestedComponentInit {
332
373
  selector: string
333
374
  /** Pre-built props object expression for the nested component. */
334
375
  propsExpr: string
335
- /** When non-null, emit a reactive textContent effect alongside `initChild`. */
336
- childrenTextEffect: { wrappedChildren: string } | null
376
+ /** When non-null, emit a reactive children-text effect alongside `initChild` — see `NestedChildrenTextEffect` (../shared.ts). */
377
+ childrenTextEffect: NestedChildrenTextEffect | null
337
378
  }
338
379
 
339
380
  /**
@@ -12,8 +12,8 @@
12
12
  * control-flow.ts -> control-flow/{plan,stringify}/* -> shared.ts
13
13
  */
14
14
 
15
- import type { LoopChildEvent, LoopChildRef, TopLevelLoop, NestedLoop, CollectedLoop } from '../types.ts'
16
- import type { IRLoopChildComponent, LoopParamBinding } from '../../types.ts'
15
+ import type { LoopChildBindings, LoopChildEvent, LoopChildRef, TopLevelLoop, NestedLoop, CollectedLoop } from '../types.ts'
16
+ import type { IRLoopChildComponent, IRExpression, IRNode, LoopParamBinding } from '../../types.ts'
17
17
  import { preambleAnalysisText } from '../../types.ts'
18
18
  import { quotePropName, wrapLoopParamAsAccessor, irChildrenFreeIds, attrValueToString } from '../utils.ts'
19
19
  import { irChildrenToJsExpr, jsxChildrenPropGetterExpr } from '../html-template.ts'
@@ -21,7 +21,7 @@ import { spliceChildValue } from '../safe-html.ts'
21
21
  import { emitListenerBlock } from './stringify/event-listener.ts'
22
22
  import { nameForRegistryRef } from '../component-scope.ts'
23
23
  import { BF_SCOPE, BF_HOST, BF_AT } from '@barefootjs/shared'
24
- import type { LoopChildRefBinding, PreambleRegionPlan } from './plan/loop.ts'
24
+ import type { LoopChildRefBinding, PreambleRegionPlan, NestedChildrenTextEffect } from './plan/loop.ts'
25
25
  import type { PreambleRegionSource } from '../../types.ts'
26
26
  import {
27
27
  extractFreeIdentifiersFromText,
@@ -406,6 +406,21 @@ export function buildCompSelector(comp: { slotId?: string | null; name: string }
406
406
  * For new elements (CSR): replaces placeholders with createComponent.
407
407
  * For SSR elements (hydration): finds scope elements and calls initChild.
408
408
  */
409
+ /**
410
+ * Whether a loop row's collected bindings need a `ReactiveEffectsPlan` at
411
+ * all (#3143). The three-field check (`reactiveAttrs` / `reactiveTexts` /
412
+ * `conditionals`) was independently duplicated across the plain, static,
413
+ * composite and component-root loop builders — see CLAUDE.md's "One
414
+ * decision, two implementations" note. This is the single implementation;
415
+ * builders that build their own `LoopChildBindings`-shaped value (e.g. a
416
+ * `BranchLoop`'s `.bindings`) call it too since the field shape is shared.
417
+ */
418
+ export function hasReactiveLoopBindings(bindings: LoopChildBindings): boolean {
419
+ return bindings.reactiveAttrs.length > 0
420
+ || bindings.reactiveTexts.length > 0
421
+ || bindings.conditionals.length > 0
422
+ }
423
+
409
424
  /** Check if an IR node is a conditional whose branches are text/expression only. */
410
425
  export function isTextOnlyConditional(node: { type: string; [k: string]: any }): boolean {
411
426
  if (node.type !== 'conditional') return false
@@ -414,6 +429,64 @@ export function isTextOnlyConditional(node: { type: string; [k: string]: any }):
414
429
  return checkNode(node.whenTrue) && checkNode(node.whenFalse)
415
430
  }
416
431
 
432
+ /**
433
+ * Decide which `NestedChildrenTextEffect` shape (`./plan/loop.ts`) applies to a nested/forwarded
434
+ * child component's text-only `children` (#3064). Called only once the
435
+ * caller has already confirmed the children are text-only AND reference the
436
+ * loop's item/index (`childrenRefsLoop` at both call sites below) — this is
437
+ * the single implementation both used to duplicate (`build-component-loop.ts`'s
438
+ * `nestedComps` map and `emitComponentAndEventSetup` here), one for
439
+ * component-root loops, one for composite loops.
440
+ *
441
+ * `rawChildrenExpr` is the already-computed `irChildrenToJsExpr(children)`
442
+ * (both call sites need it for their own `childrenFreeIds` gate too, so it's
443
+ * passed in rather than recomputed) — used verbatim for the `'textContent'`
444
+ * fallback; each `'markers'` patch instead re-derives its OWN wrapped
445
+ * expression from just that one child, not the joined array.
446
+ */
447
+ export function buildChildrenTextEffect(
448
+ children: readonly IRNode[],
449
+ rawChildrenExpr: string,
450
+ wrap: (expr: string) => string,
451
+ ): NestedChildrenTextEffect {
452
+ const exprChildren = children.filter((c): c is IRExpression => c.type === 'expression')
453
+ const hasConditional = children.some(c => c.type === 'conditional')
454
+ if (!hasConditional && exprChildren.length > 0 && exprChildren.every(c => c.slotId != null)) {
455
+ return {
456
+ kind: 'markers',
457
+ slotPatches: exprChildren.map(c => ({ slotId: c.slotId!, wrappedExpr: wrap(c.expr) })),
458
+ }
459
+ }
460
+ return { kind: 'textContent', wrappedChildren: wrap(rawChildrenExpr) }
461
+ }
462
+
463
+ /**
464
+ * Stringify a `NestedChildrenTextEffect` into the effect-only snippet
465
+ * (everything after `initChild(...)`/`upsertChild(...)` inside the
466
+ * `if (__c) { ... }` guard) — callers embed it in their own surrounding
467
+ * `{ const __c = <lookup>; if (__c) { ...; <this> } }` wrapper, since how
468
+ * `__c` is obtained differs (component-root loops resolve it via
469
+ * `qsa` + an explicit `initChild`; composite loops resolve AND init it in
470
+ * one `upsertChild` call).
471
+ *
472
+ * The `'textContent'` branch is byte-identical to the pre-#3064 inline
473
+ * emission at both call sites, so a row whose forwarded children mix in a
474
+ * text-only conditional (the one shape this fix doesn't cover, see
475
+ * `NestedChildrenTextEffect`'s docstring) keeps its exact prior output.
476
+ */
477
+ export function stringifyChildrenTextEffect(effect: NestedChildrenTextEffect): string {
478
+ if (effect.kind === 'markers') {
479
+ const { slotPatches } = effect
480
+ const ids = slotPatches.map(p => `'${p.slotId}'`).join(', ')
481
+ const varNames = slotPatches.map((_, i) => `__ct${i}`)
482
+ const writes = slotPatches
483
+ .map((p, i) => `if (${varNames[i]}) ${varNames[i]}.data = String(${p.wrappedExpr} ?? '')`)
484
+ .join('; ')
485
+ return `{ const [${varNames.join(', ')}] = $t(__c, ${ids}); createEffect(() => { ${writes} }) }`
486
+ }
487
+ return `createEffect(() => { const __v = ${effect.wrappedChildren}; __c.textContent = Array.isArray(__v) ? __v.join('') : String(__v ?? '') })`
488
+ }
489
+
417
490
  /**
418
491
  * Emit child component initialisation + event listener setup for a list of
419
492
  * components and events on `elVar`. Mode-independent: each comp emits a
@@ -465,8 +538,8 @@ export function emitComponentAndEventSetup(
465
538
  const upsertCall = `${upsertFn}(${elVar}, '${nameForRegistryRef(comp.name)}', ${slotIdLit}, ${propsExpr}${keyArg}, __scope)`
466
539
 
467
540
  if (childrenRefsLoop) {
468
- const wrappedChildren = wrap(rawChildrenExpr!)
469
- ls.push(`${indent}{ const __c = ${upsertCall}; if (__c) { createEffect(() => { const __v = ${wrappedChildren}; __c.textContent = Array.isArray(__v) ? __v.join('') : String(__v ?? '') }) } }`)
541
+ const effect = buildChildrenTextEffect(comp.children!, rawChildrenExpr!, wrap)
542
+ ls.push(`${indent}{ const __c = ${upsertCall}; if (__c) { ${stringifyChildrenTextEffect(effect)} } }`)
470
543
  } else {
471
544
  ls.push(`${indent}${upsertCall}`)
472
545
  }
@@ -2,27 +2,29 @@
2
2
  * Stringify a `ComponentLoopPlan` to source lines.
3
3
  *
4
4
  * Two output shapes (preserved byte-identical from the legacy
5
- * `emitComponentLoopReconciliation`):
5
+ * `emitComponentLoopReconciliation` for the case it covered):
6
6
  *
7
- * nestedComps.length === 0 (simple) — two-line renderItem body:
7
+ * nestedComps.length === 0 AND reactiveEffects === null (simple) —
8
+ * two-line renderItem body:
8
9
  * <i>mapArray(() => <arr>, <container>, <keyFn>, (<head>, <idx>, __existing) => {
9
10
  * <i> <unwrap?>
10
11
  * <i> if (__existing) { initChild('<C>', __existing, <props>); return __existing }
11
12
  * <i> return createComponent('<C>', <props>, <key>)
12
13
  * <i>})
13
14
  *
14
- * nestedComps.length > 0 (nested) — SSR/CSR split:
15
+ * otherwise (nested comps and/or the root's own reactiveEffects) —
16
+ * SSR/CSR split:
15
17
  * <i>mapArray(...) => {
16
18
  * <i> <unwrap?>
17
19
  * <i> if (__existing) {
18
20
  * <i> initChild('<C>', __existing, <props>)
19
21
  * <i> {<each nested initChild + optional createEffect>}
20
- * <i> <stringifyReactiveEffects on __existing if childConditionals>
22
+ * <i> <stringifyReactiveEffects on __existing if reactiveEffects>
21
23
  * <i> return __existing
22
24
  * <i> }
23
25
  * <i> const __csrEl = createComponent('<C>', <props>, <key>)
24
26
  * <i> {<each nested initChild + optional createEffect>}
25
- * <i> <stringifyReactiveEffects on __csrEl if childConditionals>
27
+ * <i> <stringifyReactiveEffects on __csrEl if reactiveEffects>
26
28
  * <i> return __csrEl
27
29
  * <i>})
28
30
  *
@@ -33,6 +35,7 @@
33
35
  import { stringifyReactiveEffects } from './reactive-effects.ts'
34
36
  import type { ComponentLoopPlan, NestedComponentInit } from '../plan/types.ts'
35
37
  import { nameForRegistryRef } from '../../component-scope.ts'
38
+ import { stringifyChildrenTextEffect } from '../shared.ts'
36
39
 
37
40
  export function stringifyComponentLoop(lines: string[], plan: ComponentLoopPlan): void {
38
41
  const {
@@ -47,7 +50,7 @@ export function stringifyComponentLoop(lines: string[], plan: ComponentLoopPlan)
47
50
  componentPropsExpr,
48
51
  keyExpr,
49
52
  nestedComps,
50
- childConditionalEffects,
53
+ reactiveEffects,
51
54
  profileLoopId,
52
55
  mapPreambleWrapped,
53
56
  } = plan
@@ -64,7 +67,17 @@ export function stringifyComponentLoop(lines: string[], plan: ComponentLoopPlan)
64
67
 
65
68
  const scopedComp = nameForRegistryRef(componentName)
66
69
 
67
- if (nestedComps.length === 0) {
70
+ // #3143: the "simple" 2-line shape must be reserved for a row with
71
+ // NEITHER nested comps NOR any reactive effect of its own (attrs/texts on
72
+ // an element forwarded as ITS OWN `children`, or a reactive conditional
73
+ // in the row) — it returns straight from `initChild`/`createComponent`
74
+ // with no handle to run an effect against. Gating on `nestedComps.length
75
+ // === 0` alone (the pre-#3143 condition) silently skipped `reactiveEffects`
76
+ // whenever a component-root loop had no nested child COMPONENTS, which is
77
+ // exactly the `<Chip><a href={...}>...</a></Chip>` shape: `<a>` isn't a
78
+ // component, so `nestedComps` was always empty, and the "simple" path
79
+ // never even looked at `reactiveEffects`.
80
+ if (nestedComps.length === 0 && !reactiveEffects) {
68
81
  lines.push(` if (__existing) { initChild('${scopedComp}', __existing, ${componentPropsExpr}); return __existing }`)
69
82
  lines.push(` return createComponent('${scopedComp}', ${componentPropsExpr}, ${keyExpr})`)
70
83
  lines.push(` }, '${markerId}'${loopBfId})`)
@@ -75,8 +88,8 @@ export function stringifyComponentLoop(lines: string[], plan: ComponentLoopPlan)
75
88
  lines.push(` if (__existing) {`)
76
89
  lines.push(` initChild('${scopedComp}', __existing, ${componentPropsExpr})`)
77
90
  for (const nc of nestedComps) emitNestedInit(lines, ' ', '__existing', nc)
78
- if (childConditionalEffects) {
79
- stringifyReactiveEffects(lines, childConditionalEffects, { indent: ' ', elVar: '__existing' })
91
+ if (reactiveEffects) {
92
+ stringifyReactiveEffects(lines, reactiveEffects, { indent: ' ', elVar: '__existing' })
80
93
  }
81
94
  lines.push(` return __existing`)
82
95
  lines.push(` }`)
@@ -84,8 +97,8 @@ export function stringifyComponentLoop(lines: string[], plan: ComponentLoopPlan)
84
97
  // CSR side
85
98
  lines.push(` const __csrEl = createComponent('${scopedComp}', ${componentPropsExpr}, ${keyExpr})`)
86
99
  for (const nc of nestedComps) emitNestedInit(lines, ' ', '__csrEl', nc)
87
- if (childConditionalEffects) {
88
- stringifyReactiveEffects(lines, childConditionalEffects, { indent: ' ', elVar: '__csrEl' })
100
+ if (reactiveEffects) {
101
+ stringifyReactiveEffects(lines, reactiveEffects, { indent: ' ', elVar: '__csrEl' })
89
102
  }
90
103
  lines.push(` return __csrEl`)
91
104
  lines.push(` }, '${markerId}'${loopBfId})`)
@@ -94,7 +107,7 @@ export function stringifyComponentLoop(lines: string[], plan: ComponentLoopPlan)
94
107
  function emitNestedInit(lines: string[], indent: string, parentVar: string, nc: NestedComponentInit): void {
95
108
  const scopedNc = nameForRegistryRef(nc.componentName)
96
109
  if (nc.childrenTextEffect) {
97
- lines.push(`${indent}{ const __c = qsa(${parentVar}, ${nc.selector}); if (__c) { initChild('${scopedNc}', __c, ${nc.propsExpr}); createEffect(() => { const __v = ${nc.childrenTextEffect.wrappedChildren}; __c.textContent = Array.isArray(__v) ? __v.join('') : String(__v ?? '') }) } }`)
110
+ lines.push(`${indent}{ const __c = qsa(${parentVar}, ${nc.selector}); if (__c) { initChild('${scopedNc}', __c, ${nc.propsExpr}); ${stringifyChildrenTextEffect(nc.childrenTextEffect)} } }`)
98
111
  } else {
99
112
  lines.push(`${indent}{ const __c = qsa(${parentVar}, ${nc.selector}); if (__c) initChild('${scopedNc}', __c, ${nc.propsExpr}) }`)
100
113
  }
@@ -11,7 +11,7 @@
11
11
  * every nesting depth.
12
12
  */
13
13
 
14
- import { varSlotId, DATA_BF_PH, keyAttrName, mapArrayKeyArgs, profileBindingId } from '../../utils.ts'
14
+ import { varSlotId, DATA_BF_PH, keyAttrName, mapArrayKeyArgs, profileBindingId, emitRefCall } from '../../utils.ts'
15
15
  import { emitComponentAndEventSetup } from '../shared.ts'
16
16
  import { emitDedupedAttrUpdate, DEDUP_STORE_DECL } from '../../emit-reactive.ts'
17
17
  import { namespaceWrapForTemplate } from './template-parse.ts'
@@ -21,6 +21,7 @@ import { claimPlanLiteral, claimWriterVarName, type ClaimSlotSpec } from './clai
21
21
  import type {
22
22
  BranchChildComponentInitsPlan,
23
23
  BranchEventBindingsPlan,
24
+ BranchRefBindingsPlan,
24
25
  BranchInnerLoopsPlan,
25
26
  LoopChildArmPlan,
26
27
  LoopChildConditionalPlan,
@@ -99,6 +100,25 @@ export function stringifyBranchEventBindings(
99
100
  }
100
101
  }
101
102
 
103
+ /**
104
+ * Emit one imperative ref call per branch-interior ref (#3009), inside
105
+ * `insert()`'s `bindEvents` so it (re-)fires on every branch activation.
106
+ * Uses `qsa(__branchScope, ...)`, not the scope-aware `$()`, for the same
107
+ * reason `stringifyBranchEventBindings` does: `__branchScope` may not carry
108
+ * a `bf-s` attribute for `$()` to anchor on.
109
+ */
110
+ export function stringifyBranchRefs(
111
+ lines: string[],
112
+ plan: BranchRefBindingsPlan,
113
+ indent: string,
114
+ ): void {
115
+ for (const ref of plan) {
116
+ const v = varSlotId(ref.slotId)
117
+ lines.push(`${indent}{ const _${v} = qsa(__branchScope, '[bf="${ref.slotId}"]')`)
118
+ lines.push(`${indent}if (_${v}) ${emitRefCall(ref.wrappedCallback, `_${v}`)} }`)
119
+ }
120
+ }
121
+
102
122
  /**
103
123
  * Emit one initChild + placeholder-replacement line per child component
104
124
  * inside an arm body. Mirrors the legacy `emitBranchChildComponentInits`
@@ -286,6 +306,7 @@ export function stringifyLoopChildArm(
286
306
  mapPreambleWrapped: string | undefined,
287
307
  ): void {
288
308
  stringifyBranchEventBindings(lines, arm.events, armIndent)
309
+ stringifyBranchRefs(lines, arm.refs, armIndent)
289
310
  stringifyBranchChildComponentInits(lines, arm.childComponents, armIndent)
290
311
  stringifyBranchInnerLoops(lines, arm.innerLoops, armIndent, pc)
291
312