@barefootjs/jsx 0.38.0 → 0.39.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 (105) hide show
  1. package/dist/action-accessor.d.ts +159 -0
  2. package/dist/action-accessor.d.ts.map +1 -0
  3. package/dist/adapters/interface.d.ts +19 -0
  4. package/dist/adapters/interface.d.ts.map +1 -1
  5. package/dist/adapters/jsx-adapter.d.ts.map +1 -1
  6. package/dist/analyzer.d.ts +49 -1
  7. package/dist/analyzer.d.ts.map +1 -1
  8. package/dist/async-action-refusal.d.ts +45 -0
  9. package/dist/async-action-refusal.d.ts.map +1 -0
  10. package/dist/compiler.d.ts.map +1 -1
  11. package/dist/debug.d.ts +61 -5
  12. package/dist/debug.d.ts.map +1 -1
  13. package/dist/errors.d.ts +4 -0
  14. package/dist/errors.d.ts.map +1 -1
  15. package/dist/format-date-refusal.d.ts +2 -6
  16. package/dist/format-date-refusal.d.ts.map +1 -1
  17. package/dist/html-types.d.ts +5 -0
  18. package/dist/html-types.d.ts.map +1 -1
  19. package/dist/index.d.ts +3 -2
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +1261 -422
  22. package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
  23. package/dist/ir-to-client-js/collect-elements.d.ts +17 -1
  24. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  25. package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
  26. package/dist/ir-to-client-js/compute-scope.d.ts.map +1 -1
  27. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -0
  28. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts.map +1 -1
  29. package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
  30. package/dist/ir-to-client-js/declaration-sort.d.ts +2 -1
  31. package/dist/ir-to-client-js/declaration-sort.d.ts.map +1 -1
  32. package/dist/ir-to-client-js/emit-module-level.d.ts.map +1 -1
  33. package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
  34. package/dist/ir-to-client-js/plan/declaration-emit.d.ts +7 -5
  35. package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
  36. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  37. package/dist/ir-to-client-js/reactivity.d.ts +10 -2
  38. package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
  39. package/dist/ir-to-client-js/types.d.ts +4 -0
  40. package/dist/ir-to-client-js/types.d.ts.map +1 -1
  41. package/dist/ir-to-client-js/utils.d.ts +52 -9
  42. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  43. package/dist/jsx-to-ir.d.ts.map +1 -1
  44. package/dist/signal-initializer.d.ts +24 -0
  45. package/dist/signal-initializer.d.ts.map +1 -0
  46. package/dist/ssr-defaults.d.ts.map +1 -1
  47. package/dist/ssr-seed-plan.d.ts.map +1 -1
  48. package/dist/template-position-walk.d.ts +68 -0
  49. package/dist/template-position-walk.d.ts.map +1 -0
  50. package/dist/types.d.ts +95 -0
  51. package/dist/types.d.ts.map +1 -1
  52. package/package.json +2 -2
  53. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +194 -4
  54. package/src/__tests__/action-accessor.test.ts +166 -0
  55. package/src/__tests__/adapter-output.test.ts +142 -0
  56. package/src/__tests__/create-mutation.test.ts +250 -0
  57. package/src/__tests__/create-query.test.ts +696 -0
  58. package/src/__tests__/csr-fragment-root-scope-shape.test.ts +156 -0
  59. package/src/__tests__/debug.test.ts +305 -0
  60. package/src/__tests__/doc-examples.test.ts +66 -3
  61. package/src/__tests__/ir-fragment-transparency.test.ts +23 -0
  62. package/src/__tests__/issue-3240-nested-static-map-event-delegation.test.ts +245 -0
  63. package/src/__tests__/jsx-text-linebreak-whitespace.test.ts +80 -0
  64. package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +445 -0
  65. package/src/__tests__/props-destructuring.test.ts +79 -4
  66. package/src/__tests__/reactive-factory-arity.test.ts +311 -0
  67. package/src/__tests__/ssr-portal-ref-callback.test.ts +5 -3
  68. package/src/action-accessor.ts +289 -0
  69. package/src/adapters/interface.ts +19 -0
  70. package/src/adapters/jsx-adapter.ts +47 -20
  71. package/src/analyzer.ts +593 -44
  72. package/src/async-action-refusal.ts +254 -0
  73. package/src/compiler.ts +81 -46
  74. package/src/debug.ts +351 -104
  75. package/src/errors.ts +39 -0
  76. package/src/format-date-refusal.ts +6 -125
  77. package/src/html-types.ts +14 -0
  78. package/src/index.ts +3 -1
  79. package/src/ir-to-client-js/build-references.ts +6 -2
  80. package/src/ir-to-client-js/collect-elements.ts +65 -4
  81. package/src/ir-to-client-js/compute-inlinability.ts +2 -1
  82. package/src/ir-to-client-js/compute-scope.ts +3 -1
  83. package/src/ir-to-client-js/control-flow/plan/build-insert.ts +1 -0
  84. package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -0
  85. package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +112 -3
  86. package/src/ir-to-client-js/control-flow/stringify/insert.ts +1 -1
  87. package/src/ir-to-client-js/csr-substitute.ts +11 -3
  88. package/src/ir-to-client-js/declaration-sort.ts +7 -3
  89. package/src/ir-to-client-js/emit-module-level.ts +7 -4
  90. package/src/ir-to-client-js/emit-registration.ts +5 -10
  91. package/src/ir-to-client-js/index.ts +7 -2
  92. package/src/ir-to-client-js/phases/ref-callbacks.ts +1 -1
  93. package/src/ir-to-client-js/plan/build-declaration-emit.ts +16 -15
  94. package/src/ir-to-client-js/plan/declaration-emit.ts +7 -5
  95. package/src/ir-to-client-js/prop-handling.ts +4 -0
  96. package/src/ir-to-client-js/reactivity.ts +32 -1
  97. package/src/ir-to-client-js/stringify/declaration-emit.ts +14 -7
  98. package/src/ir-to-client-js/types.ts +4 -0
  99. package/src/ir-to-client-js/utils.ts +184 -21
  100. package/src/jsx-to-ir.ts +111 -14
  101. package/src/signal-initializer.ts +36 -0
  102. package/src/ssr-defaults.ts +10 -0
  103. package/src/ssr-seed-plan.ts +12 -1
  104. package/src/template-position-walk.ts +203 -0
  105. package/src/types.ts +99 -0
@@ -0,0 +1,245 @@
1
+ /**
2
+ * #3240: a nested `.map()` over STATIC arrays (module-level `const`s, so the
3
+ * outer loop hydrates via `forEach` instead of `mapArray`) whose INNER row
4
+ * has an event handler closing over the inner loop's own param. The
5
+ * delegated listener lives on the outer loop's container
6
+ * (`emitStaticIndexLookup`) and, pre-fix, resolved only the outer item by
7
+ * index — it never declared the inner param, so the handler read an
8
+ * undeclared identifier and threw `ReferenceError` on every click (visible
9
+ * symptom: clicking does nothing).
10
+ *
11
+ * Root cause: `emitStaticIndexLookup` (unlike `emitKeyedLookup`, which
12
+ * already resolves `ev.nestedLoops` via `data-key-N` `closest()` lookups)
13
+ * ignored `ev.nestedLoops` entirely. Static arrays render no `data-key`
14
+ * markers to `closest()` off for the OUTER resolution (hence the
15
+ * `static-index` lookup kind in the first place), so the fix resolves each
16
+ * nested level positionally instead — scoping a `containerSlotId` lookup to
17
+ * the previously-resolved row and reading the clicked element's position
18
+ * the same way `stringify/static-array-child-init.ts`'s hydration pass
19
+ * already does for this exact nested-static-loop shape (#2798).
20
+ *
21
+ * This is the string-shape half of the regression pin; the runtime-behavior
22
+ * half (mounts the compiled output in a real DOM and clicks it) lives in
23
+ * `packages/client/__tests__/runtime/issue-3240-nested-static-map-event.test.ts`
24
+ * — mirroring how #2189 split across
25
+ * `event-delegation-index-param.test.ts` (shape) and
26
+ * `event-delegation-index-param-e2e.test.ts` (runtime).
27
+ */
28
+
29
+ import { describe, test, expect } from 'bun:test'
30
+ import { compileJSX } from '../compiler'
31
+ import { TestAdapter } from '../adapters/test-adapter'
32
+
33
+ const adapter = new TestAdapter()
34
+
35
+ function clientJsFor(source: string): string {
36
+ const result = compileJSX(source, 'Repro.tsx', { adapter })
37
+ expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
38
+ const clientJs = result.files.find(f => f.type === 'clientJs')
39
+ expect(clientJs).toBeDefined()
40
+ return clientJs!.content
41
+ }
42
+
43
+ describe('static-index event delegation resolves nested .map() params (#3240)', () => {
44
+ test('inner-loop handler param is declared and reachable inside the outer item guard', () => {
45
+ const content = clientJsFor(`
46
+ 'use client'
47
+ import { createSignal } from '@barefootjs/client'
48
+ const GROUPS = ['a', 'b']
49
+ const CHOICES = ['1', '2']
50
+ export function Repro() {
51
+ const [picked, setPicked] = createSignal('')
52
+ return (
53
+ <div>
54
+ {GROUPS.map(group => (
55
+ <div key={group}>
56
+ {CHOICES.map(choice => (
57
+ <button key={choice} onClick={() => setPicked(\`\${group}\${choice}\`)}>{group}{choice}</button>
58
+ ))}
59
+ </div>
60
+ ))}
61
+ </div>
62
+ )
63
+ }
64
+ `)
65
+
66
+ // The outer item is still resolved positionally, unchanged.
67
+ expect(content).toContain('const group = GROUPS[__idx]')
68
+ // The inner item is now resolved too (pre-fix: never declared at all).
69
+ // Guarded on the resolved INDEX, not the item's truthiness (a `0`/`''`/
70
+ // `false` item must still run the handler — see `emitNestedIndexParams`'s
71
+ // docstring), so `choice` is declared only once `__iidx1 >= 0` is known.
72
+ expect(content).toContain('if (__iidx1 >= 0) {')
73
+ expect(content).toContain('const choice = CHOICES[__iidx1]')
74
+ // The handler call is guarded on BOTH params, and runs after both are
75
+ // declared (not before — a `choice` declared after the call would still
76
+ // throw a TDZ `ReferenceError`).
77
+ const guardIdx = content.indexOf('if (__iidx1 >= 0) {')
78
+ const choiceDeclIdx = content.indexOf('const choice = CHOICES[__iidx1]')
79
+ const callIdx = content.indexOf('(() => setPicked(`${group}${choice}`))(__bfEvt)')
80
+ expect(guardIdx).toBeGreaterThan(0)
81
+ expect(choiceDeclIdx).toBeGreaterThan(guardIdx)
82
+ expect(callIdx).toBeGreaterThan(choiceDeclIdx)
83
+ })
84
+
85
+ test('byte-stability: an event with no nested loops keeps the pre-#3240 shape', () => {
86
+ const content = clientJsFor(`
87
+ 'use client'
88
+ import { createSignal } from '@barefootjs/client'
89
+ const ITEMS = ['a', 'b']
90
+ export function Repro() {
91
+ const [picked, setPicked] = createSignal('')
92
+ return (
93
+ <ul>
94
+ {ITEMS.map(item => (
95
+ <li key={item} onClick={() => setPicked(item)}>{item}</li>
96
+ ))}
97
+ </ul>
98
+ )
99
+ }
100
+ `)
101
+
102
+ // No nested-loop resolution machinery leaks into a plain (non-nested)
103
+ // static-index event — same shape as before #3240.
104
+ expect(content).not.toContain('__ic1')
105
+ expect(content).not.toContain('__iidx1')
106
+ expect(content).toMatch(/const item = ITEMS\[__idx\]\n\s*if \(item\) \{\n\s*;\(\(\) => setPicked\(item\)\)\(__bfEvt\)\n\s*\}/)
107
+ })
108
+
109
+ test('a nested array expression referencing the outer param resolves inside the outer guard', () => {
110
+ // Exercises the props-driven-lookup shape from the issue (`CHOICES[settingKey]`)
111
+ // — the nested array expression reads the outer param, so it must be
112
+ // declared AFTER the outer item guard opens, not before.
113
+ const content = clientJsFor(`
114
+ 'use client'
115
+ import { createSignal } from '@barefootjs/client'
116
+ const KEYS = ['x', 'y']
117
+ const CHOICE_MAP: Record<string, string[]> = { x: ['1', '2'], y: ['3', '4'] }
118
+ export function Repro() {
119
+ const [picked, setPicked] = createSignal('')
120
+ return (
121
+ <div>
122
+ {KEYS.map(settingKey => (
123
+ <div key={settingKey}>
124
+ {CHOICE_MAP[settingKey].map(choice => (
125
+ <button key={choice} onClick={() => setPicked(\`\${settingKey}:\${choice}\`)}>{settingKey}{choice}</button>
126
+ ))}
127
+ </div>
128
+ ))}
129
+ </div>
130
+ )
131
+ }
132
+ `)
133
+
134
+ expect(content).toContain('if (__iidx1 >= 0) {')
135
+ expect(content).toContain('const choice = CHOICE_MAP[settingKey][__iidx1]')
136
+ })
137
+
138
+ test('a static sibling before the inner .map() offsets the nested positional lookup', () => {
139
+ // #3240 follow-up: `emitNestedIndexParams` recovers a nested level's item
140
+ // from its DOM position within its container — but it read
141
+ // `nested.offset`, a `NestedLoop` field the delegated-event `NestedLoop`
142
+ // chain (`collectLoopChildEventsWithNesting`'s `loop` visitor) never
143
+ // populated, unlike the hydration-side `NestedLoop`
144
+ // (`collectInnerLoops`, which resolves it via
145
+ // `resolveLoopOffset(siblingOffsets.get(n))`). Left unset, the offset
146
+ // silently defaulted to zero: a static `<span>` before `CHOICES.map()`
147
+ // here shifts every button one slot right in `__ic1.children`, so
148
+ // `Array.from(__ic1.children).indexOf(__ir1)` must subtract 1 to land
149
+ // back on the right `CHOICES` index — the runtime half of this repro
150
+ // (`issue-3240-nested-static-map-event.test.ts`) clicks the first button
151
+ // and asserts it resolves to `CHOICES[0]`, not the silently-wrong
152
+ // `CHOICES[1]`.
153
+ const content = clientJsFor(`
154
+ 'use client'
155
+ import { createSignal } from '@barefootjs/client'
156
+ const GROUPS = ['a', 'b']
157
+ const CHOICES = ['1', '2']
158
+ export function Repro() {
159
+ const [picked, setPicked] = createSignal('')
160
+ return (
161
+ <div>
162
+ {GROUPS.map(group => (
163
+ <div key={group}>
164
+ <span>hdr</span>
165
+ {CHOICES.map(choice => (
166
+ <button key={choice} onClick={() => setPicked(\`\${group}\${choice}\`)}>{group}{choice}</button>
167
+ ))}
168
+ </div>
169
+ ))}
170
+ </div>
171
+ )
172
+ }
173
+ `)
174
+
175
+ expect(content).toContain(
176
+ 'const __iidx1 = __ir1.parentElement === __ic1 ? Array.from(__ic1.children).indexOf(__ir1) - 1 : -1',
177
+ )
178
+ })
179
+
180
+ test('a falsy inner item (0) still guards on the resolved index, not the item value', () => {
181
+ // Pullfrog follow-up on #3240: guarding on `if (choice)` (the item's
182
+ // truthiness) instead of `if (__iidx1 >= 0)` (whether the lookup
183
+ // resolved) meant a numeric array containing `0` silently dropped a
184
+ // click on that item — `if (0)` is false even though the index (0)
185
+ // resolved correctly. This asserts the emitted guard checks the index.
186
+ const content = clientJsFor(`
187
+ 'use client'
188
+ import { createSignal } from '@barefootjs/client'
189
+ const GROUPS = ['a', 'b']
190
+ const NUMS = [0, 1, 2]
191
+ export function Repro() {
192
+ const [picked, setPicked] = createSignal(-1)
193
+ return (
194
+ <div>
195
+ {GROUPS.map(group => (
196
+ <div key={group}>
197
+ {NUMS.map(n => (
198
+ <button key={n} onClick={() => setPicked(n)}>{group}{n}</button>
199
+ ))}
200
+ </div>
201
+ ))}
202
+ </div>
203
+ )
204
+ }
205
+ `)
206
+
207
+ expect(content).toContain('if (__iidx1 >= 0) {')
208
+ expect(content).toContain('const n = NUMS[__iidx1]')
209
+ expect(content).not.toContain('if (n) {')
210
+ })
211
+
212
+ test('a destructured inner param is only declared once the index has resolved', () => {
213
+ // Pullfrog follow-up on #3240: guarding on the (destructured) param's
214
+ // truthiness is meaningless — an object pattern is always truthy — and
215
+ // declaring `const { id, label } = CHOICES[__iidx1]` unconditionally
216
+ // (with `__iidx1` possibly `-1`, i.e. `CHOICES[-1]` = `undefined`) threw
217
+ // a `TypeError` destructuring `undefined`. Declaring it only inside the
218
+ // `__iidx1 >= 0` guard means it's never handed `undefined`.
219
+ const content = clientJsFor(`
220
+ 'use client'
221
+ import { createSignal } from '@barefootjs/client'
222
+ const GROUPS = ['a', 'b']
223
+ const CHOICES = [{ id: 'x', label: 'X' }, { id: 'y', label: 'Y' }]
224
+ export function Repro() {
225
+ const [picked, setPicked] = createSignal('')
226
+ return (
227
+ <div>
228
+ {GROUPS.map(group => (
229
+ <div key={group}>
230
+ {CHOICES.map(({ id, label }) => (
231
+ <button key={id} onClick={() => setPicked(\`\${group}\${id}\`)}>{group}{label}</button>
232
+ ))}
233
+ </div>
234
+ ))}
235
+ </div>
236
+ )
237
+ }
238
+ `)
239
+
240
+ const guardIdx = content.indexOf('if (__iidx1 >= 0) {')
241
+ const declIdx = content.indexOf('const { id, label } = CHOICES[__iidx1]')
242
+ expect(guardIdx).toBeGreaterThan(0)
243
+ expect(declIdx).toBeGreaterThan(guardIdx)
244
+ })
245
+ })
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Regression tests for #3237: a JSX text child that sits on its own line
3
+ * kept the surrounding whitespace (collapsed to one space on each side)
4
+ * instead of following the JSX whitespace rule TypeScript/Babel/React all
5
+ * implement — trim whitespace that straddles a line break, drop lines that
6
+ * end up empty, join what remains with a single space.
7
+ *
8
+ * `transformText` (`../jsx-to-ir.ts`) is Phase 1 of the shared
9
+ * JSX → IR → template pipeline: every adapter (CSR included, exercised
10
+ * here via `TestAdapter`) renders from the same `IRText.value`, so a fix
11
+ * at this single call site covers CSR and SSR alike. This is the same
12
+ * `compileJSX` entry point `@barefootjs/vite`'s plugin calls per file
13
+ * (`packages/vite/src/plugin.ts`), so exercising it here — rather than
14
+ * calling `transformText` in isolation — pins the real pipeline's output,
15
+ * not just a convenient unit-level call order.
16
+ */
17
+
18
+ import { describe, test, expect } from 'bun:test'
19
+ import { compileJSX } from '../compiler'
20
+ import { TestAdapter } from '../adapters/test-adapter'
21
+
22
+ const adapter = new TestAdapter()
23
+
24
+ function getClientJs(source: string, filename = '/x/Probe.tsx'): string {
25
+ const result = compileJSX(source, filename, { adapter })
26
+ expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
27
+ const clientJs = result.files.find(f => f.type === 'clientJs')
28
+ expect(clientJs).toBeDefined()
29
+ return clientJs!.content
30
+ }
31
+
32
+ describe('JSX text whitespace across line breaks (#3237)', () => {
33
+ test('text alone on its own line between tags loses the surrounding blank lines', () => {
34
+ const source = `
35
+ 'use client'
36
+ export function Probe() {
37
+ return (
38
+ <div>
39
+ <div className="label">
40
+ Light
41
+ </div>
42
+ </div>
43
+ )
44
+ }
45
+ `
46
+ const js = getClientJs(source)
47
+ expect(js).toContain('<div class="label">Light</div>')
48
+ expect(js).not.toContain('> Light <')
49
+ expect(js).not.toContain(' Light ')
50
+ })
51
+
52
+ test('multi-line text is trimmed per line and joined with a single space', () => {
53
+ const source = `
54
+ 'use client'
55
+ export function Probe() {
56
+ return (
57
+ <p>
58
+ first line
59
+ second line
60
+ </p>
61
+ )
62
+ }
63
+ `
64
+ const js = getClientJs(source)
65
+ expect(js).toContain('<p>first line second line</p>')
66
+ })
67
+
68
+ test('whitespace within a single line (no line break) is left untouched', () => {
69
+ const source = `
70
+ 'use client'
71
+ export function Probe() {
72
+ return (
73
+ <div><b>a</b> <i>b</i></div>
74
+ )
75
+ }
76
+ `
77
+ const js = getClientJs(source)
78
+ expect(js).toContain('<div><b>a</b> <i>b</i></div>')
79
+ })
80
+ })