@barefootjs/jsx 0.40.2 → 0.41.1

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 (95) hide show
  1. package/dist/adapters/callback-method.d.ts +8 -1
  2. package/dist/adapters/callback-method.d.ts.map +1 -1
  3. package/dist/adapters/component-props.d.ts +14 -5
  4. package/dist/adapters/component-props.d.ts.map +1 -1
  5. package/dist/adapters/dsl-template-adapter.d.ts +3 -12
  6. package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
  7. package/dist/adapters/loop-row-type.d.ts +7 -0
  8. package/dist/adapters/loop-row-type.d.ts.map +1 -1
  9. package/dist/adapters/nil-reading.d.ts +15 -20
  10. package/dist/adapters/nil-reading.d.ts.map +1 -1
  11. package/dist/adapters/non-primitive-equality.d.ts +1 -1
  12. package/dist/adapters/number-value.d.ts +2 -1
  13. package/dist/adapters/number-value.d.ts.map +1 -1
  14. package/dist/adapters/parsed-expr-emitter.d.ts +11 -1
  15. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  16. package/dist/adapters/primitive-plan.d.ts +40 -11
  17. package/dist/adapters/primitive-plan.d.ts.map +1 -1
  18. package/dist/adapters/prop-classes.d.ts +5 -0
  19. package/dist/adapters/prop-classes.d.ts.map +1 -1
  20. package/dist/analyzer.d.ts.map +1 -1
  21. package/dist/index.d.ts +6 -7
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +566 -367
  24. package/dist/ir-to-client-js/child-props-object.d.ts +43 -1
  25. package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -1
  26. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  27. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  28. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  29. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  30. package/dist/ir-to-client-js/imports.d.ts +2 -2
  31. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  32. package/dist/ir-to-client-js/phases/props-extraction.d.ts +3 -3
  33. package/dist/ir-to-client-js/plan/build-declaration-emit.d.ts.map +1 -1
  34. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +3 -1
  35. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
  36. package/dist/jsx-to-ir.d.ts.map +1 -1
  37. package/dist/nullable-signal-attr.d.ts +4 -46
  38. package/dist/nullable-signal-attr.d.ts.map +1 -1
  39. package/dist/props-binding.d.ts +58 -13
  40. package/dist/props-binding.d.ts.map +1 -1
  41. package/dist/ssr-defaults.d.ts +39 -3
  42. package/dist/ssr-defaults.d.ts.map +1 -1
  43. package/dist/types.d.ts +14 -0
  44. package/dist/types.d.ts.map +1 -1
  45. package/package.json +2 -2
  46. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +6 -6
  47. package/src/__tests__/body-destructured-prop-default-arrow.test.ts +2 -2
  48. package/src/__tests__/body-destructured-props-live.test.ts +5 -5
  49. package/src/__tests__/client-js-generation.test.ts +3 -3
  50. package/src/__tests__/destructure-default-reads-earlier-param.test.ts +7 -7
  51. package/src/__tests__/destructured-props-live.test.ts +1 -1
  52. package/src/__tests__/issue-3861-object-spread-child.test.ts +102 -0
  53. package/src/__tests__/memo-body-expr.test.ts +2 -2
  54. package/src/__tests__/nil-reading.test.ts +3 -2
  55. package/src/__tests__/nullable-signal-attr.test.ts +30 -103
  56. package/src/__tests__/primitive-plan.test.ts +90 -12
  57. package/src/__tests__/prop-default-undefined-only.test.ts +115 -0
  58. package/src/__tests__/prop-references.test.ts +2 -2
  59. package/src/__tests__/props-destructuring.test.ts +17 -0
  60. package/src/__tests__/prune-unused-prop-extractions.test.ts +3 -3
  61. package/src/__tests__/renamed-body-destructure-template.test.ts +7 -7
  62. package/src/__tests__/rewrite-destructured-props.test.ts +1 -1
  63. package/src/__tests__/ssr-defaults.test.ts +113 -25
  64. package/src/__tests__/static-const-child-prop.test.ts +85 -0
  65. package/src/__tests__/written-binding-substitution.test.ts +8 -8
  66. package/src/adapters/callback-method.ts +13 -5
  67. package/src/adapters/component-props.ts +20 -6
  68. package/src/adapters/dsl-template-adapter.ts +26 -28
  69. package/src/adapters/loop-row-type.ts +10 -0
  70. package/src/adapters/nil-reading.ts +23 -25
  71. package/src/adapters/non-primitive-equality.ts +1 -1
  72. package/src/adapters/number-value.ts +26 -2
  73. package/src/adapters/parsed-expr-emitter.ts +31 -2
  74. package/src/adapters/primitive-plan.ts +84 -15
  75. package/src/adapters/prop-classes.ts +16 -3
  76. package/src/analyzer.ts +27 -7
  77. package/src/index.ts +5 -12
  78. package/src/ir-to-client-js/child-props-object.ts +140 -6
  79. package/src/ir-to-client-js/collect-elements.ts +7 -14
  80. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +4 -2
  81. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +4 -2
  82. package/src/ir-to-client-js/html-template.ts +18 -9
  83. package/src/ir-to-client-js/imports.ts +1 -1
  84. package/src/ir-to-client-js/phases/props-extraction.ts +11 -7
  85. package/src/ir-to-client-js/plan/build-declaration-emit.ts +40 -7
  86. package/src/ir-to-client-js/prune-unused-prop-extractions.ts +58 -5
  87. package/src/ir-to-client-js/rewrite-destructured-props.ts +31 -1
  88. package/src/jsx-to-ir.ts +113 -60
  89. package/src/nullable-signal-attr.ts +11 -138
  90. package/src/props-binding.ts +84 -19
  91. package/src/ssr-defaults.ts +100 -11
  92. package/src/types.ts +11 -0
  93. package/dist/adapters/ambiguous-nil-arithmetic.d.ts +0 -42
  94. package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +0 -1
  95. package/src/adapters/ambiguous-nil-arithmetic.ts +0 -103
@@ -13,7 +13,7 @@
13
13
  // computations that derive from those signals (`count() * 2`).
14
14
 
15
15
  import { describe, test, expect } from 'bun:test'
16
- import { extractSsrDefaults, deriveStashFromDefaults } from '../ssr-defaults'
16
+ import { extractSsrDefaults, deriveStashFromDefaults, encodeUndefinedMembers, decodeUndefinedMembers } from '../ssr-defaults'
17
17
  import type { SsrDefault } from '../ssr-defaults'
18
18
  import { analyzeComponent } from '../analyzer'
19
19
  import { buildMetadata } from '../compiler'
@@ -80,7 +80,24 @@ describe('extractSsrDefaults', () => {
80
80
  const defaults = extractSsrDefaults(metadata)
81
81
  expect(defaults?.count).toEqual({ value: 0 })
82
82
  expect(defaults?.doubled).toEqual({ value: 0 })
83
- expect(defaults?.initial).toEqual({ propName: 'initial', value: null })
83
+ expect(defaults?.initial).toEqual({ propName: 'initial' })
84
+ })
85
+
86
+ test('a prop with no default has no `value` key; one with a default keeps it, even when null (U1)', () => {
87
+ const metadata = metadataFor(`
88
+ 'use client'
89
+ function Chip({ c, size = 'md', none = null }: { c?: string; size?: string; none?: string | null }) {
90
+ return <p className={size}>{c}{none}</p>
91
+ }
92
+ `)
93
+ // The manifest text is what every runtime reads (`toEqual` would not tell
94
+ // a missing key from an `undefined` one).
95
+ expect(JSON.parse(JSON.stringify(extractSsrDefaults(metadata)))).toEqual({
96
+ c: { propName: 'c' },
97
+ size: { propName: 'size', value: 'md' },
98
+ none: { propName: 'none', value: null },
99
+ })
100
+ expect('value' in (extractSsrDefaults(metadata)?.c ?? {})).toBe(false)
84
101
  })
85
102
 
86
103
  test('seeds every declared bare-props prop, including direct template reads (#2126)', () => {
@@ -99,7 +116,7 @@ describe('extractSsrDefaults', () => {
99
116
  `)
100
117
 
101
118
  const defaults = extractSsrDefaults(metadata)
102
- expect(defaults?.label).toEqual({ propName: 'label', value: null })
119
+ expect(defaults?.label).toEqual({ propName: 'label' })
103
120
  expect(defaults?.n).toEqual({ value: 0 })
104
121
  })
105
122
 
@@ -129,7 +146,7 @@ describe('extractSsrDefaults', () => {
129
146
  `)
130
147
 
131
148
  const defaults = extractSsrDefaults(metadata)
132
- expect(defaults?.label).toEqual({ propName: 'label', value: null })
149
+ expect(defaults?.label).toEqual({ propName: 'label' })
133
150
  })
134
151
 
135
152
  test('self-derived signal collision (#2669, shape B): non-idempotent derivation must not seed the pre-fix double-applied value', () => {
@@ -137,7 +154,7 @@ describe('extractSsrDefaults', () => {
137
154
  // the pre-fix EVALUATED value (2, for no caller prop) makes the
138
155
  // template's own recompute apply the `* 2` a second time (`2 * 2 =
139
156
  // 4`) — wrong even with no caller props at all. Pin that the entry
140
- // is the RAW-prop shape (`value: null`), not `{ value: 2 }`.
157
+ // is the RAW-prop shape (no `value`), not `{ value: 2 }`.
141
158
  const metadata = metadataFor(`
142
159
  'use client'
143
160
  import { createSignal } from '@barefootjs/client'
@@ -148,7 +165,7 @@ describe('extractSsrDefaults', () => {
148
165
  `)
149
166
 
150
167
  const defaults = extractSsrDefaults(metadata)
151
- expect(defaults?.count).toEqual({ propName: 'count', value: null })
168
+ expect(defaults?.count).toEqual({ propName: 'count' })
152
169
  })
153
170
 
154
171
  test('self-derived memo collision (#2669): the same rule applies to a memo whose computation derives from a same-named prop', () => {
@@ -169,7 +186,7 @@ describe('extractSsrDefaults', () => {
169
186
  `)
170
187
 
171
188
  const defaults = extractSsrDefaults(metadata)
172
- expect(defaults?.label).toEqual({ propName: 'label', value: null })
189
+ expect(defaults?.label).toEqual({ propName: 'label' })
173
190
  expect(defaults?.n).toEqual({ value: 0 })
174
191
  })
175
192
 
@@ -190,7 +207,7 @@ describe('extractSsrDefaults', () => {
190
207
  `)
191
208
 
192
209
  const defaults = extractSsrDefaults(metadata)
193
- expect(defaults?.label).toEqual({ propName: 'label', value: null })
210
+ expect(defaults?.label).toEqual({ propName: 'label' })
194
211
  expect(defaults?.[metadata.signals[0].getter]).toEqual({ value: 'sig' })
195
212
  })
196
213
 
@@ -214,7 +231,7 @@ describe('extractSsrDefaults', () => {
214
231
  `)
215
232
 
216
233
  const defaults = extractSsrDefaults(metadata)
217
- expect(defaults?.label).toEqual({ propName: 'label', value: null })
234
+ expect(defaults?.label).toEqual({ propName: 'label' })
218
235
  })
219
236
 
220
237
  test('self-derived memo collision THROUGH a component-scope const (#2685 review)', () => {
@@ -229,7 +246,7 @@ describe('extractSsrDefaults', () => {
229
246
  `)
230
247
 
231
248
  const defaults = extractSsrDefaults(metadata)
232
- expect(defaults?.label).toEqual({ propName: 'label', value: null })
249
+ expect(defaults?.label).toEqual({ propName: 'label' })
233
250
  })
234
251
 
235
252
  test('multi-hop const chain resolves transitively (#2685 review): `const a = props.x; const b = a`', () => {
@@ -252,7 +269,7 @@ describe('extractSsrDefaults', () => {
252
269
 
253
270
  const defaults = extractSsrDefaults(metadata)
254
271
  expect(defaults?.count).toEqual({ value: 1 })
255
- expect(defaults?.x).toEqual({ propName: 'x', value: null })
272
+ expect(defaults?.x).toEqual({ propName: 'x' })
256
273
  })
257
274
 
258
275
  test('safety-net seeding sees a prop through a component-scope const (#2685 review)', () => {
@@ -276,7 +293,7 @@ describe('extractSsrDefaults', () => {
276
293
 
277
294
  const defaults = extractSsrDefaults(metadata)
278
295
  expect(defaults?.count).toEqual({ value: 0 })
279
- expect(defaults?.initial).toEqual({ propName: 'initial', value: null })
296
+ expect(defaults?.initial).toEqual({ propName: 'initial' })
280
297
  })
281
298
 
282
299
  test('NEGATIVE (#2685 review): a const NOT derived from props keeps the signal-wins behavior', () => {
@@ -308,7 +325,7 @@ describe('extractSsrDefaults', () => {
308
325
  // under its own name, and the prop keeps its prop entry.
309
326
  const defaults = extractSsrDefaults(metadata)
310
327
  expect(defaults?.bfSignal_label).toEqual({ value: null })
311
- expect(defaults?.label).toEqual({ propName: 'label', value: null })
328
+ expect(defaults?.label).toEqual({ propName: 'label' })
312
329
  })
313
330
 
314
331
  test('aliased destructured prop (#2460): `propName` is the CALLER-facing key, not the local binding', () => {
@@ -327,9 +344,9 @@ describe('extractSsrDefaults', () => {
327
344
  const defaults = extractSsrDefaults(metadata)
328
345
  expect(defaults).toBeDefined()
329
346
  // Keyed by the LOCAL binding (the template variable name)...
330
- expect(defaults?.count).toEqual({ propName: 'n', value: null })
347
+ expect(defaults?.count).toEqual({ propName: 'n' })
331
348
  // ...but `propName` is the CALLER-facing key.
332
- expect(defaults?.text).toEqual({ propName: 'text', value: null })
349
+ expect(defaults?.text).toEqual({ propName: 'text' })
333
350
  })
334
351
 
335
352
  test('aliased destructured prop with a default (#2460): `propName` still resolves to the source key', () => {
@@ -351,8 +368,8 @@ describe('extractSsrDefaults', () => {
351
368
  `)
352
369
 
353
370
  const defaults = extractSsrDefaults(metadata)
354
- expect(defaults?.n).toEqual({ propName: 'n', value: null })
355
- expect(defaults?.text).toEqual({ propName: 'text', value: null })
371
+ expect(defaults?.n).toEqual({ propName: 'n' })
372
+ expect(defaults?.text).toEqual({ propName: 'text' })
356
373
  })
357
374
 
358
375
  test('bare-props-form BODY-destructured default (#2943) seeds the evaluated default, not null', () => {
@@ -391,7 +408,7 @@ describe('extractSsrDefaults', () => {
391
408
  }
392
409
  `)
393
410
  const defaults = extractSsrDefaults(metadata)
394
- expect(defaults?.label).toEqual({ propName: 'label', value: null })
411
+ expect(defaults?.label).toEqual({ propName: 'label' })
395
412
  expect(defaults?.text).toEqual({ propName: 'label', value: 'none' })
396
413
 
397
414
  expect(deriveStashFromDefaults(defaults!, { label: 'named' })).toMatchObject({ text: 'named' })
@@ -413,7 +430,7 @@ describe('extractSsrDefaults', () => {
413
430
  `)
414
431
 
415
432
  const defaults = extractSsrDefaults(metadata)
416
- expect(defaults?.children).toEqual({ propName: 'children', value: null })
433
+ expect(defaults?.children).toEqual({ propName: 'children' })
417
434
  expect(defaults?.kids).toEqual(defaults?.children)
418
435
  })
419
436
 
@@ -434,7 +451,7 @@ describe('extractSsrDefaults', () => {
434
451
  `)
435
452
 
436
453
  const defaults = extractSsrDefaults(metadata)
437
- expect(defaults?.children).toEqual({ propName: 'children', value: null })
454
+ expect(defaults?.children).toEqual({ propName: 'children' })
438
455
  expect(defaults?.kids).toEqual(defaults?.children)
439
456
  })
440
457
 
@@ -581,9 +598,28 @@ describe('extractSsrDefaults', () => {
581
598
 
582
599
  const defaults = extractSsrDefaults(metadata)
583
600
  // Unlike the array case, a missing key on a plain object resolves that
584
- // ONE element to `undefined` (→ `null`) and the `.map()` keeps going —
585
- // it does not abort the whole computation.
586
- expect(defaults?.x).toEqual({ value: [1, null] })
601
+ // ONE element to `undefined` and the `.map()` keeps going — it does not
602
+ // abort the whole computation. The manifest spells the `undefined`
603
+ // element with the `{"$undef": true}` sentinel.
604
+ expect(defaults?.x).toEqual({ value: [1, { $undef: true }] })
605
+ })
606
+
607
+ test('an `undefined` array element or object member is the `$undef` sentinel, apart from `null`', () => {
608
+ const metadata = metadataFor(`
609
+ 'use client'
610
+ import { createSignal } from '@barefootjs/client'
611
+ function C() {
612
+ const [a] = createSignal([undefined, null, 'x'])
613
+ const [o] = createSignal({ a: null, b: undefined, n: { c: undefined } })
614
+ return <p>{a()}{o()}</p>
615
+ }
616
+ `)
617
+
618
+ const defaults = extractSsrDefaults(metadata)
619
+ expect(defaults?.a).toEqual({ value: [{ $undef: true }, null, 'x'] })
620
+ expect(defaults?.o).toEqual({ value: { a: null, b: { $undef: true }, n: { c: { $undef: true } } } })
621
+ // The manifest is JSON: the sentinel survives it, and a present key stays present.
622
+ expect(JSON.parse(JSON.stringify(defaults?.o))).toEqual({ value: { a: null, b: { $undef: true }, n: { c: { $undef: true } } } })
587
623
  })
588
624
 
589
625
  // #2813 (pullfrog nitpick on the PR fixing it): a bare local-const alias
@@ -641,10 +677,27 @@ describe('extractSsrDefaults', () => {
641
677
  `)
642
678
 
643
679
  const defaults = extractSsrDefaults(metadata)
644
- expect(defaults?.label).toEqual({ propName: 'label', value: null })
680
+ expect(defaults?.label).toEqual({ propName: 'label' })
645
681
  expect(defaults?.labelAlias).toBe(defaults?.label)
646
682
  })
647
683
 
684
+ test('a getter alias sharing its origin entry is encoded once', () => {
685
+ const metadata = metadataFor(`
686
+ 'use client'
687
+ import { createSignal } from '@barefootjs/client'
688
+ function C() {
689
+ const [value] = createSignal<Record<string, unknown>>({ gone: undefined, payload: { $undef: true } })
690
+ const alias = value
691
+ return <p>{JSON.stringify(alias())}</p>
692
+ }
693
+ `)
694
+
695
+ const defaults = extractSsrDefaults(metadata)
696
+ expect(defaults?.alias).toBe(defaults?.value)
697
+ expect(defaults?.value).toEqual({ value: { gone: { $undef: true }, payload: { $$undef: true } } })
698
+ expect(decodeUndefinedMembers(defaults?.alias?.value)).toEqual({ gone: undefined, payload: { $undef: true } })
699
+ })
700
+
648
701
  // #2924 review follow-up (pullfrog nitpick on the PR fixing the setter
649
702
  // case): the setter-symmetric shape of the #2813 getter-alias test above
650
703
  // — a bare local-const alias of a signal SETTER must be seeded under its
@@ -676,7 +729,7 @@ describe('extractSsrDefaults', () => {
676
729
  expect(defaults?.off).toEqual({ propName: 'off', value: false })
677
730
  // A static `!off` would read `true` and win over the in-template
678
731
  // `on ?? !off` whatever `off` the caller passed.
679
- expect(defaults?.on).toEqual({ propName: 'on', value: null })
732
+ expect(defaults?.on).toEqual({ propName: 'on' })
680
733
  })
681
734
  })
682
735
 
@@ -811,6 +864,41 @@ describe('deriveStashFromDefaults', () => {
811
864
  expect(deriveStashFromDefaults(defaults, {})).toEqual({ flag: null })
812
865
  })
813
866
 
867
+ test('an entry without `value` (a prop with no default) seeds null, as `value: null` did (U1)', () => {
868
+ const missing: Record<string, SsrDefault> = { n: { propName: 'n' }, rest: { isRestProps: true } }
869
+ const explicit: Record<string, SsrDefault> = { n: { propName: 'n', value: null }, rest: { isRestProps: true, value: null } }
870
+ for (const props of [{}, { n: null }, { n: 3 }, { rest: { a: 1 } }]) {
871
+ expect(deriveStashFromDefaults(missing, props)).toEqual(deriveStashFromDefaults(explicit, props))
872
+ }
873
+ expect(deriveStashFromDefaults(missing, {})).toEqual({ n: null, rest: null })
874
+ })
875
+
876
+ test('seed data shaped like the sentinel stays data: `$`-prefixed keys get one more `$`', () => {
877
+ const data = { payload: { $undef: true }, $$x: 1, list: [{ $undef: true }, undefined], gone: undefined }
878
+ const encoded = encodeUndefinedMembers(data)
879
+ expect(encoded).toEqual({
880
+ payload: { $$undef: true },
881
+ $$$x: 1,
882
+ list: [{ $$undef: true }, { $undef: true }],
883
+ gone: { $undef: true },
884
+ })
885
+ const decoded = decodeUndefinedMembers(JSON.parse(JSON.stringify(encoded))) as Record<string, unknown>
886
+ expect(decoded).toEqual(data)
887
+ expect('gone' in decoded).toBe(true)
888
+ expect((decoded.list as unknown[])[1]).toBeUndefined()
889
+ expect(decodeUndefinedMembers({ $undef: true })).toBeUndefined()
890
+ expect(decodeUndefinedMembers({ $undef: false })).toEqual({ $undef: false })
891
+ })
892
+
893
+ test('the static fallback decodes the `$undef` sentinel to `undefined`, keeping the key', () => {
894
+ const defaults: Record<string, SsrDefault> = { o: { value: { a: null, b: { $undef: true } } }, a: { value: [{ $undef: true }] } }
895
+ const extra = deriveStashFromDefaults(defaults, {})
896
+ expect(extra.a).toEqual([undefined])
897
+ expect('b' in (extra.o as object)).toBe(true)
898
+ expect((extra.o as { b: unknown }).b).toBeUndefined()
899
+ expect(JSON.stringify(extra.o)).toBe('{"a":null}')
900
+ })
901
+
814
902
  test('mixed defaults map resolves every entry kind independently', () => {
815
903
  const defaults: Record<string, SsrDefault> = {
816
904
  count: { propName: 'n', value: null },
@@ -0,0 +1,85 @@
1
+ import { describe, expect, test } from 'bun:test'
2
+ import { analyzeComponent } from '../analyzer'
3
+ import { jsxToIR } from '../jsx-to-ir'
4
+ import type { IRNode, IRProp } from '../types'
5
+
6
+ /**
7
+ * Every component prop named `xs` in the IR tree, in source order: the
8
+ * literal a template passes (`staticConstLiteral`), or `null` when the prop
9
+ * carries none.
10
+ */
11
+ function xsProps(source: string): Array<string | null> {
12
+ const out: Array<string | null> = []
13
+ const visit = (node: IRNode | null | undefined): void => {
14
+ if (!node || typeof node !== 'object') return
15
+ if (node.type === 'component') {
16
+ for (const p of node.props as IRProp[]) {
17
+ if (p.name !== 'xs') continue
18
+ // JS-runtime consumers read `expr`, which stays the const's name.
19
+ if (p.value.kind === 'expression') expect(p.value.expr.startsWith('[') || p.value.expr.startsWith('{')).toBe(false)
20
+ out.push(p.value.kind === 'expression' ? (p.value.staticConstLiteral?.expr ?? null) : null)
21
+ }
22
+ }
23
+ for (const value of Object.values(node)) {
24
+ if (Array.isArray(value)) for (const child of value) visit(child as IRNode)
25
+ else if (value && typeof value === 'object' && 'type' in value) visit(value as IRNode)
26
+ }
27
+ }
28
+ visit(jsxToIR(analyzeComponent(source, 'C.tsx')))
29
+ return out
30
+ }
31
+
32
+ describe('a const holding a static literal, passed whole as a child prop, carries the literal for templates', () => {
33
+ test('a module array, a module object and a component-local array', () => {
34
+ expect(xsProps(`
35
+ import { Kid } from './kid'
36
+ const QS = ['a', 'b"c']
37
+ const REC = { lf: 'r', n: 3 }
38
+ export function C() {
39
+ const local = [1e3, .5]
40
+ return <div><Kid xs={QS} /><Kid xs={REC} /><Kid xs={local} /></div>
41
+ }
42
+ `)).toEqual([`['a', 'b"c']`, `{ lf: 'r', n: 3 }`, '[1e3, .5]'])
43
+ })
44
+
45
+ test('a loop row or a prop of the same name is that binding', () => {
46
+ expect(xsProps(`
47
+ import { Kid } from './kid'
48
+ const QS = ['a', 'b']
49
+ const PS = ['m']
50
+ export function C({ PS }: { PS: string[] }) {
51
+ return (
52
+ <div>
53
+ {[['r']].map(QS => <Kid key={QS[0]} xs={QS} />)}
54
+ <Kid xs={PS} />
55
+ </div>
56
+ )
57
+ }
58
+ `)).toEqual([null, null])
59
+ })
60
+
61
+ test('a const that reads a name, calls something or is changed carries no literal', () => {
62
+ expect(xsProps(`
63
+ import { Kid } from './kid'
64
+ const BASE = 'b'
65
+ const READS = [BASE]
66
+ const CALLS = [String(1)]
67
+ export function C(props: { v: string }) {
68
+ const fromProp = [props.v]
69
+ const changed = ['x']
70
+ changed.push('y')
71
+ return <div><Kid xs={READS} /><Kid xs={CALLS} /><Kid xs={fromProp} /><Kid xs={changed} /></div>
72
+ }
73
+ `)).toEqual([null, null, null, null])
74
+ })
75
+
76
+ test('a /* @client */ prop carries no literal', () => {
77
+ expect(xsProps(`
78
+ import { Kid } from './kid'
79
+ const QS = ['a']
80
+ export function C() {
81
+ return <div><Kid xs={/* @client */ QS} /></div>
82
+ }
83
+ `)).toEqual([null])
84
+ })
85
+ })
@@ -91,7 +91,7 @@ describe('a destructured prop the component writes is an init local', () => {
91
91
  return <div><button onClick={() => setItems([...items(), ++start])}>add</button><p>{items().join(',')}</p></div>`,
92
92
  P,
93
93
  )
94
- expect(js).toContain('let start = _p.start ?? 0')
94
+ expect(js).toContain('let start = _p.start === undefined ? 0 : _p.start')
95
95
  expect(js).toContain('++start')
96
96
  expect(js).not.toContain('++(_p.start')
97
97
  })
@@ -105,12 +105,12 @@ describe('a destructured prop the component writes is an init local', () => {
105
105
  )
106
106
  expect(js).toContain('const add = () => { start += 1; setItems([...items(), start]) }')
107
107
  // Until a handler runs, the binding holds the prop: the template reads it.
108
- expect(templateOf(js)).toContain('(_p.start ?? 0)')
108
+ expect(templateOf(js)).toContain('(_p.start === undefined ? 0 : _p.start)')
109
109
  })
110
110
 
111
111
  test('a top-level reassignment', () => {
112
112
  const js = clientJs(`start = start + 1\n return <p>{start}</p>`, P)
113
- expect(js).toContain('let start = _p.start ?? 0')
113
+ expect(js).toContain('let start = _p.start === undefined ? 0 : _p.start')
114
114
  expect(js).toContain('start = start + 1')
115
115
  })
116
116
 
@@ -122,7 +122,7 @@ describe('a destructured prop the component writes is an init local', () => {
122
122
  )
123
123
  expect(js).toContain('[a, b] = [b, a]')
124
124
  expect(js).toContain('for (k in')
125
- expect(js).toContain('let a = _p.a ?? 1')
125
+ expect(js).toContain('let a = _p.a === undefined ? 1 : _p.a')
126
126
  })
127
127
 
128
128
  test('a prop without a default no longer writes the props object', () => {
@@ -141,7 +141,7 @@ describe('a destructured prop the component writes is an init local', () => {
141
141
  return <div><button onClick={() => setItems([...items(), ++s])}>add</button><p>{items().join(',')}</p></div>`,
142
142
  '{ start: s = 0 }: { start?: number }',
143
143
  )
144
- expect(js).toContain('let s = _p.start ?? 0')
144
+ expect(js).toContain('let s = _p.start === undefined ? 0 : _p.start')
145
145
  expect(js).toContain('++s')
146
146
  })
147
147
 
@@ -166,7 +166,7 @@ describe('a `for (let …)` head shadows a prop of the same name', () => {
166
166
  return <div><button onClick={add}>go</button><p>{n()}</p></div>`,
167
167
  '{ i = 5 }: { i?: number }',
168
168
  )
169
- expect(js).toContain('for (let i = 0; i < 3; i++) t += i; setN(t + (_p.i ?? 5))')
169
+ expect(js).toContain('for (let i = 0; i < 3; i++) t += i; setN(t + (_p.i === undefined ? 5 : _p.i))')
170
170
  })
171
171
  })
172
172
 
@@ -207,7 +207,7 @@ describe('a written local in the client template', () => {
207
207
  describe('every template path leaves a write to init', () => {
208
208
  test('a stateless component writing a prop in a handler renders the prop from its template', () => {
209
209
  const js = clientJs(`return <button onClick={() => start++}>{start}</button>`, '{ start = 0 }: { start?: number }')
210
- expect(js).toContain('let start = _p.start ?? 0')
210
+ expect(js).toContain('let start = _p.start === undefined ? 0 : _p.start')
211
211
  expect(runTemplate(js, { start: 5 })).toContain('5')
212
212
  expect(runTemplate(js, {})).toContain('0')
213
213
  })
@@ -361,7 +361,7 @@ describe('BF065: a client component markup that writes a binding while it render
361
361
  CHILD,
362
362
  )
363
363
  expect(templateOf(js)).toContain('data-bf-ph')
364
- expect(js).toContain('let start = _p.start ?? 0')
364
+ expect(js).toContain('let start = _p.start === undefined ? 0 : _p.start')
365
365
  expect(js).toContain('return () => ++start')
366
366
  })
367
367
 
@@ -104,6 +104,14 @@ export interface EvalBody {
104
104
  captured: CallbackCapture[]
105
105
  }
106
106
 
107
+ /**
108
+ * Where an evaluator body is read, for its nil-reading rewrite
109
+ * (`rewriteNilReadingsDeep`): the enclosing scope's operand typing.
110
+ */
111
+ export interface NilReadingSite {
112
+ readonly typing: ScopedOperandTyping
113
+ }
114
+
107
115
  /**
108
116
  * Serialize a callback body for the runtime evaluator, with the captures its
109
117
  * env needs; `null` when the body is outside the evaluator surface. `bound`
@@ -118,11 +126,11 @@ export interface EvalBody {
118
126
  * else. Capturing `props` whole would bind it to an undefined template
119
127
  * variable, and the predicate would read every `props.X` as `undefined`.
120
128
  */
121
- export function evalBody(body: ParsedExpr, bound: ReadonlySet<string>, propsObject: string, typing?: ScopedOperandTyping): EvalBody | null {
122
- // The evaluator reads every nil as `null`: with the enclosing scope's
123
- // typing, an operand whose nil Phase 1 reads is rewritten first (#3824
124
- // step 4a), for a caller that reaches here without `emitParsedExpr`.
125
- const read = typing ? rewriteNilReadingsDeep(body, typing.enterCallback([...bound])) : body
129
+ export function evalBody(body: ParsedExpr, bound: ReadonlySet<string>, propsObject: string, site?: NilReadingSite): EvalBody | null {
130
+ // With the enclosing scope's typing, an operand whose nil Phase 1 reads is
131
+ // rewritten first (#3824 step 4a), for a caller that reaches here without
132
+ // `emitParsedExpr`, as `emitParsedExpr` would.
133
+ const read = site ? rewriteNilReadingsDeep(body, site.typing.enterCallback([...bound])) : body
126
134
  const lifted = liftPropsReads(read, bound, propsObject)
127
135
  const json = serializeParsedExpr(lifted.body)
128
136
  if (json === null) return null
@@ -96,10 +96,9 @@ export interface EarlierComponentProps {
96
96
  * caller actually handed over, and every optional param is `'present'`:
97
97
  * an `'entry'` would hand the child the key even when the caller left it
98
98
  * out, and the child would then read it as passed (a nil overriding its
99
- * own earlier prop in a further `{...props}`). Without `callerKeys` (Go,
100
- * whose Input struct can't tell the two nils apart), "passed" is "not
101
- * nil", and an explicit nil keeps the earlier value (limitation
102
- * `props-spread-explicit-null-override`).
99
+ * own earlier prop in a further `{...props}`). Without `callerKeys`,
100
+ * "passed" is "not nil"; Go answers it from its own `bf.Opt[T]` Input
101
+ * field (`Passed()`) instead.
103
102
  */
104
103
  export function propsSpreadParam(
105
104
  param: ComponentPropsParam,
@@ -181,6 +180,18 @@ export function planComponentProps(
181
180
  return segments
182
181
  }
183
182
 
183
+ /**
184
+ * The value a template passes for a component prop. A bare read of a const
185
+ * holding a static literal (`ExpressionAttr.staticConstLiteral`, `xs={QS}`)
186
+ * is that literal, as if the source had written it in place: a template
187
+ * holds the props, not the consts. Any other value is itself.
188
+ */
189
+ export function templatePropValue(value: AttrValue): AttrValue {
190
+ if (value.kind !== 'expression' || !value.staticConstLiteral) return value
191
+ const { expr, parsed } = value.staticConstLiteral
192
+ return { kind: 'expression', expr, parsed }
193
+ }
194
+
184
195
  /** A spelled segment: the entries' hash-literal parts, or the expression a spread folds in. */
185
196
  export type SpelledComponentPropSegment = { kind: 'entries'; parts: string[] } | { kind: 'spread'; expr: string }
186
197
 
@@ -201,7 +212,10 @@ export interface ComponentPropSpelling {
201
212
 
202
213
  /**
203
214
  * Spell a plan in source order, so the captures an entry's spelling emits
204
- * (named slots, then the children) keep their order and counter values.
215
+ * (named slots, then the children) keep their order and counter values. A
216
+ * `value` entry is spelled as the template passes it
217
+ * ({@link templatePropValue}); the plan keeps the IR's own value, which
218
+ * callers compare by identity.
205
219
  */
206
220
  export function spellComponentProps(
207
221
  segments: readonly ComponentPropSegment[],
@@ -212,7 +226,7 @@ export function spellComponentProps(
212
226
  if (segment.kind === 'present-param') return { kind: 'spread', expr: spelling.presentParam(segment.name) }
213
227
  const parts: string[] = []
214
228
  for (const entry of segment.entries) {
215
- const part = spelling.entry(entry)
229
+ const part = spelling.entry(entry.kind === 'value' ? { ...entry, value: templatePropValue(entry.value) } : entry)
216
230
  if (part) parts.push(part)
217
231
  }
218
232
  return { kind: 'entries', parts }
@@ -10,14 +10,15 @@ import { buildImportAliasMap } from '../ir-to-client-js/component-scope.ts'
10
10
  import { prepareLoweringMatchers, type LoweringMatcher } from '../lowering-registry.ts'
11
11
  import type { ParsedExpr } from '../expression-parser.ts'
12
12
  import {
13
+ arithmeticOperandCoercion,
13
14
  collectNullableSignalGetters,
14
15
  collectNullishAttrContext,
15
16
  type ArithmeticCoercion,
16
17
  type NullishAttrContext,
17
18
  } from '../nullable-signal-attr.ts'
18
19
  import { BindingScope } from '../scope/binding-scope.ts'
19
- import { declaredValueTypes, loopRowType, type DeclaredValueTypes } from './loop-row-type.ts'
20
- import { collectOperandTyping, EMPTY_OPERAND_TYPING, type OperandTyping, type ScopedOperandTyping } from './operand-typing.ts'
20
+ import { declaredValueType, declaredValueTypes, loopRowType, type DeclaredValueTypes } from './loop-row-type.ts'
21
+ import { collectOperandTyping, EMPTY_OPERAND_TYPING, factsOfType, type OperandTyping, type ScopedOperandTyping } from './operand-typing.ts'
21
22
  import { rootPropAliasesForLoop, rootPropAliasNames } from '../scope/root-prop-alias.ts'
22
23
  import type {
23
24
  CompilerError,
@@ -28,13 +29,7 @@ import type {
28
29
  IRMetadata,
29
30
  PreambleValueDeclaration,
30
31
  } from '../types.ts'
31
- import {
32
- arithmeticCoercionOrRefuse,
33
- refuseAmbiguousNilArithmetic,
34
- refuseAmbiguousNilMemoArithmetic,
35
- type AmbiguousNilArithmeticSink,
36
- } from './ambiguous-nil-arithmetic.ts'
37
- import { refuseNonPrimitiveStrictEquality } from './non-primitive-equality.ts'
32
+ import { refuseNonPrimitiveStrictEquality, type NonPrimitiveEqualitySink } from './non-primitive-equality.ts'
38
33
  import { collectImportedLoopChildComponentErrors } from './component-tree.ts'
39
34
  import { computedConstContext, type ComputedConstContext } from './computed-const.ts'
40
35
  import { searchParamsLocalNames } from './env-signal.ts'
@@ -267,9 +262,8 @@ export abstract class DslTemplateAdapter extends BaseAdapter {
267
262
 
268
263
  /**
269
264
  * The coercion an arithmetic operand's emitter spells
270
- * (`arithmeticOperandCoercion`), refusing an operand whose nil the
271
- * declared type cannot classify (`arithmeticCoercionOrRefuse`, #3700).
272
- * `isShadowed` is the adapter's scope test at the emission site.
265
+ * (`arithmeticOperandCoercion`). `isShadowed` is the adapter's scope test
266
+ * at the emission site.
273
267
  */
274
268
  protected arithmeticCoercion(
275
269
  op: string,
@@ -277,21 +271,10 @@ export abstract class DslTemplateAdapter extends BaseAdapter {
277
271
  other: ParsedExpr | undefined,
278
272
  isShadowed: (name: string) => boolean,
279
273
  ): ArithmeticCoercion {
280
- return arithmeticCoercionOrRefuse(op, operand, other, this.nullishAttrCtx, isShadowed, this.ambiguousNilSink())
281
- }
282
-
283
- /**
284
- * Refuse the ambiguous-nil arithmetic (#3700) in a whole expression the
285
- * adapter lowers through its own renderer rather than per operator: a
286
- * `.filter()` predicate, read in the current scope with the callback's
287
- * own `params` (its parameter and block locals) entered as a callback
288
- * frame.
289
- */
290
- protected refuseAmbiguousNilArithmeticIn(expr: ParsedExpr, params: readonly string[]): void {
291
- refuseAmbiguousNilArithmetic(expr, this.nullishAttrCtx, this.scope.enterCallback(params), this.ambiguousNilSink())
274
+ return arithmeticOperandCoercion(op, operand, other, this.nullishAttrCtx, isShadowed)
292
275
  }
293
276
 
294
- private ambiguousNilSink(): AmbiguousNilArithmeticSink {
277
+ private errorSink(): NonPrimitiveEqualitySink {
295
278
  return { errors: this.errors, componentName: this.componentName }
296
279
  }
297
280
 
@@ -440,10 +423,13 @@ export abstract class DslTemplateAdapter extends BaseAdapter {
440
423
  this.rootPropAliases = rootPropAliasNames(ir, this.propsParams.map(p => p.name))
441
424
  this.scope = BindingScope.forComponent(this.propsObjectName)
442
425
  this.nullableOptionalProps = collectNullableOptionalProps(ir)
443
- this.nullableSignalGetters = collectNullableSignalGetters(ir)
444
426
  this.nullishAttrCtx = collectNullishAttrContext(ir)
445
427
  this.stringValueNames = this.stringValueNamesFor(ir)
446
428
  this.declaredTypes = declaredValueTypes(ir)
429
+ this.nullableSignalGetters = new Set([
430
+ ...collectNullableSignalGetters(ir),
431
+ ...memberSeededNilSignals(ir, this.declaredTypes, this.scope),
432
+ ])
447
433
  this.operandTyping = collectOperandTyping(ir.metadata, { namedFields: this.declaredTypes.namedFields })
448
434
  this.moduleStringConsts = collectModuleStringConsts(ir.metadata.localConstants)
449
435
  this._searchParamsLocals = searchParamsLocalNames(ir.metadata)
@@ -471,8 +457,20 @@ export abstract class DslTemplateAdapter extends BaseAdapter {
471
457
  if (!options?.siblingTemplatesRegistered) {
472
458
  this.errors.push(...collectImportedLoopChildComponentErrors(ir, this.componentName, this.loopChildDiagnosticName))
473
459
  }
474
- refuseAmbiguousNilMemoArithmetic(ir.metadata.memos ?? [], this.nullishAttrCtx, this.ambiguousNilSink())
475
460
  // `===` / `!==` over an object or array (#3774, #3824 step 8).
476
- refuseNonPrimitiveStrictEquality(ir, this.operandTyping, this.ambiguousNilSink())
461
+ refuseNonPrimitiveStrictEquality(ir, this.operandTyping, this.errorSink())
477
462
  }
478
463
  }
464
+
465
+ /**
466
+ * Getters of signals seeded from a member of a prop whose declared type
467
+ * admits `null` or `undefined` (`type Init = { label?: string }`,
468
+ * `createSignal(initial.label)`): the signal holds the field's JS value, so
469
+ * an attribute bound to it is omitted when that value is nullish, as for any
470
+ * other nullable signal (`collectNullableSignalGetters`).
471
+ */
472
+ function memberSeededNilSignals(ir: ComponentIR, types: DeclaredValueTypes, scope: BindingScope): string[] {
473
+ return (ir.metadata.signals ?? [])
474
+ .filter(s => s.parsed?.kind === 'member' && factsOfType(declaredValueType(types, s.parsed, scope)).types.has('nil'))
475
+ .map(s => s.getter)
476
+ }
@@ -67,6 +67,16 @@ export function loopRowType(
67
67
  return arrayElementType(type) ?? null
68
68
  }
69
69
 
70
+ /**
71
+ * The declared type of the value `expr` reads in `scope` (a prop, a member
72
+ * path below one, a loop row, a signal or memo getter), or null when the
73
+ * declarations do not say. An optional field below the root admits
74
+ * `undefined` (`loopValueTypeAt`).
75
+ */
76
+ export function declaredValueType(types: DeclaredValueTypes, expr: ParsedExpr, scope: BindingScope): LoopValueType | null {
77
+ return valueType(types, expr, scope)
78
+ }
79
+
70
80
  function valueType(types: DeclaredValueTypes, expr: ParsedExpr, scope: BindingScope): LoopValueType | null {
71
81
  switch (expr.kind) {
72
82
  case 'identifier': {