@barefootjs/jsx 0.35.3 → 0.35.4
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.
- package/dist/debug.d.ts +33 -3
- package/dist/debug.d.ts.map +1 -1
- package/dist/index.js +137 -60
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/types.d.ts +25 -0
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/debug.test.ts +246 -0
- package/src/__tests__/issue-2897-static-loop-conditional.test.ts +340 -0
- package/src/__tests__/lazy-conditional.test.ts +1 -0
- package/src/debug.ts +214 -84
- package/src/ir-to-client-js/collect-elements.ts +83 -3
- package/src/ir-to-client-js/types.ts +37 -1
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { type IRNode, type IRElement, type IRComponent, type IRLoop, type IRProp, pickAttrMetaFromIR } from '../types.ts'
|
|
6
6
|
import type { ClientJsContext, ConditionalBranchChildComponent, ConditionalBranchReactiveAttr, BranchLoop, ConditionalBranchTextEffect, ConditionalElement, LoopChildBindings, LoopChildBranchSummary, LoopChildConditional, LoopOffset, NestedLoop } from './types.ts'
|
|
7
7
|
import { attrValueToString, freeIdsFromRefs, quotePropName, PROPS_PARAM, type LoopParamSpec } from './utils.ts'
|
|
8
|
-
import { classifyReactivity, decideWrapForAttr, decideWrapForChildProp, decideWrapFromAstFlags, collectEventHandlersFromIR, collectConditionalBranchEvents, collectConditionalBranchRefs, collectConditionalBranchChildComponents, collectLoopChildEventsWithNesting, collectLoopChildReactiveAttrs, collectLoopChildReactiveTexts, collectLoopChildRefs, emptyLoopChildBindings, buildLoopRowScope, anyNameIn } from './reactivity.ts'
|
|
8
|
+
import { classifyReactivity, needsEffectWrapper, decideWrapForAttr, decideWrapForChildProp, decideWrapFromAstFlags, collectEventHandlersFromIR, collectConditionalBranchEvents, collectConditionalBranchRefs, collectConditionalBranchChildComponents, collectLoopChildEventsWithNesting, collectLoopChildReactiveAttrs, collectLoopChildReactiveTexts, collectLoopChildRefs, emptyLoopChildBindings, buildLoopRowScope, anyNameIn } from './reactivity.ts'
|
|
9
9
|
import { irToHtmlTemplate, irToPlaceholderTemplate, irChildrenToJsExpr, jsxChildrenPropGetterExpr, buildLoopSkeletonTemplate, computeSkeletonSlotPaths, renderFlatMapClientBody, renderFlatMapProjectionClientBody, flatMapCallbackHasKeyedLeaf, type SkeletonSlotPaths } from './html-template.ts'
|
|
10
10
|
import { detectRootNamespaceWrapTag } from './control-flow/stringify/template-parse.ts'
|
|
11
11
|
import { expandDynamicPropValue, expandConstantForReactivity, resolveRestSpreadOrigin, resolveRestSpreadNames } from './prop-handling.ts'
|
|
@@ -752,10 +752,78 @@ export function collectElements(
|
|
|
752
752
|
// Determine rendering strategy for dynamic arrays:
|
|
753
753
|
// Use element reconciliation when the loop body has nested components,
|
|
754
754
|
// or when inner loops need their own mapArray for events/reactive text.
|
|
755
|
-
const { useElementReconciliation, innerLoops } = projectionInner
|
|
755
|
+
const { useElementReconciliation: reconciliationBase, innerLoops } = projectionInner
|
|
756
756
|
? { useElementReconciliation: false, innerLoops: undefined }
|
|
757
757
|
: decideLoopRendering(l, siblingOffsets, ctx)
|
|
758
758
|
|
|
759
|
+
// #2897: a static (non-signal) array's forEach bake and the
|
|
760
|
+
// `inner-loop-nested` clone-and-wire architecture (#2798) have NO
|
|
761
|
+
// conditional-handling machinery at all — a `{cond ? A : B}` inside
|
|
762
|
+
// either the outer row or a depth-1 inner loop's row bakes to its
|
|
763
|
+
// initial branch at compile time and never updates, since neither
|
|
764
|
+
// path ever re-evaluates the condition. `elem.bodyIsItemConditional`
|
|
765
|
+
// (below) already carves out the WHOLE-row, one-empty-branch shape;
|
|
766
|
+
// this widens the same idea to any conditional anywhere in the row
|
|
767
|
+
// tree. `bindings.conditionals` is the outer row's own conditionals
|
|
768
|
+
// (never recurses into `loop` nodes, see `collectLoopChildConditionals`'s
|
|
769
|
+
// `stopAt(..., 'loop', ...)`); `innerLoops[i].bindings.conditionals` is
|
|
770
|
+
// collected separately per inner loop (#2706) — together they cover
|
|
771
|
+
// the whole tree with no overlap. Excludes a child-component-rooted
|
|
772
|
+
// outer item (`l.childComponent`) — out of scope, matching #2798's own
|
|
773
|
+
// plain-element-only scoping for the identical reason (#1725's
|
|
774
|
+
// document-order-zip shape doesn't address plain-element bindings).
|
|
775
|
+
//
|
|
776
|
+
// Gated on `reactiveBeyondLoopScope`, not mere presence: EVERY
|
|
777
|
+
// loop-param-referencing conditional lands in `bindings.conditionals`
|
|
778
|
+
// (`classifyReactivity` reports `'loop-param'` for `item.key === 'x'`
|
|
779
|
+
// exactly like it does for `flag()` — array static-ness plays no part
|
|
780
|
+
// in that classification, by design). For a static array the row
|
|
781
|
+
// item never changes after first render, so a condition that reads
|
|
782
|
+
// ONLY the item/index (`item.key === 'messages'`) is exactly as
|
|
783
|
+
// compile-time-bakeable as the #2798 static path already assumes —
|
|
784
|
+
// regardless of whether its branches are a bare element or a child
|
|
785
|
+
// component (`SocialShell`'s `NAV_ITEMS.map()` row: `{item.key ===
|
|
786
|
+
// 'messages' ? <SocialUnreadBadge /> : null}` inside a static
|
|
787
|
+
// function-local array — the static `outer-nested`/`inner-loop-nested`
|
|
788
|
+
// init already tolerates an absent-this-row child component). Only a
|
|
789
|
+
// condition that can ITSELF change value later (`flag()`, or a mixed
|
|
790
|
+
// `item.x && flag()`) needs the escalation #2897 actually fixed.
|
|
791
|
+
const staticRowHasConditional = !projectionInner && l.isStaticArray && !l.childComponent
|
|
792
|
+
&& bindings.conditionals.some(c => c.reactiveBeyondLoopScope)
|
|
793
|
+
const staticInnerLoopHasConditional = !projectionInner && l.isStaticArray && !l.childComponent
|
|
794
|
+
&& (innerLoops ?? []).some(il => il.bindings.conditionals.some(c => c.reactiveBeyondLoopScope))
|
|
795
|
+
const hasInnerStructure = (l.nestedComponents?.length ?? 0) > 0 || (innerLoops?.length ?? 0) > 0
|
|
796
|
+
// An own-row conditional alone needs no inner structure to reconcile —
|
|
797
|
+
// it falls through (via `clientIsStaticArray` below) straight to the
|
|
798
|
+
// plain dynamic-array path, which already wires a bare conditional's
|
|
799
|
+
// live branch-swap correctly (`buildLoopReactiveEffectsPlan`, proven
|
|
800
|
+
// by the identical shape on a signal-backed array). But when the row
|
|
801
|
+
// ALSO has a sibling inner loop (`hasInnerStructure`), that plain path
|
|
802
|
+
// (`buildPlainLoopPlan`) never reads `TopLevelLoop.innerLoops` at all —
|
|
803
|
+
// only the composite and static-array-child-init plans do — so an
|
|
804
|
+
// inner loop's own ref/reactive-attr/reactive-text bindings would be
|
|
805
|
+
// silently dropped (pullfrog review on #2899: confirmed by compiling
|
|
806
|
+
// a static row with a signal-driven sibling conditional next to an
|
|
807
|
+
// inner loop with a `ref` — the ref callback never appeared in the
|
|
808
|
+
// emitted JS). Escalating to the composite path here, exactly as an
|
|
809
|
+
// inner-loop conditional already does, keeps those bindings wired.
|
|
810
|
+
// An inner-loop conditional always DOES need reconciliation on its
|
|
811
|
+
// own (no `hasInnerStructure` gate needed there — `innerLoops` being
|
|
812
|
+
// non-empty is exactly what `hasInnerStructure` already asks), since
|
|
813
|
+
// #2798's static machinery it would otherwise take has no conditional
|
|
814
|
+
// support to add to — `buildTopLevelCompositePlan` already handles
|
|
815
|
+
// nested loops of arbitrary depth, including the conditional case,
|
|
816
|
+
// unconditionally.
|
|
817
|
+
const useElementReconciliation =
|
|
818
|
+
reconciliationBase || staticInnerLoopHasConditional || (staticRowHasConditional && hasInnerStructure)
|
|
819
|
+
// The client-side `isStaticArray` field (NOT the IR-level `l.isStaticArray`,
|
|
820
|
+
// which SSR adapters like Go template's static-loop baking still read
|
|
821
|
+
// directly and are unaffected by — #2897 is client-JS-only): false
|
|
822
|
+
// whenever a conditional forces this loop off the static fast path,
|
|
823
|
+
// so `buildLoopPlan`'s dispatcher and `buildStaticArrayChildInitsPlan`
|
|
824
|
+
// both skip the static machinery for it automatically.
|
|
825
|
+
const clientIsStaticArray = l.isStaticArray && !staticRowHasConditional && !staticInnerLoopHasConditional
|
|
826
|
+
|
|
759
827
|
let template = ''
|
|
760
828
|
let templateIndexed: string | undefined
|
|
761
829
|
let staticItemTemplate: string | undefined
|
|
@@ -878,7 +946,7 @@ export function collectElements(
|
|
|
878
946
|
bindings,
|
|
879
947
|
childComponent: l.childComponent,
|
|
880
948
|
nestedComponents: l.nestedComponents,
|
|
881
|
-
isStaticArray:
|
|
949
|
+
isStaticArray: clientIsStaticArray,
|
|
882
950
|
useElementReconciliation,
|
|
883
951
|
innerLoops: (useElementReconciliation || (l.isStaticArray && innerLoops?.length)) ? innerLoops : undefined,
|
|
884
952
|
offset: resolveLoopOffset(siblingOffsets.get(l)),
|
|
@@ -1565,6 +1633,17 @@ export function collectLoopChildConditionals(
|
|
|
1565
1633
|
// accessors; classifyReactivity sees all three paths (signal/memo/prop
|
|
1566
1634
|
// + loop-param + loop-index, #2861).
|
|
1567
1635
|
if (!readsPreamble && classifyReactivity(expanded.expr, ctx, scope, expanded.freeIds).kind === 'none') return
|
|
1636
|
+
// #2897 follow-up: does the condition read anything beyond the loop
|
|
1637
|
+
// item/index? `classifyReactivity`'s `kind` can't answer this — it
|
|
1638
|
+
// reports `'loop-param'` first whenever a loop-bound name appears,
|
|
1639
|
+
// even alongside a signal read (`item.x && flag()`), which is the
|
|
1640
|
+
// right precedence for its own two typed callers but the wrong
|
|
1641
|
+
// question here. Computed independently so a static array's
|
|
1642
|
+
// escalation decision (`collect-elements.ts`'s `loop:` visitor) can
|
|
1643
|
+
// ask "can this branch decision change after first render" instead
|
|
1644
|
+
// of "does this touch the loop param at all" — see
|
|
1645
|
+
// `LoopChildConditional.reactiveBeyondLoopScope`'s doc comment.
|
|
1646
|
+
const reactiveBeyondLoopScope = n.reactive || readsPreamble || needsEffectWrapper(expanded.expr, ctx, expanded.freeIds)
|
|
1568
1647
|
|
|
1569
1648
|
const loopParamsForCond =
|
|
1570
1649
|
loopParams ?? (loopParam ? [{ param: loopParam, bindings: loopParamBindings, index: loopIndex }] : undefined)
|
|
@@ -1583,6 +1662,7 @@ export function collectLoopChildConditionals(
|
|
|
1583
1662
|
whenFalse: summarizeLoopChildBranch(n.whenFalse, ctx, siblingOffsets, loopParam, loopParamBindings, preambleNames, loopIndex),
|
|
1584
1663
|
...(expanded.freeIds !== undefined && { conditionFreeIdentifiers: expanded.freeIds }),
|
|
1585
1664
|
...(readsPreamble && { readsPreamble: true }),
|
|
1665
|
+
reactiveBeyondLoopScope,
|
|
1586
1666
|
})
|
|
1587
1667
|
},
|
|
1588
1668
|
})
|
|
@@ -625,6 +625,31 @@ export interface LoopChildConditional {
|
|
|
625
625
|
* preamble-bound identifier unexpanded on purpose (#2482 Stage 1b).
|
|
626
626
|
*/
|
|
627
627
|
readsPreamble?: boolean
|
|
628
|
+
/**
|
|
629
|
+
* `condition` can change value after first render — it reads a signal,
|
|
630
|
+
* memo, prop, or preamble-derived reactive local, as opposed to reading
|
|
631
|
+
* ONLY the (for a static array, fixed-forever) loop item/index (#2897
|
|
632
|
+
* follow-up). `classifyReactivity`'s `kind` can't answer this: it
|
|
633
|
+
* reports `'loop-param'` for ANY expression referencing a loop-bound
|
|
634
|
+
* name, even one that ALSO reads a signal (`item.x && flag()`) — that
|
|
635
|
+
* precedence is deliberate for its own two `ReactivitySource`-typed
|
|
636
|
+
* callers and must not change. This field is computed independently,
|
|
637
|
+
* at collection time, as `n.reactive || readsPreamble ||
|
|
638
|
+
* needsEffectWrapper(...)` — the same "does it need a patchable slot
|
|
639
|
+
* beyond the row's own item" question, without the loop-param
|
|
640
|
+
* short-circuit.
|
|
641
|
+
*
|
|
642
|
+
* Consumers: a static array's row/inner-loop conditional only needs
|
|
643
|
+
* escalation off the compile-time-bakeable static path when THIS is
|
|
644
|
+
* true — a condition that reads only the item/index never changes for
|
|
645
|
+
* a static array, so the row's branch decision (bare element OR child
|
|
646
|
+
* component) is correctly baked once by SSR and wired once by the
|
|
647
|
+
* static `initChild` pass, exactly as it was before #2897. A dynamic
|
|
648
|
+
* (signal-backed) array's `mapArray`/`insert()` path is unaffected by
|
|
649
|
+
* this field — every loop-param conditional is genuinely per-row
|
|
650
|
+
* reactive there, so `bindings.conditionals` itself is never filtered.
|
|
651
|
+
*/
|
|
652
|
+
reactiveBeyondLoopScope: boolean
|
|
628
653
|
}
|
|
629
654
|
|
|
630
655
|
export interface TopLevelLoop extends LoopCore {
|
|
@@ -673,7 +698,18 @@ export interface TopLevelLoop extends LoopCore {
|
|
|
673
698
|
nestedComponents?: IRLoopChildComponent[] // For nested components in loop bodies
|
|
674
699
|
// Per-item bindings (events / reactiveAttrs / reactiveTexts / refs / conditionals)
|
|
675
700
|
// now live on `LoopCore.bindings` — see issue #1244 §B.
|
|
676
|
-
|
|
701
|
+
// True when this loop takes the static forEach fast path — the IR's own
|
|
702
|
+
// `isStaticArray` (a static, non-signal array) MINUS a fast-path refusal
|
|
703
|
+
// (#2897): a conditional anywhere in the row tree (this row's own
|
|
704
|
+
// `bindings.conditionals`, or any depth-1 `innerLoops[i].bindings.conditionals`)
|
|
705
|
+
// forces this false even when the IR flag is true, since neither the static
|
|
706
|
+
// forEach bake nor `inner-loop-nested`'s clone-and-wire architecture has any
|
|
707
|
+
// conditional-handling machinery — the row falls through to the plain/
|
|
708
|
+
// composite dynamic path instead, which already wires a conditional's live
|
|
709
|
+
// branch-swap correctly. SSR adapters (e.g. Go template's static-loop
|
|
710
|
+
// baking) read the IR-level `isStaticArray` directly and are unaffected;
|
|
711
|
+
// this field is client-JS routing only.
|
|
712
|
+
isStaticArray: boolean
|
|
677
713
|
useElementReconciliation?: boolean // True: mapArray/mapArrayAnchored + composite rendering (native root with child components)
|
|
678
714
|
/** Inner loop metadata for composite element reconciliation (array, param, key, container) */
|
|
679
715
|
innerLoops?: NestedLoop[]
|