@barefootjs/jsx 0.40.1 → 0.40.2

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 (176) hide show
  1. package/dist/adapters/ambiguous-nil-arithmetic.d.ts +4 -3
  2. package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -1
  3. package/dist/adapters/boolean-result.d.ts +32 -10
  4. package/dist/adapters/boolean-result.d.ts.map +1 -1
  5. package/dist/adapters/brace-delimiter-text.d.ts +18 -0
  6. package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
  7. package/dist/adapters/callback-method.d.ts +38 -5
  8. package/dist/adapters/callback-method.d.ts.map +1 -1
  9. package/dist/adapters/computed-const.d.ts +11 -1
  10. package/dist/adapters/computed-const.d.ts.map +1 -1
  11. package/dist/adapters/dsl-template-adapter.d.ts +21 -0
  12. package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
  13. package/dist/adapters/loop-bound-names.d.ts +9 -1
  14. package/dist/adapters/loop-bound-names.d.ts.map +1 -1
  15. package/dist/adapters/loop-row-type.d.ts +52 -0
  16. package/dist/adapters/loop-row-type.d.ts.map +1 -0
  17. package/dist/adapters/nil-reading.d.ts +68 -0
  18. package/dist/adapters/nil-reading.d.ts.map +1 -0
  19. package/dist/adapters/non-primitive-equality.d.ts +33 -0
  20. package/dist/adapters/non-primitive-equality.d.ts.map +1 -0
  21. package/dist/adapters/number-value.d.ts +5 -3
  22. package/dist/adapters/number-value.d.ts.map +1 -1
  23. package/dist/adapters/operand-typing.d.ts +126 -0
  24. package/dist/adapters/operand-typing.d.ts.map +1 -0
  25. package/dist/adapters/parsed-expr-emitter.d.ts +70 -12
  26. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  27. package/dist/adapters/primitive-plan.d.ts +291 -0
  28. package/dist/adapters/primitive-plan.d.ts.map +1 -0
  29. package/dist/analyzer-context.d.ts +4 -0
  30. package/dist/analyzer-context.d.ts.map +1 -1
  31. package/dist/analyzer.d.ts.map +1 -1
  32. package/dist/augment-inherited-props.d.ts +63 -8
  33. package/dist/augment-inherited-props.d.ts.map +1 -1
  34. package/dist/compiler.d.ts.map +1 -1
  35. package/dist/event-handler-name.d.ts +12 -0
  36. package/dist/event-handler-name.d.ts.map +1 -1
  37. package/dist/expression-parser.d.ts +8 -0
  38. package/dist/expression-parser.d.ts.map +1 -1
  39. package/dist/index.d.ts +19 -9
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +4628 -2198
  42. package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
  43. package/dist/ir-to-client-js/child-props-object.d.ts +64 -0
  44. package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
  45. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  46. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  47. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  48. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  49. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +4 -2
  50. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
  51. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  52. package/dist/ir-to-client-js/imports.d.ts +2 -2
  53. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  54. package/dist/ir-to-client-js/index.d.ts +2 -6
  55. package/dist/ir-to-client-js/index.d.ts.map +1 -1
  56. package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
  57. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +30 -18
  58. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
  59. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  60. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
  61. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
  62. package/dist/jsx-to-ir.d.ts +1 -0
  63. package/dist/jsx-to-ir.d.ts.map +1 -1
  64. package/dist/nullable-signal-attr.d.ts +68 -9
  65. package/dist/nullable-signal-attr.d.ts.map +1 -1
  66. package/dist/prop-rewrite.d.ts +7 -0
  67. package/dist/prop-rewrite.d.ts.map +1 -1
  68. package/dist/props-object-whole-read.d.ts +15 -0
  69. package/dist/props-object-whole-read.d.ts.map +1 -0
  70. package/dist/rich-type-refusal.d.ts.map +1 -1
  71. package/dist/scope/binding-scope.d.ts +85 -10
  72. package/dist/scope/binding-scope.d.ts.map +1 -1
  73. package/dist/scope/lexical-scope-walk.d.ts +3 -2
  74. package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
  75. package/dist/scope/root-prop-alias.d.ts +10 -7
  76. package/dist/scope/root-prop-alias.d.ts.map +1 -1
  77. package/dist/strip-types.d.ts +13 -0
  78. package/dist/strip-types.d.ts.map +1 -1
  79. package/dist/template-position-walk.d.ts +21 -3
  80. package/dist/template-position-walk.d.ts.map +1 -1
  81. package/dist/types.d.ts +90 -0
  82. package/dist/types.d.ts.map +1 -1
  83. package/package.json +2 -2
  84. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +3 -3
  85. package/src/__tests__/binding-scope.test.ts +89 -1
  86. package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
  87. package/src/__tests__/boolean-result.test.ts +22 -5
  88. package/src/__tests__/brace-delimiter-text.test.ts +26 -0
  89. package/src/__tests__/callback-method.test.ts +62 -2
  90. package/src/__tests__/child-components-in-map.test.ts +1 -1
  91. package/src/__tests__/computed-const.test.ts +103 -0
  92. package/src/__tests__/emit-to-string-call.test.ts +2 -1
  93. package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
  94. package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
  95. package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
  96. package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
  97. package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
  98. package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
  99. package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +2 -1
  100. package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
  101. package/src/__tests__/lookup-literal-const.test.ts +9 -3
  102. package/src/__tests__/loop-index-reactivity.test.ts +2 -2
  103. package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
  104. package/src/__tests__/loop-plan.test.ts +1 -1
  105. package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
  106. package/src/__tests__/loop-row-type.test.ts +64 -0
  107. package/src/__tests__/lowering-value-position.test.ts +4 -0
  108. package/src/__tests__/module-const-destructure-default.test.ts +156 -0
  109. package/src/__tests__/nested-loop-index-param.test.ts +1 -1
  110. package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
  111. package/src/__tests__/nested-row-shadowing.test.ts +2 -2
  112. package/src/__tests__/nil-reading.test.ts +77 -0
  113. package/src/__tests__/nullable-signal-attr.test.ts +134 -0
  114. package/src/__tests__/primitive-plan.test.ts +416 -0
  115. package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
  116. package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
  117. package/src/__tests__/props-object-whole-read.test.ts +40 -0
  118. package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
  119. package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
  120. package/src/__tests__/rest-binding-gaps.test.ts +192 -0
  121. package/src/__tests__/root-prop-alias.test.ts +53 -8
  122. package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
  123. package/src/__tests__/static-record-literal-array.test.ts +171 -0
  124. package/src/__tests__/string-concat-identifier.test.ts +40 -1
  125. package/src/adapters/ambiguous-nil-arithmetic.ts +22 -13
  126. package/src/adapters/boolean-result.ts +72 -66
  127. package/src/adapters/brace-delimiter-text.ts +25 -0
  128. package/src/adapters/callback-method.ts +151 -27
  129. package/src/adapters/computed-const.ts +238 -4
  130. package/src/adapters/dsl-template-adapter.ts +40 -2
  131. package/src/adapters/loop-bound-names.ts +9 -1
  132. package/src/adapters/loop-plan.ts +1 -1
  133. package/src/adapters/loop-row-type.ts +139 -0
  134. package/src/adapters/nil-reading.ts +297 -0
  135. package/src/adapters/non-primitive-equality.ts +92 -0
  136. package/src/adapters/number-value.ts +17 -4
  137. package/src/adapters/operand-typing.ts +522 -0
  138. package/src/adapters/parsed-expr-emitter.ts +154 -19
  139. package/src/adapters/primitive-plan.ts +696 -0
  140. package/src/analyzer-context.ts +6 -0
  141. package/src/analyzer.ts +505 -19
  142. package/src/augment-inherited-props.ts +108 -10
  143. package/src/compiler.ts +17 -0
  144. package/src/event-handler-name.ts +15 -0
  145. package/src/expression-parser.ts +70 -1
  146. package/src/index.ts +33 -7
  147. package/src/ir-to-client-js/build-references.ts +69 -0
  148. package/src/ir-to-client-js/child-props-object.ts +106 -0
  149. package/src/ir-to-client-js/collect-elements.ts +30 -55
  150. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +6 -1
  151. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +29 -28
  152. package/src/ir-to-client-js/control-flow/shared.ts +20 -22
  153. package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +5 -3
  154. package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +2 -2
  155. package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +1 -1
  156. package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +2 -2
  157. package/src/ir-to-client-js/control-flow/stringify/loop.ts +1 -1
  158. package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +3 -3
  159. package/src/ir-to-client-js/html-template.ts +25 -8
  160. package/src/ir-to-client-js/imports.ts +1 -1
  161. package/src/ir-to-client-js/index.ts +53 -11
  162. package/src/ir-to-client-js/plan/build-static-array-child-init.ts +31 -14
  163. package/src/ir-to-client-js/plan/static-array-child-init.ts +27 -18
  164. package/src/ir-to-client-js/prop-handling.ts +22 -3
  165. package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
  166. package/src/jsx-to-ir.ts +446 -37
  167. package/src/nullable-signal-attr.ts +246 -29
  168. package/src/prop-rewrite.ts +30 -6
  169. package/src/props-object-whole-read.ts +87 -0
  170. package/src/rich-type-refusal.ts +2 -1
  171. package/src/scope/binding-scope.ts +182 -18
  172. package/src/scope/lexical-scope-walk.ts +4 -3
  173. package/src/scope/root-prop-alias.ts +15 -8
  174. package/src/strip-types.ts +31 -3
  175. package/src/template-position-walk.ts +57 -28
  176. package/src/types.ts +87 -0
@@ -13,7 +13,9 @@
13
13
  import ts from 'typescript'
14
14
 
15
15
  import type { ComponentIR, IRMetadata, IRNode, IRElement, IRTemplatePart, TypeInfo } from './types.ts'
16
+ import type { ParsedExpr } from './expression-parser.ts'
16
17
  import { isBooleanAttr } from './html-constants.ts'
18
+ import { literalMemberIndex } from './literal-member-index.ts'
17
19
 
18
20
  /**
19
21
  * A `const x = useContext(SomeContext)` consumer in a component body. SSR
@@ -466,13 +468,56 @@ export function collectModuleStringConsts(
466
468
  return map
467
469
  }
468
470
 
471
+ type StaticConstants = IRMetadata['localConstants'] | undefined
472
+
473
+ /**
474
+ * The const declaration a read of `name` reaches, when its declared value
475
+ * is what the read sees — the ONE decision behind every compile-time lookup
476
+ * of a literal const's member (`lookupStaticRecordLiteral`,
477
+ * `lookupStaticArrayElement`, the Go adapter's record lookup). Returns
478
+ * `null` when the read must fall back to the generic lowering:
479
+ *
480
+ * - `isShadowed(name)`: an enclosing loop callback's param, index,
481
+ * destructure or preamble binding of the name is what the read reaches;
482
+ * - a component-scope declaration of the name is the binding — exactly
483
+ * one `const` whose value the component does not change during render
484
+ * (`mutatedAfterDeclaration`) and that is not a destructure of the props
485
+ * object. A `let`, or more than one component-scope entry of the name
486
+ * (the IR does not record which block each sits in, so which one a read
487
+ * reaches is unknown), is refused, and so is the module const of the
488
+ * same name, which the component-scope binding hides;
489
+ * - otherwise the module const of the name, unless a prop or another
490
+ * component binding shadows it (`shadowedInComponent`) or the component
491
+ * changes it during render.
492
+ */
493
+ export function resolveStaticConstBinding(
494
+ name: string,
495
+ constants: StaticConstants,
496
+ isShadowed: (name: string) => boolean,
497
+ ): IRMetadata['localConstants'][number] | null {
498
+ if (isShadowed(name)) return null
499
+ const named = (constants ?? []).filter(c => c.name === name)
500
+ const local = named.filter(c => !c.isModule)
501
+ if (local.length > 0) {
502
+ const c = local[0]
503
+ if (local.length > 1 || c.declarationKind !== 'const' || c.propsDestructure) return null
504
+ return c.mutatedAfterDeclaration ? null : c
505
+ }
506
+ const moduleConst = named.find(c => c.isModule)
507
+ if (!moduleConst || moduleConst.shadowedInComponent || moduleConst.mutatedAfterDeclaration) return null
508
+ return moduleConst
509
+ }
510
+
469
511
  /**
470
- * Resolve `IDENT.key` / `IDENT['key']` where `IDENT` is a module-scope
471
- * object-literal const and the key/value are static literals — a fully
512
+ * Resolve `IDENT.key` / `IDENT['key']` where `IDENT` is a module-scope or
513
+ * component-local object-literal const and the key/value are static
514
+ * literals — a fully
472
515
  * compile-time lookup (the icon registry's `strokePaths['chevron-down']`,
473
516
  * pagination's `variantClasses.ghost`; #1896 / #1897). Returns the
474
517
  * looked-up scalar, or `null` for any other shape so callers fall back
475
- * to their generic lowering. Shared by all seven template-string adapters;
518
+ * to their generic lowering. A module array-literal const read with a
519
+ * literal index (`QS[0]`) goes through `lookupStaticArrayElement` instead.
520
+ * Shared by all seven template-string adapters;
476
521
  * the prop-KEYED variant of the pattern lives in `parseRecordIndexAccess`.
477
522
  *
478
523
  * `isShadowed` is REQUIRED (#2482 Stage 2) rather than left to caller
@@ -488,11 +533,10 @@ export function collectModuleStringConsts(
488
533
  export function lookupStaticRecordLiteral(
489
534
  objectName: string,
490
535
  key: string,
491
- constants: IRMetadata['localConstants'] | undefined,
536
+ constants: StaticConstants,
492
537
  isShadowed: (name: string) => boolean,
493
538
  ): { kind: 'string' | 'number'; text: string } | null {
494
- if (isShadowed(objectName)) return null
495
- const constInfo = (constants ?? []).find(c => c.name === objectName && c.isModule)
539
+ const constInfo = resolveStaticConstBinding(objectName, constants, isShadowed)
496
540
  if (constInfo?.value === undefined) return null
497
541
  const sf = ts.createSourceFile(
498
542
  '__rec.ts', `(${constInfo.value})`, ts.ScriptTarget.Latest, /*setParentNodes*/ true,
@@ -522,6 +566,60 @@ export function lookupStaticRecordLiteral(
522
566
  return null
523
567
  }
524
568
 
569
+ /**
570
+ * The element at `key` of a module or component-local array-literal const
571
+ * (`QS[0]`, `QS['1']`)
572
+ * or its `length`, as the element's own typed `ParsedExpr`: a string or
573
+ * number `literal`, or a unary `-` over a number literal. The caller emits
574
+ * it through its own expression emitter, exactly as if the source had
575
+ * written the element in place, so a value such as `-0` keeps its sign in
576
+ * arithmetic and a comparison (`1 / QS[0] < 0`) and is spelled `0` only
577
+ * where JS stringifies it. An index key must be canonical (`'01'` names no
578
+ * element). A hole or spread at or before the element (either also makes
579
+ * `length` unknown), an out-of-range index or any other element shape is
580
+ * `null`, so the caller falls back to its generic lowering.
581
+ *
582
+ * `isShadowed` is REQUIRED for the same reason as in
583
+ * `lookupStaticRecordLiteral`; which declaration the name reaches is
584
+ * `resolveStaticConstBinding`'s decision.
585
+ */
586
+ export function lookupStaticArrayElement(
587
+ objectName: string,
588
+ key: string,
589
+ constants: StaticConstants,
590
+ isShadowed: (name: string) => boolean,
591
+ ): ParsedExpr | null {
592
+ const array = resolveStaticConstBinding(objectName, constants, isShadowed)?.parsed
593
+ if (array?.kind !== 'array-literal') return null
594
+ if (key === 'length') {
595
+ if (array.elements.some(e => e.kind === 'unsupported')) return null
596
+ return { kind: 'literal', value: array.elements.length, literalType: 'number', raw: String(array.elements.length) }
597
+ }
598
+ const index = literalMemberIndex(key)
599
+ if (index.kind !== 'literal' || typeof index.value !== 'number') return null
600
+ const upTo = array.elements.slice(0, index.value + 1)
601
+ if (upTo.length <= index.value || upTo.some(e => e.kind === 'unsupported')) return null
602
+ const element = upTo[index.value]
603
+ if (element.kind === 'literal' && (element.literalType === 'string' || element.literalType === 'number')) return element
604
+ if (
605
+ element.kind === 'unary' && element.op === '-' &&
606
+ element.argument.kind === 'literal' && element.argument.literalType === 'number'
607
+ ) {
608
+ return element
609
+ }
610
+ return null
611
+ }
612
+
613
+ /**
614
+ * A literal-initialized const's value. A number carries its `value` (a
615
+ * `const NZ = -0` is negative zero, which `text` cannot say), which each
616
+ * adapter spells through the primitive plan's number-literal decision
617
+ * (#3824 step 6), as it does a literal written in place.
618
+ */
619
+ export type LiteralConst =
620
+ | { kind: 'number'; text: string; value: number }
621
+ | { kind: 'string' | 'boolean' | 'null'; text: string }
622
+
525
623
  /**
526
624
  * Resolve a bare identifier bound to a literal-initialized `const` — module
527
625
  * or component-function scope — to that literal, or `null` for any other
@@ -542,7 +640,7 @@ export function lookupLiteralConst(
542
640
  name: string,
543
641
  constants: IRMetadata['localConstants'] | undefined,
544
642
  isShadowed: (name: string) => boolean,
545
- ): { kind: 'string' | 'number' | 'boolean' | 'null'; text: string } | null {
643
+ ): LiteralConst | null {
546
644
  if (isShadowed(name)) return null
547
645
  const c = (constants ?? []).find(lc => lc.name === name)
548
646
  if (c?.value === undefined) return null
@@ -552,7 +650,7 @@ export function lookupLiteralConst(
552
650
  if (p.literalType === 'boolean' && typeof p.value === 'boolean') return { kind: 'boolean', text: String(p.value) }
553
651
  if (p.literalType === 'null') return { kind: 'null', text: 'null' }
554
652
  if (p.literalType === 'number' && typeof p.value === 'number' && Number.isFinite(p.value)) {
555
- return { kind: 'number', text: String(p.value) }
653
+ return { kind: 'number', text: String(p.value), value: p.value }
556
654
  }
557
655
  }
558
656
  if (
@@ -560,11 +658,11 @@ export function lookupLiteralConst(
560
658
  p.argument.kind === 'literal' && p.argument.literalType === 'number' &&
561
659
  typeof p.argument.value === 'number' && Number.isFinite(p.argument.value)
562
660
  ) {
563
- return { kind: 'number', text: String(-p.argument.value) }
661
+ return { kind: 'number', text: String(-p.argument.value), value: -p.argument.value }
564
662
  }
565
663
  if (p) return null
566
664
  const v = c.value.trim()
567
- if (/^-?\d+(\.\d+)?$/.test(v)) return { kind: 'number', text: v }
665
+ if (/^-?\d+(\.\d+)?$/.test(v)) return { kind: 'number', text: v, value: Number(v) }
568
666
  if (v === 'true' || v === 'false') return { kind: 'boolean', text: v }
569
667
  if (v === 'null') return { kind: 'null', text: 'null' }
570
668
  const strLit = /^'([^'\\]*)'$/.exec(v) ?? /^"([^"\\]*)"$/.exec(v)
package/src/compiler.ts CHANGED
@@ -27,6 +27,7 @@ import { applyCssLayerPrefix, applyCssLayerPrefixToFile } from './css-layer-pref
27
27
  import { preprocessInlineJsxCallbacks } from './preprocess-inline-jsx-callbacks.ts'
28
28
  import { extractSsrDefaults } from './ssr-defaults.ts'
29
29
  import { computeSsrSeedPlan } from './ssr-seed-plan.ts'
30
+ import { componentScopeNames } from './adapters/computed-const.ts'
30
31
  import { checkRichTypeMethodCalls, checkRichTypePropSerialization } from './rich-type-refusal.ts'
31
32
  import { checkAuthoredFormatDateCalls } from './format-date-refusal.ts'
32
33
  import { checkAsyncActionReads } from './async-action-refusal.ts'
@@ -677,8 +678,10 @@ export function buildMetadata(
677
678
  typeParameters: componentTypeParametersText(ctx.componentNode, ctx.sourceFile),
678
679
  propsParams: ctx.propsParams,
679
680
  propsObjectName: ctx.propsObjectName,
681
+ propsObjectObservedWhole: ctx.propsObjectObservedWhole,
680
682
  restPropsName: ctx.restPropsName,
681
683
  restPropsExpandedKeys: ctx.restPropsExpandedKeys,
684
+ restPropsOmittedKeys: ctx.restPropsOmittedKeys,
682
685
  signals: ctx.signals,
683
686
  memos: ctx.memos,
684
687
  effects: ctx.effects,
@@ -698,12 +701,26 @@ export function buildMetadata(
698
701
  localFunctions: ctx.localFunctions,
699
702
  localConstants: ctx.localConstants,
700
703
  }
704
+ metadata.localConstants = markShadowedModuleConsts(metadata)
701
705
  // Computed from the assembled metadata (not `ctx`): the plan reads the
702
706
  // exact fields adapters see, so the two can never disagree.
703
707
  metadata.ssrSeedPlan = computeSsrSeedPlan(metadata)
704
708
  return metadata
705
709
  }
706
710
 
711
+ /**
712
+ * `metadata.localConstants` with each module const the component scope
713
+ * shadows marked `shadowedInComponent` (`const QS = [...]` beside
714
+ * `function C({ QS })`). A copy, not a mutation: the analyzer's records may
715
+ * be shared by the other components of the file.
716
+ */
717
+ function markShadowedModuleConsts(metadata: IRMetadata): IRMetadata['localConstants'] {
718
+ const names = componentScopeNames(metadata)
719
+ return metadata.localConstants.map(c =>
720
+ c.isModule && names.has(c.name) ? { ...c, shadowedInComponent: true } : c,
721
+ )
722
+ }
723
+
707
724
  // =============================================================================
708
725
  // Main Entry Point
709
726
  // =============================================================================
@@ -12,3 +12,18 @@
12
12
  export function isEventHandlerName(name: string): boolean {
13
13
  return /^on[A-Z]/.test(name)
14
14
  }
15
+
16
+ /**
17
+ * Whether a prop can travel in the `bf-p` hydration payload by name: not an
18
+ * event handler (`isEventHandlerName`, wired through the listener path) and
19
+ * not `__`-prefixed hydration plumbing. A data prop named `one`, `only` or
20
+ * `once` travels like any other (#3845).
21
+ *
22
+ * Shared by the reference adapter's serialization (`propsToSerialize`) and
23
+ * the two compile-time classifications of the same set (`computeLiveOnlyProps`,
24
+ * `checkRichTypePropSerialization`), so the three agree on which props the
25
+ * payload carries.
26
+ */
27
+ export function isHydrationPayloadPropName(name: string): boolean {
28
+ return !isEventHandlerName(name) && !name.startsWith('__')
29
+ }
@@ -3070,6 +3070,63 @@ export function inlineBinding(
3070
3070
  return captured ? null : result
3071
3071
  }
3072
3072
 
3073
+ /**
3074
+ * `expr` rebuilt top-down: `replace` sees each node with the param names the
3075
+ * arrows enclosing it bind, and returns the node's replacement (not walked
3076
+ * further) or `null` to keep the node and walk its children. Every
3077
+ * `ParsedExpr` kind is handled, so a new kind surfaces as a compile error
3078
+ * here.
3079
+ */
3080
+ export function mapParsedExpr(
3081
+ expr: ParsedExpr,
3082
+ replace: (e: ParsedExpr, enclosing: ReadonlySet<string>) => ParsedExpr | null,
3083
+ enclosing: ReadonlySet<string> = new Set(),
3084
+ ): ParsedExpr {
3085
+ const replaced = replace(expr, enclosing)
3086
+ if (replaced) return replaced
3087
+ const walk = (e: ParsedExpr): ParsedExpr => mapParsedExpr(e, replace, enclosing)
3088
+ switch (expr.kind) {
3089
+ case 'call':
3090
+ return { kind: 'call', callee: walk(expr.callee), args: expr.args.map(walk) }
3091
+ case 'member':
3092
+ return { ...expr, object: walk(expr.object) }
3093
+ case 'index-access':
3094
+ return { kind: 'index-access', object: walk(expr.object), index: walk(expr.index) }
3095
+ case 'binary':
3096
+ return { kind: 'binary', op: expr.op, left: walk(expr.left), right: walk(expr.right) }
3097
+ case 'logical':
3098
+ return { kind: 'logical', op: expr.op, left: walk(expr.left), right: walk(expr.right) }
3099
+ case 'unary':
3100
+ return { kind: 'unary', op: expr.op, argument: walk(expr.argument) }
3101
+ case 'conditional':
3102
+ return { kind: 'conditional', test: walk(expr.test), consequent: walk(expr.consequent), alternate: walk(expr.alternate) }
3103
+ case 'template-literal':
3104
+ return { kind: 'template-literal', parts: expr.parts.map(p => (p.type === 'expression' ? { type: 'expression', expr: walk(p.expr) } : p)) }
3105
+ case 'arrow': {
3106
+ const inner = expr.params.length === 0 ? enclosing : new Set([...enclosing, ...expr.params])
3107
+ return { ...expr, body: mapParsedExpr(expr.body, replace, inner) }
3108
+ }
3109
+ case 'array-literal':
3110
+ return { kind: 'array-literal', elements: expr.elements.map(walk) }
3111
+ case 'array-method':
3112
+ if (expr.method === 'flat') {
3113
+ return { ...expr, object: walk(expr.object), ...(expr.depthExpr ? { depthExpr: walk(expr.depthExpr) } : {}) }
3114
+ }
3115
+ return { ...expr, object: walk(expr.object), args: expr.args.map(walk) }
3116
+ case 'object-literal':
3117
+ return {
3118
+ kind: 'object-literal',
3119
+ raw: expr.raw,
3120
+ properties: expr.properties.map(p => (p.kind === 'spread' ? { ...p, expr: walk(p.expr) } : { ...p, value: walk(p.value) })),
3121
+ }
3122
+ case 'identifier':
3123
+ case 'literal':
3124
+ case 'regex':
3125
+ case 'unsupported':
3126
+ return expr
3127
+ }
3128
+ }
3129
+
3073
3130
  /**
3074
3131
  * Fold a value-producing block body — a {@link ParsedStatement} sequence of
3075
3132
  * `const` bindings, value-producing `if` / early `return`, and a terminal
@@ -3786,10 +3843,22 @@ export function readsNestedCallbackIndex(expr: ParsedExpr): boolean {
3786
3843
  }
3787
3844
 
3788
3845
  /** Build the evaluator's minimal node object, or null for an out-of-surface kind. */
3846
+ /**
3847
+ * A literal's evaluator node. JSON has no `NaN` / `Infinity` (the
3848
+ * nil-reading rewrite's `x ?? NaN`, #3824 step 4): such a literal travels as
3849
+ * the builtin `Number('NaN')`.
3850
+ */
3851
+ function literalEvalNode(value: string | number | boolean | null): Record<string, unknown> {
3852
+ if (typeof value === 'number' && !Number.isFinite(value)) {
3853
+ return { kind: 'call', callee: { kind: 'identifier', name: 'Number' }, args: [{ kind: 'literal', value: String(value) }] }
3854
+ }
3855
+ return { kind: 'literal', value }
3856
+ }
3857
+
3789
3858
  function toEvalNode(e: ParsedExpr): Record<string, unknown> | null {
3790
3859
  switch (e.kind) {
3791
3860
  case 'literal':
3792
- return { kind: 'literal', value: e.value }
3861
+ return literalEvalNode(e.value)
3793
3862
  case 'identifier':
3794
3863
  return { kind: 'identifier', name: e.name }
3795
3864
  case 'binary': {
package/src/index.ts CHANGED
@@ -19,7 +19,7 @@ export { isOpaqueLocalAccessorName } from './opaque-local-accessor.ts'
19
19
  // The one event-handler-name classification (`on[A-Z]`) — `only`/`once` are
20
20
  // data props, not handlers. Shared by the IR builder, client-JS prop wiring
21
21
  // and the SSR adapters' "a handler has no SSR value" skips.
22
- export { isEventHandlerName } from './event-handler-name.ts'
22
+ export { isEventHandlerName, isHydrationPayloadPropName } from './event-handler-name.ts'
23
23
  export { fallbackChainParts, nullishChainParts } from './children-nullish.ts'
24
24
 
25
25
  // Shared props-destructure binding + alias-map helpers (#2524, #2788)
@@ -130,8 +130,27 @@ export type {
130
130
  export { JsxAdapter } from './adapters/jsx-adapter.ts'
131
131
  export type { JsxAdapterConfig } from './adapters/jsx-adapter.ts'
132
132
  export { rewriteImportsForTemplate, rewriteDynamicImportsInSource } from './adapters/template-imports.ts'
133
- export { emitParsedExpr, arithmeticHelper, groupBinaryOperand, groupUnaryOperand, groupObjectLiteralSegments, isStringTypedOperand, isStringConcatBinary, isNumericArithmeticResult } from './adapters/parsed-expr-emitter.ts'
133
+ export { emitParsedExpr, emitParsedValue, primitivePlanFor, arithmeticHelper, groupBinaryOperand, groupUnaryOperand, groupObjectLiteralSegments, isStringTypedOperand, isStringConcatBinary, isNumericArithmeticResult } from './adapters/parsed-expr-emitter.ts'
134
+ export type { StringNameTest } from './adapters/parsed-expr-emitter.ts'
135
+ export { concatType, declaredValueTypes, loopRowType, type DeclaredValueTypes } from './adapters/loop-row-type.ts'
134
136
  export { collectNumberValueNames, isNumberValuedExpr, numberValuedSeedNames } from './adapters/number-value.ts'
137
+ export { operandFacts, planPrimitive, planTest, spellTest, primitivesAt, isNativePlan, relationalHelper, isNativeToNumber, booleanValueSites, planTextDisplay, foldNegatedNumberLiteral, numberLiteralSpelling, isRuntimeNumberLiteral, plusSpelling, planToString, needsToString, strictEqSpelling, mayHoldObject, isNullishTest, isNilLiteralOperand, NON_PRIMITIVE_EQUALITY_REASON } from './adapters/primitive-plan.ts'
138
+ export { refuseNonPrimitiveStrictEquality, type NonPrimitiveEqualitySink } from './adapters/non-primitive-equality.ts'
139
+ export { rewriteNilReadings, rewriteNilReadingsDeep, isNilReadingFallback, NAN_LITERAL } from './adapters/nil-reading.ts'
140
+ export type { PrimitiveOp, Proof, NativeSafeTable, PrimitiveHelper, PrimitivePlan, PrimitiveInputs, TextDisplay, NumberLiteralSpelling, PlusSpelling, StrictEqSpelling } from './adapters/primitive-plan.ts'
141
+ export {
142
+ collectOperandTyping,
143
+ EMPTY_OPERAND_TYPING,
144
+ UNKNOWN_FACTS,
145
+ factsOf,
146
+ factsOfType,
147
+ isExactly,
148
+ isNeverNil,
149
+ provenPrimitive,
150
+ typingFromNameTest,
151
+ unionFacts,
152
+ } from './adapters/operand-typing.ts'
153
+ export type { OperandFacts, OperandType, NilReading, OperandTyping, ScopedOperandTyping, OperandTypingOptions } from './adapters/operand-typing.ts'
135
154
  export { collectBooleanCarrierNames, mayYieldBooleanCarrier } from './adapters/boolean-carrier.ts'
136
155
  export type { ArithmeticHelper, ParsedExprEmitter, LoweringEmitter, HigherOrderMethod, ArrayMethod, SortMethod, LiteralType } from './adapters/parsed-expr-emitter.ts'
137
156
  export { collectLoopBoundNames } from './adapters/loop-bound-names.ts'
@@ -147,10 +166,10 @@ export {
147
166
  export {
148
167
  collectBooleanValueNames,
149
168
  isAriaBooleanAttr,
150
- isBooleanResultExpr,
169
+ isBooleanResultExpr, planTestOfSource,
151
170
  isBooleanResultParsed,
152
171
  isExplicitStringCall,
153
- stringifyBooleanTernaryBranches,
172
+ stringifyBooleanBranches,
154
173
  } from './adapters/boolean-result.ts'
155
174
  export { isBareStringLiteral, isLiteralIndex, isStringTypeInfo } from './adapters/literal-value.ts'
156
175
  export { collectBooleanTypedProps, collectNullableOptionalProps, isBooleanTypedPropRef } from './adapters/prop-classes.ts'
@@ -164,6 +183,7 @@ export {
164
183
  } from './adapters/array-method.ts'
165
184
  export {
166
185
  emitCallbackEval,
186
+ evalBody,
167
187
  isPredicateMethod,
168
188
  lowerCallbackMethod,
169
189
  lowerSortComparator,
@@ -176,12 +196,14 @@ export {
176
196
  sortRefusal,
177
197
  } from './adapters/callback-method.ts'
178
198
  export type {
199
+ CallbackCapture,
179
200
  CallbackEvalHelper,
180
201
  CallbackEvalOperand,
181
202
  CallbackEvalPlan,
182
203
  CallbackEvalSpelling,
183
204
  CallbackLoweringOptions,
184
205
  CallbackPlan,
206
+ EvalBody,
185
207
  PredicateMethod,
186
208
  SortPlan,
187
209
  } from './adapters/callback-method.ts'
@@ -226,6 +248,8 @@ export {
226
248
  collectAbsentPropSeedReads,
227
249
  collectNullableSignalGetters,
228
250
  collectNullishAttrContext,
251
+ nullablePropsYieldedBy,
252
+ isNeverNullishOperand,
229
253
  isNullableSignal,
230
254
  isNullishLiteral,
231
255
  NULL_COERCING_ARITHMETIC_OPS,
@@ -264,6 +288,7 @@ export type { IRNodeEmitter, EmitIRNode } from './adapters/ir-node-emitter.ts'
264
288
  export { emitAttrValue } from './adapters/attr-value-emitter.ts'
265
289
  export type { AttrValueEmitter } from './adapters/attr-value-emitter.ts'
266
290
  export { escapeLineStatementSigil } from './adapters/line-statement-text.ts'
291
+ export { escapeBraceDelimiters } from './adapters/brace-delimiter-text.ts'
267
292
  export {
268
293
  isDangerousInnerHtmlAttr,
269
294
  resolveDangerousInnerHtml,
@@ -360,8 +385,9 @@ export type { LoopChainInputs } from './loop-chain.ts'
360
385
  export { isLowerableLoopDestructure, isLowerableObjectRestDestructure } from './loop-destructure.ts'
361
386
 
362
387
  // Binding scope (#2482) — shared loop-bound-name resolution service
363
- export { BindingScope } from './scope/binding-scope.ts'
364
- export type { ScopeBindingSource, ScopeBinding, ScopeFrame, LoopBindingSource } from './scope/binding-scope.ts'
388
+ export { BindingScope, PROPS_OBJECT_IDENTIFIER, propsObjectIdentifier } from './scope/binding-scope.ts'
389
+ export type { ScopeBindingSource, ScopeBinding, ScopeFrame, LoopBindingSource, LoopValueType, NamedTypeFields } from './scope/binding-scope.ts'
390
+ export { arrayElementType, isSameLoopValueShape, loopValueTypeAt, unionOfLoopValueTypes } from './scope/binding-scope.ts'
365
391
  export { isRootPropsObject, rootPropAliasNames, rootPropAliasesForLoop, rootPropReadName } from './scope/root-prop-alias.ts'
366
392
 
367
393
  // Debug analysis
@@ -477,7 +503,7 @@ export { BOOLEAN_ATTRS, isBooleanAttr } from './html-constants.ts'
477
503
  export { USE_CLIENT_DIRECTIVE, CLIENT_EXPRESSION_DIRECTIVE } from './directives.ts'
478
504
 
479
505
  // Shared props-object-pattern helpers for the Go / Mojo template adapters
480
- export { augmentInheritedPropAccesses, parseRecordIndexAccess, evalStringArrayJoin, collectModuleStringConsts, lookupStaticRecordLiteral, lookupLiteralConst, collectContextConsumers, contextConsumerSeed } from './augment-inherited-props.ts'
506
+ export { augmentInheritedPropAccesses, parseRecordIndexAccess, evalStringArrayJoin, collectModuleStringConsts, lookupStaticRecordLiteral, lookupStaticArrayElement, resolveStaticConstBinding, lookupLiteralConst, collectContextConsumers, contextConsumerSeed, type LiteralConst } from './augment-inherited-props.ts'
481
507
  export type { RecordIndexAccess, RecordIndexEntry, ContextConsumer } from './augment-inherited-props.ts'
482
508
 
483
509
  // HTML element attribute types
@@ -406,9 +406,78 @@ export function buildReferencesGraph(ctx: ClientJsContext, irRoot: IRNode): Refe
406
406
  }
407
407
  }
408
408
 
409
+ addPropDefaultEdges(ctx, edges, propNames)
410
+
409
411
  return { edges, declaredNames, propNames }
410
412
  }
411
413
 
414
+ /**
415
+ * Phase 5 — destructure defaults of the props the client reads.
416
+ *
417
+ * A destructured prop's read is emitted as `_p.m ?? <default>`, so the
418
+ * default's own references (a module const `{ m = DEFAULT }`, a module
419
+ * helper `{ m = fmt('x') }`) are client reads too. No earlier phase sees
420
+ * them: the IR expression names only `m`. Without these edges
421
+ * `computeDeclarationScopes` drops `DEFAULT` as unused while the emitted
422
+ * effect still reads it — a `ReferenceError` at hydrate.
423
+ *
424
+ * The names come from the analyzer's `ParamInfo.defaultFreeIdentifiers`
425
+ * (the initializer's own AST), not a token scan of `defaultValue`: an
426
+ * object key, member name or callback parameter in the default
427
+ * (`{ m = ({ label: 'm' }).label }`) is no read, and an edge to it would
428
+ * keep an unrelated module `const label = …` in `initX`.
429
+ *
430
+ * Only a default whose prop is read gets traced (an unread prop's default
431
+ * is never emitted). A default that reads an earlier prop of the same
432
+ * pattern (`{ a = DEFAULT, b = a }`) renders that prop's own read in turn
433
+ * (`PropDefaultPart`), so its default is traced as well; the earlier prop
434
+ * itself is not added as an edge — the emitted text reads it as `_p.a`,
435
+ * never as the bare binding.
436
+ *
437
+ * A default is evaluated before the component body runs, so it never sees
438
+ * a body binding. A name a body binding shadows gets no edge here: the
439
+ * edge would only add the module declaration next to the body one, and
440
+ * two `const`s of one name in `initX` are a SyntaxError for the whole
441
+ * module.
442
+ */
443
+ function addPropDefaultEdges(
444
+ ctx: ClientJsContext,
445
+ edges: ReferenceEdge[],
446
+ propNames: ReadonlySet<string>,
447
+ ): void {
448
+ const bodyBound = componentBodyBindings(ctx)
449
+ const used = graphUsedIdentifiers({ edges, declaredNames: new Set(), propNames: new Set() })
450
+ const byName = new Map(ctx.propsParams.map(p => [p.name, p]))
451
+ const pending = ctx.propsParams.filter(p => used.has(p.name)).map(p => p.name)
452
+ const traced = new Set<string>()
453
+ while (pending.length > 0) {
454
+ const name = pending.pop()!
455
+ if (traced.has(name)) continue
456
+ traced.add(name)
457
+ const param = byName.get(name)
458
+ if (!param?.defaultFreeIdentifiers) continue
459
+ for (const ref of param.defaultFreeIdentifiers) {
460
+ if (propNames.has(ref)) pending.push(ref)
461
+ else if (!bodyBound.has(ref)) edges.push({ from: ROOT_SOURCE, to: ref, context: 'init-body' })
462
+ }
463
+ }
464
+ }
465
+
466
+ /** Names the component body itself binds (not module-level declarations). */
467
+ function componentBodyBindings(ctx: ClientJsContext): Set<string> {
468
+ const names = new Set<string>()
469
+ for (const c of ctx.localConstants) if (!c.isModule) names.add(c.name)
470
+ for (const f of ctx.localFunctions) if (!f.isModule) names.add(f.name)
471
+ for (const s of ctx.signals) {
472
+ if (s.isModule) continue
473
+ names.add(s.getter)
474
+ const second = signalSecondBinding(s)
475
+ if (second) names.add(second)
476
+ }
477
+ for (const m of ctx.memos) if (!m.isModule) names.add(m.name)
478
+ return names
479
+ }
480
+
412
481
  // =============================================================================
413
482
  // Graph queries (pure; no state)
414
483
  // =============================================================================
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Spelling of a child component's props object (`initChild` /
3
+ * `createComponent` / `upsertChild` argument).
4
+ *
5
+ * Every call-site builder (top-level child, loop row, composite loop row,
6
+ * conditional branch, static-array row) decides each explicit prop's own
7
+ * value spelling, then hands the list here. This module is the one place
8
+ * that decides how a spread of the component's own props object
9
+ * (`SpreadAttr.propsObject`, set in Phase 1) merges with the explicit props
10
+ * in source order (#3688, #3713):
11
+ *
12
+ * - a spread prop never becomes an entry of its own;
13
+ * - an explicit prop written before the last props-object spread is only a
14
+ * default: the caller's own value wins when the caller passed that key;
15
+ * - an explicit prop written after it wins as written;
16
+ * - every key the caller passed and the call did not name is forwarded
17
+ * through `forwardProps`, as getters, so it stays reactive.
18
+ */
19
+
20
+ import type { AttrValue } from '../types.ts'
21
+ import { PROPS_PARAM, quotePropName } from './utils.ts'
22
+
23
+ /** One explicit prop's value as a call-site builder spells it. */
24
+ export interface ChildPropValue {
25
+ expr: string
26
+ /** `get key() { return expr }` when true, `key: expr` otherwise. */
27
+ getter: boolean
28
+ }
29
+
30
+ interface ChildPropLike {
31
+ name: string
32
+ value: AttrValue
33
+ }
34
+
35
+ /** True for the IR name of a spread prop (`...` / `...expr`). */
36
+ export function isSpreadPropName(name: string): boolean {
37
+ return name.startsWith('...')
38
+ }
39
+
40
+ /**
41
+ * Index of the last spread of the component's own props object in a child
42
+ * component call (`<Tag tone="first" {...props} />`), or -1. An explicit
43
+ * prop written before it is only a default: the spread overrides it when
44
+ * the caller passed that key, as in JS (#3688). Both the props object built
45
+ * here and the top-level reactive DOM mirrors read this one decision, so an
46
+ * overridden explicit expression can't overwrite the spread-selected value
47
+ * on update.
48
+ */
49
+ export function lastPropsObjectSpreadIndex(props: readonly ChildPropLike[]): number {
50
+ let at = -1
51
+ props.forEach((p, index) => {
52
+ if (p.value.kind === 'spread' && p.value.propsObject === true) at = index
53
+ })
54
+ return at
55
+ }
56
+
57
+ /**
58
+ * The value of an explicit prop a later props-object spread overrides (see
59
+ * `lastPropsObjectSpreadIndex`): the caller's own value when it passed the
60
+ * key, `expr` otherwise. `forwardProps` lets every override win, so the
61
+ * merge is spelled into the value itself.
62
+ */
63
+ export function callerFirstPropExpr(name: string, expr: string): string {
64
+ const key = JSON.stringify(name)
65
+ return `(${key} in ${PROPS_PARAM} ? ${PROPS_PARAM}[${key}] : ${expr})`
66
+ }
67
+
68
+ export interface ChildPropsObjectOptions {
69
+ /** Entries appended after the explicit props (e.g. a row's `children` getter). */
70
+ trailing?: readonly string[]
71
+ /**
72
+ * Forward the caller's props even without a props-object spread — the
73
+ * top-level path's `{...rest}` forwarding, which predates the marker.
74
+ */
75
+ forwardCaller?: boolean
76
+ }
77
+
78
+ /**
79
+ * Build a child component's props object expression. `valueOf` spells one
80
+ * explicit prop's value, or returns null to leave it out (e.g. `key`).
81
+ */
82
+ export function childPropsObjectExpr<P extends ChildPropLike>(
83
+ props: readonly P[],
84
+ valueOf: (prop: P) => ChildPropValue | null,
85
+ options: ChildPropsObjectOptions = {},
86
+ ): string {
87
+ const spreadAt = lastPropsObjectSpreadIndex(props)
88
+ const entries: string[] = []
89
+ const explicitNames: string[] = []
90
+ props.forEach((prop, index) => {
91
+ if (isSpreadPropName(prop.name)) return
92
+ const value = valueOf(prop)
93
+ if (!value) return
94
+ explicitNames.push(prop.name)
95
+ const key = quotePropName(prop.name)
96
+ if (index < spreadAt) {
97
+ entries.push(`get ${key}() { return ${callerFirstPropExpr(prop.name, value.expr)} }`)
98
+ } else {
99
+ entries.push(value.getter ? `get ${key}() { return ${value.expr} }` : `${key}: ${value.expr}`)
100
+ }
101
+ })
102
+ entries.push(...(options.trailing ?? []))
103
+ const object = entries.length > 0 ? `{ ${entries.join(', ')} }` : '{}'
104
+ if (spreadAt < 0 && !options.forwardCaller) return object
105
+ return `forwardProps(${PROPS_PARAM}, ${object}, ${JSON.stringify(explicitNames)})`
106
+ }