@barefootjs/jsx 0.37.0 → 0.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/env-signal.d.ts +0 -8
- package/dist/adapters/env-signal.d.ts.map +1 -1
- package/dist/adapters/interface.d.ts +12 -0
- package/dist/adapters/interface.d.ts.map +1 -1
- package/dist/analyzer.d.ts +40 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/builtin-lowering-plugins.d.ts.map +1 -1
- package/dist/compiler.d.ts.map +1 -1
- package/dist/component-root-scope-comment.d.ts +36 -0
- package/dist/component-root-scope-comment.d.ts.map +1 -0
- package/dist/errors.d.ts +3 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/event-handler-name.d.ts +13 -0
- package/dist/event-handler-name.d.ts.map +1 -0
- package/dist/expression-parser.d.ts +9 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/format-date-refusal.d.ts +37 -0
- package/dist/format-date-refusal.d.ts.map +1 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2973 -2310
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-branch-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-composite-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts +13 -2
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-reactive-effects.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/lazy-conditional.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/lazy-row-eligibility.d.ts +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts +9 -0
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +57 -12
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts +44 -3
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts +7 -5
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/loop-child-arm.d.ts +9 -1
- package/dist/ir-to-client-js/control-flow/stringify/loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-reactive.d.ts +43 -2
- package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/reactivity.d.ts +16 -1
- package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/source-map.d.ts.map +1 -1
- package/dist/ir-to-client-js/types.d.ts +2 -0
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +26 -0
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/opaque-local-accessor.d.ts +27 -0
- package/dist/opaque-local-accessor.d.ts.map +1 -0
- package/dist/to-locale-date-lowering.d.ts +7 -4
- package/dist/to-locale-date-lowering.d.ts.map +1 -1
- package/dist/types.d.ts +53 -17
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +724 -4564
- package/src/__tests__/binding-scope-ratchet.test.ts +21 -10
- package/src/__tests__/branch-local-in-raw-capture.test.ts +1 -1
- package/src/__tests__/branch-local-same-name-collision.test.ts +9 -9
- package/src/__tests__/client-js-generation.test.ts +175 -0
- package/src/__tests__/component-root-scope-comment.test.ts +106 -0
- package/src/__tests__/doc-examples.test.ts +12 -22
- package/src/__tests__/event-handler-name.test.ts +74 -0
- package/src/__tests__/flatmap-segments.test.ts +11 -9
- package/src/__tests__/format-date-refusal.test.ts +134 -0
- package/src/__tests__/fragment-wrapped-conditional-return-branch.test.ts +132 -0
- package/src/__tests__/hydrate-template.test.ts +9 -1
- package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +2 -2
- package/src/__tests__/issue-3009-loop-branch-ref.test.ts +80 -0
- package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +213 -0
- package/src/__tests__/issue-3113-init-fn-name-collision.test.ts +133 -0
- package/src/__tests__/jsx-return-type-assertion.test.ts +2 -2
- package/src/__tests__/lazy-row-eligibility.test.ts +5 -4
- package/src/__tests__/loop-row-child-children-reactive-attrs.test.ts +108 -0
- package/src/__tests__/loop-row-child-text-children-markers.test.ts +158 -0
- package/src/__tests__/opaque-local-accessor.test.ts +90 -0
- package/src/__tests__/profiler-e2e.test.ts +13 -8
- package/src/__tests__/ref-attr-absent-at-ssr.test.ts +472 -0
- package/src/__tests__/ssr-portal-ref-callback.test.ts +217 -0
- package/src/adapters/env-signal.ts +2 -21
- package/src/adapters/interface.ts +13 -0
- package/src/analyzer.ts +40 -28
- package/src/builtin-lowering-plugins.ts +0 -2
- package/src/compiler.ts +24 -0
- package/src/component-root-scope-comment.ts +59 -0
- package/src/errors.ts +44 -1
- package/src/event-handler-name.ts +14 -0
- package/src/expression-parser.ts +41 -17
- package/src/format-date-refusal.ts +266 -0
- package/src/index.ts +13 -3
- package/src/ir-to-client-js/child-components.ts +8 -3
- package/src/ir-to-client-js/collect-elements.ts +29 -13
- package/src/ir-to-client-js/control-flow/plan/build-branch-loop.ts +2 -4
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +67 -5
- package/src/ir-to-client-js/control-flow/plan/build-composite-loop.ts +3 -6
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +31 -1
- package/src/ir-to-client-js/control-flow/plan/build-loop.ts +23 -11
- package/src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts +5 -0
- package/src/ir-to-client-js/control-flow/plan/lazy-conditional.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/lazy-row-eligibility.ts +3 -3
- package/src/ir-to-client-js/control-flow/plan/loop-child-arm.ts +11 -0
- package/src/ir-to-client-js/control-flow/plan/loop.ts +51 -10
- package/src/ir-to-client-js/control-flow/shared.ts +78 -5
- package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +25 -12
- package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +22 -1
- package/src/ir-to-client-js/emit-reactive.ts +140 -10
- package/src/ir-to-client-js/emit-registration.ts +2 -2
- package/src/ir-to-client-js/generate-init.ts +2 -2
- package/src/ir-to-client-js/html-template.ts +41 -19
- package/src/ir-to-client-js/index.ts +4 -3
- package/src/ir-to-client-js/reactivity.ts +28 -17
- package/src/ir-to-client-js/source-map.ts +2 -1
- package/src/ir-to-client-js/types.ts +2 -0
- package/src/ir-to-client-js/utils.ts +34 -0
- package/src/jsx-to-ir.ts +737 -49
- package/src/opaque-local-accessor.ts +44 -0
- package/src/rich-type-refusal.ts +1 -1
- package/src/to-locale-date-lowering.ts +7 -4
- package/src/types.ts +51 -15
- package/dist/format-date-lowering.d.ts +0 -30
- package/dist/format-date-lowering.d.ts.map +0 -1
- package/src/__tests__/create-selector.test.ts +0 -124
- package/src/__tests__/format-date-lowering.test.ts +0 -125
- package/src/format-date-lowering.ts +0 -55
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Diagnostic tests for BF063 (`REF_ATTR_ABSENT_AT_SSR`).
|
|
3
|
+
*
|
|
4
|
+
* A `ref` callback never runs at SSR, so an attribute it writes
|
|
5
|
+
* unconditionally on mount — and that the element's own JSX never renders —
|
|
6
|
+
* is always missing from the server HTML and always added by hydration: a
|
|
7
|
+
* visible snap. The refusal is narrowed to that decidable shape; every
|
|
8
|
+
* exemption below is a shape where the divergence is NOT certain (or the
|
|
9
|
+
* author opted out), and must keep compiling clean.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { describe, test, expect } from 'bun:test'
|
|
13
|
+
import { compileJSX } from '../compiler'
|
|
14
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
15
|
+
import { ErrorCodes } from '../errors'
|
|
16
|
+
|
|
17
|
+
const adapter = new TestAdapter()
|
|
18
|
+
|
|
19
|
+
const DEFAULT_IMPORTS = `import { createSignal, createEffect, onMount } from '@barefootjs/client'`
|
|
20
|
+
|
|
21
|
+
function bf063(body: string, jsx: string, imports = DEFAULT_IMPORTS) {
|
|
22
|
+
const source = `
|
|
23
|
+
'use client'
|
|
24
|
+
${imports}
|
|
25
|
+
export function Comp(props: { delay?: number; rest?: Record<string, string> }) {
|
|
26
|
+
const [open, setOpen] = createSignal(false)
|
|
27
|
+
${body}
|
|
28
|
+
return ${jsx}
|
|
29
|
+
}
|
|
30
|
+
`
|
|
31
|
+
const result = compileJSX(source, 'Comp.tsx', { adapter })
|
|
32
|
+
return result.errors.filter(e => e.code === ErrorCodes.REF_ATTR_ABSENT_AT_SSR)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
describe('BF063 — ref callback writes an attribute the JSX never renders', () => {
|
|
36
|
+
describe('fires', () => {
|
|
37
|
+
test('setAttribute at the top level of a named ref callback', () => {
|
|
38
|
+
const errors = bf063(
|
|
39
|
+
`const handleMount = (el: HTMLElement) => { el.setAttribute('data-mounted', '1') }`,
|
|
40
|
+
`<div ref={handleMount}>x</div>`,
|
|
41
|
+
)
|
|
42
|
+
expect(errors).toHaveLength(1)
|
|
43
|
+
expect(errors[0].severity).toBe('error')
|
|
44
|
+
expect(errors[0].message).toContain("'data-mounted'")
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('dataset write inside a directly nested createEffect (camelCase key → data-kebab)', () => {
|
|
48
|
+
const errors = bf063(
|
|
49
|
+
`const handleMount = (el: HTMLElement) => {
|
|
50
|
+
createEffect(() => { el.dataset.nmOpenDelay = String(props.delay ?? 200) })
|
|
51
|
+
}`,
|
|
52
|
+
`<nav ref={handleMount}>x</nav>`,
|
|
53
|
+
)
|
|
54
|
+
expect(errors).toHaveLength(1)
|
|
55
|
+
expect(errors[0].message).toContain("'data-nm-open-delay'")
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('inline arrow ref, concise body', () => {
|
|
59
|
+
const errors = bf063('', `<div ref={(el) => el.setAttribute('aria-busy', 'true')}>x</div>`)
|
|
60
|
+
expect(errors).toHaveLength(1)
|
|
61
|
+
expect(errors[0].message).toContain("'aria-busy'")
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('function declaration ref, onMount body, element-access dataset key', () => {
|
|
65
|
+
const errors = bf063(
|
|
66
|
+
`function handleMount(el: HTMLElement) {
|
|
67
|
+
onMount(() => { el.dataset['ready'] = 'yes' })
|
|
68
|
+
}`,
|
|
69
|
+
`<div ref={handleMount}>x</div>`,
|
|
70
|
+
)
|
|
71
|
+
expect(errors).toHaveLength(1)
|
|
72
|
+
expect(errors[0].message).toContain("'data-ready'")
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('self-closing element', () => {
|
|
76
|
+
const errors = bf063(
|
|
77
|
+
`const handleMount = (el: HTMLInputElement) => { el.setAttribute('data-kind', 'x') }`,
|
|
78
|
+
`<input ref={handleMount} />`,
|
|
79
|
+
)
|
|
80
|
+
expect(errors).toHaveLength(1)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('reports each attribute once, only the ones the JSX does not render', () => {
|
|
84
|
+
const errors = bf063(
|
|
85
|
+
`const handleMount = (el: HTMLElement) => {
|
|
86
|
+
el.setAttribute('data-a', '1')
|
|
87
|
+
createEffect(() => {
|
|
88
|
+
el.setAttribute('data-a', open() ? '1' : '0')
|
|
89
|
+
el.setAttribute('data-b', '1')
|
|
90
|
+
el.setAttribute('data-state', open() ? 'open' : 'closed')
|
|
91
|
+
})
|
|
92
|
+
}`,
|
|
93
|
+
`<div data-state="closed" ref={handleMount}>x</div>`,
|
|
94
|
+
)
|
|
95
|
+
expect(errors.map(e => e.message.match(/'([^']+)'/)![1])).toEqual(['data-a', 'data-b'])
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('message and suggestion name the attribute and both escapes', () => {
|
|
99
|
+
const [error] = bf063(
|
|
100
|
+
`const handleMount = (el: HTMLElement) => { el.setAttribute('data-mounted', '1') }`,
|
|
101
|
+
`<div ref={handleMount}>x</div>`,
|
|
102
|
+
)
|
|
103
|
+
expect(error.message).toContain('never runs at SSR')
|
|
104
|
+
expect(error.suggestion?.message).toContain('data-mounted={…}')
|
|
105
|
+
expect(error.suggestion?.message).toContain('/* @client */')
|
|
106
|
+
expect(error.suggestion?.escape).toEqual([{ kind: 'rewrite' }, { kind: 'client-directive' }])
|
|
107
|
+
// Located at the write, not at the element.
|
|
108
|
+
expect(error.loc.start.line).toBe(6)
|
|
109
|
+
})
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
describe('exempt', () => {
|
|
113
|
+
test('attribute already rendered by the JSX (literal later overwritten by an effect)', () => {
|
|
114
|
+
expect(
|
|
115
|
+
bf063(
|
|
116
|
+
`const handleMount = (el: HTMLElement) => {
|
|
117
|
+
createEffect(() => { el.setAttribute('aria-expanded', String(open())) })
|
|
118
|
+
}`,
|
|
119
|
+
`<button aria-expanded="false" ref={handleMount}>x</button>`,
|
|
120
|
+
),
|
|
121
|
+
).toEqual([])
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('attribute rendered from props (the rewrite escape)', () => {
|
|
125
|
+
expect(
|
|
126
|
+
bf063(
|
|
127
|
+
`const handleMount = (el: HTMLElement) => {
|
|
128
|
+
createEffect(() => { el.dataset.nmOpenDelay = String(props.delay ?? 200) })
|
|
129
|
+
}`,
|
|
130
|
+
`<nav data-nm-open-delay={String(props.delay ?? 200)} ref={handleMount}>x</nav>`,
|
|
131
|
+
),
|
|
132
|
+
).toEqual([])
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test('conditional write', () => {
|
|
136
|
+
expect(
|
|
137
|
+
bf063(
|
|
138
|
+
`const handleMount = (el: HTMLElement) => {
|
|
139
|
+
if (props.delay) el.setAttribute('data-delayed', '1')
|
|
140
|
+
createEffect(() => { if (open()) el.setAttribute('data-open', '') })
|
|
141
|
+
}`,
|
|
142
|
+
`<div ref={handleMount}>x</div>`,
|
|
143
|
+
),
|
|
144
|
+
).toEqual([])
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('write inside an event listener or timer closure', () => {
|
|
148
|
+
expect(
|
|
149
|
+
bf063(
|
|
150
|
+
`const handleMount = (el: HTMLElement) => {
|
|
151
|
+
el.addEventListener('click', () => { el.setAttribute('data-clicked', '1') })
|
|
152
|
+
setTimeout(() => { el.dataset.late = '1' }, 10)
|
|
153
|
+
}`,
|
|
154
|
+
`<div ref={handleMount}>x</div>`,
|
|
155
|
+
),
|
|
156
|
+
).toEqual([])
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('write to another node', () => {
|
|
160
|
+
expect(
|
|
161
|
+
bf063(
|
|
162
|
+
`const handleMount = (el: HTMLElement) => {
|
|
163
|
+
const root = el.closest('nav') as HTMLElement
|
|
164
|
+
root.dataset.owner = '1'
|
|
165
|
+
el.querySelector('span')?.setAttribute('data-inner', '1')
|
|
166
|
+
}`,
|
|
167
|
+
`<div ref={handleMount}><span>x</span></div>`,
|
|
168
|
+
),
|
|
169
|
+
).toEqual([])
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
test('element with a spread', () => {
|
|
173
|
+
expect(
|
|
174
|
+
bf063(
|
|
175
|
+
`const handleMount = (el: HTMLElement) => { el.setAttribute('data-mounted', '1') }`,
|
|
176
|
+
`<div {...props.rest} ref={handleMount}>x</div>`,
|
|
177
|
+
),
|
|
178
|
+
).toEqual([])
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
test('leading /* @client */ on the ref expression', () => {
|
|
182
|
+
expect(
|
|
183
|
+
bf063(
|
|
184
|
+
`const handleMount = (el: HTMLElement) => { el.setAttribute('data-mounted', '1') }`,
|
|
185
|
+
`<div ref={/* @client */ handleMount}>x</div>`,
|
|
186
|
+
),
|
|
187
|
+
).toEqual([])
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
test('a createEffect nested deeper than one level, or one shadowing the param', () => {
|
|
191
|
+
expect(
|
|
192
|
+
bf063(
|
|
193
|
+
`const handleMount = (el: HTMLElement) => {
|
|
194
|
+
createEffect(() => { createEffect(() => { el.setAttribute('data-deep', '1') }) })
|
|
195
|
+
createEffect((el: HTMLElement) => { el.setAttribute('data-shadow', '1') })
|
|
196
|
+
}`,
|
|
197
|
+
`<div ref={handleMount}>x</div>`,
|
|
198
|
+
),
|
|
199
|
+
).toEqual([])
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
test('a dynamic attribute name, or an opaque (non-local) ref callback', () => {
|
|
203
|
+
expect(
|
|
204
|
+
bf063(
|
|
205
|
+
`const name = 'data-x'
|
|
206
|
+
const handleMount = (el: HTMLElement) => { el.setAttribute(name, '1') }`,
|
|
207
|
+
`<div ref={handleMount}><span ref={props.rest as never}>x</span></div>`,
|
|
208
|
+
),
|
|
209
|
+
).toEqual([])
|
|
210
|
+
})
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
describe('early exits make every later write conditional', () => {
|
|
214
|
+
test('an if-guarded return in the ref body', () => {
|
|
215
|
+
expect(
|
|
216
|
+
bf063(
|
|
217
|
+
`const handleMount = (el: HTMLElement) => {
|
|
218
|
+
if (!props.delay) return
|
|
219
|
+
el.setAttribute('data-x', '1')
|
|
220
|
+
}`,
|
|
221
|
+
`<div ref={handleMount}>x</div>`,
|
|
222
|
+
),
|
|
223
|
+
).toEqual([])
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
test('an if-guarded return inside a nested createEffect', () => {
|
|
227
|
+
expect(
|
|
228
|
+
bf063(
|
|
229
|
+
`const handleMount = (el: HTMLElement) => {
|
|
230
|
+
createEffect(() => {
|
|
231
|
+
if (!open()) return
|
|
232
|
+
el.setAttribute('data-x', '1')
|
|
233
|
+
})
|
|
234
|
+
}`,
|
|
235
|
+
`<div ref={handleMount}>x</div>`,
|
|
236
|
+
),
|
|
237
|
+
).toEqual([])
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
test('a throw, or a try / loop / switch that can return, before the write', () => {
|
|
241
|
+
for (const guard of [
|
|
242
|
+
`if (!props.delay) { throw new Error('no delay') }`,
|
|
243
|
+
`try { if (!props.delay) return } catch {}`,
|
|
244
|
+
`for (const k of Object.keys(props)) { if (k === 'x') return }`,
|
|
245
|
+
`switch (props.delay) { case 0: return }`,
|
|
246
|
+
`return`,
|
|
247
|
+
]) {
|
|
248
|
+
expect(
|
|
249
|
+
bf063(
|
|
250
|
+
`const handleMount = (el: HTMLElement) => {
|
|
251
|
+
${guard}
|
|
252
|
+
el.setAttribute('data-x', '1')
|
|
253
|
+
}`,
|
|
254
|
+
`<div ref={handleMount}>x</div>`,
|
|
255
|
+
),
|
|
256
|
+
).toEqual([])
|
|
257
|
+
}
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
test('an early exit inside a nested mount body ends collection for the rest of the ref body too', () => {
|
|
261
|
+
expect(
|
|
262
|
+
bf063(
|
|
263
|
+
`const handleMount = (el: HTMLElement) => {
|
|
264
|
+
onMount(() => { if (!open()) throw new Error('closed') })
|
|
265
|
+
el.setAttribute('data-x', '1')
|
|
266
|
+
}`,
|
|
267
|
+
`<div ref={handleMount}>x</div>`,
|
|
268
|
+
),
|
|
269
|
+
).toEqual([])
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
test('writes before the guard, and after a guard that cannot exit, still fire', () => {
|
|
273
|
+
const errors = bf063(
|
|
274
|
+
`const handleMount = (el: HTMLElement) => {
|
|
275
|
+
el.setAttribute('data-before', '1')
|
|
276
|
+
if (props.delay) el.dataset.delayed = '1'
|
|
277
|
+
el.setAttribute('data-middle', '1')
|
|
278
|
+
if (!open()) return
|
|
279
|
+
el.setAttribute('data-after', '1')
|
|
280
|
+
}`,
|
|
281
|
+
`<div ref={handleMount}>x</div>`,
|
|
282
|
+
)
|
|
283
|
+
expect(errors.map(e => e.message.match(/'([^']+)'/)![1])).toEqual(['data-before', 'data-middle'])
|
|
284
|
+
})
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
describe('a later removal cancels the write', () => {
|
|
288
|
+
test('removeAttribute / delete dataset after the write', () => {
|
|
289
|
+
expect(
|
|
290
|
+
bf063(
|
|
291
|
+
`const handleMount = (el: HTMLElement) => {
|
|
292
|
+
el.setAttribute('data-a', '1')
|
|
293
|
+
el.dataset.b = '1'
|
|
294
|
+
createEffect(() => {
|
|
295
|
+
el.removeAttribute('data-a')
|
|
296
|
+
delete el.dataset.b
|
|
297
|
+
})
|
|
298
|
+
}`,
|
|
299
|
+
`<div ref={handleMount}>x</div>`,
|
|
300
|
+
),
|
|
301
|
+
).toEqual([])
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
test('a conditional removal makes the write uncertain', () => {
|
|
305
|
+
expect(
|
|
306
|
+
bf063(
|
|
307
|
+
`const handleMount = (el: HTMLElement) => {
|
|
308
|
+
el.setAttribute('data-a', '1')
|
|
309
|
+
if (!open()) el.removeAttribute('data-a')
|
|
310
|
+
}`,
|
|
311
|
+
`<div ref={handleMount}>x</div>`,
|
|
312
|
+
),
|
|
313
|
+
).toEqual([])
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
test('a removal before the write does not cancel it', () => {
|
|
317
|
+
const errors = bf063(
|
|
318
|
+
`const handleMount = (el: HTMLElement) => {
|
|
319
|
+
el.removeAttribute('data-a')
|
|
320
|
+
el.setAttribute('data-a', '1')
|
|
321
|
+
}`,
|
|
322
|
+
`<div ref={handleMount}>x</div>`,
|
|
323
|
+
)
|
|
324
|
+
expect(errors).toHaveLength(1)
|
|
325
|
+
expect(errors[0].loc.start.line).toBe(8)
|
|
326
|
+
})
|
|
327
|
+
})
|
|
328
|
+
|
|
329
|
+
describe('client-only subtrees never render at SSR', () => {
|
|
330
|
+
test('/* @client */ && and ternary children', () => {
|
|
331
|
+
for (const child of [
|
|
332
|
+
`{/* @client */ open() && <span ref={handleMount}>a</span>}`,
|
|
333
|
+
`{/* @client */ open() ? <span ref={handleMount}>a</span> : null}`,
|
|
334
|
+
`{/* @client */ [1, 2].map(n => <span key={n} ref={handleMount}>{n}</span>)}`,
|
|
335
|
+
]) {
|
|
336
|
+
expect(
|
|
337
|
+
bf063(
|
|
338
|
+
`const handleMount = (el: HTMLElement) => { el.setAttribute('data-x', '1') }`,
|
|
339
|
+
`<div>${child}</div>`,
|
|
340
|
+
),
|
|
341
|
+
).toEqual([])
|
|
342
|
+
}
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
test('the same element in an SSR-rendered branch still fires', () => {
|
|
346
|
+
const errors = bf063(
|
|
347
|
+
`const handleMount = (el: HTMLElement) => { el.setAttribute('data-x', '1') }`,
|
|
348
|
+
`<div>{open() && <span ref={handleMount}>a</span>}</div>`,
|
|
349
|
+
)
|
|
350
|
+
expect(errors).toHaveLength(1)
|
|
351
|
+
})
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
describe('a local declaration shadowing the element parameter', () => {
|
|
355
|
+
test('const in a nested createEffect body', () => {
|
|
356
|
+
expect(
|
|
357
|
+
bf063(
|
|
358
|
+
`const handleMount = (el: HTMLElement) => {
|
|
359
|
+
createEffect(() => {
|
|
360
|
+
const el = document.body
|
|
361
|
+
el.setAttribute('data-x', '1')
|
|
362
|
+
})
|
|
363
|
+
}`,
|
|
364
|
+
`<div ref={handleMount}>x</div>`,
|
|
365
|
+
),
|
|
366
|
+
).toEqual([])
|
|
367
|
+
})
|
|
368
|
+
|
|
369
|
+
test('var / function / destructuring in the ref body', () => {
|
|
370
|
+
for (const decl of [
|
|
371
|
+
`var el = document.body`,
|
|
372
|
+
`function el() { return document.body }`,
|
|
373
|
+
`const { el } = { el: document.body }`,
|
|
374
|
+
`const [el] = [document.body]`,
|
|
375
|
+
]) {
|
|
376
|
+
expect(
|
|
377
|
+
bf063(
|
|
378
|
+
`const handleMount = (el: HTMLElement) => {
|
|
379
|
+
el.setAttribute('data-x', '1')
|
|
380
|
+
${decl}
|
|
381
|
+
}`,
|
|
382
|
+
`<div ref={handleMount}>x</div>`,
|
|
383
|
+
),
|
|
384
|
+
).toEqual([])
|
|
385
|
+
}
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
test('a shadow in a nested body leaves the ref body writes intact', () => {
|
|
389
|
+
const errors = bf063(
|
|
390
|
+
`const handleMount = (el: HTMLElement) => {
|
|
391
|
+
createEffect(() => {
|
|
392
|
+
let el = document.body
|
|
393
|
+
el.setAttribute('data-inner', '1')
|
|
394
|
+
})
|
|
395
|
+
el.setAttribute('data-outer', '1')
|
|
396
|
+
}`,
|
|
397
|
+
`<div ref={handleMount}>x</div>`,
|
|
398
|
+
)
|
|
399
|
+
expect(errors.map(e => e.message.match(/'([^']+)'/)![1])).toEqual(['data-outer'])
|
|
400
|
+
})
|
|
401
|
+
})
|
|
402
|
+
|
|
403
|
+
describe('named ref resolution', () => {
|
|
404
|
+
test('a handler declared in the component body, used inside a .map() row', () => {
|
|
405
|
+
const errors = bf063(
|
|
406
|
+
`const handleMount = (el: HTMLElement) => { el.setAttribute('data-x', '1') }`,
|
|
407
|
+
`<ul>{[1, 2].map(n => <li key={n} ref={handleMount}>{n}</li>)}</ul>`,
|
|
408
|
+
)
|
|
409
|
+
expect(errors).toHaveLength(1)
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
test('a row-arrow parameter of the same name shadows the component handler', () => {
|
|
413
|
+
expect(
|
|
414
|
+
bf063(
|
|
415
|
+
`const handleMount = (el: HTMLElement) => { el.setAttribute('data-x', '1') }`,
|
|
416
|
+
`<ul>{[(e: HTMLElement) => e].map(handleMount => <li ref={handleMount}>x</li>)}</ul>`,
|
|
417
|
+
),
|
|
418
|
+
).toEqual([])
|
|
419
|
+
})
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
describe('a handler shared by several elements', () => {
|
|
423
|
+
test('reports once per write, naming every element that lacks the attribute', () => {
|
|
424
|
+
const errors = bf063(
|
|
425
|
+
`const handleMount = (el: HTMLElement) => { el.setAttribute('data-x', '1') }`,
|
|
426
|
+
`<div>
|
|
427
|
+
<span ref={handleMount}>a</span>
|
|
428
|
+
<b data-x="1" ref={handleMount}>b</b>
|
|
429
|
+
<i ref={handleMount}>c</i>
|
|
430
|
+
</div>`,
|
|
431
|
+
)
|
|
432
|
+
expect(errors).toHaveLength(1)
|
|
433
|
+
expect(errors[0].message).toContain('<span> at 8:10')
|
|
434
|
+
expect(errors[0].message).toContain('<i> at 10:10')
|
|
435
|
+
expect(errors[0].message).not.toContain('<b>')
|
|
436
|
+
})
|
|
437
|
+
|
|
438
|
+
test('a single element is named in the message', () => {
|
|
439
|
+
const [error] = bf063(
|
|
440
|
+
`const handleMount = (el: HTMLElement) => { el.setAttribute('data-x', '1') }`,
|
|
441
|
+
`<section ref={handleMount}>x</section>`,
|
|
442
|
+
)
|
|
443
|
+
expect(error.message).toContain('<section> at 7:13')
|
|
444
|
+
})
|
|
445
|
+
})
|
|
446
|
+
|
|
447
|
+
describe('mount primitives resolve through the import bindings', () => {
|
|
448
|
+
test('an aliased createEffect import is recognized', () => {
|
|
449
|
+
const errors = bf063(
|
|
450
|
+
`const handleMount = (el: HTMLElement) => {
|
|
451
|
+
ce(() => { el.setAttribute('data-x', '1') })
|
|
452
|
+
}`,
|
|
453
|
+
`<div ref={handleMount}>x</div>`,
|
|
454
|
+
`import { createSignal, createEffect as ce } from '@barefootjs/client'`,
|
|
455
|
+
)
|
|
456
|
+
expect(errors).toHaveLength(1)
|
|
457
|
+
})
|
|
458
|
+
|
|
459
|
+
test('a user-defined onMount is not', () => {
|
|
460
|
+
expect(
|
|
461
|
+
bf063(
|
|
462
|
+
`const onMount = (fn: () => void) => { setTimeout(fn, 100) }
|
|
463
|
+
const handleMount = (el: HTMLElement) => {
|
|
464
|
+
onMount(() => { el.setAttribute('data-x', '1') })
|
|
465
|
+
}`,
|
|
466
|
+
`<div ref={handleMount}>x</div>`,
|
|
467
|
+
`import { createSignal } from '@barefootjs/client'`,
|
|
468
|
+
),
|
|
469
|
+
).toEqual([])
|
|
470
|
+
})
|
|
471
|
+
})
|
|
472
|
+
})
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `isSsrPortalRefCallback` (#3059) — structural (AST-only) recognition of
|
|
3
|
+
* the `ref`-callback SSR-portal pattern the Dialog/DropdownMenu/Popover/
|
|
4
|
+
* Portal primitives use: a `ref` bound to a local named callback whose
|
|
5
|
+
* body directly calls `createPortal(<own param>, document.body, {
|
|
6
|
+
* ownerScope: … })`. A match sets `IRElement.ssrPortalOwnerScope`, which
|
|
7
|
+
* the Hono adapter (the only one with an SSR portal outlet so far) uses
|
|
8
|
+
* to route the element through its `<BfPortals />` outlet instead of
|
|
9
|
+
* rendering it inline.
|
|
10
|
+
*/
|
|
11
|
+
import { describe, test, expect } from 'bun:test'
|
|
12
|
+
import { compileJSX, analyzeComponent, jsxToIR, type ComponentIR, type IRElement, type IRNode } from '../index'
|
|
13
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
14
|
+
|
|
15
|
+
function root(src: string): IRNode {
|
|
16
|
+
const result = compileJSX(src.trimStart(), 'T.tsx', { adapter: new TestAdapter(), outputIR: true })
|
|
17
|
+
const ir = JSON.parse(result.files.find(f => f.type === 'ir')!.content) as ComponentIR
|
|
18
|
+
return ir.root
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Like {@link root}, but targets ONE named component in a multi-export
|
|
22
|
+
* file — `compileJSX`'s single-IR output only carries one component's
|
|
23
|
+
* tree, so a same-file scoping test needs to pick which. */
|
|
24
|
+
function rootFor(src: string, componentName: string): IRNode {
|
|
25
|
+
const ctx = analyzeComponent(src.trimStart(), 'T.tsx', componentName)
|
|
26
|
+
const node = jsxToIR(ctx)
|
|
27
|
+
if (!node) throw new Error(`jsxToIR returned null for ${componentName}`)
|
|
28
|
+
return node
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function flaggedRefs(node: IRNode): string[] {
|
|
32
|
+
const out: string[] = []
|
|
33
|
+
const visit = (n: IRNode): void => {
|
|
34
|
+
if (n.type === 'element') {
|
|
35
|
+
if ((n as IRElement).ssrPortalOwnerScope) out.push((n as IRElement).ref!)
|
|
36
|
+
for (const c of (n as IRElement).children) visit(c)
|
|
37
|
+
} else if ('children' in n && Array.isArray((n as { children?: IRNode[] }).children)) {
|
|
38
|
+
for (const c of (n as { children: IRNode[] }).children) visit(c)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
visit(node)
|
|
42
|
+
return out
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('isSsrPortalRefCallback (#3059)', () => {
|
|
46
|
+
test('flags an element whose ref callback directly calls createPortal(el, document.body, { ownerScope })', () => {
|
|
47
|
+
const ir = root(`
|
|
48
|
+
'use client'
|
|
49
|
+
import { createSignal, createPortal, isSSRPortal } from '@barefootjs/client'
|
|
50
|
+
export function P() {
|
|
51
|
+
const [open, setOpen] = createSignal(false)
|
|
52
|
+
const moveToBody = (el: HTMLElement) => {
|
|
53
|
+
if (el && el.parentNode !== document.body && !isSSRPortal(el)) {
|
|
54
|
+
const ownerScope = el.closest('[bf-s]') ?? undefined
|
|
55
|
+
createPortal(el, document.body, { ownerScope })
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return (
|
|
59
|
+
<div>
|
|
60
|
+
<div data-testid="content" hidden={!open()} ref={moveToBody} />
|
|
61
|
+
</div>
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
`)
|
|
65
|
+
expect(flaggedRefs(ir)).toEqual(['moveToBody'])
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('does not flag a ref callback with no createPortal call', () => {
|
|
69
|
+
const ir = root(`
|
|
70
|
+
'use client'
|
|
71
|
+
export function P() {
|
|
72
|
+
const handleMount = (el: HTMLElement) => {
|
|
73
|
+
el.focus()
|
|
74
|
+
}
|
|
75
|
+
return <input ref={handleMount} />
|
|
76
|
+
}
|
|
77
|
+
`)
|
|
78
|
+
expect(flaggedRefs(ir)).toEqual([])
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test('does not flag createPortal(el, document.body) with no ownerScope option', () => {
|
|
82
|
+
// Without `ownerScope`, the client `createPortal` never stamps
|
|
83
|
+
// `bf-po` — SSR placing the element at the outlet and adding
|
|
84
|
+
// `bf-po` would itself be a NEW SSR/hydration mismatch.
|
|
85
|
+
const ir = root(`
|
|
86
|
+
'use client'
|
|
87
|
+
import { createPortal } from '@barefootjs/client'
|
|
88
|
+
export function P() {
|
|
89
|
+
const moveToBody = (el: HTMLElement) => {
|
|
90
|
+
createPortal(el, document.body)
|
|
91
|
+
}
|
|
92
|
+
return <div ref={moveToBody} />
|
|
93
|
+
}
|
|
94
|
+
`)
|
|
95
|
+
expect(flaggedRefs(ir)).toEqual([])
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('flags createPortal deferred through queueMicrotask — same callback, not a separate helper', () => {
|
|
99
|
+
// Regression pin: `SelectContent` (`ui/components/ui/select/index.tsx`)
|
|
100
|
+
// defers its `createPortal` call through
|
|
101
|
+
// `queueMicrotask(() => createPortal(el, document.body, { ownerScope }))`
|
|
102
|
+
// specifically so sibling `initChild` calls can find their elements
|
|
103
|
+
// before the element leaves its DOM subtree. This must still match:
|
|
104
|
+
// the guard that decides WHETHER to call runs synchronously inside
|
|
105
|
+
// the `ref` callback itself, only the call is deferred — the "DIRECT
|
|
106
|
+
// call only" rule (see `containsSsrPortalPlacementCall`'s docstring)
|
|
107
|
+
// is about not crossing into a SEPARATELY-DECLARED helper function's
|
|
108
|
+
// own body, not about a same-callback scheduling deferral like this
|
|
109
|
+
// one. An earlier draft over-corrected this by stopping at ANY
|
|
110
|
+
// nested function/arrow boundary, which wrongly excluded exactly
|
|
111
|
+
// this shape — confirmed against the real fixture pipeline
|
|
112
|
+
// (`generate-expected-html.ts`), where `select`'s own reference
|
|
113
|
+
// output flags its Content element the same as `combobox`'s (whose
|
|
114
|
+
// `createPortal` call is direct, not deferred).
|
|
115
|
+
const ir = root(`
|
|
116
|
+
'use client'
|
|
117
|
+
import { createPortal, isSSRPortal } from '@barefootjs/client'
|
|
118
|
+
export function P() {
|
|
119
|
+
const handleMount = (el: HTMLElement) => {
|
|
120
|
+
if (el.parentNode !== document.body && !isSSRPortal(el)) {
|
|
121
|
+
const ownerScope = el.closest('[bf-s]') ?? undefined
|
|
122
|
+
queueMicrotask(() => createPortal(el, document.body, { ownerScope }))
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return <div ref={handleMount} />
|
|
126
|
+
}
|
|
127
|
+
`)
|
|
128
|
+
expect(flaggedRefs(ir)).toEqual(['handleMount'])
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('does not flag createPortal targeting a DIFFERENT container', () => {
|
|
132
|
+
const ir = root(`
|
|
133
|
+
'use client'
|
|
134
|
+
import { createPortal } from '@barefootjs/client'
|
|
135
|
+
export function P() {
|
|
136
|
+
const moveElsewhere = (el: HTMLElement) => {
|
|
137
|
+
const target = document.getElementById('portal-root')!
|
|
138
|
+
createPortal(el, target, { ownerScope: el })
|
|
139
|
+
}
|
|
140
|
+
return <div ref={moveElsewhere} />
|
|
141
|
+
}
|
|
142
|
+
`)
|
|
143
|
+
expect(flaggedRefs(ir)).toEqual([])
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
test('two sibling components with their own same-named, differently-behaved ref callback are scoped independently (no cross-closure false positive)', () => {
|
|
147
|
+
// Regression pin: an earlier whole-file scan for the callback's
|
|
148
|
+
// declaration matched the LAST `handleMount` in the file regardless
|
|
149
|
+
// of which component's closure the `ref` actually referenced — e.g.
|
|
150
|
+
// `DialogTrigger`'s own `handleMount` (unrelated to portals) getting
|
|
151
|
+
// misattributed to `DialogOverlay`'s `handleMount` (which portals)
|
|
152
|
+
// just because it was declared later in the same source file.
|
|
153
|
+
const src = `
|
|
154
|
+
'use client'
|
|
155
|
+
import { createPortal, isSSRPortal } from '@barefootjs/client'
|
|
156
|
+
export function Trigger() {
|
|
157
|
+
const handleMount = (el: HTMLElement) => {
|
|
158
|
+
el.dataset.state = 'closed'
|
|
159
|
+
}
|
|
160
|
+
return <button ref={handleMount} />
|
|
161
|
+
}
|
|
162
|
+
export function Overlay() {
|
|
163
|
+
const handleMount = (el: HTMLElement) => {
|
|
164
|
+
if (el.parentNode !== document.body && !isSSRPortal(el)) {
|
|
165
|
+
createPortal(el, document.body, { ownerScope: el })
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return <div ref={handleMount} />
|
|
169
|
+
}
|
|
170
|
+
`
|
|
171
|
+
// Trigger's own `handleMount` (declared FIRST in the file) must never
|
|
172
|
+
// be treated as a match just because a LATER same-named declaration
|
|
173
|
+
// elsewhere in the file happens to portal.
|
|
174
|
+
expect(flaggedRefs(rootFor(src, 'Trigger'))).toEqual([])
|
|
175
|
+
// Overlay's own `handleMount` is flagged from ITS OWN closure.
|
|
176
|
+
expect(flaggedRefs(rootFor(src, 'Overlay'))).toEqual(['handleMount'])
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
// BF063 resolves a `ref` name outward through every enclosing function
|
|
180
|
+
// scope; the portal recognition keeps the pre-BF063 limit (the callback
|
|
181
|
+
// must be declared in the ref's innermost function scope) so sharing the
|
|
182
|
+
// resolution does not widen what is recognized. These two tests pin that
|
|
183
|
+
// scope limit only. A portal element inside a `.map()` row is broken
|
|
184
|
+
// either way — the second test's row-declared callback is still
|
|
185
|
+
// recognized and still breaks the row's hydration — which is a
|
|
186
|
+
// pre-existing, separately tracked defect: the control pins today's
|
|
187
|
+
// behaviour as a known gap, not as intended behaviour.
|
|
188
|
+
test('does not flag a .map() row ref naming a portal callback declared in the component body', () => {
|
|
189
|
+
const ir = root(`
|
|
190
|
+
'use client'
|
|
191
|
+
import { createPortal } from '@barefootjs/client'
|
|
192
|
+
export function List(props: { rows: string[] }) {
|
|
193
|
+
const mountContent = (el: HTMLElement) => { createPortal(el, document.body, { ownerScope: 'x' }) }
|
|
194
|
+
return <ul>{props.rows.map(r => (<li key={r}><div ref={mountContent}>{r}</div></li>))}</ul>
|
|
195
|
+
}
|
|
196
|
+
`)
|
|
197
|
+
expect(flaggedRefs(ir)).toEqual([])
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
test('still flags a .map() row ref naming a portal callback declared in the row callback itself', () => {
|
|
201
|
+
const ir = root(`
|
|
202
|
+
'use client'
|
|
203
|
+
import { createPortal } from '@barefootjs/client'
|
|
204
|
+
export function List(props: { rows: string[] }) {
|
|
205
|
+
return (
|
|
206
|
+
<ul>
|
|
207
|
+
{props.rows.map(r => {
|
|
208
|
+
const mountContent = (el: HTMLElement) => { createPortal(el, document.body, { ownerScope: 'x' }) }
|
|
209
|
+
return <li key={r}><div ref={mountContent}>{r}</div></li>
|
|
210
|
+
})}
|
|
211
|
+
</ul>
|
|
212
|
+
)
|
|
213
|
+
}
|
|
214
|
+
`)
|
|
215
|
+
expect(flaggedRefs(ir)).toEqual(['mountContent'])
|
|
216
|
+
})
|
|
217
|
+
})
|