@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,197 @@
1
+ /**
2
+ * BarefootJS Compiler — container's own handler vs. a `.map()` row's
3
+ * delegated handler for the same event (#2930).
4
+ *
5
+ * `.map()` rows compiled via delegation share their delegated listener's
6
+ * DOM node with the loop's static container. When the container ALSO
7
+ * carries its own directly-authored handler for the same event, both used
8
+ * to become separate `addEventListener` calls on the identical node, so a
9
+ * row's `stopPropagation()` could never stop the container's own handler
10
+ * (native DOM: `stopPropagation()` only blocks bubbling to *other nodes*,
11
+ * never other listeners already registered on the *same* node).
12
+ *
13
+ * The fix merges the two into ONE listener: the delegated dispatch runs
14
+ * first, and the container's own handler only fires when the event's
15
+ * `cancelBubble` flag is still false — i.e. no row handler called
16
+ * `stopPropagation()`/`stopImmediatePropagation()`.
17
+ */
18
+
19
+ import { describe, test, expect } from 'bun:test'
20
+ import { compileJSX } from '../compiler'
21
+ import { TestAdapter } from '../adapters/test-adapter'
22
+
23
+ const adapter = new TestAdapter()
24
+
25
+ function compileClientJs(source: string): string {
26
+ const result = compileJSX(source, 'Repro.tsx', { adapter })
27
+ expect(result.errors).toHaveLength(0)
28
+ const clientJs = result.files.find(f => f.type === 'clientJs')
29
+ expect(clientJs).toBeDefined()
30
+ return clientJs!.content
31
+ }
32
+
33
+ describe('event delegation vs. container own handler (#2930)', () => {
34
+ test('static array: container onContextMenu merges into the row delegation listener', () => {
35
+ const source = `
36
+ 'use client'
37
+ const items = ['a', 'b', 'c']
38
+
39
+ export function Repro() {
40
+ return (
41
+ <div
42
+ id="container"
43
+ onContextMenu={(e) => {
44
+ e.preventDefault()
45
+ console.log('container handler')
46
+ }}
47
+ >
48
+ {items.map(item => (
49
+ <div
50
+ key={item}
51
+ data-item={item}
52
+ onContextMenu={(e) => {
53
+ e.preventDefault()
54
+ e.stopPropagation()
55
+ console.log('row handler', item)
56
+ }}
57
+ >
58
+ row {item}
59
+ </div>
60
+ ))}
61
+ </div>
62
+ )
63
+ }
64
+ `
65
+ const content = compileClientJs(source)
66
+
67
+ // Exactly one addEventListener('contextmenu', ...) on the shared
68
+ // container — not two separate registrations.
69
+ const registrations = content.match(/\.addEventListener\('contextmenu'/g) ?? []
70
+ expect(registrations).toHaveLength(1)
71
+
72
+ // The merged listener fires the row handler, then gates the
73
+ // container's own handler on `cancelBubble`, and also fires it
74
+ // unconditionally when no row matched (click on the container's own
75
+ // background).
76
+ expect(content).toContain('row handler')
77
+ expect(content).toContain('container handler')
78
+ expect(content).toContain('!__bfEvt.cancelBubble')
79
+
80
+ // The row handler must be checked (and able to call stopPropagation)
81
+ // BEFORE the cancelBubble-gated container call.
82
+ const rowPos = content.indexOf("console.log('row handler'")
83
+ const gatePos = content.indexOf('!__bfEvt.cancelBubble')
84
+ expect(rowPos).toBeGreaterThan(-1)
85
+ expect(gatePos).toBeGreaterThan(-1)
86
+ expect(rowPos).toBeLessThan(gatePos)
87
+ })
88
+
89
+ test('dynamic loop: container onContextMenu merges into the row delegation listener', () => {
90
+ const source = `
91
+ 'use client'
92
+ import { createSignal } from '@barefootjs/client'
93
+
94
+ export function Repro() {
95
+ const [items, setItems] = createSignal(['a', 'b', 'c'])
96
+ return (
97
+ <div
98
+ onContextMenu={(e) => {
99
+ e.preventDefault()
100
+ console.log('container handler')
101
+ }}
102
+ >
103
+ {items().map(item => (
104
+ <div
105
+ key={item}
106
+ onContextMenu={(e) => {
107
+ e.preventDefault()
108
+ e.stopPropagation()
109
+ console.log('row handler', item)
110
+ }}
111
+ >
112
+ row {item}
113
+ </div>
114
+ ))}
115
+ </div>
116
+ )
117
+ }
118
+ `
119
+ const content = compileClientJs(source)
120
+
121
+ const registrations = content.match(/\.addEventListener\('contextmenu'/g) ?? []
122
+ expect(registrations).toHaveLength(1)
123
+ expect(content).toContain('!__bfEvt.cancelBubble')
124
+ })
125
+
126
+ test('container handler alone (no row collision) stays byte-unaffected', () => {
127
+ const source = `
128
+ 'use client'
129
+ const items = ['a', 'b', 'c']
130
+
131
+ export function Repro() {
132
+ return (
133
+ <div onContextMenu={(e) => console.log('container handler')}>
134
+ {items.map(item => <div key={item}>row {item}</div>)}
135
+ </div>
136
+ )
137
+ }
138
+ `
139
+ const content = compileClientJs(source)
140
+
141
+ // No loop event to delegate at all — the container's own listener is
142
+ // the ordinary direct-listener emission, unaffected by this change.
143
+ expect(content).not.toContain('cancelBubble')
144
+ const registrations = content.match(/\.addEventListener\('contextmenu'/g) ?? []
145
+ expect(registrations).toHaveLength(1)
146
+ })
147
+
148
+ test('row handler alone (no container collision) stays byte-unaffected', () => {
149
+ const source = `
150
+ 'use client'
151
+ const items = ['a', 'b', 'c']
152
+
153
+ export function Repro() {
154
+ return (
155
+ <div>
156
+ {items.map(item => (
157
+ <div key={item} onContextMenu={(e) => { e.stopPropagation(); console.log('row handler', item) }}>
158
+ row {item}
159
+ </div>
160
+ ))}
161
+ </div>
162
+ )
163
+ }
164
+ `
165
+ const content = compileClientJs(source)
166
+
167
+ // Delegation-only — no own handler to gate, so no cancelBubble check
168
+ // is introduced.
169
+ expect(content).not.toContain('cancelBubble')
170
+ const registrations = content.match(/\.addEventListener\('contextmenu'/g) ?? []
171
+ expect(registrations).toHaveLength(1)
172
+ })
173
+
174
+ test('two real nested elements (no .map()) keep two independent listeners', () => {
175
+ // The structurally simpler case from the issue report: no delegation
176
+ // involved at all, so native bubbling already does the right thing and
177
+ // this change must not touch it.
178
+ const source = `
179
+ 'use client'
180
+
181
+ export function Repro() {
182
+ return (
183
+ <div onContextMenu={(e) => console.log('outer')}>
184
+ <div onContextMenu={(e) => { e.stopPropagation(); console.log('inner') }}>
185
+ inner
186
+ </div>
187
+ </div>
188
+ )
189
+ }
190
+ `
191
+ const content = compileClientJs(source)
192
+
193
+ expect(content).not.toContain('cancelBubble')
194
+ const registrations = content.match(/\.addEventListener\('contextmenu'/g) ?? []
195
+ expect(registrations).toHaveLength(2)
196
+ })
197
+ })
@@ -28,8 +28,10 @@ describe('formatError — diagnostic rendering contract', () => {
28
28
  })
29
29
 
30
30
  test('warning severity also lowercases', () => {
31
+ // Any code works: `severity` is an explicit override on `createError`,
32
+ // independent of the code's own default.
31
33
  const err = createError(
32
- ErrorCodes.PROPS_DESTRUCTURING,
34
+ ErrorCodes.MISSING_KEY_IN_LIST,
33
35
  {
34
36
  file: 'components/Display.tsx',
35
37
  start: { line: 1, column: 17 },
@@ -39,7 +41,7 @@ describe('formatError — diagnostic rendering contract', () => {
39
41
  )
40
42
 
41
43
  const out = formatError(err)
42
- expect(out).toContain('warning[BF043]:')
44
+ expect(out).toContain('warning[BF023]:')
43
45
  })
44
46
 
45
47
  test('--> row carries the documented file:line:col location', () => {
@@ -90,7 +92,7 @@ describe('formatError — diagnostic rendering contract', () => {
90
92
 
91
93
  test('suggestion renders as a `= help:` row at the tail', () => {
92
94
  const err = createError(
93
- ErrorCodes.PROPS_DESTRUCTURING,
95
+ ErrorCodes.MISSING_KEY_IN_LIST,
94
96
  {
95
97
  file: 'Display.tsx',
96
98
  start: { line: 1, column: 17 },
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Regression tests for #2910 (bug 2): a `const`/`let` binding mutated by a
3
+ * same-scope statement AFTER its declaration must never be inlined by its
4
+ * (now stale) declaration-time initializer text wherever the bare
5
+ * identifier is referenced as a component prop value.
6
+ *
7
+ * `const config = {}; for (const s of series) config[s.key] = {...}` is the
8
+ * `@barefootjs/chart` shape the issue reports: `expandDynamicPropValue`
9
+ * (`ir-to-client-js/prop-handling.ts`) used to look up `config` in
10
+ * `ctx.localConstants` and re-embed its initializer text (`"{}"`)
11
+ * verbatim — a snapshot from BEFORE the mutating `for` loop ran — instead
12
+ * of a live reference to the `config` binding. `markMutatedConstants`
13
+ * (analyzer.ts) now flags such bindings `mutatedAfterDeclaration`, and
14
+ * every inlining consumer checks that flag first.
15
+ */
16
+ import { describe, expect, test } from 'bun:test'
17
+ import { compileJSX } from '../index.ts'
18
+ import { TestAdapter } from '../adapters/test-adapter.ts'
19
+
20
+ function clientJsOf(source: string, path: string): string {
21
+ const result = compileJSX(source, path, { adapter: new TestAdapter() })
22
+ const errors = result.errors.filter(e => e.severity === 'error')
23
+ expect(errors).toEqual([])
24
+ const js = result.files.find(f => f.path.endsWith('.client.js'))?.content
25
+ if (!js) throw new Error(`no client JS emitted for ${path}`)
26
+ return js
27
+ }
28
+
29
+ describe('#2910 — a mutated-after-declaration const is never re-inlined', () => {
30
+ test('a for-of-loop-mutated object literal reaches the child prop as a live reference', () => {
31
+ const js = clientJsOf(
32
+ `'use client'
33
+ function Child(props: { config: Record<string, { color: string }> }) {
34
+ const handleMount = (el: HTMLElement) => {
35
+ for (const [k, v] of Object.entries(props.config)) el.style.setProperty('--color-' + k, v.color)
36
+ }
37
+ return <div ref={handleMount} />
38
+ }
39
+
40
+ export function Host() {
41
+ const config: Record<string, { color: string }> = {}
42
+ const series = [{ key: 'a', color: 'red' }, { key: 'b', color: 'blue' }]
43
+ for (const s of series) config[s.key] = { color: s.color }
44
+ return <Child config={config} />
45
+ }
46
+ `,
47
+ 'host.tsx',
48
+ )
49
+
50
+ expect(js).toContain('get config() { return config }')
51
+ expect(js).not.toContain('return {}')
52
+ // The prop can no longer be known statically at template time — the
53
+ // child render must defer to init (`upsertChild`), not an inline
54
+ // `initChild(..., { config: {} })` call.
55
+ expect(js).toContain("upsertChild(__scope, 'Child")
56
+ expect(js).not.toContain('config: ')
57
+ })
58
+
59
+ test('a reassigned let binding is treated the same as an in-place mutation', () => {
60
+ const js = clientJsOf(
61
+ `'use client'
62
+ function Child(props: { label: string }) {
63
+ return <span>{props.label}</span>
64
+ }
65
+
66
+ export function Host() {
67
+ let label = 'initial'
68
+ if (Math.random() > 0.5) label = 'changed'
69
+ return <Child label={label} />
70
+ }
71
+ `,
72
+ 'host-let.tsx',
73
+ )
74
+
75
+ expect(js).toContain('get label() { return label }')
76
+ expect(js).not.toContain("get label() { return 'initial' }")
77
+ })
78
+
79
+ test('a .push()-mutated array reaches the child prop as a live reference', () => {
80
+ const js = clientJsOf(
81
+ `'use client'
82
+ function List(props: { items: string[] }) {
83
+ return <span>{props.items.join(',')}</span>
84
+ }
85
+
86
+ export function Host() {
87
+ const items: string[] = []
88
+ items.push('a')
89
+ items.push('b')
90
+ return <List items={items} />
91
+ }
92
+ `,
93
+ 'host-push.tsx',
94
+ )
95
+
96
+ expect(js).toContain('get items() { return items }')
97
+ expect(js).not.toContain('items: []')
98
+ })
99
+
100
+ test('an untouched object-literal const is still inlined (no regression)', () => {
101
+ const js = clientJsOf(
102
+ `'use client'
103
+ function Child(props: { config: Record<string, { color: string }> }) {
104
+ return <span>{Object.keys(props.config).length}</span>
105
+ }
106
+
107
+ export function Host() {
108
+ const config: Record<string, { color: string }> = { a: { color: 'red' }, b: { color: 'blue' } }
109
+ return <Child config={config} />
110
+ }
111
+ `,
112
+ 'host-literal.tsx',
113
+ )
114
+
115
+ // Never mutated — the existing inlining behaviour is unaffected.
116
+ expect(js).toContain("config: ({ a: { color: 'red' }, b: { color: 'blue' } })")
117
+ })
118
+ })
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Regression test for #2910 (bug 1): `generateInitFunction` must emit
3
+ * `const __scopeId = ownScopeId(__scope)` for a comment-scoped root (a
4
+ * fragment root, or a root that is itself a single child-component call,
5
+ * #2649) and keep the plain `__scope.getAttribute('bf-s')` read for an
6
+ * ordinary element-scoped root. See `packages/client/src/runtime/scope.ts`'s
7
+ * `ownScopeId` for why the two differ: a comment-scoped component is
8
+ * mounted on a PROXY element whose own `bf-s` names its host/parent scope,
9
+ * not its own — reading it directly resolves every `.map()`-produced
10
+ * child lookup (`[bf-h="${__scopeId}"]`) against the wrong id.
11
+ */
12
+ import { describe, expect, test } from 'bun:test'
13
+ import { compileJSX } from '../index.ts'
14
+ import { TestAdapter } from '../adapters/test-adapter.ts'
15
+
16
+ function clientJsOf(source: string, path: string): string {
17
+ const result = compileJSX(source, path, { adapter: new TestAdapter() })
18
+ const errors = result.errors.filter(e => e.severity === 'error')
19
+ expect(errors).toEqual([])
20
+ const js = result.files.find(f => f.path.endsWith('.client.js'))?.content
21
+ if (!js) throw new Error(`no client JS emitted for ${path}`)
22
+ return js
23
+ }
24
+
25
+ describe('#2910 — __scopeId reads the comment-scope registry for a comment-scoped root', () => {
26
+ test('root is a single child-component call (#2649 shape) uses ownScopeId', () => {
27
+ const js = clientJsOf(
28
+ `'use client'
29
+ function Panel(props: { children?: unknown }) {
30
+ return <div className="panel">{props.children}</div>
31
+ }
32
+
33
+ export function Host() {
34
+ return <Panel><span>hi</span></Panel>
35
+ }
36
+ `,
37
+ 'host.tsx',
38
+ )
39
+
40
+ expect(js).toContain('function initHost')
41
+ const initHost = js.slice(js.indexOf('function initHost'))
42
+ expect(initHost).toContain('const __scopeId = ownScopeId(__scope)')
43
+ expect(initHost).not.toContain("__scope.getAttribute('bf-s')")
44
+ })
45
+
46
+ test('an ordinary element-scoped root keeps reading the bf-s attribute directly', () => {
47
+ const js = clientJsOf(
48
+ `'use client'
49
+ function Leaf(props: { label: string }) {
50
+ return <span>{props.label}</span>
51
+ }
52
+
53
+ export function Host() {
54
+ return <div className="panel"><Leaf label="hi" /></div>
55
+ }
56
+ `,
57
+ 'host-plain.tsx',
58
+ )
59
+
60
+ const initHost = js.slice(js.indexOf('function initHost'))
61
+ expect(initHost).toContain("const __scopeId = __scope.getAttribute('bf-s')")
62
+ expect(initHost).not.toContain('ownScopeId')
63
+ })
64
+ })
@@ -0,0 +1,160 @@
1
+ /**
2
+ * `liveOnlyProps` classification (Move C, Prop Boundary Contract).
3
+ *
4
+ * `computeLiveOnlyProps` (`ir-to-client-js/index.ts`) marks which props the
5
+ * client code CALLS rather than reads, so they cannot arrive as JSON. It
6
+ * only classifies — whether that matters for a given mount is decided at
7
+ * render time by the Hono adapter's gated `serializeHydrationProps` call
8
+ * (`serialize-hydration-props.test.ts` covers that half).
9
+ */
10
+
11
+ import { describe, test, expect } from 'bun:test'
12
+ import { analyzeComponent } from '../analyzer'
13
+ import { jsxToIR } from '../jsx-to-ir'
14
+ import { buildMetadata } from '../compiler'
15
+ import { analyzeClientNeeds } from '../ir-to-client-js'
16
+ import type { ComponentIR } from '../types'
17
+
18
+ function analyze(source: string): ReturnType<typeof analyzeClientNeeds> {
19
+ const ctx = analyzeComponent(source, 'Test.tsx')
20
+ const ir = jsxToIR(ctx)
21
+ expect(ir).not.toBeNull()
22
+ const componentIR: ComponentIR = {
23
+ version: '0.1',
24
+ metadata: buildMetadata(ctx),
25
+ root: ir!,
26
+ errors: [],
27
+ }
28
+ return analyzeClientNeeds(componentIR)
29
+ }
30
+
31
+ describe('liveOnlyProps — fires', () => {
32
+ test('a function-typed prop actually called by client code lands in liveOnlyProps', () => {
33
+ const analysis = analyze(`
34
+ 'use client'
35
+ export function Display({ value }: { value: () => number }) {
36
+ return <button onClick={() => console.log(value())}>{value()}</button>
37
+ }
38
+ `)
39
+ expect(analysis.needsInit).toBe(true)
40
+ expect(analysis.usedProps).toContain('value')
41
+ expect(Object.keys(analysis.liveOnlyProps)).toContain('value')
42
+ expect(analysis.liveOnlyProps.value).toBe('() => number')
43
+ })
44
+
45
+ test('object-form Function-typed prop actually called by client code also lands in liveOnlyProps', () => {
46
+ const analysis = analyze(`
47
+ 'use client'
48
+ export function Foo({ cb }: { cb: Function }) {
49
+ return <button onClick={() => cb()}>go</button>
50
+ }
51
+ `)
52
+ expect(Object.keys(analysis.liveOnlyProps)).toContain('cb')
53
+ expect(analysis.liveOnlyProps.cb).toBe('Function')
54
+ })
55
+
56
+ test('renamed destructured prop is keyed by its caller-facing (source) name, not the local binding', () => {
57
+ const analysis = analyze(`
58
+ 'use client'
59
+ export function Display({ value: getValue }: { value: () => number }) {
60
+ return <button onClick={() => console.log(getValue())}>go</button>
61
+ }
62
+ `)
63
+ expect(Object.keys(analysis.liveOnlyProps)).toContain('value')
64
+ expect(analysis.liveOnlyProps).not.toHaveProperty('getValue')
65
+ })
66
+
67
+ test('an OPTIONAL function-typed prop still resolves (stripUnion drops the undefined arm)', () => {
68
+ const analysis = analyze(`
69
+ 'use client'
70
+ export function Display({ value }: { value?: () => number }) {
71
+ return <button onClick={() => console.log(value?.())}>go</button>
72
+ }
73
+ `)
74
+ expect(Object.keys(analysis.liveOnlyProps)).toContain('value')
75
+ })
76
+
77
+ test('props-object mode (props.value) resolves the same way', () => {
78
+ const analysis = analyze(`
79
+ 'use client'
80
+ export function Display(props: { value: () => number }) {
81
+ return <button onClick={() => console.log(props.value())}>go</button>
82
+ }
83
+ `)
84
+ expect(Object.keys(analysis.liveOnlyProps)).toContain('value')
85
+ })
86
+ })
87
+
88
+ describe('liveOnlyProps — silent', () => {
89
+ test('a function-typed prop declared but never read by client code is not classified', () => {
90
+ // `usedProps` filtering already excludes it upstream (never even
91
+ // reaches the client init), so it can't land in liveOnlyProps either.
92
+ const analysis = analyze(`
93
+ 'use client'
94
+ export function Display({ value, label }: { value: () => number; label: string }) {
95
+ return <button onClick={() => console.log(label)}>{label}</button>
96
+ }
97
+ `)
98
+ expect(analysis.usedProps).not.toContain('value')
99
+ expect(analysis.liveOnlyProps).not.toHaveProperty('value')
100
+ })
101
+
102
+ test('onClick-shaped (event-handler-convention) prop is excluded even though it is function-typed', () => {
103
+ const analysis = analyze(`
104
+ interface Props { onClick: () => void }
105
+ export function Clickable({ onClick }: Props) {
106
+ return <div onClick={onClick}>Click me</div>
107
+ }
108
+ `)
109
+ expect(analysis.liveOnlyProps).not.toHaveProperty('onClick')
110
+ })
111
+
112
+ test('__-prefixed internal prop is excluded even though it is function-typed', () => {
113
+ const analysis = analyze(`
114
+ 'use client'
115
+ export function Internal({ __cb }: { __cb: () => void }) {
116
+ return <button onClick={() => __cb()}>go</button>
117
+ }
118
+ `)
119
+ expect(analysis.liveOnlyProps).not.toHaveProperty('__cb')
120
+ })
121
+
122
+ test('a non-function-typed prop (number) is never in liveOnlyProps', () => {
123
+ const analysis = analyze(`
124
+ 'use client'
125
+ export function Counter({ value }: { value: number }) {
126
+ return <button onClick={() => console.log(value)}>{value}</button>
127
+ }
128
+ `)
129
+ expect(analysis.usedProps).toContain('value')
130
+ expect(analysis.liveOnlyProps).not.toHaveProperty('value')
131
+ })
132
+
133
+ test('an ALIASED function type is a known conservative miss, not a classification', () => {
134
+ // `typeNodeToTypeInfo` lowers a named type reference to
135
+ // `{ kind: 'interface', raw: 'Filter' }` with no member walk, so the
136
+ // function shape behind the alias is invisible here and the prop keeps
137
+ // the pre-Move-C silent drop. BF049 has the same blind spot for aliased
138
+ // rich types. Pinned so a future alias-resolution change is a deliberate
139
+ // flip of this expectation, not an accident.
140
+ const analysis = analyze(`
141
+ 'use client'
142
+ type Filter = (s: string) => boolean
143
+ export function Display({ filter }: { filter: Filter }) {
144
+ return <button onClick={() => console.log(filter('x'))}>go</button>
145
+ }
146
+ `)
147
+ expect(analysis.usedProps).toContain('filter')
148
+ expect(analysis.liveOnlyProps).not.toHaveProperty('filter')
149
+ })
150
+
151
+ test('a static (non-client) component has no clientAnalysis needs at all — liveOnlyProps is empty', () => {
152
+ const analysis = analyze(`
153
+ export function Static({ value }: { value: () => number }) {
154
+ return <span>static</span>
155
+ }
156
+ `)
157
+ expect(analysis.needsInit).toBe(false)
158
+ expect(analysis.liveOnlyProps).toEqual({})
159
+ })
160
+ })
@@ -212,7 +212,7 @@ describe('PropReference extraction', () => {
212
212
  })
213
213
 
214
214
  describe('ClientJS generation with semantic prop refs', () => {
215
- test('destructured props are captured once, NOT transformed in createEffect', () => {
215
+ test('destructured props compile to a live _p.open read, no captured-once local', () => {
216
216
  const source = `
217
217
  'use client'
218
218
 
@@ -231,10 +231,10 @@ describe('ClientJS generation with semantic prop refs', () => {
231
231
 
232
232
  const clientJs = result.files.find((f) => f.type === 'clientJs')
233
233
  expect(clientJs).toBeDefined()
234
- // Should have capture: const open = props.open
235
- expect(clientJs?.content).toContain('const open = _p.open')
236
- // createEffect should use 'open' directly, NOT 'props.open'
237
- // (destructured props are static, captured once)
234
+ // No captured-once local: the conditional reads `_p.open` live.
235
+ expect(clientJs?.content).not.toContain('const open = ')
236
+ expect(clientJs?.content).toContain('insert(__scope, \'s0\', () => _p.open,')
237
+ // Never a stale `props.` prefix either.
238
238
  expect(clientJs?.content).not.toMatch(/insert\([^)]*props\.open/)
239
239
  })
240
240
 
@@ -287,7 +287,7 @@ describe('ClientJS generation with semantic prop refs', () => {
287
287
  expect(clientJs?.content).not.toContain('window.props.open')
288
288
  })
289
289
 
290
- test('destructured props with default value are captured once', () => {
290
+ test('destructured props with default value read live, with the default applied at every read', () => {
291
291
  const source = `
292
292
  'use client'
293
293
 
@@ -306,8 +306,10 @@ describe('ClientJS generation with semantic prop refs', () => {
306
306
 
307
307
  const clientJs = result.files.find((f) => f.type === 'clientJs')
308
308
  expect(clientJs).toBeDefined()
309
- // Should capture with default value: const open = props.open ?? false
310
- expect(clientJs?.content).toMatch(/const open = _p\.open \?\? false/)
309
+ // No captured-once local — the live read carries the default at the
310
+ // read site itself: `(_p.open ?? false)`.
311
+ expect(clientJs?.content).not.toContain('const open = ')
312
+ expect(clientJs?.content).toMatch(/\(_p\.open \?\? false\)/)
311
313
  })
312
314
  })
313
315
 
@@ -319,8 +321,13 @@ describe('ClientJS generation with semantic prop refs', () => {
319
321
  // reachable), but was never wired to `ctx.clientOnlyElements` either, so no
320
322
  // edge of ANY kind reached `neededProps` and `emitPropsExtraction` had no
321
323
  // evidence the prop was used at all.
324
+ //
325
+ // The bug class is now structurally impossible for a destructured prop:
326
+ // `rewriteDestructuredPropReads` walks the joined init body and consults
327
+ // neither `neededProps` nor the reference graph, so there is no edge left
328
+ // for an emitter to forget to wire up.
322
329
  describe('Issue #2634 regression', () => {
323
- test('a destructured prop used only inside /* @client */ is extracted from _p', () => {
330
+ test('a destructured prop used only inside /* @client */ reads _p.label directly (no unbound identifier)', () => {
324
331
  const source = `
325
332
  interface Props {
326
333
  label: string
@@ -336,11 +343,11 @@ describe('Issue #2634 regression', () => {
336
343
  expect(result.errors).toHaveLength(0)
337
344
  const clientJs = result.files.find((f) => f.type === 'clientJs')
338
345
  expect(clientJs).toBeDefined()
339
- // The prop must be bound before the createEffect body reads it.
340
- expect(clientJs?.content).toContain('const label = _p.label')
341
- // A bare `label` reference with no preceding binding is exactly the
342
- // unbound-identifier shape that threw ReferenceError at hydrate.
343
- expect(clientJs?.content).toMatch(/const label = _p\.label[\s\S]*label\.toUpperCase\(\)/)
346
+ // A bare, unbound `label` is the shape that threw ReferenceError at
347
+ // hydrate pre-#2634; the live `_p.label` read is what replaces both
348
+ // that bug and its original captured-once fix.
349
+ expect(clientJs?.content).not.toContain('const label = ')
350
+ expect(clientJs?.content).toContain('_p.label.toUpperCase()')
344
351
  })
345
352
 
346
353
  test('a props-object member used only inside /* @client */ needs no extraction (already _p.x)', () => {
@@ -362,7 +369,7 @@ describe('Issue #2634 regression', () => {
362
369
  expect(clientJs?.content).toContain('_p.label.toUpperCase()')
363
370
  })
364
371
 
365
- test('a prop used both in a normal reactive position AND inside /* @client */ is still extracted once', () => {
372
+ test('a prop used both in a normal reactive position AND inside /* @client */ reads _p.label live at BOTH sites (no extraction)', () => {
366
373
  const source = `
367
374
  interface Props {
368
375
  label: string
@@ -378,8 +385,10 @@ describe('Issue #2634 regression', () => {
378
385
  expect(result.errors).toHaveLength(0)
379
386
  const clientJs = result.files.find((f) => f.type === 'clientJs')
380
387
  expect(clientJs).toBeDefined()
381
- const matches = clientJs?.content.match(/const label = _p\.label/g) ?? []
382
- expect(matches).toHaveLength(1)
388
+ // Each of the two reference sites reads `_p.label` live, independently.
389
+ expect(clientJs?.content).not.toContain('const label = ')
390
+ const matches = clientJs?.content.match(/_p\.label/g) ?? []
391
+ expect(matches.length).toBeGreaterThanOrEqual(2)
383
392
  })
384
393
  })
385
394