@barefootjs/jsx 0.35.4 → 0.35.6

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 (113) hide show
  1. package/dist/analyzer-context.d.ts +5 -4
  2. package/dist/analyzer-context.d.ts.map +1 -1
  3. package/dist/analyzer.d.ts.map +1 -1
  4. package/dist/errors.d.ts +0 -1
  5. package/dist/errors.d.ts.map +1 -1
  6. package/dist/index.js +5306 -4949
  7. package/dist/ir-to-client-js/compute-inlinability.d.ts +8 -0
  8. package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
  9. package/dist/ir-to-client-js/compute-prop-usage.d.ts +6 -0
  10. package/dist/ir-to-client-js/compute-prop-usage.d.ts.map +1 -1
  11. package/dist/ir-to-client-js/control-flow/plan/build-event-delegation.d.ts +3 -2
  12. package/dist/ir-to-client-js/control-flow/plan/build-event-delegation.d.ts.map +1 -1
  13. package/dist/ir-to-client-js/control-flow/plan/build-loop.d.ts +12 -2
  14. package/dist/ir-to-client-js/control-flow/plan/build-loop.d.ts.map +1 -1
  15. package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts +40 -0
  16. package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts.map +1 -0
  17. package/dist/ir-to-client-js/control-flow/plan/event-delegation.d.ts +13 -0
  18. package/dist/ir-to-client-js/control-flow/plan/event-delegation.d.ts.map +1 -1
  19. package/dist/ir-to-client-js/control-flow/plan/loop-delegation-index.d.ts +45 -0
  20. package/dist/ir-to-client-js/control-flow/plan/loop-delegation-index.d.ts.map +1 -0
  21. package/dist/ir-to-client-js/control-flow/plan/types.d.ts +2 -0
  22. package/dist/ir-to-client-js/control-flow/plan/types.d.ts.map +1 -1
  23. package/dist/ir-to-client-js/control-flow/stringify/event-delegation.d.ts +12 -0
  24. package/dist/ir-to-client-js/control-flow/stringify/event-delegation.d.ts.map +1 -1
  25. package/dist/ir-to-client-js/control-flow.d.ts +2 -1
  26. package/dist/ir-to-client-js/control-flow.d.ts.map +1 -1
  27. package/dist/ir-to-client-js/emit-reactive.d.ts +7 -3
  28. package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
  29. package/dist/ir-to-client-js/generate-init.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/index.d.ts +1 -0
  33. package/dist/ir-to-client-js/index.d.ts.map +1 -1
  34. package/dist/ir-to-client-js/phases/event-handlers.d.ts +9 -1
  35. package/dist/ir-to-client-js/phases/event-handlers.d.ts.map +1 -1
  36. package/dist/ir-to-client-js/phases/props-event-handlers.d.ts +5 -0
  37. package/dist/ir-to-client-js/phases/props-event-handlers.d.ts.map +1 -1
  38. package/dist/ir-to-client-js/phases/props-extraction.d.ts +15 -13
  39. package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
  40. package/dist/ir-to-client-js/phases.d.ts +12 -1
  41. package/dist/ir-to-client-js/phases.d.ts.map +1 -1
  42. package/dist/ir-to-client-js/plan/build-declaration-emit.d.ts.map +1 -1
  43. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  44. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +50 -0
  45. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
  46. package/dist/ir-to-client-js/rewrite-destructured-props.d.ts +29 -0
  47. package/dist/ir-to-client-js/rewrite-destructured-props.d.ts.map +1 -0
  48. package/dist/ir-to-client-js/utils.d.ts +21 -1
  49. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  50. package/dist/jsx-to-ir.d.ts.map +1 -1
  51. package/dist/prop-rewrite.d.ts.map +1 -1
  52. package/dist/props-binding.d.ts +112 -1
  53. package/dist/props-binding.d.ts.map +1 -1
  54. package/dist/ssr-defaults.d.ts.map +1 -1
  55. package/dist/static-literal.d.ts +20 -8
  56. package/dist/static-literal.d.ts.map +1 -1
  57. package/dist/types.d.ts +38 -0
  58. package/dist/types.d.ts.map +1 -1
  59. package/package.json +2 -2
  60. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +31 -35
  61. package/src/__tests__/aliased-destructured-prop-csr.test.ts +19 -7
  62. package/src/__tests__/body-destructured-prop-default-propsparams.test.ts +153 -0
  63. package/src/__tests__/body-destructured-props-live.test.ts +245 -0
  64. package/src/__tests__/client-js-generation.test.ts +11 -4
  65. package/src/__tests__/client-only-date-lowering.test.ts +5 -2
  66. package/src/__tests__/destructured-from-props-localconst-value.test.ts +17 -6
  67. package/src/__tests__/destructured-props-live.test.ts +167 -0
  68. package/src/__tests__/doc-examples.test.ts +2 -4
  69. package/src/__tests__/event-delegation-container-collision-2930.test.ts +197 -0
  70. package/src/__tests__/format-error.test.ts +5 -3
  71. package/src/__tests__/issue-2910-mutated-const-not-inlined.test.ts +118 -0
  72. package/src/__tests__/issue-2910-own-scope-id-emission.test.ts +64 -0
  73. package/src/__tests__/live-only-props.test.ts +160 -0
  74. package/src/__tests__/prop-references.test.ts +26 -17
  75. package/src/__tests__/props-destructuring.test.ts +6 -275
  76. package/src/__tests__/prune-unused-prop-extractions.test.ts +11 -5
  77. package/src/__tests__/signal-getter-not-called.test.ts +5 -0
  78. package/src/__tests__/ssr-defaults.test.ts +83 -0
  79. package/src/__tests__/staged-ir/02-shadow-guards.test.ts +12 -5
  80. package/src/__tests__/static-literal.test.ts +39 -4
  81. package/src/analyzer-context.ts +8 -7
  82. package/src/analyzer.ts +233 -106
  83. package/src/errors.ts +7 -8
  84. package/src/ir-to-client-js/compute-inlinability.ts +8 -1
  85. package/src/ir-to-client-js/compute-prop-usage.ts +19 -0
  86. package/src/ir-to-client-js/control-flow/plan/build-event-delegation.ts +5 -0
  87. package/src/ir-to-client-js/control-flow/plan/build-loop.ts +27 -11
  88. package/src/ir-to-client-js/control-flow/plan/event-collision.ts +55 -0
  89. package/src/ir-to-client-js/control-flow/plan/event-delegation.ts +13 -0
  90. package/src/ir-to-client-js/control-flow/plan/loop-delegation-index.ts +79 -0
  91. package/src/ir-to-client-js/control-flow/plan/types.ts +2 -0
  92. package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +50 -10
  93. package/src/ir-to-client-js/control-flow.ts +68 -31
  94. package/src/ir-to-client-js/emit-reactive.ts +12 -8
  95. package/src/ir-to-client-js/emit-registration.ts +2 -2
  96. package/src/ir-to-client-js/generate-init.ts +22 -2
  97. package/src/ir-to-client-js/imports.ts +6 -0
  98. package/src/ir-to-client-js/index.ts +51 -3
  99. package/src/ir-to-client-js/phases/event-handlers.ts +10 -0
  100. package/src/ir-to-client-js/phases/props-event-handlers.ts +6 -0
  101. package/src/ir-to-client-js/phases/props-extraction.ts +29 -48
  102. package/src/ir-to-client-js/phases.ts +17 -3
  103. package/src/ir-to-client-js/plan/build-declaration-emit.ts +6 -5
  104. package/src/ir-to-client-js/prop-handling.ts +9 -0
  105. package/src/ir-to-client-js/prune-unused-prop-extractions.ts +31 -10
  106. package/src/ir-to-client-js/rewrite-destructured-props.ts +210 -0
  107. package/src/ir-to-client-js/utils.ts +29 -1
  108. package/src/jsx-to-ir.ts +110 -48
  109. package/src/prop-rewrite.ts +11 -0
  110. package/src/props-binding.ts +171 -6
  111. package/src/ssr-defaults.ts +10 -5
  112. package/src/static-literal.ts +21 -9
  113. package/src/types.ts +38 -0
@@ -0,0 +1,245 @@
1
+ /**
2
+ * Body-destructured (or plain member-access alias) props stay reactive too
3
+ * (#2934) — the body-destructure twin of `destructured-props-live.test.ts`.
4
+ * A pure single-prop passthrough local in a bare-props-form component
5
+ * (`function Foo(props) { const { value } = props; ... }`, or the
6
+ * equivalent `const value = props.value`) has its own extraction dropped
7
+ * and every reference rewritten to a live `_p.<key>` read (or
8
+ * `(_p.<key> ?? <fallback>)`), same as `props.xxx` access and parameter
9
+ * destructuring. See `props-binding.ts`'s `resolveBodyPropAliases` and
10
+ * `ir-to-client-js/rewrite-destructured-props.ts`'s `rewriteBodyAliasReads`.
11
+ */
12
+
13
+ import { describe, test, expect } from 'bun:test'
14
+ import { compileJSX } from '../compiler'
15
+ import { TestAdapter } from '../adapters/test-adapter'
16
+
17
+ const adapter = new TestAdapter()
18
+
19
+ function compileClientJs(source: string, filename = 'Component.tsx') {
20
+ const result = compileJSX(source, filename, { adapter })
21
+ const errors = result.errors.filter((e) => e.severity === 'error')
22
+ expect(errors).toEqual([])
23
+ const clientJs = result.files.find((f) => f.type === 'clientJs')
24
+ expect(clientJs).toBeDefined()
25
+ return { content: clientJs!.content, result }
26
+ }
27
+
28
+ describe('body-destructured props compile to live reads', () => {
29
+ test('a body-destructured prop no longer gets a captured-once extraction line', () => {
30
+ const source = `
31
+ 'use client'
32
+ import { createSignal, createMemo, createEffect } from '@barefootjs/client'
33
+
34
+ function Child(props: {
35
+ value: number
36
+ onClick: () => void
37
+ label?: string
38
+ size?: string
39
+ items?: string[]
40
+ }) {
41
+ const { value, onClick, label = 'n', size = 'md', items = [] } = props
42
+ const doubled = createMemo(() => value * 2)
43
+ const [local, setLocal] = createSignal(value)
44
+ createEffect(() => {
45
+ console.log('value is', value)
46
+ })
47
+ return (
48
+ <div className={size}>
49
+ <span data-label={label}>{value}</span>
50
+ <span>{doubled()}</span>
51
+ <span>{items.length}</span>
52
+ <button onClick={() => { setLocal(value); onClick() }}>click</button>
53
+ </div>
54
+ )
55
+ }
56
+ `
57
+ const { content } = compileClientJs(source, 'Child.tsx')
58
+
59
+ // No captured-once local for any body-destructured prop.
60
+ expect(content).not.toMatch(/const value = /)
61
+ expect(content).not.toMatch(/const onClick = /)
62
+ expect(content).not.toMatch(/const label = /)
63
+ expect(content).not.toMatch(/const size = /)
64
+ expect(content).not.toMatch(/const items = /)
65
+
66
+ // Memo computation reads the live prop directly.
67
+ expect(content).toMatch(/createMemo\(\(\) => _p\.value \* 2\)/)
68
+
69
+ // The user effect reads the live prop, not a captured local.
70
+ expect(content).toContain("console.log('value is', _p.value)")
71
+
72
+ // The handler reads the live prop for both the signal update and the
73
+ // callback-prop invocation.
74
+ expect(content).toContain('_p.onClick()')
75
+ expect(content).toContain('setLocal(_p.value)')
76
+
77
+ // `items`'s destructure default survives as a live fallback, not a
78
+ // one-time extraction.
79
+ expect(content).toContain('(_p.items ?? []).length')
80
+ })
81
+
82
+ test('the CSR template lambda also reads a defaulted body alias live, matching the parameter form (#2934)', () => {
83
+ // This is the sibling bug the init-body fix alone doesn't cover: the
84
+ // CSR `template:` lambda (used for a fresh client-side mount with no
85
+ // SSR markup) is built at Phase 1 from `ctx.analyzer.propsParams`
86
+ // (via `jsx-to-ir.ts`'s `_destructuredPropInfoByName` cache), which
87
+ // for a props-object-mode component used to hold only type-member
88
+ // info with no default of its own — so `data-label={label}` compiled
89
+ // to a bare `_p.label` there, omitting the attribute entirely on a
90
+ // CSR-fresh mount where `_p.label` is `undefined`, instead of
91
+ // applying the `'none'` default like the parameter-destructured form
92
+ // already did. Fixed at the source (#2943): the analyzer now overlays
93
+ // a body-destructure default directly onto `propsParams` itself
94
+ // (`collectConstant`, `analyzer.ts`) — the SAME shared `ParamInfo`
95
+ // every other consumer (SSR prop classification, `extractSsrDefaults`)
96
+ // reads, so this CSR cache needs no separate default-resolution logic
97
+ // of its own anymore.
98
+ const source = `
99
+ 'use client'
100
+ function Child(props: { label?: string }) {
101
+ const { label = 'none' } = props
102
+ return <div data-label={label}>{label}</div>
103
+ }
104
+ `
105
+ const { content } = compileClientJs(source, 'Child.tsx')
106
+ expect(content).not.toMatch(/const label = /)
107
+ expect(content).toContain("escapeAttr((_p.label ?? 'none'))")
108
+ expect(content).toContain("escapeTextOrMarkup((_p.label ?? 'none'))")
109
+ // Never the bare, default-dropping form.
110
+ expect(content).not.toMatch(/\(_p\.label\) != null/)
111
+ })
112
+
113
+ test('a RENAMED body-destructured prop WITH A DEFAULT (`label: text = "none"`) applies the default in the CSR template lambda too (#2943)', () => {
114
+ // The renamed twin of the unrenamed test above. Before #2943 this
115
+ // combination had no fixture and no dedicated check — the CSR
116
+ // template lambda's default application only ever reached an
117
+ // UNRENAMED alias.
118
+ const source = `
119
+ 'use client'
120
+ function Child(props: { label?: string }) {
121
+ const { label: text = 'none' } = props
122
+ return <div data-label={text}>{text}</div>
123
+ }
124
+ `
125
+ const { content } = compileClientJs(source, 'Child.tsx')
126
+ expect(content).not.toMatch(/const text = /)
127
+ expect(content).toContain("escapeAttr((_p.label ?? 'none'))")
128
+ expect(content).toContain("escapeTextOrMarkup((_p.label ?? 'none'))")
129
+ })
130
+
131
+ test('a renamed body-destructured prop (`onPick: pick`) reads the caller-facing key live everywhere', () => {
132
+ const source = `
133
+ 'use client'
134
+ import { createEffect } from '@barefootjs/client'
135
+ function Badge(props: { text: string; n: number }) {
136
+ const { text, n: count } = props
137
+ createEffect(() => {
138
+ console.log(count)
139
+ })
140
+ return <span>{text}:{count}</span>
141
+ }
142
+ `
143
+ const { content } = compileClientJs(source, 'Badge.tsx')
144
+ expect(content).not.toMatch(/const count = /)
145
+ expect(content).not.toContain('_p.count')
146
+ expect(content).toContain('console.log(_p.n)')
147
+ })
148
+
149
+ test('a plain member-access alias (`const label = props.label`, no destructure) gets identical treatment', () => {
150
+ const source = `
151
+ 'use client'
152
+ import { createEffect } from '@barefootjs/client'
153
+ function Child(props: { label: string }) {
154
+ const label = props.label
155
+ createEffect(() => {
156
+ console.log(label)
157
+ })
158
+ return <span>{label}</span>
159
+ }
160
+ `
161
+ const { content } = compileClientJs(source, 'Child.tsx')
162
+ expect(content).not.toMatch(/const label = /)
163
+ expect(content).toContain('console.log(_p.label)')
164
+ })
165
+
166
+ test('a computation derived from a body-destructured prop stays an ordinary once-evaluated local', () => {
167
+ const source = `
168
+ 'use client'
169
+ function Child(props: { value: number }) {
170
+ const { value } = props
171
+ const doubled = value * 2
172
+ return <span>{doubled}</span>
173
+ }
174
+ `
175
+ const { content } = compileClientJs(source, 'Child.tsx')
176
+ expect(content).toContain('const doubled = _p.value * 2')
177
+ })
178
+
179
+ test('a `let` body destructure is never treated as a live alias (reassignment stays a real local)', () => {
180
+ const source = `
181
+ 'use client'
182
+ import { createEffect } from '@barefootjs/client'
183
+ function Child(props: { count: number }) {
184
+ let { count } = props
185
+ count += 1
186
+ createEffect(() => {
187
+ console.log(count)
188
+ })
189
+ return <span>{count}</span>
190
+ }
191
+ `
192
+ const { content } = compileClientJs(source, 'Child.tsx')
193
+ expect(content).toMatch(/let count = _p\.count/)
194
+ expect(content).toContain('count += 1')
195
+ })
196
+
197
+ test('shadowing is preserved: a `.map()` callback param and a handler-local both keep their own binding', () => {
198
+ const source = `
199
+ 'use client'
200
+ import { createSignal } from '@barefootjs/client'
201
+ function List(props: { title: string; items: { a: string }[] }) {
202
+ const { title, items } = props
203
+ const [count, setCount] = createSignal(0)
204
+ return (
205
+ <div>
206
+ <span>{title}</span>
207
+ <ul>
208
+ {items.map((title) => <li key={title.a}>{title.a}</li>)}
209
+ </ul>
210
+ <button onClick={() => {
211
+ const title = 'local'
212
+ console.log(title)
213
+ setCount(count() + 1)
214
+ }}>click</button>
215
+ </div>
216
+ )
217
+ }
218
+ `
219
+ const { content } = compileClientJs(source, 'List.tsx')
220
+ // The outer `title` prop is rewritten wherever it is a free reference.
221
+ expect(content).toContain('_p.title')
222
+ // The `.map()` callback's OWN `title` parameter must never be rewritten
223
+ // — `(_p.title) => _p.title.a` would be invalid JS.
224
+ expect(content).not.toMatch(/\(_p\.title\)\s*=>/)
225
+ expect(content).toMatch(/\(title\)\s*=>[\s\S]*?title\.a/)
226
+ // The handler-local `const title = 'local'` shadows the prop for the
227
+ // rest of that handler — its own reference must stay bare.
228
+ expect(content).toContain("const title = 'local'")
229
+ expect(content).toContain('console.log(title)')
230
+ })
231
+
232
+ test('`children` keeps its one-time captured-once extraction (not rewritten live)', () => {
233
+ const source = `
234
+ 'use client'
235
+ import { createSignal } from '@barefootjs/client'
236
+ function Wrapper(props: { children: unknown }) {
237
+ const { children } = props
238
+ const [open, setOpen] = createSignal(true)
239
+ return <div onClick={() => setOpen(!open())}>{open() ? children : null}</div>
240
+ }
241
+ `
242
+ const { content } = compileClientJs(source, 'Wrapper.tsx')
243
+ expect(content).toContain('const children = _p.children')
244
+ })
245
+ })
@@ -301,9 +301,12 @@ describe('Client JS generation', () => {
301
301
  expect(clientJs).toBeDefined()
302
302
  const content = clientJs!.content
303
303
 
304
- // Props used as conditional guards should NOT get ?? {} default
305
- expect(content).toContain('const prev = _p.prev\n')
306
- expect(content).toContain('const next = _p.next\n')
304
+ // Props used as conditional guards must NOT get a `?? {}` default,
305
+ // at the live read sites they now compile to.
306
+ expect(content).not.toContain('const prev = ')
307
+ expect(content).not.toContain('const next = ')
308
+ expect(content).toContain('() => _p.prev,')
309
+ expect(content).toContain('() => _p.next,')
307
310
  expect(content).not.toContain('_p.prev ?? {}')
308
311
  expect(content).not.toContain('_p.next ?? {}')
309
312
  })
@@ -2874,7 +2877,11 @@ describe('Client JS generation', () => {
2874
2877
  const result = compileJSX(source, 'Probe.tsx', { adapter })
2875
2878
  expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
2876
2879
  const js = result.files.find(f => f.type === 'clientJs')!.content
2877
- expect(js).toContain('const size = _p.size')
2880
+ // The bare `size` inside the user's own `size ?? 1` reads live as
2881
+ // `_p.size`, keeping their `?? 1` default exactly as written — the
2882
+ // compiler must not synthesize a competing one.
2883
+ expect(js).not.toContain('const size = ')
2884
+ expect(js).toContain('createSignal(_p.size ?? 1)')
2878
2885
  expect(js).not.toContain('_p.size ?? 0')
2879
2886
  })
2880
2887
 
@@ -70,7 +70,9 @@ describe('#2640 — /* @client */ text expressions route catalogued calls throug
70
70
  }
71
71
  `)
72
72
  expect(bf021).toHaveLength(0)
73
- expect(clientJs).toContain('date(createdAt, "toISOString")')
73
+ // The receiver is a destructured prop, so it reads live as
74
+ // `_p.createdAt`; the catalogued-call lowering itself is unaffected.
75
+ expect(clientJs).toContain('date(_p.createdAt, "toISOString")')
74
76
  expect(clientJs).not.toContain('createdAt.toISOString()')
75
77
  expect(clientJs).toMatch(/import\s*\{[^}]*\bdate\b[^}]*\}\s*from\s*'@barefootjs\/client\/runtime'/)
76
78
  })
@@ -82,7 +84,8 @@ describe('#2640 — /* @client */ text expressions route catalogued calls throug
82
84
  }
83
85
  `)
84
86
  expect(bf021).toHaveLength(0)
85
- expect(clientJs).toContain('formatDate(createdAt, "YYYY/M/D", "UTC")')
87
+ // Same live-receiver rewrite as the zero-arg case above.
88
+ expect(clientJs).toContain('formatDate(_p.createdAt, "YYYY/M/D", "UTC")')
86
89
  expect(clientJs).not.toContain(".toLocaleDateString('ja-JP'")
87
90
  })
88
91
 
@@ -82,7 +82,17 @@ describe('destructured-from-props-object → localConstants value rewrite', () =
82
82
  expect(content).not.toMatch(/const\s+doubled\s*=\s*_p\.count\(\)/)
83
83
  })
84
84
 
85
- test('shadow guard: earlier local that shadows a prop is NOT rewritten', () => {
85
+ // #2934: `const label = props.label ?? 'fallback'` is a PURE single-prop
86
+ // alias — the analyzer's IR can't distinguish it from a destructure
87
+ // (`const { label = 'fallback' } = props`), and `resolveBodyPropAliases`
88
+ // deliberately treats both identically (see its docstring). So `label`'s
89
+ // own declaration is now removed and every reference to it — including
90
+ // from a DEPENDENT local's own initializer, `upper` here — becomes a
91
+ // live `_p.label` read. This used to be named a "shadow guard" test, back
92
+ // when `label` was expected to stay a captured-once local distinct from
93
+ // the prop; that framing no longer applies now that a same-named pure
94
+ // alias IS made live rather than left as its own local.
95
+ test('a plain member-access prop alias is rewritten live, including from a dependent local', () => {
86
96
  const source = `
87
97
  'use client'
88
98
 
@@ -102,10 +112,11 @@ describe('destructured-from-props-object → localConstants value rewrite', () =
102
112
  const clientJs = result.files.find((f) => f.type === 'clientJs')
103
113
  const content = clientJs?.content ?? ''
104
114
 
105
- // `label` here is the local const (shadowing the prop). The init body
106
- // emits `const label = _p.label ?? 'fallback'` (late-stage rename),
107
- // and `upper` should reference the LOCAL `label`, not `_p.label`.
108
- expect(content).toMatch(/const\s+upper\s*=\s*label\.toUpperCase\(\)/)
109
- expect(content).not.toMatch(/const\s+upper\s*=\s*_p\.label\.toUpperCase\(\)/)
115
+ // `label`'s own extraction is gone — it was a pure alias, not a real
116
+ // computation of its own.
117
+ expect(content).not.toMatch(/const\s+label\s*=/)
118
+ // `upper` (still a real once-evaluated local — `.toUpperCase()` is a
119
+ // computation) now sources its value directly from the live prop read.
120
+ expect(content).toMatch(/const\s+upper\s*=\s*\(_p\.label\s*\?\?\s*'fallback'\)\.toUpperCase\(\)/)
110
121
  })
111
122
  })
@@ -0,0 +1,167 @@
1
+ /**
2
+ * Destructured props stay reactive: every value-position read of a
3
+ * destructured prop name in the compiled `init*` body is a live `_p.<key>`
4
+ * read (or `(_p.<key> ?? <fallback>)`), the same shape `props.xxx` access
5
+ * compiles to. See `props-binding.ts`'s `livePropReadExpr` and
6
+ * `ir-to-client-js/rewrite-destructured-props.ts`.
7
+ */
8
+
9
+ import { describe, test, expect } from 'bun:test'
10
+ import { compileJSX } from '../compiler'
11
+ import { TestAdapter } from '../adapters/test-adapter'
12
+ import { ErrorCodes } from '../errors'
13
+
14
+ const adapter = new TestAdapter()
15
+
16
+ function compileClientJs(source: string, filename = 'Component.tsx') {
17
+ const result = compileJSX(source, filename, { adapter })
18
+ const errors = result.errors.filter((e) => e.severity === 'error')
19
+ expect(errors).toEqual([])
20
+ const clientJs = result.files.find((f) => f.type === 'clientJs')
21
+ expect(clientJs).toBeDefined()
22
+ return { content: clientJs!.content, result }
23
+ }
24
+
25
+ describe('destructured props compile to live reads', () => {
26
+ test('BF043 no longer exists as an error code', () => {
27
+ // Checked by string value, not by a removed `ErrorCodes` key (that
28
+ // would be a compile error) — same convention as
29
+ // `invalid-signal-usage.audit.test.ts`'s BF012 deletion audit.
30
+ expect(Object.values(ErrorCodes)).not.toContain('BF043')
31
+ })
32
+
33
+ test('a destructured prop no longer gets a captured-once extraction line, and no BF043 diagnostic appears', () => {
34
+ const source = `
35
+ 'use client'
36
+ import { createSignal, createMemo, createEffect } from '@barefootjs/client'
37
+
38
+ function Child({ value, onClick, label = 'n', size = 'md', items = [] }: {
39
+ value: number
40
+ onClick: () => void
41
+ label?: string
42
+ size?: string
43
+ items?: string[]
44
+ }) {
45
+ const doubled = createMemo(() => value * 2)
46
+ const [local, setLocal] = createSignal(value)
47
+ createEffect(() => {
48
+ console.log('value is', value)
49
+ })
50
+ return (
51
+ <div className={size}>
52
+ <span data-label={label}>{value}</span>
53
+ <span>{doubled()}</span>
54
+ <span>{items.length}</span>
55
+ <button onClick={() => { setLocal(value); onClick() }}>click</button>
56
+ </div>
57
+ )
58
+ }
59
+ `
60
+ const { content, result } = compileClientJs(source, 'Child.tsx')
61
+
62
+ // No captured-once local for any destructured prop.
63
+ expect(content).not.toContain('const value = _p.value')
64
+ expect(content).not.toContain('const onClick = _p.onClick')
65
+ expect(content).not.toContain('const label = ')
66
+ expect(content).not.toContain('const size = ')
67
+ expect(content).not.toContain('const items = ')
68
+
69
+ // Memo computation reads the live prop directly.
70
+ expect(content).toMatch(/createMemo\(\(\) => _p\.value \* 2\)/)
71
+
72
+ // The user effect reads the live prop, not a captured local.
73
+ expect(content).toContain("console.log('value is', _p.value)")
74
+
75
+ // The handler reads the live prop for both the signal update and the
76
+ // callback-prop invocation.
77
+ expect(content).toContain('_p.onClick()')
78
+ expect(content).toContain('setLocal(_p.value)')
79
+
80
+ // `items`'s destructure default survives as a live fallback, not a
81
+ // one-time extraction.
82
+ expect(content).toContain('(_p.items ?? []).length')
83
+
84
+ // No BF043 diagnostic (retired) anywhere in errors/warnings.
85
+ expect(result.errors.some((e) => e.code === 'BF043')).toBe(false)
86
+ })
87
+
88
+ test('an aliased destructured prop (`{ n: count }`) reads the caller-facing key live everywhere', () => {
89
+ const source = `
90
+ 'use client'
91
+ import { createEffect } from '@barefootjs/client'
92
+ function Badge({ text, n: count }: { text: string; n: number }) {
93
+ createEffect(() => {
94
+ console.log(count)
95
+ })
96
+ return <span>{text}:{count}</span>
97
+ }
98
+ `
99
+ const { content } = compileClientJs(source, 'Badge.tsx')
100
+ expect(content).not.toContain('const count = ')
101
+ expect(content).not.toContain('_p.count')
102
+ expect(content).toContain('console.log(_p.n)')
103
+ })
104
+
105
+ test('an accessor-typed prop (`value: () => number`) still gets CALLED at the live read site', () => {
106
+ const source = `
107
+ 'use client'
108
+ import { createEffect } from '@barefootjs/client'
109
+ function Child({ value }: { value: () => number }) {
110
+ createEffect(() => {
111
+ console.log(value())
112
+ })
113
+ return <span>{value()}</span>
114
+ }
115
+ `
116
+ const { content } = compileClientJs(source, 'Child.tsx')
117
+ expect(content).not.toContain('const value = _p.value')
118
+ expect(content).toContain('_p.value()')
119
+ })
120
+
121
+ test('shadowing is preserved: a `.map()` callback param and a handler-local both keep their own binding', () => {
122
+ const source = `
123
+ 'use client'
124
+ import { createSignal } from '@barefootjs/client'
125
+ function List({ title, items }: { title: string; items: { a: string }[] }) {
126
+ const [count, setCount] = createSignal(0)
127
+ return (
128
+ <div>
129
+ <span>{title}</span>
130
+ <ul>
131
+ {items.map((title) => <li key={title.a}>{title.a}</li>)}
132
+ </ul>
133
+ <button onClick={() => {
134
+ const title = 'local'
135
+ console.log(title)
136
+ setCount(count() + 1)
137
+ }}>click</button>
138
+ </div>
139
+ )
140
+ }
141
+ `
142
+ const { content } = compileClientJs(source, 'List.tsx')
143
+ // The outer `title` prop is rewritten wherever it is a free reference.
144
+ expect(content).toContain('_p.title')
145
+ // The `.map()` callback's OWN `title` parameter must never be rewritten
146
+ // — `(_p.title) => _p.title.a` would be invalid JS.
147
+ expect(content).not.toMatch(/\(_p\.title\)\s*=>/)
148
+ expect(content).toMatch(/\(title\)\s*=>[\s\S]*?title\.a/)
149
+ // The handler-local `const title = 'local'` shadows the prop for the
150
+ // rest of that handler — its own reference must stay bare.
151
+ expect(content).toContain("const title = 'local'")
152
+ expect(content).toContain('console.log(title)')
153
+ })
154
+
155
+ test('`children` keeps its one-time captured-once extraction (not rewritten live)', () => {
156
+ const source = `
157
+ 'use client'
158
+ import { createSignal } from '@barefootjs/client'
159
+ function Wrapper({ children }: { children: unknown }) {
160
+ const [open, setOpen] = createSignal(true)
161
+ return <div onClick={() => setOpen(!open())}>{open() ? children : null}</div>
162
+ }
163
+ `
164
+ const { content } = compileClientJs(source, 'Wrapper.tsx')
165
+ expect(content).toContain('const children = _p.children')
166
+ })
167
+ })
@@ -433,9 +433,8 @@ for (const page of PAGES) {
433
433
  //
434
434
  // - "Implemented but doc snippet too minimal": the code IS emitted
435
435
  // by the compiler in real programs, but the literal snippet in
436
- // `error-codes.md` doesn't carry enough context (e.g. BF043 needs
437
- // a stateful component; BF044 needs a declared signal getter).
438
- // These are doc-edit follow-ups.
436
+ // `error-codes.md` doesn't carry enough context (e.g. BF044 needs
437
+ // a declared signal getter). These are doc-edit follow-ups.
439
438
  //
440
439
  // Previously-documented-but-unimplemented codes were removed from the
441
440
  // doc to keep it honest.
@@ -458,7 +457,6 @@ interface ErrorCodeContract {
458
457
  // surrounding context the snippet omits).
459
458
  const ERROR_CODES_DOC_TOO_MINIMAL: Record<string, string> = {
460
459
  BF021: 'BF021 has multiple ❌ snippets in placeholder form (`.map(...)`) — covered concretely by `jsx-compatibility.md` tests; the doc-form snippets here remain placeholder',
461
- BF043: 'BF043 fires only for STATEFUL components (signals/memos/effects). The doc snippet `function Child({ count }: Props)` has no reactivity, so the check is correctly silent — doc snippet needs enrichment to be testable as-is',
462
460
  BF044: 'BF044 fires only when a real signal getter is bound. The doc snippet `<Child count={count} />` references an undeclared `count`, so the check is silent — doc snippet needs enrichment',
463
461
  BF101: 'BF101 is raised by the non-JS template adapters\' own per-adapter checks (Go/Mojo/Xslate/… `renderLoop` / callback lowering), not by the shared `isSupported` gate every adapter shares — `TestAdapter` here is JS-runtime-style like Hono and executes both doc snippets verbatim, so the check is correctly silent. Covered concretely (compiled against the real 8 template adapters, with verbatim diagnostic text) by `jsx-compatibility.md`\'s doc-examples coverage and the `filter-nested-callback-predicate` / `filter-nested-find-predicate` / `static-array-from-props` adapter-conformance fixtures.',
464
462
  }