@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
@@ -15,6 +15,31 @@ import ts from 'typescript'
15
15
  export interface ExcludeRange {
16
16
  start: number
17
17
  end: number
18
+ /**
19
+ * Text that stands in for the range instead of dropping it: a read the
20
+ * analyzer respells (`addSourceReplacement`). Applied only when the whole
21
+ * range lies inside the node being reconstructed; a node inside the range
22
+ * (`rest` of `rest.id`) keeps its own text.
23
+ */
24
+ replacement?: string
25
+ }
26
+
27
+ /**
28
+ * Insert `range` (with its `replacement`) into the sorted, merged `ranges`.
29
+ * The span must not overlap a range already there; one that does is not
30
+ * added, and false is returned.
31
+ */
32
+ export function addSourceReplacement(ranges: ExcludeRange[], range: Required<ExcludeRange>): boolean {
33
+ let i = 0
34
+ while (i < ranges.length && ranges[i].end <= range.start) i++
35
+ if (i < ranges.length && ranges[i].start < range.end) return false
36
+ ranges.splice(i, 0, range)
37
+ return true
38
+ }
39
+
40
+ /** Whether `range` applies to the node spanning `[nodeStart, nodeEnd)`. */
41
+ function appliesWithin(range: ExcludeRange, nodeStart: number, nodeEnd: number): boolean {
42
+ return range.replacement === undefined || (range.start >= nodeStart && range.end <= nodeEnd)
18
43
  }
19
44
 
20
45
  /**
@@ -59,9 +84,11 @@ export function reconstructWithoutTypes(
59
84
  for (let i = lo; i < ranges.length; i++) {
60
85
  const range = ranges[i]
61
86
  if (range.start >= nodeEnd) break
87
+ if (!appliesWithin(range, nodeStart, nodeEnd)) continue
62
88
  if (range.start > pos) {
63
89
  result += fullText.slice(pos, range.start)
64
90
  }
91
+ if (range.replacement !== undefined) result += range.replacement
65
92
  pos = Math.max(pos, range.end)
66
93
  }
67
94
  if (pos < nodeEnd) {
@@ -91,11 +118,11 @@ export function reconstructAsSegments(
91
118
  const nodeEnd = node.getEnd()
92
119
  const fullText = sourceFile.text
93
120
 
94
- type Edit = { start: number; end: number; marker: number | null }
121
+ type Edit = { start: number; end: number; marker: number | null; replacement?: string }
95
122
  const edits: Edit[] = []
96
123
  for (const r of ranges) {
97
- if (r.end <= nodeStart || r.start >= nodeEnd) continue
98
- edits.push({ start: r.start, end: r.end, marker: null })
124
+ if (r.end <= nodeStart || r.start >= nodeEnd || !appliesWithin(r, nodeStart, nodeEnd)) continue
125
+ edits.push({ start: r.start, end: r.end, marker: null, ...(r.replacement !== undefined && { replacement: r.replacement }) })
99
126
  }
100
127
  markers.forEach((m, i) => edits.push({ start: m.start, end: m.end, marker: i }))
101
128
  edits.sort((a, b) => a.start - b.start || b.end - a.end)
@@ -106,6 +133,7 @@ export function reconstructAsSegments(
106
133
  for (const edit of edits) {
107
134
  if (edit.start < pos) continue // subsumed by an already-emitted span
108
135
  jsBuf += fullText.slice(pos, edit.start)
136
+ if (edit.replacement !== undefined) jsBuf += edit.replacement
109
137
  if (edit.marker !== null) {
110
138
  if (jsBuf) segments.push({ js: jsBuf })
111
139
  jsBuf = ''
@@ -9,7 +9,9 @@
9
9
  * (expression, spread, and structured template parts), loop arrays, and the
10
10
  * children of elements, components, fragments, providers, async boundaries,
11
11
  * loops (including child components, nested components, and `flatMap` /
12
- * preamble JSX segments) and conditional branches.
12
+ * preamble JSX segments) and conditional branches, and a loop preamble's
13
+ * value-declaration initializers (`const same = a === b` in a `.map()` body),
14
+ * which a template adapter evaluates at SSR as a per-row local.
13
15
  *
14
16
  * A `/* @client *\/`-marked position (`clientOnly`) is skipped: the whole read
15
17
  * is deferred to the client, where real JS evaluates it.
@@ -29,12 +31,14 @@ import type {
29
31
  ExpressionAttr,
30
32
  IRConditional,
31
33
  IRIfStatement,
34
+ IRLoop,
32
35
  IRNode,
33
36
  IRTemplatePart,
34
37
  SourceLocation,
35
38
  } from './types.ts'
36
39
  import type { ParsedExpr } from './expression-parser.ts'
37
40
  import { parseExpression } from './expression-parser.ts'
41
+ import type { BindingScope } from './scope/binding-scope.ts'
38
42
 
39
43
  /**
40
44
  * How the template uses the value at a position:
@@ -51,6 +55,10 @@ import { parseExpression } from './expression-parser.ts'
51
55
  * text parsed on demand here; every adapter lowers them from that raw text,
52
56
  * so nothing attached to the IR can stand in for them.
53
57
  * - `loop-array` — a loop's array expression.
58
+ * - `preamble-init` — the initializer of a loop preamble's value declaration
59
+ * (`MapCallbackPreamble.declarations`), bound as a per-row local. The read
60
+ * of that local is a bare name elsewhere in the row, so the initializer is
61
+ * the only position that shows what the template computes.
54
62
  */
55
63
  export type TemplatePositionRole =
56
64
  | 'text'
@@ -60,6 +68,7 @@ export type TemplatePositionRole =
60
68
  | 'spread'
61
69
  | 'template-part'
62
70
  | 'loop-array'
71
+ | 'preamble-init'
63
72
 
64
73
  /**
65
74
  * The IR slot a position's `expr` lives in, for a pass that rewrites the
@@ -79,10 +88,21 @@ export interface TemplatePosition {
79
88
  /** The attribute or prop name, when the position is an attribute / prop value. */
80
89
  attrName?: string
81
90
  host?: TemplatePositionHost
91
+ /**
92
+ * The scope the position is read in, when the walk was given the
93
+ * component's scope: each loop's children are read in its row
94
+ * (`BindingScope.enterLoopRow`), its array outside it. The innermost
95
+ * scope wins: an outer wrapper never replaces a row's.
96
+ */
97
+ scope?: BindingScope
82
98
  }
83
99
 
84
- export function walkTemplatePositions(root: IRNode, visit: (position: TemplatePosition) => void): void {
85
- walkNode(root, visit)
100
+ /**
101
+ * Visit every template-lowered position under `root`. With `scope` (the
102
+ * component's), each position also carries the scope it is read in.
103
+ */
104
+ export function walkTemplatePositions(root: IRNode, visit: (position: TemplatePosition) => void, scope?: BindingScope): void {
105
+ walkNode(root, scope ? (position) => visit({ scope, ...position }) : visit, scope)
86
106
  }
87
107
 
88
108
  /**
@@ -142,7 +162,7 @@ function walkAttrValue(
142
162
  }
143
163
  }
144
164
 
145
- function walkNode(node: IRNode, visit: (position: TemplatePosition) => void): void {
165
+ function walkNode(node: IRNode, visit: (position: TemplatePosition) => void, scope?: BindingScope): void {
146
166
  if (node.type === 'expression') {
147
167
  if (!node.clientOnly && node.parsed) visit({ expr: node.parsed, loc: node.loc, role: 'text' })
148
168
  } else if (node.type === 'conditional') {
@@ -168,38 +188,47 @@ function walkNode(node: IRNode, visit: (position: TemplatePosition) => void): vo
168
188
  case 'component':
169
189
  case 'fragment':
170
190
  case 'provider':
171
- for (const child of node.children) walkNode(child, visit)
191
+ for (const child of node.children) walkNode(child, visit, scope)
172
192
  break
173
193
  case 'async':
174
- walkNode(node.fallback, visit)
175
- for (const child of node.children) walkNode(child, visit)
194
+ walkNode(node.fallback, visit, scope)
195
+ for (const child of node.children) walkNode(child, visit, scope)
176
196
  break
177
- case 'loop': {
178
- if (node.clientOnly) break
179
- if (node.arrayParsed) visit({ expr: node.arrayParsed, loc: node.loc, role: 'loop-array' })
180
- for (const child of node.children) walkNode(child, visit)
181
- if (node.childComponent) {
182
- for (const child of node.childComponent.children) walkNode(child, visit)
183
- }
184
- for (const nested of node.nestedComponents ?? []) {
185
- for (const child of nested.children) walkNode(child, visit)
186
- }
187
- for (const seg of node.flatMapCallback?.segments ?? []) {
188
- if (seg.kind === 'jsx') walkNode(seg.ir, visit)
189
- }
190
- for (const seg of node.preamble?.segments ?? []) {
191
- if (seg.kind === 'jsx') walkNode(seg.ir, visit)
192
- }
197
+ case 'loop':
198
+ walkLoop(node, visit, scope)
193
199
  break
194
- }
195
200
  case 'conditional':
196
201
  if (node.clientOnly) break
197
- walkNode(node.whenTrue, visit)
198
- walkNode(node.whenFalse, visit)
202
+ walkNode(node.whenTrue, visit, scope)
203
+ walkNode(node.whenFalse, visit, scope)
199
204
  break
200
205
  case 'if-statement':
201
- walkNode(node.consequent, visit)
202
- if (node.alternate) walkNode(node.alternate, visit)
206
+ walkNode(node.consequent, visit, scope)
207
+ if (node.alternate) walkNode(node.alternate, visit, scope)
203
208
  break
204
209
  }
205
210
  }
211
+
212
+ /**
213
+ * A loop's array, then its row's positions, each read in the row's scope:
214
+ * the preamble's declaration initializers first (the row scope binds every
215
+ * preamble name, and an initializer can only read an earlier one), then the
216
+ * row's children.
217
+ */
218
+ function walkLoop(node: IRLoop, visit: (position: TemplatePosition) => void, scope?: BindingScope): void {
219
+ if (node.clientOnly) return
220
+ if (node.arrayParsed) visit({ expr: node.arrayParsed, loc: node.loc, role: 'loop-array' })
221
+ const row = scope?.enterLoopRow(node)
222
+ const rowVisit = row ? (position: TemplatePosition) => visit({ scope: row, ...position }) : visit
223
+ for (const declaration of node.preamble?.declarations ?? []) {
224
+ rowVisit({ expr: declaration.valueParsed, loc: node.loc, role: 'preamble-init' })
225
+ }
226
+ const children = [
227
+ ...node.children,
228
+ ...(node.childComponent?.children ?? []),
229
+ ...(node.nestedComponents ?? []).flatMap(nested => nested.children),
230
+ ...(node.flatMapCallback?.segments ?? []).flatMap(seg => (seg.kind === 'jsx' ? [seg.ir] : [])),
231
+ ...(node.preamble?.segments ?? []).flatMap(seg => (seg.kind === 'jsx' ? [seg.ir] : [])),
232
+ ]
233
+ for (const child of children) walkNode(child, rowVisit, row)
234
+ }
package/src/types.ts CHANGED
@@ -112,6 +112,16 @@ export interface ParamInfo {
112
112
  * `type` for the `props.n` form.
113
113
  */
114
114
  declaredType?: TypeInfo
115
+ /**
116
+ * The values the prop's declared type admits when it is only string
117
+ * literals, `null` and `undefined` (`'ok' | 'err'`, a same-file alias of
118
+ * such a union, through a props type that is an intersection or an
119
+ * interface): the string keys, and which of `null` / `undefined` it also
120
+ * admits (the `?` modifier not included). Absent for any other type. A
121
+ * read of a const record indexed by the prop is projected from it
122
+ * (`computed-const.ts`).
123
+ */
124
+ literalKeys?: { keys: string[]; nullish: Array<'null' | 'undefined'> }
115
125
  optional: boolean
116
126
  defaultValue?: JsSourceText
117
127
  /**
@@ -126,6 +136,15 @@ export interface ParamInfo {
126
136
  parsed?: ParsedExpr
127
137
  /** When true, the default value contains an arrow function or function expression (computed from AST). */
128
138
  defaultContainsArrow?: boolean
139
+ /**
140
+ * The free identifiers the default reads, from the initializer's own AST
141
+ * (`extractFreeIdentifiersFromNode`): an object key, a member name or a
142
+ * callback parameter inside the default is not one (`{ m = ({ label: 'm' })
143
+ * .label }` reads nothing outer). The client reference graph traces these,
144
+ * since the emitted read `_p.m ?? <default>` evaluates the default. Set
145
+ * only when the default reads at least one name.
146
+ */
147
+ defaultFreeIdentifiers?: readonly string[]
129
148
  /**
130
149
  * `defaultValue` split at each read of an EARLIER binding of the same props
131
150
  * destructure (`{ off = false, on = off }` → `on`'s parts are one `param`
@@ -735,6 +754,16 @@ export interface IRLoop {
735
754
  param: string
736
755
  predicate?: ParsedExpr // Boolean predicate expression (folded from any block body)
737
756
  raw: string // Original string for error messages
757
+ /**
758
+ * `raw` with each destructured-prop read rewritten to its live `_p.<key>`
759
+ * read, the predicate's `templateArray` twin (#3845). The client
760
+ * template runs at module scope, where the component's destructured
761
+ * locals don't exist, so its `.filter(...)` reads this text. Decided in
762
+ * Phase 1 against the binding scope at the predicate (the predicate's
763
+ * own parameter and any enclosing row binding shadow a prop of the same
764
+ * name). Unset when the predicate reads no destructured prop.
765
+ */
766
+ templateRaw?: string
738
767
  }
739
768
 
740
769
  /**
@@ -1511,6 +1540,17 @@ export interface SpreadAttr {
1511
1540
  * any other spread, including a `{...rest}` bag.
1512
1541
  */
1513
1542
  propsObject?: true
1543
+ /**
1544
+ * Which of the caller's own prop bags this spread forwards whole: `'props'`
1545
+ * for the component's props object, `'rest'` for its destructured
1546
+ * `...rest` binding, through any alias hop (`resolveRestSpreadOriginCore`).
1547
+ * Decided once in Phase 1, where a name an enclosing loop row or callback
1548
+ * binds is that binding, not the bag. Unset for any other spread, and never
1549
+ * set on a closed-type rest spread (that one unrolls per key). The client
1550
+ * reads every key of such a spread off `_p`, so the hydration payload must
1551
+ * carry the caller's whole bag (`ClientAnalysis.readsCallerBag`, #3845).
1552
+ */
1553
+ callerBag?: 'props' | 'rest'
1514
1554
  }
1515
1555
 
1516
1556
  export interface JsxChildrenAttr {
@@ -1660,6 +1700,15 @@ export interface IRAttribute extends AttrMeta {
1660
1700
  * like SSR / hydrated / csr-mount agree it should.
1661
1701
  */
1662
1702
  restExpandedKey?: boolean
1703
+ /**
1704
+ * With `restExpandedKey`: the value an earlier attribute gave the same key
1705
+ * (`<p title="fixed" {...rest}>`), which the key keeps when the caller
1706
+ * omits it (#3851, `closedRestFallbacks` in jsx-to-ir.ts). Client-template
1707
+ * source text (destructured props spelled `_p.xxx`), so `collectElements`
1708
+ * binds `_p.<key> ?? (restFallback)` instead of a bare `_p.<key>` that
1709
+ * would remove the attribute SSR rendered.
1710
+ */
1711
+ restFallback?: JsSourceText
1663
1712
  }
1664
1713
 
1665
1714
  export interface IREvent {
@@ -2328,6 +2377,15 @@ export interface ConstantInfo {
2328
2377
  * was supposed to do.
2329
2378
  */
2330
2379
  mutatedAfterDeclaration?: boolean
2380
+ /**
2381
+ * When true, this module-level const is shadowed inside the component: a
2382
+ * destructured prop, the props object, or a component-scope const,
2383
+ * function, signal or memo binds the same name, so a read of the name in
2384
+ * the component is that binding, never this const. Set by
2385
+ * `buildMetadata`; a compile-time lookup of a module const's value must
2386
+ * check it before resolving a read.
2387
+ */
2388
+ shadowedInComponent?: boolean
2331
2389
  /**
2332
2390
  * Set when the component writes the binding anywhere in its body, event
2333
2391
  * handlers and other callbacks included — the same fact
@@ -2401,9 +2459,25 @@ export interface IRMetadata {
2401
2459
  propsParams: ParamInfo[]
2402
2460
  /** Name of the props object parameter (e.g., 'props' in `function Component(props: Props)`) */
2403
2461
  propsObjectName: string | null
2462
+ /**
2463
+ * Whether the component reads its props object as a whole (`{...props}`,
2464
+ * `Object.keys(props)`, `props[key]`, passing `props` on), so a key its
2465
+ * props type does not declare can still reach the output. Undefined on
2466
+ * hand-built metadata, which reads as false.
2467
+ */
2468
+ propsObjectObservedWhole?: boolean
2404
2469
  restPropsName: string | null
2405
2470
  /** Keys statically expanded from rest props via type analysis (closed type only) */
2406
2471
  restPropsExpandedKeys: string[]
2472
+ /**
2473
+ * Set when `restPropsName` is bound by a component-body destructure of the
2474
+ * props object (`function C(props) { const { label, ...rest } = props }`)
2475
+ * rather than by the signature: the caller keys that destructure takes
2476
+ * before its rest element. A backend that runs the component body as JS
2477
+ * declares the binding from the props object without them. Absent (or
2478
+ * null) for a signature rest and on hand-built metadata.
2479
+ */
2480
+ restPropsOmittedKeys?: string[] | null
2407
2481
  signals: SignalInfo[]
2408
2482
  memos: MemoInfo[]
2409
2483
  effects: EffectInfo[]
@@ -2582,6 +2656,19 @@ export interface PropUsage {
2582
2656
  export interface ClientAnalysis {
2583
2657
  needsInit: boolean
2584
2658
  usedProps: string[]
2659
+ /**
2660
+ * Caller keys of a closed-type `...rest` (`restPropsExpandedKeys`) the
2661
+ * client reads as `_p.<key>`. They are not `propsParams`, so `usedProps`
2662
+ * never lists them, but the hydration payload must carry them (#3845).
2663
+ */
2664
+ usedRestKeys: string[]
2665
+ /**
2666
+ * The client reads one of the caller's prop bags whole (a spread whose
2667
+ * `SpreadAttr.callerBag` is set): every key the caller passed reaches the
2668
+ * client through `_p`, so the hydration payload carries the caller's whole
2669
+ * bag, not only the named props (#3845).
2670
+ */
2671
+ readsCallerBag: boolean
2585
2672
  /**
2586
2673
  * The subset of `usedProps` whose declared type is a function — the client
2587
2674
  * code CALLS these, so they cannot arrive as JSON. Keyed by caller-facing
@@ -1,41 +0,0 @@
1
- import { type ParsedExpr } from '../expression-parser.ts';
2
- import { type ArithmeticCoercion, type NullishAttrContext } from '../nullable-signal-attr.ts';
3
- import { BindingScope } from '../scope/binding-scope.ts';
4
- import type { CompilerError, MemoInfo } from '../types.ts';
5
- /**
6
- * Where a template adapter records the refusal of an arithmetic operand
7
- * whose nil the declared type cannot classify (#3700).
8
- */
9
- export interface AmbiguousNilArithmeticSink {
10
- readonly errors: CompilerError[];
11
- readonly componentName: string;
12
- }
13
- /**
14
- * The shared arithmetic-operand decision for a template adapter's emission
15
- * site: the coercion the adapter spells (`arithmeticOperandCoercion`), after
16
- * refusing (BF101) an operand that reads a number prop declared both
17
- * optional and nullable (`n?: number | null`, `arithmeticOperandNilIsAmbiguous`).
18
- * JS reads that prop as `undefined` when omitted (`NaN` in arithmetic) and
19
- * as `0` when `null`; the engine holds both as one nil, so any spelling
20
- * renders one of them wrong. Asked at the emission site so the operand is
21
- * classified in the scope it is read in (`isShadowed`). One error per
22
- * operand text per compile.
23
- */
24
- export declare function arithmeticCoercionOrRefuse(op: string, operand: ParsedExpr, other: ParsedExpr | undefined, ctx: NullishAttrContext, isShadowed: (name: string) => boolean, sink: AmbiguousNilArithmeticSink): ArithmeticCoercion;
25
- /**
26
- * Refuse (BF101) each arithmetic operand in a whole expression that a
27
- * template adapter lowers without asking {@link arithmeticCoercionOrRefuse}
28
- * per operator: a `.filter()` predicate, an expression the runtime evaluator
29
- * runs, a condition lowered by its own renderer (`ambiguousNilArithmeticOperands`).
30
- * `scope` is the scope the expression is read in.
31
- */
32
- export declare function refuseAmbiguousNilArithmetic(expr: ParsedExpr, ctx: NullishAttrContext, scope: BindingScope, sink: AmbiguousNilArithmeticSink): void;
33
- /**
34
- * Refuse the ambiguous arithmetic in the component's memo bodies. A template
35
- * adapter seeds every memo at SSR, and some seed a memo through a path that
36
- * never reaches its operator emitter (a static seed after a speculative
37
- * lowering, a constructor-time evaluation), so the bodies are checked as a
38
- * whole, in the component scope they are declared in.
39
- */
40
- export declare function refuseAmbiguousNilMemoArithmetic(memos: readonly MemoInfo[], ctx: NullishAttrContext, sink: AmbiguousNilArithmeticSink): void;
41
- //# sourceMappingURL=ambiguous-nil-arithmetic.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ambiguous-nil-arithmetic.d.ts","sourceRoot":"","sources":["../../src/adapters/ambiguous-nil-arithmetic.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACvE,OAAO,EAIL,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,EACxB,MAAM,4BAA4B,CAAA;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAE1D;;;GAGG;AACH,MAAM,WAAW,0BAA0B;IACzC,QAAQ,CAAC,MAAM,EAAE,aAAa,EAAE,CAAA;IAChC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAA;CAC/B;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,0BAA0B,CACxC,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,UAAU,EACnB,KAAK,EAAE,UAAU,GAAG,SAAS,EAC7B,GAAG,EAAE,kBAAkB,EACvB,UAAU,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,EACrC,IAAI,EAAE,0BAA0B,GAC/B,kBAAkB,CAKpB;AAED;;;;;;GAMG;AACH,wBAAgB,4BAA4B,CAC1C,IAAI,EAAE,UAAU,EAChB,GAAG,EAAE,kBAAkB,EACvB,KAAK,EAAE,YAAY,EACnB,IAAI,EAAE,0BAA0B,GAC/B,IAAI,CAIN;AAED;;;;;;GAMG;AACH,wBAAgB,gCAAgC,CAC9C,KAAK,EAAE,SAAS,QAAQ,EAAE,EAC1B,GAAG,EAAE,kBAAkB,EACvB,IAAI,EAAE,0BAA0B,GAC/B,IAAI,CAIN"}
@@ -1,94 +0,0 @@
1
- import { exprToString, type ParsedExpr } from '../expression-parser.ts'
2
- import {
3
- ambiguousNilArithmeticOperands,
4
- arithmeticOperandCoercion,
5
- arithmeticOperandNilIsAmbiguous,
6
- type ArithmeticCoercion,
7
- type NullishAttrContext,
8
- } from '../nullable-signal-attr.ts'
9
- import { BindingScope } from '../scope/binding-scope.ts'
10
- import type { CompilerError, MemoInfo } from '../types.ts'
11
-
12
- /**
13
- * Where a template adapter records the refusal of an arithmetic operand
14
- * whose nil the declared type cannot classify (#3700).
15
- */
16
- export interface AmbiguousNilArithmeticSink {
17
- readonly errors: CompilerError[]
18
- readonly componentName: string
19
- }
20
-
21
- /**
22
- * The shared arithmetic-operand decision for a template adapter's emission
23
- * site: the coercion the adapter spells (`arithmeticOperandCoercion`), after
24
- * refusing (BF101) an operand that reads a number prop declared both
25
- * optional and nullable (`n?: number | null`, `arithmeticOperandNilIsAmbiguous`).
26
- * JS reads that prop as `undefined` when omitted (`NaN` in arithmetic) and
27
- * as `0` when `null`; the engine holds both as one nil, so any spelling
28
- * renders one of them wrong. Asked at the emission site so the operand is
29
- * classified in the scope it is read in (`isShadowed`). One error per
30
- * operand text per compile.
31
- */
32
- export function arithmeticCoercionOrRefuse(
33
- op: string,
34
- operand: ParsedExpr,
35
- other: ParsedExpr | undefined,
36
- ctx: NullishAttrContext,
37
- isShadowed: (name: string) => boolean,
38
- sink: AmbiguousNilArithmeticSink,
39
- ): ArithmeticCoercion {
40
- if (arithmeticOperandNilIsAmbiguous(op, operand, other, ctx, isShadowed)) {
41
- recordAmbiguousNilArithmetic(exprToString(operand), sink)
42
- }
43
- return arithmeticOperandCoercion(op, operand, other, ctx, isShadowed)
44
- }
45
-
46
- /**
47
- * Refuse (BF101) each arithmetic operand in a whole expression that a
48
- * template adapter lowers without asking {@link arithmeticCoercionOrRefuse}
49
- * per operator: a `.filter()` predicate, an expression the runtime evaluator
50
- * runs, a condition lowered by its own renderer (`ambiguousNilArithmeticOperands`).
51
- * `scope` is the scope the expression is read in.
52
- */
53
- export function refuseAmbiguousNilArithmetic(
54
- expr: ParsedExpr,
55
- ctx: NullishAttrContext,
56
- scope: BindingScope,
57
- sink: AmbiguousNilArithmeticSink,
58
- ): void {
59
- for (const operand of ambiguousNilArithmeticOperands(expr, ctx, scope)) {
60
- recordAmbiguousNilArithmetic(exprToString(operand), sink)
61
- }
62
- }
63
-
64
- /**
65
- * Refuse the ambiguous arithmetic in the component's memo bodies. A template
66
- * adapter seeds every memo at SSR, and some seed a memo through a path that
67
- * never reaches its operator emitter (a static seed after a speculative
68
- * lowering, a constructor-time evaluation), so the bodies are checked as a
69
- * whole, in the component scope they are declared in.
70
- */
71
- export function refuseAmbiguousNilMemoArithmetic(
72
- memos: readonly MemoInfo[],
73
- ctx: NullishAttrContext,
74
- sink: AmbiguousNilArithmeticSink,
75
- ): void {
76
- for (const memo of memos) {
77
- if (memo.parsed) refuseAmbiguousNilArithmetic(memo.parsed, ctx, BindingScope.EMPTY, sink)
78
- }
79
- }
80
-
81
- function recordAmbiguousNilArithmetic(operandText: string, sink: AmbiguousNilArithmeticSink): void {
82
- const message = `Arithmetic over \`${operandText}\` reads a number prop declared both optional and nullable (\`?: number | null\`). JS reads it as \`undefined\` when the caller omits it (\`NaN\` in arithmetic) and as \`0\` when it is \`null\`, but a template engine holds both as the same nil, so the server render cannot tell which one it is.`
83
- if (sink.errors.some(e => e.code === 'BF101' && e.message === message)) return
84
- sink.errors.push({
85
- code: 'BF101',
86
- severity: 'error',
87
- message,
88
- loc: { file: `${sink.componentName}.tsx`, start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
89
- suggestion: {
90
- message: 'Declare the prop as only optional (`?: number`) or only nullable (`: number | null`), or give it a default value.',
91
- escape: [{ kind: 'rewrite' }],
92
- },
93
- })
94
- }