@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
@@ -16,8 +16,8 @@
16
16
  */
17
17
 
18
18
  import type { ParsedExpr } from './expression-parser.ts'
19
- import { BindingScope } from './scope/binding-scope.ts'
20
- import type { ComponentIR, MemoInfo, SignalInfo, TypeInfo } from './types.ts'
19
+ import { isBooleanResultParsed } from './adapters/boolean-result.ts'
20
+ import type { ComponentIR, IRMetadata, MemoInfo, SignalInfo, TypeInfo } from './types.ts'
21
21
 
22
22
  function isNullishType(type: TypeInfo): boolean {
23
23
  return type.kind === 'primitive' && (type.primitive === 'undefined' || type.primitive === 'null')
@@ -102,6 +102,57 @@ function collectNullablePropReads(ir: ComponentIR): ReadonlyMap<string, string>
102
102
  return names
103
103
  }
104
104
 
105
+ /** A required string / number / boolean, or a union of them: a value that is never nullish. */
106
+ function isNonNullishScalar(type: TypeInfo | undefined): boolean {
107
+ if (!type) return false
108
+ if (type.kind === 'union') return (type.unionTypes ?? []).length > 0 && (type.unionTypes ?? []).every(isNonNullishScalar)
109
+ return type.kind === 'primitive' && (type.primitive === 'string' || type.primitive === 'number' || type.primitive === 'boolean')
110
+ }
111
+
112
+ /**
113
+ * Whether `expr` is never `undefined` / `null` at SSR, so JS `expr ?? y` is
114
+ * `expr`, even when it is `false`, `''` or `0`: a boolean-result expression
115
+ * (a comparison, `!`, a boolean literal, `isBooleanResultParsed`), or a read
116
+ * of a required (not optional, not rest) prop declared as a string, number
117
+ * or boolean, or a union of them, read as its binding (`flag`) or as
118
+ * `<propsObjectName>.<key>`, with no binding shadowing the read.
119
+ */
120
+ export function isNeverNullishOperand(
121
+ expr: ParsedExpr,
122
+ metadata: Pick<IRMetadata, 'propsParams' | 'propsObjectName'>,
123
+ isBound: (name: string) => boolean,
124
+ ): boolean {
125
+ if (isBooleanResultParsed(expr)) return true
126
+ const objectName = metadata.propsObjectName
127
+ let matches: (p: IRMetadata['propsParams'][number]) => boolean
128
+ if (objectName && expr.kind === 'member' && !expr.computed && expr.object.kind === 'identifier' && expr.object.name === objectName) {
129
+ if (isBound(objectName)) return false
130
+ matches = p => (p.sourceName ?? p.name) === expr.property
131
+ } else if (!objectName && expr.kind === 'identifier') {
132
+ if (isBound(expr.name)) return false
133
+ matches = p => p.name === expr.name
134
+ } else {
135
+ return false
136
+ }
137
+ const param = metadata.propsParams.find(matches)
138
+ return !!param && !param.optional && !param.isRest && isNonNullishScalar(param.declaredType ?? param.type)
139
+ }
140
+
141
+ /**
142
+ * The params of `nullableProps` (`NullishAttrContext.nullableProps`) that
143
+ * `expr`'s value can be read whole from (`collectSeedPropReads`): the props
144
+ * whose absence an attribute bound to `expr` omits it for. A backend that
145
+ * types a prop by value (Go) makes these props nillable.
146
+ */
147
+ export function nullablePropsYieldedBy(
148
+ expr: ParsedExpr | undefined,
149
+ nullableProps: ReadonlyMap<string, string>,
150
+ ): Set<string> {
151
+ const out = new Set<string>()
152
+ collectSeedPropReads(expr, nullableProps, out)
153
+ return out
154
+ }
155
+
105
156
  /**
106
157
  * Add to `out` the params of `nullableProps` a seed's value can be read
107
158
  * whole from: a bare read, a ternary branch, the right operand of `??` /
@@ -176,24 +227,39 @@ export interface NullishAttrContext {
176
227
  * (`arithmeticOperandIsUndefined`). */
177
228
  readonly undefinedSeededSignals?: ReadonlySet<string>
178
229
  readonly memos: ReadonlyMap<string, MemoInfo>
230
+ /** Props that can be absent or `null` at SSR (no default, optional or
231
+ * typed with `undefined` / `null`), by the name a read uses
232
+ * (`collectNullablePropReads`). */
233
+ readonly nullableProps?: ReadonlyMap<string, string>
179
234
  /** Props declared as a number that may be `null` or absent (`n: number |
180
235
  * null`, `n?: number`), by the name a read uses: the binding name, and
181
236
  * `<propsObjectName>.<key>` for a member read (#3474), mapped to what the
182
237
  * engine's nil stands for there (`NullableNumberPropNil`, #3533). */
183
238
  readonly nullableNumberProps?: ReadonlyMap<string, NullableNumberPropNil>
239
+ /** Props declared as a string or a boolean that may be `null` or absent
240
+ * (`s?: string`, `b: boolean | null`), keyed like {@link nullableNumberProps}.
241
+ * Read only for a relational operand (#3824 step 3): JS converts it with
242
+ * ToNumber unless both sides are strings, so its nil's reading decides the
243
+ * comparison (`undefined < 1` is false, `null < 1` is true). */
244
+ readonly nullableScalarProps?: ReadonlyMap<string, NullableScalarProp>
245
+ }
246
+
247
+ /** A string or boolean prop's nil reading ({@link NullishAttrContext.nullableScalarProps}). */
248
+ export interface NullableScalarProp {
249
+ readonly family: 'string' | 'boolean'
250
+ readonly nil: NullableNumberPropNil
184
251
  }
185
252
 
186
253
  /**
187
254
  * What a nullable number prop's nil stands for at SSR (#3533). JS reads an
188
- * absent prop as `undefined` (`NaN` in arithmetic) and a `null` one as `0`,
189
- * while a template engine holds both as the same nil, so the declared type
190
- * decides. A prop whose type admits `null` (`n: number | null`) reads its
255
+ * absent prop as `undefined` (`NaN` in arithmetic) and a `null` one as `0`;
256
+ * the declared type decides the coercion an adapter spells statically. A prop whose type admits `null` (`n: number | null`) reads its
191
257
  * nil as `null`. A prop that can only be absent (`n?: number`,
192
258
  * `n: number | undefined`) reads it as `undefined`; a present value, `0`
193
259
  * included, is unaffected. A prop that admits both (`n?: number | null`,
194
260
  * `n: number | null | undefined`) is `'either'`: the type cannot say which
195
- * one its nil stands for, so arithmetic over it is refused
196
- * (`arithmeticOperandNilIsAmbiguous`).
261
+ * one its nil stands for, so the operand is left to the runtime's helper,
262
+ * which reads the value itself (spec/js-undefined-value.md).
197
263
  */
198
264
  export type NullableNumberPropNil = 'null' | 'undefined' | 'either'
199
265
 
@@ -208,7 +274,9 @@ export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
208
274
  signals.filter(s => s.parsed?.kind === 'identifier' && s.parsed.name === 'undefined').map(s => s.getter),
209
275
  ),
210
276
  memos: new Map((ir.metadata.memos ?? []).map(m => [m.name, m])),
277
+ nullableProps: collectNullablePropReads(ir),
211
278
  nullableNumberProps: collectNullableNumberProps(ir),
279
+ nullableScalarProps: collectNullableScalarProps(ir),
212
280
  }
213
281
  }
214
282
 
@@ -216,7 +284,10 @@ export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
216
284
  * What the nil of a number prop that admits `null` / `undefined` and no other
217
285
  * family stands for (`NullableNumberPropNil`); `null` for any other type.
218
286
  */
219
- function nullableNumberPropNil(type: TypeInfo | undefined, optional: boolean): NullableNumberPropNil | null {
287
+ function nullablePrimitivePropNil(
288
+ type: TypeInfo | undefined,
289
+ optional: boolean,
290
+ ): { family: string; nil: NullableNumberPropNil } | null {
220
291
  if (!type) return null
221
292
  const members = type.kind === 'union' ? (type.unionTypes ?? []) : [type]
222
293
  const families = new Set(members.map(m => (m.kind === 'primitive' ? m.primitive : m.kind)))
@@ -224,22 +295,38 @@ function nullableNumberPropNil(type: TypeInfo | undefined, optional: boolean): N
224
295
  const admitsAbsent = optional || families.has('undefined')
225
296
  families.delete('null')
226
297
  families.delete('undefined')
227
- if (!(admitsNull || admitsAbsent) || families.size !== 1 || !families.has('number')) return null
228
- if (admitsNull && admitsAbsent) return 'either'
229
- return admitsNull ? 'null' : 'undefined'
298
+ if (!(admitsNull || admitsAbsent) || families.size !== 1) return null
299
+ const [family] = [...families] as [string]
300
+ if (admitsNull && admitsAbsent) return { family, nil: 'either' }
301
+ return { family, nil: admitsNull ? 'null' : 'undefined' }
230
302
  }
231
303
 
232
- function collectNullableNumberProps(ir: ComponentIR): ReadonlyMap<string, NullableNumberPropNil> {
233
- const names = new Map<string, NullableNumberPropNil>()
304
+ /** Each no-default prop whose declared type `pick` accepts, keyed by the name a read uses. */
305
+ function collectNullablePrimitiveProps<T>(
306
+ ir: ComponentIR,
307
+ pick: (read: { family: string; nil: NullableNumberPropNil }) => T | null,
308
+ ): ReadonlyMap<string, T> {
309
+ const names = new Map<string, T>()
234
310
  const objectName = ir.metadata.propsObjectName
235
311
  for (const p of ir.metadata.propsParams) {
236
312
  if (p.isRest || p.defaultValue !== undefined) continue
237
- const nil = nullableNumberPropNil(p.declaredType ?? p.type, p.optional)
238
- if (nil !== null) names.set(objectName ? `${objectName}.${p.sourceName ?? p.name}` : p.name, nil)
313
+ const read = nullablePrimitivePropNil(p.declaredType ?? p.type, p.optional)
314
+ const picked = read === null ? null : pick(read)
315
+ if (picked !== null) names.set(objectName ? `${objectName}.${p.sourceName ?? p.name}` : p.name, picked)
239
316
  }
240
317
  return names
241
318
  }
242
319
 
320
+ function collectNullableNumberProps(ir: ComponentIR): ReadonlyMap<string, NullableNumberPropNil> {
321
+ return collectNullablePrimitiveProps(ir, read => (read.family === 'number' ? read.nil : null))
322
+ }
323
+
324
+ function collectNullableScalarProps(ir: ComponentIR): ReadonlyMap<string, NullableScalarProp> {
325
+ return collectNullablePrimitiveProps(ir, ({ family, nil }) =>
326
+ family === 'string' || family === 'boolean' ? { family, nil } : null,
327
+ )
328
+ }
329
+
243
330
  /**
244
331
  * Whether an attribute value's SSR value can be `undefined` / `null`, so a
245
332
  * DSL adapter must guard the attribute on a nil check to omit it the way
@@ -248,7 +335,8 @@ function collectNullableNumberProps(ir: ComponentIR): ReadonlyMap<string, Nullab
248
335
  * nullish value through:
249
336
  *
250
337
  * - a read of a nullable signal or of a memo whose type or body can be
251
- * nullish (`label()` with `label = createMemo(() => s())`);
338
+ * nullish (`label()` with `label = createMemo(() => s())`), or of a prop
339
+ * that can be absent (`props.on && 'y'`);
252
340
  * - an optional member read (`user()?.name`), an index or computed-key read
253
341
  * (`pair[1]`, `row[key]`), or a member read of a value that can itself be
254
342
  * nullish;
@@ -273,7 +361,7 @@ export function attrValueMayBeNullish(
273
361
  // loop bindings at the top level, none inside a memo body — its getters
274
362
  // resolve where the memo is declared, not where it is read.
275
363
  const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): boolean => {
276
- if (methodMayReturnUndefined(expr)) return true
364
+ if (readsUndefinedLeaf(expr, ctx, shadowed)) return true
277
365
  switch (expr.kind) {
278
366
  case 'identifier':
279
367
  return expr.name === 'undefined'
@@ -310,6 +398,22 @@ export function attrValueMayBeNullish(
310
398
  return parsed !== undefined && walk(parsed, isShadowed)
311
399
  }
312
400
 
401
+ /** A method call that can miss (`methodMayReturnUndefined`), or a read of a prop that can be absent. */
402
+ function readsUndefinedLeaf(expr: ParsedExpr, ctx: NullishAttrContext, shadowed: (name: string) => boolean): boolean {
403
+ return methodMayReturnUndefined(expr) || readsNullableProp(expr, ctx, shadowed)
404
+ }
405
+
406
+ /**
407
+ * A read of a prop that can be absent (`NullishAttrContext.nullableProps`):
408
+ * its binding (`on`) or `props.<key>`, unless a binding shadows the name.
409
+ */
410
+ function readsNullableProp(expr: ParsedExpr, ctx: NullishAttrContext, shadowed: (name: string) => boolean): boolean {
411
+ if (!ctx.nullableProps?.size) return false
412
+ if (expr.kind === 'identifier') return !shadowed(expr.name) && ctx.nullableProps.has(expr.name)
413
+ if (expr.kind !== 'member' || expr.computed || expr.object.kind !== 'identifier' || shadowed(expr.object.name)) return false
414
+ return ctx.nullableProps.has(`${expr.object.name}.${expr.property}`)
415
+ }
416
+
313
417
  /** Array / string methods that read `undefined` when nothing matches. */
314
418
  const UNDEFINED_ON_MISS_METHODS: ReadonlySet<string> = new Set(['find', 'findLast'])
315
419
 
@@ -405,14 +509,50 @@ function nullableNumberPropRead(
405
509
  ctx: NullishAttrContext,
406
510
  shadowed: (name: string) => boolean,
407
511
  ): NullableNumberPropNil | null {
408
- if (!ctx.nullableNumberProps?.size) return null
409
- if (expr.kind === 'identifier') return shadowed(expr.name) ? null : (ctx.nullableNumberProps.get(expr.name) ?? null)
512
+ return propReadIn(ctx.nullableNumberProps, expr, shadowed)
513
+ }
514
+
515
+ /** The entry of `props` (keyed as {@link NullishAttrContext.nullableNumberProps}) that `expr` reads, or `null`. */
516
+ function propReadIn<T>(
517
+ props: ReadonlyMap<string, T> | undefined,
518
+ expr: ParsedExpr,
519
+ shadowed: (name: string) => boolean,
520
+ ): T | null {
521
+ if (!props?.size) return null
522
+ if (expr.kind === 'identifier') return shadowed(expr.name) ? null : (props.get(expr.name) ?? null)
410
523
  if (expr.kind === 'member' && !expr.computed && expr.object.kind === 'identifier' && !shadowed(expr.object.name)) {
411
- return ctx.nullableNumberProps.get(`${expr.object.name}.${expr.property}`) ?? null
524
+ return props.get(`${expr.object.name}.${expr.property}`) ?? null
412
525
  }
413
526
  return null
414
527
  }
415
528
 
529
+ /**
530
+ * The string or boolean prop (`NullishAttrContext.nullableScalarProps`) a
531
+ * relational operand reads, directly or through a memo whose body is such a
532
+ * read, or `null`.
533
+ */
534
+ function scalarPropRead(
535
+ parsed: ParsedExpr | undefined,
536
+ ctx: NullishAttrContext,
537
+ isShadowed: (name: string) => boolean,
538
+ ): NullableScalarProp | null {
539
+ if (!ctx.nullableScalarProps?.size || parsed === undefined) return null
540
+ const visiting = new Set<string>()
541
+ const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): NullableScalarProp | null => {
542
+ const prop = propReadIn(ctx.nullableScalarProps, expr, shadowed)
543
+ if (prop !== null) return prop
544
+ const getter = expr.kind === 'identifier' ? expr.name : bareGetterCallName(expr)
545
+ if (getter === null || shadowed(getter)) return null
546
+ const memo = ctx.memos.get(getter)
547
+ if (!memo?.parsed || visiting.has(getter)) return null
548
+ visiting.add(getter)
549
+ const result = walk(memo.parsed, () => false)
550
+ visiting.delete(getter)
551
+ return result
552
+ }
553
+ return walk(parsed, isShadowed)
554
+ }
555
+
416
556
  /**
417
557
  * Arithmetic operators JS applies `ToNumber` to, so a `null` operand reads
418
558
  * as `0` (`null * 2` is `0`). A template engine's native operator rejects a
@@ -425,8 +565,15 @@ export const NULL_COERCING_ARITHMETIC_OPS: ReadonlySet<string> = new Set(['-', '
425
565
  /** How a DSL adapter coerces an arithmetic operand (#3474): `'null'` reads a
426
566
  * nil as `0` (JS `ToNumber(null)`), `'undefined'` reads it as `NaN`
427
567
  * (`ToNumber(undefined)`), `null` leaves the operand as is. Either coercion
428
- * leaves a non-nil operand's value as it is. */
429
- export type ArithmeticCoercion = 'null' | 'undefined' | null
568
+ * leaves a non-nil operand's value as it is: the operand is a number, so the
569
+ * runtime's JS `Number()` may spell `'undefined'`.
570
+ *
571
+ * `'undefined-keep'` (#3824 step 3) also reads a nil as `NaN`, but the
572
+ * operand is a string or boolean relational operand, which must reach the
573
+ * relational helper unconverted (`'a' < 'b'` compares strings), so the
574
+ * adapter spells it as a nil test: `NaN` when nil, the operand otherwise.
575
+ * `NaN` makes the comparison false, as `undefined < x` is in JS for any `x`. */
576
+ export type ArithmeticCoercion = 'null' | 'undefined' | 'undefined-keep' | null
430
577
 
431
578
  /**
432
579
  * The one decision behind every DSL adapter's arithmetic over a nullish
@@ -436,9 +583,10 @@ export type ArithmeticCoercion = 'null' | 'undefined' | null
436
583
  * (`NullableNumberPropNil`, #3533), is coerced under the operators JS
437
584
  * applies `ToNumber` to ({@link NULL_COERCING_ARITHMETIC_OPS}), and under
438
585
  * `+` when the other operand is a number literal, where `+` cannot
439
- * concatenate. Unary minus applies `ToNumber` too: it passes `'-'` with no
440
- * `other`. Each adapter only spells the coercion; one whose engine already
441
- * reads nil as `0` spells `'null'` as the operand itself.
586
+ * concatenate. Unary minus and unary plus apply `ToNumber` too: each passes
587
+ * its operator (`'-'` / `'+'`) with no `other`. Each adapter only spells the
588
+ * coercion; one whose engine already reads nil as `0` spells `'null'` as the
589
+ * operand itself.
442
590
  */
443
591
  export function arithmeticOperandCoercion(
444
592
  op: string,
@@ -447,103 +595,45 @@ export function arithmeticOperandCoercion(
447
595
  ctx: NullishAttrContext,
448
596
  isShadowed: (name: string) => boolean = () => false,
449
597
  ): ArithmeticCoercion {
598
+ if (RELATIONAL_OPS.has(op)) return relationalOperandCoercion(operand, ctx, isShadowed)
450
599
  if (!isNumericArithmetic(op, other)) return null
451
600
  const seed = arithmeticOperandSeed(operand, ctx, isShadowed)
452
601
  if (seed === 'absent') return 'undefined'
453
- // `'either'` is refused (`arithmeticOperandNilIsAmbiguous`); the spelling
454
- // keeps the `null` reading so the refused template stays well-formed.
455
- return seed === 'either' ? 'null' : seed
602
+ // `'either'`: the type cannot say which nil the operand holds, so it is
603
+ // left as it is and the runtime's helper reads its value, `null` or
604
+ // `UNDEF`, exactly (spec/js-undefined-value.md).
605
+ return seed === 'either' ? null : seed
456
606
  }
457
607
 
458
- /**
459
- * Whether an arithmetic operand reads a number prop whose type admits both
460
- * `null` and absence (`n?: number | null`), directly or through a memo
461
- * (#3700). JS reads the omitted prop as `undefined` (`NaN` in arithmetic)
462
- * and a `null` one as `0`; a template engine holds both as one nil and the
463
- * type cannot say which, so no spelling of {@link arithmeticOperandCoercion}
464
- * renders both. Template adapters refuse such an operand instead of
465
- * rendering one reading silently. Same operators and scope as
466
- * {@link arithmeticOperandCoercion}.
467
- */
468
- export function arithmeticOperandNilIsAmbiguous(
469
- op: string,
470
- operand: ParsedExpr | undefined,
471
- other: ParsedExpr | undefined,
472
- ctx: NullishAttrContext,
473
- isShadowed: (name: string) => boolean = () => false,
474
- ): boolean {
475
- return isNumericArithmetic(op, other) && arithmeticOperandSeed(operand, ctx, isShadowed) === 'either'
476
- }
608
+ /** The relational operators, whose operands JS converts with ToNumber unless both are strings. */
609
+ const RELATIONAL_OPS: ReadonlySet<string> = new Set(['<', '<=', '>', '>='])
477
610
 
478
611
  /**
479
- * Every arithmetic operand in `expr` that {@link arithmeticOperandNilIsAmbiguous}
480
- * matches, for a whole expression a template adapter lowers without visiting
481
- * each operator through its emitter (a memo body, a `.filter()` predicate, an
482
- * expression the runtime evaluator runs). Operands of binary operators and of
483
- * unary minus count. `scope` is where the expression is read; each arrow in
484
- * it enters its params as a callback frame (`BindingScope.enterCallback`),
485
- * so they shadow within its body only.
612
+ * The coercion of a relational operand (#3824 step 3). The runtime's
613
+ * relational helpers read a nil operand as JS `null` (`0`), so only an operand
614
+ * whose nil reads as `undefined` needs spelling: it goes through `NaN`, which
615
+ * makes the comparison false, as `undefined < 1` is in JS. A nil whose
616
+ * reading the type leaves open (`n?: number | null`, `s?: string | null`) is
617
+ * left as it is: the relational helper reads its value, `null` or `UNDEF`,
618
+ * exactly.
486
619
  */
487
- export function ambiguousNilArithmeticOperands(
488
- expr: ParsedExpr,
620
+ function relationalOperandCoercion(
621
+ operand: ParsedExpr | undefined,
489
622
  ctx: NullishAttrContext,
490
- scope: BindingScope = BindingScope.EMPTY,
491
- ): ParsedExpr[] {
492
- const found: ParsedExpr[] = []
493
- const visit = (e: ParsedExpr, at: BindingScope): void => {
494
- if (e.kind === 'arrow') {
495
- visit(e.body, at.enterCallback(e.params))
496
- return
497
- }
498
- for (const [op, operand, other] of arithmeticOperandsOf(e)) {
499
- if (arithmeticOperandNilIsAmbiguous(op, operand, other, ctx, at.asShadowPredicate())) found.push(operand)
500
- }
501
- for (const child of parsedExprChildren(e)) visit(child, at)
502
- }
503
- if (!ctx.nullableNumberProps?.size) return found
504
- visit(expr, scope)
505
- return found
506
- }
507
-
508
- /** The `[op, operand, other]` triples of an arithmetic node's operands. */
509
- function arithmeticOperandsOf(e: ParsedExpr): Array<[string, ParsedExpr, ParsedExpr | undefined]> {
510
- if (e.kind === 'binary') return [[e.op, e.left, e.right], [e.op, e.right, e.left]]
511
- if (e.kind === 'unary' && e.op === '-') return [['-', e.argument, undefined]]
512
- return []
513
- }
514
-
515
- /** The value-position sub-expressions of `e` (an arrow's body included). */
516
- function parsedExprChildren(e: ParsedExpr): ParsedExpr[] {
517
- switch (e.kind) {
518
- case 'call':
519
- return [e.callee, ...e.args]
520
- case 'member':
521
- return [e.object]
522
- case 'index-access':
523
- return [e.object, e.index]
524
- case 'binary':
525
- case 'logical':
526
- return [e.left, e.right]
527
- case 'unary':
528
- return [e.argument]
529
- case 'conditional':
530
- return [e.test, e.consequent, e.alternate]
531
- case 'template-literal':
532
- return e.parts.flatMap(p => (p.type === 'expression' ? [p.expr] : []))
533
- case 'array-literal':
534
- return e.elements
535
- case 'array-method':
536
- return [e.object, ...e.args, ...(e.method === 'flat' && e.depthExpr ? [e.depthExpr] : [])]
537
- case 'object-literal':
538
- return e.properties.map(p => (p.kind === 'spread' ? p.expr : p.value))
539
- default:
540
- return []
541
- }
623
+ isShadowed: (name: string) => boolean,
624
+ ): ArithmeticCoercion {
625
+ const seed = arithmeticOperandSeed(operand, ctx, isShadowed)
626
+ if (seed === 'absent' || seed === 'undefined') return 'undefined'
627
+ if (seed !== null) return null
628
+ // A string or boolean prop that may only be absent (`s?: string`): its nil
629
+ // is `undefined` too, but a present value must reach the helper as it is.
630
+ return scalarPropRead(operand, ctx, isShadowed)?.nil === 'undefined' ? 'undefined-keep' : null
542
631
  }
543
632
 
544
- /** The operators {@link arithmeticOperandCoercion} applies `ToNumber` under. */
633
+ /** The operators {@link arithmeticOperandCoercion} applies `ToNumber` under:
634
+ * a `+` with no `other` is unary plus, which always does. */
545
635
  function isNumericArithmetic(op: string, other: ParsedExpr | undefined): boolean {
546
- return NULL_COERCING_ARITHMETIC_OPS.has(op) || (op === '+' && isNumberLiteral(other))
636
+ return NULL_COERCING_ARITHMETIC_OPS.has(op) || (op === '+' && (other === undefined || isNumberLiteral(other)))
547
637
  }
548
638
 
549
639
  function isNumberLiteral(expr: ParsedExpr | undefined): boolean {
@@ -307,7 +307,35 @@ export function rewriteBarePropRefs(
307
307
  propAliases?: ReadonlyMap<string, string>,
308
308
  replacementFor?: (localName: string, callerKey: string) => string,
309
309
  ): string | undefined {
310
- // Walk AST to find which prop names are actually used as value references
310
+ const foundPropRefs = foundBarePropRefs(node, propNames, extraPropRefs)
311
+ if (foundPropRefs.size === 0) return undefined
312
+ return (
313
+ applyScopedPropRefRewrite(text, foundPropRefs, propAliases, replacementFor) ??
314
+ applyRegexPropRefRewrite(text, foundPropRefs, propAliases, replacementFor)
315
+ )
316
+ }
317
+
318
+ /**
319
+ * `rewriteBarePropRefs` without the regex fallback: `null` when `text`
320
+ * does not parse as an expression, so the caller refuses the shape instead
321
+ * of splicing names the scope-blind regex would also hit inside an inner
322
+ * binding (`const one = 'a'` → `const _p.one = 'a'`, #3845).
323
+ */
324
+ export function rewriteBarePropRefsStructural(
325
+ text: string,
326
+ node: ts.Node,
327
+ propNames: Set<string>,
328
+ extraPropRefs?: ReadonlySet<string>,
329
+ propAliases?: ReadonlyMap<string, string>,
330
+ replacementFor?: (localName: string, callerKey: string) => string,
331
+ ): string | undefined | null {
332
+ const foundPropRefs = foundBarePropRefs(node, propNames, extraPropRefs)
333
+ if (foundPropRefs.size === 0) return undefined
334
+ return applyScopedPropRefRewrite(text, foundPropRefs, propAliases, replacementFor)
335
+ }
336
+
337
+ /** The prop names `node` reads as values, plus the `extraPropRefs` that are props. */
338
+ function foundBarePropRefs(node: ts.Node, propNames: Set<string>, extraPropRefs?: ReadonlySet<string>): Set<string> {
311
339
  const foundPropRefs = new Set<string>()
312
340
  collectAstPropRefs(node, propNames, foundPropRefs)
313
341
  if (extraPropRefs) {
@@ -315,9 +343,5 @@ export function rewriteBarePropRefs(
315
343
  if (propNames.has(ref)) foundPropRefs.add(ref)
316
344
  }
317
345
  }
318
- if (foundPropRefs.size === 0) return undefined
319
- return (
320
- applyScopedPropRefRewrite(text, foundPropRefs, propAliases, replacementFor) ??
321
- applyRegexPropRefRewrite(text, foundPropRefs, propAliases, replacementFor)
322
- )
346
+ return foundPropRefs
323
347
  }
@@ -455,7 +455,14 @@ const EMPTY_SET: ReadonlySet<string> = new Set()
455
455
  * never bound a parameter — so callers synthesize one of these instead of a
456
456
  * full `ParamInfo` (which would need a fabricated `type`/`optional`).
457
457
  */
458
- export type LivePropReadInfo = Pick<ParamInfo, 'name' | 'sourceName' | 'defaultValue' | 'defaultContainsArrow' | 'defaultParts'>
458
+ export type LivePropReadInfo = Pick<ParamInfo, 'name' | 'sourceName' | 'defaultValue' | 'defaultContainsArrow' | 'defaultParts'> & {
459
+ /**
460
+ * The fallback is the author's own `??` (`const label = props.label ??
461
+ * 'x'`, a body alias), which replaces `null` too, not a destructure
462
+ * default, which replaces only `undefined`.
463
+ */
464
+ readonly coalescesNull?: boolean
465
+ }
459
466
 
460
467
  /** `_p.<callerKey>` — `_p` is always keyed by the caller-facing name (#2524 CSR half). */
461
468
  export function propReadBase(p: LivePropReadInfo): string {
@@ -463,8 +470,8 @@ export function propReadBase(p: LivePropReadInfo): string {
463
470
  }
464
471
 
465
472
  /**
466
- * The text a destructure default contributes as the right operand of a `??`
467
- * prop read. An arrow (or function-expression) default needs its own
473
+ * The text a destructure default contributes to a prop read
474
+ * ({@link composePropRead}). An arrow (or function-expression) default needs its own
468
475
  * parens — `??` binds tighter than an arrow head, so `_p.onInput ?? () =>
469
476
  * {}` is a SyntaxError. The one implementation of that decision, shared by
470
477
  * `propReadFallback` below (every live `_p.X` read) and `collectConstant`'s
@@ -477,11 +484,24 @@ export function coalesceDefaultText(defaultValue: string, defaultContainsArrow:
477
484
  }
478
485
 
479
486
  /**
480
- * The `??` right-hand side for a prop read, or null when the bare read is
481
- * correct. Precedence:
487
+ * What a prop read falls back to:
488
+ * - `default`: the prop's destructure default, which JS applies to an
489
+ * absent or `undefined` prop only, never to `null`
490
+ * (`spec/js-undefined-value.md`, "Decisions" (6)).
491
+ * - `guard`: a value the compiler supplies for a defaultless prop so the
492
+ * read cannot throw (`[]` for a loop array, `{}` for a member read); it
493
+ * replaces either nil.
494
+ */
495
+ export interface PropReadFallback {
496
+ readonly kind: 'default' | 'guard'
497
+ readonly text: string
498
+ }
499
+
500
+ /**
501
+ * The fallback of a prop read, or null when the bare read is correct.
502
+ * Precedence:
482
503
  * 1. an explicit destructure default (`{ x = 1 }`) — an arrow default gets
483
- * an extra paren, since `??` binds tighter than the arrow head and
484
- * `_p.onInput ?? () => {}` would be a syntax error.
504
+ * an extra paren, since `??` / `?:` bind tighter than the arrow head.
485
505
  * 2. `usedAsLoopArray` → `[]`.
486
506
  * 3. property/index access → `{}`, UNLESS the prop also guards a
487
507
  * conditional: `{}` is truthy, so that would always render the branch.
@@ -494,28 +514,72 @@ export function propReadFallback(
494
514
  p: LivePropReadInfo,
495
515
  usage: PropUsage | undefined,
496
516
  usedAsCondition: boolean,
497
- ): string | null {
498
- const defaultText = propDefaultText(p)
499
- if (defaultText) return coalesceDefaultText(defaultText, p.defaultContainsArrow)
500
- if (usage?.usedAsLoopArray) return '[]'
501
- if (propHasPropertyAccess(usage) && !usedAsCondition) return '{}'
517
+ retained?: ReadonlySet<string>,
518
+ ): PropReadFallback | null {
519
+ const defaultText = propDefaultText(p, retained)
520
+ if (defaultText) return { kind: p.coalescesNull ? 'guard' : 'default', text: coalesceDefaultText(defaultText, p.defaultContainsArrow) }
521
+ if (usage?.usedAsLoopArray) return { kind: 'guard', text: '[]' }
522
+ if (propHasPropertyAccess(usage) && !usedAsCondition) return { kind: 'guard', text: '{}' }
502
523
  return null
503
524
  }
504
525
 
526
+ /**
527
+ * A prop read `base` (a plain property read, `_p.x` / `props.x`, so reading
528
+ * it twice is safe) with its fallback, unparenthesized: a destructure
529
+ * default as `base === undefined ? default : base`, which keeps an explicit
530
+ * `null`, as JS does; a guard as `base ?? guard`. The one spelling every
531
+ * client-JS prop read and the body-destructure expansion share, and the
532
+ * shape `parsePropReadInitializer` recognizes.
533
+ */
534
+ export function composePropRead(base: string, fallback: PropReadFallback | null): string {
535
+ if (fallback === null) return base
536
+ return fallback.kind === 'default' ? `${base} === undefined ? ${fallback.text} : ${base}` : `${base} ?? ${fallback.text}`
537
+ }
538
+
505
539
  /**
506
540
  * A prop's destructure default as client JS. A default that reads an earlier
507
541
  * binding of its own pattern (`{ off = false, on = off }`, #3531) renders each
508
- * read as that prop's own live read (`_p.off ?? false`), since the emitted
509
- * scope binds no `off`; any other default is its source text. Undefined when
510
- * the prop has no default.
542
+ * read as that prop's own live read (`_p.off === undefined ? false :
543
+ * _p.off`) where the emitted scope binds no `off`; any other default is its
544
+ * source text. `retained` names the earlier bindings that ARE locals of the
545
+ * emitted scope (`retainedPropLocals`): a read of one stays a read of that
546
+ * local, as in JS, so a default sharing an earlier binding's array, or
547
+ * reading a value a default assigned to it, sees that binding. Undefined
548
+ * when the prop has no default.
511
549
  */
512
- export function propDefaultText(p: LivePropReadInfo): string | undefined {
550
+ export function propDefaultText(p: LivePropReadInfo, retained: ReadonlySet<string> = EMPTY_SET): string | undefined {
513
551
  if (!p.defaultParts) return p.defaultValue
514
552
  return p.defaultParts
515
- .map(part => (part.kind === 'text' ? part.text : livePropReadExpr(part.param, undefined, false)))
553
+ .map(part => {
554
+ if (part.kind === 'text') return part.text
555
+ return retained.has(part.param.name) ? part.param.name : livePropReadExpr(part.param, undefined, false, retained)
556
+ })
516
557
  .join('')
517
558
  }
518
559
 
560
+ /**
561
+ * The destructured prop bindings the init body keeps as locals, by local
562
+ * name: a written or mutated parameter (`let start = …`, `emitPropsExtraction`)
563
+ * and a body destructure of the props object that does not keep its declared
564
+ * value (`let { a } = props`, or one mutated later). Every other destructured
565
+ * binding is read live (`keepsDeclaredValue`). A default reading one of these
566
+ * reads the local ({@link propDefaultText}).
567
+ */
568
+ export function retainedPropLocals(b: {
569
+ propsObjectName: string | null
570
+ propsParams: readonly ParamInfo[]
571
+ localConstants: readonly ConstantInfo[]
572
+ }): ReadonlySet<string> {
573
+ const names = new Set<string>()
574
+ if (b.propsObjectName === null) {
575
+ for (const p of b.propsParams) if (!p.isRest && !keepsDeclaredValue(p)) names.add(p.name)
576
+ }
577
+ for (const c of b.localConstants) {
578
+ if (c.propsDestructure && !c.isModule && !keepsDeclaredValue(c)) names.add(c.name)
579
+ }
580
+ return names
581
+ }
582
+
519
583
  /**
520
584
  * The live read of a destructured prop inside init scope, in the same shape
521
585
  * props-object mode emits for `props.X`. The one formula every live-read
@@ -536,10 +600,11 @@ export function livePropReadExpr(
536
600
  p: LivePropReadInfo,
537
601
  usage: PropUsage | undefined,
538
602
  usedAsCondition: boolean,
603
+ retained?: ReadonlySet<string>,
539
604
  ): string {
540
605
  const base = propReadBase(p)
541
- const fallback = propReadFallback(p, usage, usedAsCondition)
542
- return fallback === null ? base : `(${base} ?? ${fallback})`
606
+ const fallback = propReadFallback(p, usage, usedAsCondition, retained)
607
+ return fallback === null ? base : `(${composePropRead(base, fallback)})`
543
608
  }
544
609
 
545
610
  /**