@barefootjs/jsx 0.40.0 → 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 (249) hide show
  1. package/dist/adapters/ambiguous-nil-arithmetic.d.ts +42 -0
  2. package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -0
  3. package/dist/adapters/attr-plan.d.ts.map +1 -1
  4. package/dist/adapters/boolean-carrier.d.ts +31 -0
  5. package/dist/adapters/boolean-carrier.d.ts.map +1 -0
  6. package/dist/adapters/boolean-result.d.ts +32 -10
  7. package/dist/adapters/boolean-result.d.ts.map +1 -1
  8. package/dist/adapters/brace-delimiter-text.d.ts +18 -0
  9. package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
  10. package/dist/adapters/callback-method.d.ts +38 -5
  11. package/dist/adapters/callback-method.d.ts.map +1 -1
  12. package/dist/adapters/component-props.d.ts +22 -7
  13. package/dist/adapters/component-props.d.ts.map +1 -1
  14. package/dist/adapters/computed-const.d.ts +28 -1
  15. package/dist/adapters/computed-const.d.ts.map +1 -1
  16. package/dist/adapters/dsl-template-adapter.d.ts +39 -1
  17. package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
  18. package/dist/adapters/loop-bound-names.d.ts +9 -1
  19. package/dist/adapters/loop-bound-names.d.ts.map +1 -1
  20. package/dist/adapters/loop-row-type.d.ts +52 -0
  21. package/dist/adapters/loop-row-type.d.ts.map +1 -0
  22. package/dist/adapters/nil-reading.d.ts +68 -0
  23. package/dist/adapters/nil-reading.d.ts.map +1 -0
  24. package/dist/adapters/non-primitive-equality.d.ts +33 -0
  25. package/dist/adapters/non-primitive-equality.d.ts.map +1 -0
  26. package/dist/adapters/number-value.d.ts +45 -0
  27. package/dist/adapters/number-value.d.ts.map +1 -0
  28. package/dist/adapters/operand-typing.d.ts +126 -0
  29. package/dist/adapters/operand-typing.d.ts.map +1 -0
  30. package/dist/adapters/parsed-expr-emitter.d.ts +70 -12
  31. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  32. package/dist/adapters/primitive-plan.d.ts +291 -0
  33. package/dist/adapters/primitive-plan.d.ts.map +1 -0
  34. package/dist/analyzer-context.d.ts +11 -0
  35. package/dist/analyzer-context.d.ts.map +1 -1
  36. package/dist/analyzer.d.ts +34 -0
  37. package/dist/analyzer.d.ts.map +1 -1
  38. package/dist/augment-inherited-props.d.ts +63 -8
  39. package/dist/augment-inherited-props.d.ts.map +1 -1
  40. package/dist/compiler.d.ts.map +1 -1
  41. package/dist/event-handler-name.d.ts +12 -0
  42. package/dist/event-handler-name.d.ts.map +1 -1
  43. package/dist/expression-parser.d.ts +8 -0
  44. package/dist/expression-parser.d.ts.map +1 -1
  45. package/dist/index.d.ts +24 -11
  46. package/dist/index.d.ts.map +1 -1
  47. package/dist/index.js +5671 -2586
  48. package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
  49. package/dist/ir-to-client-js/child-props-object.d.ts +64 -0
  50. package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
  51. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  52. package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
  53. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts +29 -0
  54. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  55. package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts +2 -0
  56. package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts.map +1 -1
  57. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  58. package/dist/ir-to-client-js/control-flow/plan/build-reactive-effects.d.ts.map +1 -1
  59. package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts +5 -4
  60. package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts.map +1 -1
  61. package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts +2 -0
  62. package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts.map +1 -1
  63. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +15 -0
  64. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
  65. package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts +2 -0
  66. package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts.map +1 -1
  67. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  68. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +22 -0
  69. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
  70. package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
  71. package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +1 -1
  72. package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
  73. package/dist/ir-to-client-js/control-flow/stringify/lazy-row.d.ts.map +1 -1
  74. package/dist/ir-to-client-js/html-template.d.ts +3 -2
  75. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  76. package/dist/ir-to-client-js/imports.d.ts +2 -2
  77. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  78. package/dist/ir-to-client-js/index.d.ts +2 -6
  79. package/dist/ir-to-client-js/index.d.ts.map +1 -1
  80. package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
  81. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +37 -19
  82. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
  83. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  84. package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
  85. package/dist/ir-to-client-js/safe-html.d.ts +9 -3
  86. package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
  87. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
  88. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
  89. package/dist/ir-to-client-js/types.d.ts +30 -0
  90. package/dist/ir-to-client-js/types.d.ts.map +1 -1
  91. package/dist/ir-to-client-js/utils.d.ts +8 -0
  92. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  93. package/dist/jsx-to-ir.d.ts +1 -0
  94. package/dist/jsx-to-ir.d.ts.map +1 -1
  95. package/dist/nullable-signal-attr.d.ts +95 -10
  96. package/dist/nullable-signal-attr.d.ts.map +1 -1
  97. package/dist/prop-rewrite.d.ts +7 -0
  98. package/dist/prop-rewrite.d.ts.map +1 -1
  99. package/dist/props-binding.d.ts +27 -3
  100. package/dist/props-binding.d.ts.map +1 -1
  101. package/dist/props-object-whole-read.d.ts +15 -0
  102. package/dist/props-object-whole-read.d.ts.map +1 -0
  103. package/dist/rich-type-refusal.d.ts.map +1 -1
  104. package/dist/scope/binding-scope.d.ts +100 -4
  105. package/dist/scope/binding-scope.d.ts.map +1 -1
  106. package/dist/scope/lexical-scope-walk.d.ts +3 -2
  107. package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
  108. package/dist/scope/root-prop-alias.d.ts +16 -0
  109. package/dist/scope/root-prop-alias.d.ts.map +1 -1
  110. package/dist/signal-prop-name-collision.d.ts.map +1 -1
  111. package/dist/ssr-defaults.d.ts +4 -2
  112. package/dist/ssr-defaults.d.ts.map +1 -1
  113. package/dist/ssr-seed-plan.d.ts +14 -0
  114. package/dist/ssr-seed-plan.d.ts.map +1 -1
  115. package/dist/strip-types.d.ts +13 -0
  116. package/dist/strip-types.d.ts.map +1 -1
  117. package/dist/template-parts.d.ts.map +1 -1
  118. package/dist/template-position-walk.d.ts +21 -3
  119. package/dist/template-position-walk.d.ts.map +1 -1
  120. package/dist/types.d.ts +120 -0
  121. package/dist/types.d.ts.map +1 -1
  122. package/package.json +2 -2
  123. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +3 -3
  124. package/src/__tests__/binding-scope-ratchet.test.ts +12 -2
  125. package/src/__tests__/binding-scope.test.ts +89 -1
  126. package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
  127. package/src/__tests__/boolean-carrier.test.ts +74 -0
  128. package/src/__tests__/boolean-result.test.ts +22 -5
  129. package/src/__tests__/brace-delimiter-text.test.ts +26 -0
  130. package/src/__tests__/callback-method.test.ts +62 -2
  131. package/src/__tests__/child-components-in-map.test.ts +1 -1
  132. package/src/__tests__/component-props.test.ts +14 -0
  133. package/src/__tests__/computed-const.test.ts +188 -0
  134. package/src/__tests__/emit-to-string-call.test.ts +2 -1
  135. package/src/__tests__/expression-parser.test.ts +24 -0
  136. package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
  137. package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
  138. package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
  139. package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
  140. package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
  141. package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
  142. package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +143 -0
  143. package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
  144. package/src/__tests__/lookup-literal-const.test.ts +9 -3
  145. package/src/__tests__/loop-index-reactivity.test.ts +2 -2
  146. package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
  147. package/src/__tests__/loop-plan.test.ts +1 -1
  148. package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
  149. package/src/__tests__/loop-row-prop-read.test.ts +111 -0
  150. package/src/__tests__/loop-row-type.test.ts +64 -0
  151. package/src/__tests__/lowering-value-position.test.ts +4 -0
  152. package/src/__tests__/memo-body-expr.test.ts +137 -0
  153. package/src/__tests__/module-const-destructure-default.test.ts +156 -0
  154. package/src/__tests__/nested-loop-index-param.test.ts +1 -1
  155. package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
  156. package/src/__tests__/nested-row-shadowing.test.ts +2 -2
  157. package/src/__tests__/nil-reading.test.ts +77 -0
  158. package/src/__tests__/nullable-signal-attr.test.ts +231 -2
  159. package/src/__tests__/primitive-plan.test.ts +416 -0
  160. package/src/__tests__/profile-binding-ids.test.ts +14 -0
  161. package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
  162. package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
  163. package/src/__tests__/props-object-whole-read.test.ts +40 -0
  164. package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
  165. package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
  166. package/src/__tests__/rest-binding-gaps.test.ts +192 -0
  167. package/src/__tests__/root-prop-alias.test.ts +79 -1
  168. package/src/__tests__/signal-prop-name-collision.test.ts +102 -2
  169. package/src/__tests__/source-file-cache.test.ts +105 -0
  170. package/src/__tests__/ssr-seed-plan.test.ts +43 -0
  171. package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
  172. package/src/__tests__/static-record-literal-array.test.ts +171 -0
  173. package/src/__tests__/string-concat-identifier.test.ts +40 -1
  174. package/src/__tests__/template-parts-literal-boundary.test.ts +19 -0
  175. package/src/adapters/ambiguous-nil-arithmetic.ts +103 -0
  176. package/src/adapters/attr-plan.ts +6 -2
  177. package/src/adapters/boolean-carrier.ts +76 -0
  178. package/src/adapters/boolean-result.ts +72 -66
  179. package/src/adapters/brace-delimiter-text.ts +25 -0
  180. package/src/adapters/callback-method.ts +151 -27
  181. package/src/adapters/component-props.ts +28 -8
  182. package/src/adapters/computed-const.ts +443 -10
  183. package/src/adapters/dsl-template-adapter.ts +79 -2
  184. package/src/adapters/loop-bound-names.ts +9 -1
  185. package/src/adapters/loop-plan.ts +1 -1
  186. package/src/adapters/loop-row-type.ts +139 -0
  187. package/src/adapters/nil-reading.ts +297 -0
  188. package/src/adapters/non-primitive-equality.ts +92 -0
  189. package/src/adapters/number-value.ts +108 -0
  190. package/src/adapters/operand-typing.ts +522 -0
  191. package/src/adapters/parsed-expr-emitter.ts +154 -19
  192. package/src/adapters/primitive-plan.ts +696 -0
  193. package/src/analyzer-context.ts +14 -0
  194. package/src/analyzer.ts +692 -85
  195. package/src/augment-inherited-props.ts +108 -10
  196. package/src/compiler.ts +17 -0
  197. package/src/event-handler-name.ts +15 -0
  198. package/src/expression-parser.ts +74 -2
  199. package/src/index.ts +45 -9
  200. package/src/ir-to-client-js/build-references.ts +69 -0
  201. package/src/ir-to-client-js/child-props-object.ts +106 -0
  202. package/src/ir-to-client-js/collect-elements.ts +100 -67
  203. package/src/ir-to-client-js/compute-inlinability.ts +1 -1
  204. package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +15 -1
  205. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +85 -5
  206. package/src/ir-to-client-js/control-flow/plan/build-lazy-row.ts +3 -0
  207. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +30 -28
  208. package/src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts +1 -0
  209. package/src/ir-to-client-js/control-flow/plan/inner-loop.ts +5 -4
  210. package/src/ir-to-client-js/control-flow/plan/loop-child-arm.ts +2 -0
  211. package/src/ir-to-client-js/control-flow/plan/loop.ts +12 -0
  212. package/src/ir-to-client-js/control-flow/plan/reactive-effects.ts +2 -0
  213. package/src/ir-to-client-js/control-flow/shared.ts +23 -24
  214. package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +29 -0
  215. package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +8 -2
  216. package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +8 -1
  217. package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +8 -7
  218. package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +8 -7
  219. package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +4 -4
  220. package/src/ir-to-client-js/control-flow/stringify/loop.ts +3 -3
  221. package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +6 -6
  222. package/src/ir-to-client-js/emit-reactive.ts +1 -1
  223. package/src/ir-to-client-js/html-template.ts +95 -38
  224. package/src/ir-to-client-js/imports.ts +1 -1
  225. package/src/ir-to-client-js/index.ts +53 -11
  226. package/src/ir-to-client-js/plan/build-static-array-child-init.ts +42 -15
  227. package/src/ir-to-client-js/plan/static-array-child-init.ts +34 -19
  228. package/src/ir-to-client-js/prop-handling.ts +22 -3
  229. package/src/ir-to-client-js/reactivity.ts +14 -0
  230. package/src/ir-to-client-js/safe-html.ts +11 -4
  231. package/src/ir-to-client-js/stringify/static-array-child-init.ts +30 -12
  232. package/src/ir-to-client-js/types.ts +23 -0
  233. package/src/ir-to-client-js/utils.ts +15 -1
  234. package/src/jsx-to-ir.ts +510 -58
  235. package/src/nullable-signal-attr.ts +366 -28
  236. package/src/prop-rewrite.ts +30 -6
  237. package/src/props-binding.ts +54 -3
  238. package/src/props-object-whole-read.ts +87 -0
  239. package/src/rich-type-refusal.ts +2 -1
  240. package/src/scope/binding-scope.ts +203 -11
  241. package/src/scope/lexical-scope-walk.ts +4 -3
  242. package/src/scope/root-prop-alias.ts +23 -0
  243. package/src/signal-prop-name-collision.ts +113 -22
  244. package/src/ssr-defaults.ts +10 -4
  245. package/src/ssr-seed-plan.ts +82 -17
  246. package/src/strip-types.ts +31 -3
  247. package/src/template-parts.ts +21 -1
  248. package/src/template-position-walk.ts +57 -28
  249. package/src/types.ts +117 -0
@@ -44,6 +44,7 @@ import {
44
44
  stringifyParsedExpr,
45
45
  type ParsedExpr,
46
46
  } from './expression-parser.ts'
47
+ import { templatePropReadParams } from './props-binding.ts'
47
48
  import type { IRMetadata, ParamInfo } from './types.ts'
48
49
  import { isElidedFactoryGetter } from './types.ts'
49
50
 
@@ -216,31 +217,95 @@ function memoSeedExpr(memo: IRMetadata['memos'][number]): string {
216
217
  }
217
218
 
218
219
  /**
219
- * A `derived` step per prop whose destructure default reads an earlier prop of
220
- * the same pattern (`ParamInfo.defaultParts`, #3531): `on ?? <default>`, where
221
- * the default's identifiers are the earlier props' template variables. Every
222
- * prop is already in `available` (baseScope), and the steps run in dependency
223
- * order ({@link inDefaultReadOrder}), so a chain (`{ a = 1, b = a, c = b }`)
224
- * reads `b` after its own step re-bound it. A default the plan cannot lower
225
- * (or a function default) adds no step and keeps the static ssr-defaults
226
- * seed. A callback inside the default (`a.filter((x) => …)`) does not make
227
- * it a function default.
220
+ * Whether `p`'s destructure default is computed when the prop is absent: a
221
+ * default that is neither a function nor literal data
222
+ * ({@link isLiteralData}). A default reading an earlier prop
223
+ * (`ParamInfo.defaultParts`, `{ a, b = a }`) is one, and so is one that
224
+ * calls or reads anything else (`{ b = ['y', ''].filter((x) => x !== '') }`,
225
+ * `{ n = 'abc'.length }`, `{ m = DEFAULT }`). A callback inside the default
226
+ * does not make it a function default.
227
+ *
228
+ * Every backend applies such a default when it renders (the SSR seed plan's
229
+ * prop steps, Go's props constructor) instead of a value fixed at compile
230
+ * time, which only literal data has.
228
231
  */
229
- function dependentPropDefaultSteps(
230
- metadata: IRMetadata,
231
- available: ReadonlySet<string>,
232
- inlined: ReadonlySet<string>,
233
- ): SsrSeedStep[] {
232
+ export function hasComputedDefault(p: ParamInfo): boolean {
233
+ if (p.isRest || p.defaultValue === undefined || !p.parsed || p.parsed.kind === 'arrow') return false
234
+ return p.defaultParts !== undefined || !isLiteralData(p.parsed)
235
+ }
236
+
237
+ /**
238
+ * A literal (`'x'`, `-1`, `null`, `undefined`), a template literal with no
239
+ * substitution, or an array / object literal of literal data: a value that
240
+ * reads and calls nothing.
241
+ */
242
+ function isLiteralData(e: ParsedExpr): boolean {
243
+ switch (e.kind) {
244
+ case 'literal':
245
+ return true
246
+ case 'identifier':
247
+ return e.name === 'undefined'
248
+ case 'unary':
249
+ return (e.op === '-' || e.op === '+') && e.argument.kind === 'literal' && e.argument.literalType === 'number'
250
+ case 'template-literal':
251
+ return e.parts.every(part => part.type === 'string')
252
+ case 'array-literal':
253
+ return e.elements.every(isLiteralData)
254
+ case 'object-literal':
255
+ return e.properties.every(prop => prop.kind !== 'spread' && isLiteralData(prop.value))
256
+ default:
257
+ return false
258
+ }
259
+ }
260
+
261
+ /**
262
+ * A `derived` step per prop with a computed destructure default
263
+ * ({@link hasComputedDefault}): `on ?? <default>`. A default reading an
264
+ * earlier prop of the same pattern (`ParamInfo.defaultParts`, #3531) reads
265
+ * the earlier props' template variables. Every prop is already in
266
+ * `available` (baseScope), and the steps run in dependency order
267
+ * ({@link inDefaultReadOrder}), so a chain (`{ a = 1, b = a, c = b }`) reads
268
+ * `b` after its own step re-bound it. A default the plan cannot lower adds
269
+ * no step and keeps the static ssr-defaults seed.
270
+ */
271
+ function dependentPropDefaultSteps(metadata: IRMetadata, inlined: ReadonlySet<string>): SsrSeedStep[] {
272
+ const { readable, available, unshadowed } = propDefaultScope(metadata, inlined)
234
273
  const steps: SsrSeedStep[] = []
235
274
  for (const p of inDefaultReadOrder(metadata.propsParams)) {
236
- if (p.isRest || !p.defaultParts || !p.parsed || p.parsed.kind === 'arrow') continue
275
+ if (!hasComputedDefault(p) || !p.parsed || !readable.has(p.name)) continue
237
276
  const parsed: ParsedExpr = { kind: 'logical', op: '??', left: { kind: 'identifier', name: p.name }, right: p.parsed }
238
- const step = classify(p.name, 'prop', stringifyParsedExpr(parsed), parsed, available, inlined)
277
+ const step = classify(p.name, 'prop', stringifyParsedExpr(parsed), parsed, available, unshadowed)
239
278
  if (step.kind === 'derived') steps.push(step)
240
279
  }
241
280
  return steps
242
281
  }
243
282
 
283
+ /**
284
+ * The names a prop default step may read: the props a template reads as
285
+ * their live prop (`templatePropReadParams`, so a binding that a local of
286
+ * the same name shadows gets no step), the props object, and the module
287
+ * string consts no component-scope binding shadows (`const DEF = 'z'` in
288
+ * the body hides a module `DEF`, whatever the declaration order).
289
+ */
290
+ function propDefaultScope(
291
+ metadata: IRMetadata,
292
+ inlined: ReadonlySet<string>,
293
+ ): { readable: ReadonlySet<string>; available: ReadonlySet<string>; unshadowed: ReadonlySet<string> } {
294
+ const readable = new Set(templatePropReadParams(metadata).map(p => p.name))
295
+ const locals = new Set<string>()
296
+ for (const c of metadata.localConstants ?? []) if (!c.isModule) locals.add(c.name)
297
+ for (const f of metadata.localFunctions ?? []) if (!f.isModule) locals.add(f.name)
298
+ for (const s of metadata.signals) {
299
+ locals.add(s.getter)
300
+ if (s.setter) locals.add(s.setter)
301
+ }
302
+ for (const m of metadata.memos) locals.add(m.name)
303
+ const unshadowed = new Set([...inlined].filter(name => !locals.has(name)))
304
+ const available = new Set([...readable, ...unshadowed])
305
+ if (metadata.propsObjectName) available.add(metadata.propsObjectName)
306
+ return { readable, available, unshadowed }
307
+ }
308
+
244
309
  /**
245
310
  * `params` with every prop placed after the props its destructure default
246
311
  * reads (`ParamInfo.defaultParts`). `propsParams` follows the props TYPE's
@@ -278,7 +343,7 @@ export function computeSsrSeedPlan(metadata: IRMetadata): SsrSeedPlan {
278
343
  const localConsts = localConstExprsByName(metadata)
279
344
  const steps: SsrSeedStep[] = []
280
345
 
281
- steps.push(...dependentPropDefaultSteps(metadata, available, inlined))
346
+ steps.push(...dependentPropDefaultSteps(metadata, inlined))
282
347
 
283
348
  for (const signal of metadata.signals) {
284
349
  if (signal.envReader) {
@@ -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 = ''
@@ -60,6 +60,26 @@ export function lookupPartToJsExpr(
60
60
  return `(${obj}${typed})[${key}]`
61
61
  }
62
62
 
63
+ /**
64
+ * Spell a `ternary` part's branch — the literal's COOKED value, as the IR
65
+ * carries it — as a single-quoted JS string literal. The cooked value of
66
+ * `'a\nb'` holds a real newline and that of `"it's"` a bare quote, so
67
+ * wrapping it in quotes unescaped yields an unterminated or unbalanced
68
+ * literal. Escapes only what a single-quoted literal cannot hold raw (the
69
+ * backslash, the quote and the line terminators), so a branch without
70
+ * them prints exactly as before.
71
+ */
72
+ function singleQuotedJsString(value: string): string {
73
+ const body = value
74
+ .replace(/\\/g, '\\\\')
75
+ .replace(/'/g, "\\'")
76
+ .replace(/\n/g, '\\n')
77
+ .replace(/\r/g, '\\r')
78
+ .replace(/\u2028/g, '\\u2028')
79
+ .replace(/\u2029/g, '\\u2029')
80
+ return `'${body}'`
81
+ }
82
+
63
83
  /** Convert a `template` variant's parts into a JS template-literal string. */
64
84
  export function templatePartsToJsExpr(
65
85
  parts: readonly IRTemplatePart[],
@@ -73,7 +93,7 @@ export function templatePartsToJsExpr(
73
93
  result += `\${${opts?.useTemplate ? part.templateExpr ?? part.expr : part.expr}}`
74
94
  } else if (part.type === 'ternary') {
75
95
  const cond = (opts?.useTemplate && part.templateCondition) ? part.templateCondition : part.condition
76
- result += `\${${cond} ? '${part.whenTrue}' : '${part.whenFalse}'}`
96
+ result += `\${${cond} ? ${singleQuotedJsString(part.whenTrue)} : ${singleQuotedJsString(part.whenFalse)}}`
77
97
  } else if (part.type === 'lookup') {
78
98
  result += `\${${lookupPartToJsExpr(part, opts)}}`
79
99
  }
@@ -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
@@ -102,6 +102,26 @@ export interface PropertyInfo {
102
102
  export interface ParamInfo {
103
103
  name: string
104
104
  type: TypeInfo
105
+ /**
106
+ * The binding's declared type when `type` is `unknown` only because a
107
+ * destructured binding's declared type does not resolve to a concrete
108
+ * representation (`isResolvableMemberType` declines a union such as
109
+ * `n?: number | null`). `type` stays `unknown` so typed backends keep
110
+ * their untyped field; a reader that classifies the value's nil from the
111
+ * declaration (`collectNullishAttrContext`) reads this, as it reads
112
+ * `type` for the `props.n` form.
113
+ */
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'> }
105
125
  optional: boolean
106
126
  defaultValue?: JsSourceText
107
127
  /**
@@ -116,6 +136,15 @@ export interface ParamInfo {
116
136
  parsed?: ParsedExpr
117
137
  /** When true, the default value contains an arrow function or function expression (computed from AST). */
118
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[]
119
148
  /**
120
149
  * `defaultValue` split at each read of an EARLIER binding of the same props
121
150
  * destructure (`{ off = false, on = off }` → `on`'s parts are one `param`
@@ -522,6 +551,17 @@ export interface IRExpression {
522
551
  * escaping it, while a non-empty fallback stays escaped text.
523
552
  */
524
553
  childrenRead?: 'bare' | 'nullish-empty' | 'nullish-fallback' | 'or-fallback'
554
+ /**
555
+ * A child-position read, inside a loop row, of one of the component's own
556
+ * props other than `children` (`{props.footer}`, a destructured `{footer}`,
557
+ * or a const alias of either), not shadowed by a row binding. Decided once
558
+ * in Phase 1 from the binding. The caller may pass a JSX element at that
559
+ * prop, which reaches the client as a `bfMarkup()` value or a `__slot()`
560
+ * marker, so the client writes the row slot as markup, exactly like the
561
+ * same read outside a loop. A row's own data (the item, a preamble local)
562
+ * is never flagged and stays escaped text.
563
+ */
564
+ loopRowPropRead?: true
525
565
  /** When true, expression calls signal getters or memos (has reactive `foo()` pattern). */
526
566
  callsReactiveGetters?: boolean
527
567
  /** When true, expression contains function call(s) — any `identifier()` pattern (computed from AST). */
@@ -714,6 +754,16 @@ export interface IRLoop {
714
754
  param: string
715
755
  predicate?: ParsedExpr // Boolean predicate expression (folded from any block body)
716
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
717
767
  }
718
768
 
719
769
  /**
@@ -1481,6 +1531,26 @@ export interface SpreadAttr {
1481
1531
  * per-key expansion earlier and leave this unset (#1407).
1482
1532
  */
1483
1533
  slotId?: string
1534
+ /**
1535
+ * Set on a child-component call's spread (`<Child {...props} />`) whose
1536
+ * source is the component's own props object, through any alias hop
1537
+ * (`resolveRestSpreadOriginCore` → `'props'`). Such a spread carries
1538
+ * exactly the keys the caller passed, so the client JS merges it with the
1539
+ * call's explicit props in source order, as JS does (#3688). Unset for
1540
+ * any other spread, including a `{...rest}` bag.
1541
+ */
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'
1484
1554
  }
1485
1555
 
1486
1556
  export interface JsxChildrenAttr {
@@ -1630,6 +1700,15 @@ export interface IRAttribute extends AttrMeta {
1630
1700
  * like SSR / hydrated / csr-mount agree it should.
1631
1701
  */
1632
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
1633
1712
  }
1634
1713
 
1635
1714
  export interface IREvent {
@@ -2298,6 +2377,15 @@ export interface ConstantInfo {
2298
2377
  * was supposed to do.
2299
2378
  */
2300
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
2301
2389
  /**
2302
2390
  * Set when the component writes the binding anywhere in its body, event
2303
2391
  * handlers and other callbacks included — the same fact
@@ -2371,9 +2459,25 @@ export interface IRMetadata {
2371
2459
  propsParams: ParamInfo[]
2372
2460
  /** Name of the props object parameter (e.g., 'props' in `function Component(props: Props)`) */
2373
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
2374
2469
  restPropsName: string | null
2375
2470
  /** Keys statically expanded from rest props via type analysis (closed type only) */
2376
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
2377
2481
  signals: SignalInfo[]
2378
2482
  memos: MemoInfo[]
2379
2483
  effects: EffectInfo[]
@@ -2552,6 +2656,19 @@ export interface PropUsage {
2552
2656
  export interface ClientAnalysis {
2553
2657
  needsInit: boolean
2554
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
2555
2672
  /**
2556
2673
  * The subset of `usedProps` whose declared type is a function — the client
2557
2674
  * code CALLS these, so they cannot arrive as JSON. Keyed by caller-facing