@barefootjs/jsx 0.40.1 → 0.41.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 (211) hide show
  1. package/dist/adapters/boolean-result.d.ts +32 -10
  2. package/dist/adapters/boolean-result.d.ts.map +1 -1
  3. package/dist/adapters/brace-delimiter-text.d.ts +18 -0
  4. package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
  5. package/dist/adapters/callback-method.d.ts +45 -5
  6. package/dist/adapters/callback-method.d.ts.map +1 -1
  7. package/dist/adapters/component-props.d.ts +3 -4
  8. package/dist/adapters/component-props.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 +24 -12
  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 +59 -0
  16. package/dist/adapters/loop-row-type.d.ts.map +1 -0
  17. package/dist/adapters/nil-reading.d.ts +63 -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 +80 -12
  26. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  27. package/dist/adapters/primitive-plan.d.ts +320 -0
  28. package/dist/adapters/primitive-plan.d.ts.map +1 -0
  29. package/dist/adapters/prop-classes.d.ts +5 -0
  30. package/dist/adapters/prop-classes.d.ts.map +1 -1
  31. package/dist/analyzer-context.d.ts +4 -0
  32. package/dist/analyzer-context.d.ts.map +1 -1
  33. package/dist/analyzer.d.ts.map +1 -1
  34. package/dist/augment-inherited-props.d.ts +63 -8
  35. package/dist/augment-inherited-props.d.ts.map +1 -1
  36. package/dist/compiler.d.ts.map +1 -1
  37. package/dist/event-handler-name.d.ts +12 -0
  38. package/dist/event-handler-name.d.ts.map +1 -1
  39. package/dist/expression-parser.d.ts +8 -0
  40. package/dist/expression-parser.d.ts.map +1 -1
  41. package/dist/index.d.ts +20 -11
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +4903 -2311
  44. package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
  45. package/dist/ir-to-client-js/child-props-object.d.ts +106 -0
  46. package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
  47. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  48. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  49. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  50. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  51. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +4 -2
  52. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
  53. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  54. package/dist/ir-to-client-js/imports.d.ts +2 -2
  55. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  56. package/dist/ir-to-client-js/index.d.ts +2 -6
  57. package/dist/ir-to-client-js/index.d.ts.map +1 -1
  58. package/dist/ir-to-client-js/phases/props-extraction.d.ts +3 -3
  59. package/dist/ir-to-client-js/plan/build-declaration-emit.d.ts.map +1 -1
  60. package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
  61. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +30 -18
  62. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
  63. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  64. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +3 -1
  65. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
  66. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
  67. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
  68. package/dist/jsx-to-ir.d.ts +1 -0
  69. package/dist/jsx-to-ir.d.ts.map +1 -1
  70. package/dist/nullable-signal-attr.d.ts +51 -34
  71. package/dist/nullable-signal-attr.d.ts.map +1 -1
  72. package/dist/prop-rewrite.d.ts +7 -0
  73. package/dist/prop-rewrite.d.ts.map +1 -1
  74. package/dist/props-binding.d.ts +58 -13
  75. package/dist/props-binding.d.ts.map +1 -1
  76. package/dist/props-object-whole-read.d.ts +15 -0
  77. package/dist/props-object-whole-read.d.ts.map +1 -0
  78. package/dist/rich-type-refusal.d.ts.map +1 -1
  79. package/dist/scope/binding-scope.d.ts +85 -10
  80. package/dist/scope/binding-scope.d.ts.map +1 -1
  81. package/dist/scope/lexical-scope-walk.d.ts +3 -2
  82. package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
  83. package/dist/scope/root-prop-alias.d.ts +10 -7
  84. package/dist/scope/root-prop-alias.d.ts.map +1 -1
  85. package/dist/ssr-defaults.d.ts +39 -3
  86. package/dist/ssr-defaults.d.ts.map +1 -1
  87. package/dist/strip-types.d.ts +13 -0
  88. package/dist/strip-types.d.ts.map +1 -1
  89. package/dist/template-position-walk.d.ts +21 -3
  90. package/dist/template-position-walk.d.ts.map +1 -1
  91. package/dist/types.d.ts +90 -0
  92. package/dist/types.d.ts.map +1 -1
  93. package/package.json +2 -2
  94. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +9 -9
  95. package/src/__tests__/binding-scope.test.ts +89 -1
  96. package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
  97. package/src/__tests__/body-destructured-prop-default-arrow.test.ts +2 -2
  98. package/src/__tests__/body-destructured-props-live.test.ts +5 -5
  99. package/src/__tests__/boolean-result.test.ts +22 -5
  100. package/src/__tests__/brace-delimiter-text.test.ts +26 -0
  101. package/src/__tests__/callback-method.test.ts +62 -2
  102. package/src/__tests__/child-components-in-map.test.ts +1 -1
  103. package/src/__tests__/client-js-generation.test.ts +3 -3
  104. package/src/__tests__/computed-const.test.ts +103 -0
  105. package/src/__tests__/destructure-default-reads-earlier-param.test.ts +7 -7
  106. package/src/__tests__/destructured-props-live.test.ts +1 -1
  107. package/src/__tests__/emit-to-string-call.test.ts +2 -1
  108. package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
  109. package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
  110. package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
  111. package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
  112. package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
  113. package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
  114. package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +2 -1
  115. package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
  116. package/src/__tests__/issue-3861-object-spread-child.test.ts +102 -0
  117. package/src/__tests__/lookup-literal-const.test.ts +9 -3
  118. package/src/__tests__/loop-index-reactivity.test.ts +2 -2
  119. package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
  120. package/src/__tests__/loop-plan.test.ts +1 -1
  121. package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
  122. package/src/__tests__/loop-row-type.test.ts +64 -0
  123. package/src/__tests__/lowering-value-position.test.ts +4 -0
  124. package/src/__tests__/memo-body-expr.test.ts +2 -2
  125. package/src/__tests__/module-const-destructure-default.test.ts +156 -0
  126. package/src/__tests__/nested-loop-index-param.test.ts +1 -1
  127. package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
  128. package/src/__tests__/nested-row-shadowing.test.ts +2 -2
  129. package/src/__tests__/nil-reading.test.ts +78 -0
  130. package/src/__tests__/nullable-signal-attr.test.ts +109 -48
  131. package/src/__tests__/primitive-plan.test.ts +494 -0
  132. package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
  133. package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
  134. package/src/__tests__/prop-default-undefined-only.test.ts +115 -0
  135. package/src/__tests__/prop-references.test.ts +2 -2
  136. package/src/__tests__/props-destructuring.test.ts +17 -0
  137. package/src/__tests__/props-object-whole-read.test.ts +40 -0
  138. package/src/__tests__/prune-unused-prop-extractions.test.ts +3 -3
  139. package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
  140. package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
  141. package/src/__tests__/renamed-body-destructure-template.test.ts +7 -7
  142. package/src/__tests__/rest-binding-gaps.test.ts +192 -0
  143. package/src/__tests__/rewrite-destructured-props.test.ts +1 -1
  144. package/src/__tests__/root-prop-alias.test.ts +53 -8
  145. package/src/__tests__/ssr-defaults.test.ts +113 -25
  146. package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
  147. package/src/__tests__/static-record-literal-array.test.ts +171 -0
  148. package/src/__tests__/string-concat-identifier.test.ts +40 -1
  149. package/src/__tests__/written-binding-substitution.test.ts +8 -8
  150. package/src/adapters/boolean-result.ts +72 -66
  151. package/src/adapters/brace-delimiter-text.ts +25 -0
  152. package/src/adapters/callback-method.ts +159 -27
  153. package/src/adapters/component-props.ts +3 -4
  154. package/src/adapters/computed-const.ts +238 -4
  155. package/src/adapters/dsl-template-adapter.ts +62 -26
  156. package/src/adapters/loop-bound-names.ts +9 -1
  157. package/src/adapters/loop-plan.ts +1 -1
  158. package/src/adapters/loop-row-type.ts +149 -0
  159. package/src/adapters/nil-reading.ts +295 -0
  160. package/src/adapters/non-primitive-equality.ts +92 -0
  161. package/src/adapters/number-value.ts +17 -4
  162. package/src/adapters/operand-typing.ts +522 -0
  163. package/src/adapters/parsed-expr-emitter.ts +183 -19
  164. package/src/adapters/primitive-plan.ts +765 -0
  165. package/src/adapters/prop-classes.ts +16 -3
  166. package/src/analyzer-context.ts +6 -0
  167. package/src/analyzer.ts +532 -26
  168. package/src/augment-inherited-props.ts +108 -10
  169. package/src/compiler.ts +17 -0
  170. package/src/event-handler-name.ts +15 -0
  171. package/src/expression-parser.ts +70 -1
  172. package/src/index.ts +35 -16
  173. package/src/ir-to-client-js/build-references.ts +69 -0
  174. package/src/ir-to-client-js/child-props-object.ts +240 -0
  175. package/src/ir-to-client-js/collect-elements.ts +31 -63
  176. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +8 -1
  177. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +31 -28
  178. package/src/ir-to-client-js/control-flow/shared.ts +20 -22
  179. package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +5 -3
  180. package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +2 -2
  181. package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +1 -1
  182. package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +2 -2
  183. package/src/ir-to-client-js/control-flow/stringify/loop.ts +1 -1
  184. package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +3 -3
  185. package/src/ir-to-client-js/html-template.ts +43 -17
  186. package/src/ir-to-client-js/imports.ts +2 -2
  187. package/src/ir-to-client-js/index.ts +53 -11
  188. package/src/ir-to-client-js/phases/props-extraction.ts +11 -7
  189. package/src/ir-to-client-js/plan/build-declaration-emit.ts +40 -7
  190. package/src/ir-to-client-js/plan/build-static-array-child-init.ts +31 -14
  191. package/src/ir-to-client-js/plan/static-array-child-init.ts +27 -18
  192. package/src/ir-to-client-js/prop-handling.ts +22 -3
  193. package/src/ir-to-client-js/prune-unused-prop-extractions.ts +58 -5
  194. package/src/ir-to-client-js/rewrite-destructured-props.ts +31 -1
  195. package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
  196. package/src/jsx-to-ir.ts +459 -37
  197. package/src/nullable-signal-attr.ts +200 -110
  198. package/src/prop-rewrite.ts +30 -6
  199. package/src/props-binding.ts +84 -19
  200. package/src/props-object-whole-read.ts +87 -0
  201. package/src/rich-type-refusal.ts +2 -1
  202. package/src/scope/binding-scope.ts +182 -18
  203. package/src/scope/lexical-scope-walk.ts +4 -3
  204. package/src/scope/root-prop-alias.ts +15 -8
  205. package/src/ssr-defaults.ts +100 -11
  206. package/src/strip-types.ts +31 -3
  207. package/src/template-position-walk.ts +57 -28
  208. package/src/types.ts +87 -0
  209. package/dist/adapters/ambiguous-nil-arithmetic.d.ts +0 -41
  210. package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +0 -1
  211. package/src/adapters/ambiguous-nil-arithmetic.ts +0 -94
@@ -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
@@ -9,7 +9,7 @@ export { compileJSX, buildMetadata } from './compiler.ts'
9
9
  export type { CompileResult, CompileOptions, CompileOptionsWithAdapter, FileOutput } from './compiler.ts'
10
10
 
11
11
  // SSR template-variable defaults (manifest seeds for stash-based adapters)
12
- export { extractSsrDefaults, deriveStashFromDefaults } from './ssr-defaults.ts'
12
+ export { extractSsrDefaults, deriveStashFromDefaults, encodeUndefinedMembers, decodeUndefinedMembers, UNDEFINED_SENTINEL } from './ssr-defaults.ts'
13
13
 
14
14
  // #3144: shared "is this a component-body local bound to an opaque call"
15
15
  // check, consulted by every non-JS-runtime adapter's `call()` fallback
@@ -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,27 +130,40 @@ 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, nilTestOperand, refuseUnspelledNilTest, 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, NativeSafeClaims, NativeSafeTable, PrimitiveHelper, NilTestHelper, 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'
138
157
  export { planStyleObject, type StyleObjectPlanEntry } from './adapters/style-object.ts'
139
158
  export { filterCaptureCollisions } from './adapters/filter-capture-collisions.ts'
140
159
  export { templateNameCollisionErrors } from './adapters/template-name-collisions.ts'
141
- export {
142
- arithmeticCoercionOrRefuse,
143
- refuseAmbiguousNilArithmetic,
144
- refuseAmbiguousNilMemoArithmetic,
145
- type AmbiguousNilArithmeticSink,
146
- } from './adapters/ambiguous-nil-arithmetic.ts'
147
160
  export {
148
161
  collectBooleanValueNames,
149
162
  isAriaBooleanAttr,
150
- isBooleanResultExpr,
163
+ isBooleanResultExpr, planTestOfSource,
151
164
  isBooleanResultParsed,
152
165
  isExplicitStringCall,
153
- stringifyBooleanTernaryBranches,
166
+ stringifyBooleanBranches,
154
167
  } from './adapters/boolean-result.ts'
155
168
  export { isBareStringLiteral, isLiteralIndex, isStringTypeInfo } from './adapters/literal-value.ts'
156
169
  export { collectBooleanTypedProps, collectNullableOptionalProps, isBooleanTypedPropRef } from './adapters/prop-classes.ts'
@@ -164,6 +177,7 @@ export {
164
177
  } from './adapters/array-method.ts'
165
178
  export {
166
179
  emitCallbackEval,
180
+ evalBody,
167
181
  isPredicateMethod,
168
182
  lowerCallbackMethod,
169
183
  lowerSortComparator,
@@ -176,12 +190,15 @@ export {
176
190
  sortRefusal,
177
191
  } from './adapters/callback-method.ts'
178
192
  export type {
193
+ CallbackCapture,
179
194
  CallbackEvalHelper,
180
195
  CallbackEvalOperand,
181
196
  CallbackEvalPlan,
182
197
  CallbackEvalSpelling,
183
198
  CallbackLoweringOptions,
184
199
  CallbackPlan,
200
+ EvalBody,
201
+ NilReadingSite,
185
202
  PredicateMethod,
186
203
  SortPlan,
187
204
  } from './adapters/callback-method.ts'
@@ -219,13 +236,13 @@ export {
219
236
  arithmeticOperandIsNull,
220
237
  arithmeticOperandIsUndefined,
221
238
  arithmeticOperandCoercion,
222
- arithmeticOperandNilIsAmbiguous,
223
- ambiguousNilArithmeticOperands,
224
239
  bareGetterCallName,
225
240
  attrValueMayBeNullish,
226
241
  collectAbsentPropSeedReads,
227
242
  collectNullableSignalGetters,
228
243
  collectNullishAttrContext,
244
+ nullablePropsYieldedBy,
245
+ isNeverNullishOperand,
229
246
  isNullableSignal,
230
247
  isNullishLiteral,
231
248
  NULL_COERCING_ARITHMETIC_OPS,
@@ -264,6 +281,7 @@ export type { IRNodeEmitter, EmitIRNode } from './adapters/ir-node-emitter.ts'
264
281
  export { emitAttrValue } from './adapters/attr-value-emitter.ts'
265
282
  export type { AttrValueEmitter } from './adapters/attr-value-emitter.ts'
266
283
  export { escapeLineStatementSigil } from './adapters/line-statement-text.ts'
284
+ export { escapeBraceDelimiters } from './adapters/brace-delimiter-text.ts'
267
285
  export {
268
286
  isDangerousInnerHtmlAttr,
269
287
  resolveDangerousInnerHtml,
@@ -360,8 +378,9 @@ export type { LoopChainInputs } from './loop-chain.ts'
360
378
  export { isLowerableLoopDestructure, isLowerableObjectRestDestructure } from './loop-destructure.ts'
361
379
 
362
380
  // 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'
381
+ export { BindingScope, PROPS_OBJECT_IDENTIFIER, propsObjectIdentifier } from './scope/binding-scope.ts'
382
+ export type { ScopeBindingSource, ScopeBinding, ScopeFrame, LoopBindingSource, LoopValueType, NamedTypeFields } from './scope/binding-scope.ts'
383
+ export { arrayElementType, isSameLoopValueShape, loopValueTypeAt, unionOfLoopValueTypes } from './scope/binding-scope.ts'
365
384
  export { isRootPropsObject, rootPropAliasNames, rootPropAliasesForLoop, rootPropReadName } from './scope/root-prop-alias.ts'
366
385
 
367
386
  // Debug analysis
@@ -477,7 +496,7 @@ export { BOOLEAN_ATTRS, isBooleanAttr } from './html-constants.ts'
477
496
  export { USE_CLIENT_DIRECTIVE, CLIENT_EXPRESSION_DIRECTIVE } from './directives.ts'
478
497
 
479
498
  // 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'
499
+ export { augmentInheritedPropAccesses, parseRecordIndexAccess, evalStringArrayJoin, collectModuleStringConsts, lookupStaticRecordLiteral, lookupStaticArrayElement, resolveStaticConstBinding, lookupLiteralConst, collectContextConsumers, contextConsumerSeed, type LiteralConst } from './augment-inherited-props.ts'
481
500
  export type { RecordIndexAccess, RecordIndexEntry, ContextConsumer } from './augment-inherited-props.ts'
482
501
 
483
502
  // 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
  // =============================================================================