@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.
- package/dist/action-accessor.d.ts +159 -0
- package/dist/action-accessor.d.ts.map +1 -0
- package/dist/adapters/interface.d.ts +19 -0
- package/dist/adapters/interface.d.ts.map +1 -1
- package/dist/adapters/jsx-adapter.d.ts.map +1 -1
- package/dist/analyzer.d.ts +49 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/async-action-refusal.d.ts +45 -0
- package/dist/async-action-refusal.d.ts.map +1 -0
- package/dist/compiler.d.ts.map +1 -1
- package/dist/debug.d.ts +61 -5
- package/dist/debug.d.ts.map +1 -1
- package/dist/errors.d.ts +4 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/format-date-refusal.d.ts +2 -6
- package/dist/format-date-refusal.d.ts.map +1 -1
- package/dist/html-types.d.ts +5 -0
- package/dist/html-types.d.ts.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1261 -422
- package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
- package/dist/ir-to-client-js/collect-elements.d.ts +17 -1
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
- package/dist/ir-to-client-js/compute-scope.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/insert.d.ts.map +1 -1
- package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
- package/dist/ir-to-client-js/declaration-sort.d.ts +2 -1
- package/dist/ir-to-client-js/declaration-sort.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-module-level.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts +7 -5
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/ir-to-client-js/reactivity.d.ts +10 -2
- package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/types.d.ts +4 -0
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +52 -9
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/signal-initializer.d.ts +24 -0
- package/dist/signal-initializer.d.ts.map +1 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/template-position-walk.d.ts +68 -0
- package/dist/template-position-walk.d.ts.map +1 -0
- package/dist/types.d.ts +95 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +194 -4
- package/src/__tests__/action-accessor.test.ts +166 -0
- package/src/__tests__/adapter-output.test.ts +142 -0
- package/src/__tests__/create-mutation.test.ts +250 -0
- package/src/__tests__/create-query.test.ts +696 -0
- package/src/__tests__/csr-fragment-root-scope-shape.test.ts +156 -0
- package/src/__tests__/debug.test.ts +305 -0
- package/src/__tests__/doc-examples.test.ts +66 -3
- package/src/__tests__/ir-fragment-transparency.test.ts +23 -0
- package/src/__tests__/issue-3240-nested-static-map-event-delegation.test.ts +245 -0
- package/src/__tests__/jsx-text-linebreak-whitespace.test.ts +80 -0
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +445 -0
- package/src/__tests__/props-destructuring.test.ts +79 -4
- package/src/__tests__/reactive-factory-arity.test.ts +311 -0
- package/src/__tests__/ssr-portal-ref-callback.test.ts +5 -3
- package/src/action-accessor.ts +289 -0
- package/src/adapters/interface.ts +19 -0
- package/src/adapters/jsx-adapter.ts +47 -20
- package/src/analyzer.ts +593 -44
- package/src/async-action-refusal.ts +254 -0
- package/src/compiler.ts +81 -46
- package/src/debug.ts +351 -104
- package/src/errors.ts +39 -0
- package/src/format-date-refusal.ts +6 -125
- package/src/html-types.ts +14 -0
- package/src/index.ts +3 -1
- package/src/ir-to-client-js/build-references.ts +6 -2
- package/src/ir-to-client-js/collect-elements.ts +65 -4
- package/src/ir-to-client-js/compute-inlinability.ts +2 -1
- package/src/ir-to-client-js/compute-scope.ts +3 -1
- package/src/ir-to-client-js/control-flow/plan/build-insert.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -0
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +112 -3
- package/src/ir-to-client-js/control-flow/stringify/insert.ts +1 -1
- package/src/ir-to-client-js/csr-substitute.ts +11 -3
- package/src/ir-to-client-js/declaration-sort.ts +7 -3
- package/src/ir-to-client-js/emit-module-level.ts +7 -4
- package/src/ir-to-client-js/emit-registration.ts +5 -10
- package/src/ir-to-client-js/index.ts +7 -2
- package/src/ir-to-client-js/phases/ref-callbacks.ts +1 -1
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +16 -15
- package/src/ir-to-client-js/plan/declaration-emit.ts +7 -5
- package/src/ir-to-client-js/prop-handling.ts +4 -0
- package/src/ir-to-client-js/reactivity.ts +32 -1
- package/src/ir-to-client-js/stringify/declaration-emit.ts +14 -7
- package/src/ir-to-client-js/types.ts +4 -0
- package/src/ir-to-client-js/utils.ts +184 -21
- package/src/jsx-to-ir.ts +111 -14
- package/src/signal-initializer.ts +36 -0
- package/src/ssr-defaults.ts +10 -0
- package/src/ssr-seed-plan.ts +12 -1
- package/src/template-position-walk.ts +203 -0
- package/src/types.ts +99 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A fragment-rooted component's CSR output must reproduce the scope shape
|
|
3
|
+
* SSR renders for it. Two emission sites used to diverge (found by the
|
|
4
|
+
* nightly mutation sweep's `fragment-wrap` mutant, real-browser pins:
|
|
5
|
+
* `portal-fragment-root` / `loop-row-fragment-root-child` in
|
|
6
|
+
* `packages/adapter-tests/fixtures/`):
|
|
7
|
+
*
|
|
8
|
+
* 1. The template-only registration (`generateTemplateOnlyMount`, a
|
|
9
|
+
* stateless child with no init) declared neither `comment` nor
|
|
10
|
+
* `fragmentRoot`, so `renderChild()` stamped `bf-s` on the child's first
|
|
11
|
+
* element while SSR scoped it with a `<!--bf-scope:-->` comment pair.
|
|
12
|
+
* 2. An `ssrPortalOwnerScope` element is SSR-placed at the portal outlet
|
|
13
|
+
* with `bf-po="<own scope id>"`; on a CSR mount the owner came only from
|
|
14
|
+
* the ref callback's `el.closest('[bf-s]')`, which finds nothing for a
|
|
15
|
+
* fragment root. The init now stamps the same owner after the callback.
|
|
16
|
+
*/
|
|
17
|
+
import { describe, test, expect } from 'bun:test'
|
|
18
|
+
import { compileJSX } from '../index'
|
|
19
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
20
|
+
|
|
21
|
+
function clientJs(src: string): string {
|
|
22
|
+
const result = compileJSX(src.trimStart(), 'T.tsx', { adapter: new TestAdapter() })
|
|
23
|
+
const file = result.files.find(f => f.type === 'clientJs')
|
|
24
|
+
if (!file) throw new Error('no clientJs emitted')
|
|
25
|
+
return file.content
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function hydrateLine(js: string, key: string): string {
|
|
29
|
+
const line = js.split('\n').find(l => l.startsWith(`hydrate('${key}`))
|
|
30
|
+
if (!line) throw new Error(`no hydrate() registration for ${key}`)
|
|
31
|
+
return line
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
describe('template-only registration declares the fragment-root scope shape', () => {
|
|
35
|
+
test('a stateless fragment-rooted child registers comment + fragmentRoot', () => {
|
|
36
|
+
const js = clientJs(`
|
|
37
|
+
'use client'
|
|
38
|
+
import { createSignal } from '@barefootjs/client'
|
|
39
|
+
function Tag({ children }: { children?: any }) {
|
|
40
|
+
return <><span>{children}</span></>
|
|
41
|
+
}
|
|
42
|
+
export function Host() {
|
|
43
|
+
const [n, setN] = createSignal(0)
|
|
44
|
+
return <div>{['a', 'b'].map(x => <Tag key={x}>{x}</Tag>)}<button onClick={() => setN(n() + 1)}>{n()}</button></div>
|
|
45
|
+
}
|
|
46
|
+
`)
|
|
47
|
+
// Pin the template-only emitter (`generateTemplateOnlyMount`'s empty
|
|
48
|
+
// init): the init-bearing one already declared both flags, so a
|
|
49
|
+
// rerouted shape would pass the flag assertions without covering it.
|
|
50
|
+
expect(js).toContain('function initTag() {}')
|
|
51
|
+
const tag = hydrateLine(js, 'Tag')
|
|
52
|
+
expect(tag).toContain('init: initTag')
|
|
53
|
+
expect(tag).toContain('comment: true')
|
|
54
|
+
expect(tag).toContain('fragmentRoot: true')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('a stateless element-rooted child declares neither flag', () => {
|
|
58
|
+
const js = clientJs(`
|
|
59
|
+
'use client'
|
|
60
|
+
import { createSignal } from '@barefootjs/client'
|
|
61
|
+
function Tag({ children }: { children?: any }) {
|
|
62
|
+
return <span>{children}</span>
|
|
63
|
+
}
|
|
64
|
+
export function Host() {
|
|
65
|
+
const [n, setN] = createSignal(0)
|
|
66
|
+
return <div>{['a', 'b'].map(x => <Tag key={x}>{x}</Tag>)}<button onClick={() => setN(n() + 1)}>{n()}</button></div>
|
|
67
|
+
}
|
|
68
|
+
`)
|
|
69
|
+
expect(js).toContain('function initTag() {}')
|
|
70
|
+
const tag = hydrateLine(js, 'Tag')
|
|
71
|
+
expect(tag).not.toContain('comment: true')
|
|
72
|
+
expect(tag).not.toContain('fragmentRoot: true')
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('a stateless component-call root takes the init-bearing path and declares comment only', () => {
|
|
76
|
+
// `componentDefScopeFlags` gives both registration emitters one answer
|
|
77
|
+
// because this shape never reaches the template-only one: the root
|
|
78
|
+
// child call is a `childInits` entry, so `needsClientJs` is true.
|
|
79
|
+
const js = clientJs(`
|
|
80
|
+
'use client'
|
|
81
|
+
import { createSignal } from '@barefootjs/client'
|
|
82
|
+
function Inner({ children }: { children?: any }) {
|
|
83
|
+
return <span>{children}</span>
|
|
84
|
+
}
|
|
85
|
+
function Tag({ children }: { children?: any }) {
|
|
86
|
+
return <Inner>{children}</Inner>
|
|
87
|
+
}
|
|
88
|
+
export function Host() {
|
|
89
|
+
const [n, setN] = createSignal(0)
|
|
90
|
+
return <div>{['a', 'b'].map(x => <Tag key={x}>{x}</Tag>)}<button onClick={() => setN(n() + 1)}>{n()}</button></div>
|
|
91
|
+
}
|
|
92
|
+
`)
|
|
93
|
+
expect(js).not.toContain('function initTag() {}')
|
|
94
|
+
const tag = hydrateLine(js, 'Tag')
|
|
95
|
+
expect(tag).toContain('comment: true')
|
|
96
|
+
expect(tag).not.toContain('fragmentRoot: true')
|
|
97
|
+
})
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
const PORTAL_CALLBACK = `
|
|
101
|
+
const moveToBody = (el: HTMLElement) => {
|
|
102
|
+
if (el && el.parentNode !== document.body && !isSSRPortal(el)) {
|
|
103
|
+
const ownerScope = el.closest('[bf-s]') ?? undefined
|
|
104
|
+
createPortal(el, document.body, { ownerScope })
|
|
105
|
+
}
|
|
106
|
+
}`
|
|
107
|
+
|
|
108
|
+
describe('an SSR-portal ref element stamps its owner after the ref callback', () => {
|
|
109
|
+
test('top-level element: owner stamped right after the callback call', () => {
|
|
110
|
+
const js = clientJs(`
|
|
111
|
+
'use client'
|
|
112
|
+
import { createSignal, createPortal, isSSRPortal } from '@barefootjs/client'
|
|
113
|
+
export function P() {
|
|
114
|
+
const [open, setOpen] = createSignal(false)${PORTAL_CALLBACK}
|
|
115
|
+
return (
|
|
116
|
+
<>
|
|
117
|
+
<button onClick={() => setOpen(true)}>open</button>
|
|
118
|
+
<div className="panel" hidden={!open()} ref={moveToBody} />
|
|
119
|
+
</>
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
`)
|
|
123
|
+
expect(js).toMatch(/if \(_s\d+\) \{ \(moveToBody\)\(_s\d+\); if \(__scopeId\) _s\d+\.setAttribute\('bf-po', __scopeId\) \}/)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
test('conditional-branch element: owner stamped in the branch bindEvents too', () => {
|
|
127
|
+
const js = clientJs(`
|
|
128
|
+
'use client'
|
|
129
|
+
import { createSignal, createPortal, isSSRPortal } from '@barefootjs/client'
|
|
130
|
+
export function P() {
|
|
131
|
+
const [open, setOpen] = createSignal(false)${PORTAL_CALLBACK}
|
|
132
|
+
return (
|
|
133
|
+
<div>
|
|
134
|
+
<button onClick={() => setOpen(!open())}>toggle</button>
|
|
135
|
+
{open() ? <div className="panel" ref={moveToBody} /> : null}
|
|
136
|
+
</div>
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
`)
|
|
140
|
+
expect(js).toMatch(/\{ \(moveToBody\)\(_s\d+\); if \(__scopeId\) _s\d+\.setAttribute\('bf-po', __scopeId\) \}/)
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('a ref callback that is not the SSR-portal pattern keeps the bare call', () => {
|
|
144
|
+
const js = clientJs(`
|
|
145
|
+
'use client'
|
|
146
|
+
import { createSignal } from '@barefootjs/client'
|
|
147
|
+
export function P() {
|
|
148
|
+
const [n, setN] = createSignal(0)
|
|
149
|
+
const focus = (el: HTMLElement) => el.focus()
|
|
150
|
+
return <div><input ref={focus} /><button onClick={() => setN(n() + 1)}>{n()}</button></div>
|
|
151
|
+
}
|
|
152
|
+
`)
|
|
153
|
+
expect(js).toMatch(/if \(_s\d+\) \(focus\)\(_s\d+\)\n/)
|
|
154
|
+
expect(js).not.toContain("setAttribute('bf-po'")
|
|
155
|
+
})
|
|
156
|
+
})
|
|
@@ -502,6 +502,89 @@ describe('DomBinding classification (#944)', () => {
|
|
|
502
502
|
expect(loop!.deps).toContain('items')
|
|
503
503
|
})
|
|
504
504
|
|
|
505
|
+
test('filter().map() loop deps include signals read by the filter predicate (#3246)', () => {
|
|
506
|
+
// Repro from #3246: `filter` is only read inside the `.filter()`
|
|
507
|
+
// predicate, lifted off the chain into `IRLoop.filterPredicate` and kept
|
|
508
|
+
// separate from `array` (`items()`) by `jsx-to-ir.ts` — so it must still
|
|
509
|
+
// show up as a loop dependency, exactly as it does for the DOM
|
|
510
|
+
// bindings that live inside the loop body.
|
|
511
|
+
const source = `
|
|
512
|
+
'use client'
|
|
513
|
+
import { createSignal } from '@barefootjs/client'
|
|
514
|
+
|
|
515
|
+
export function ListA() {
|
|
516
|
+
const [items] = createSignal([{ id: 1, done: false }])
|
|
517
|
+
const [filter, setFilter] = createSignal('all')
|
|
518
|
+
return (
|
|
519
|
+
<div>
|
|
520
|
+
<button onClick={() => setFilter('active')}>Active</button>
|
|
521
|
+
<ul>{items().filter(t => filter() === 'all' || !t.done).map(t => <li key={t.id}>{t.id}</li>)}</ul>
|
|
522
|
+
</div>
|
|
523
|
+
)
|
|
524
|
+
}
|
|
525
|
+
`
|
|
526
|
+
const graph = buildComponentGraph(source, 'ListA.tsx')
|
|
527
|
+
const loop = graph.domBindings.find(d => d.type === 'loop')
|
|
528
|
+
expect(loop).toBeDefined()
|
|
529
|
+
expect(loop!.deps).toContain('items')
|
|
530
|
+
expect(loop!.deps).toContain('filter')
|
|
531
|
+
|
|
532
|
+
// `bf debug trace ListA.tsx filter` must show `filter -> loop`.
|
|
533
|
+
const filterPath = traceUpdatePath(graph, 'filter')
|
|
534
|
+
expect(filterPath).not.toBeNull()
|
|
535
|
+
const loopDependent = filterPath!.dependents.find(d => d.kind === 'dom' && d.name === loop!.label)
|
|
536
|
+
expect(loopDependent).toBeDefined()
|
|
537
|
+
})
|
|
538
|
+
|
|
539
|
+
test('filter().map() loop deps include signals read by a block-bodied predicate (#3246)', () => {
|
|
540
|
+
const source = `
|
|
541
|
+
'use client'
|
|
542
|
+
import { createSignal } from '@barefootjs/client'
|
|
543
|
+
|
|
544
|
+
export function ListB() {
|
|
545
|
+
const [items] = createSignal([{ id: 1, done: false }])
|
|
546
|
+
const [filter, setFilter] = createSignal('all')
|
|
547
|
+
return (
|
|
548
|
+
<div>
|
|
549
|
+
<button onClick={() => setFilter('active')}>Active</button>
|
|
550
|
+
<ul>{items().filter(t => { const f = filter(); return f === 'all' || !t.done }).map(t => <li key={t.id}>{t.id}</li>)}</ul>
|
|
551
|
+
</div>
|
|
552
|
+
)
|
|
553
|
+
}
|
|
554
|
+
`
|
|
555
|
+
const graph = buildComponentGraph(source, 'ListB.tsx')
|
|
556
|
+
const loop = graph.domBindings.find(d => d.type === 'loop')
|
|
557
|
+
expect(loop).toBeDefined()
|
|
558
|
+
expect(loop!.deps).toContain('filter')
|
|
559
|
+
})
|
|
560
|
+
|
|
561
|
+
test("filter predicate's own param is excluded from deps even when it shadows a signal name and is called as a function (#3246)", () => {
|
|
562
|
+
// The predicate's parameter is named `filter`, same as the unrelated
|
|
563
|
+
// outer `filter` signal, and — unlike the signal getter — is itself a
|
|
564
|
+
// function being invoked (`filter()`). Naive text matching on the raw
|
|
565
|
+
// predicate body would mistake that call for a read of the `filter`
|
|
566
|
+
// signal; the predicate's own param must be excluded first.
|
|
567
|
+
const source = `
|
|
568
|
+
'use client'
|
|
569
|
+
import { createSignal } from '@barefootjs/client'
|
|
570
|
+
|
|
571
|
+
export function ListC() {
|
|
572
|
+
const [items] = createSignal([() => true, () => false])
|
|
573
|
+
const [filter, setFilter] = createSignal('all')
|
|
574
|
+
return (
|
|
575
|
+
<div>
|
|
576
|
+
<button onClick={() => setFilter('active')}>Active</button>
|
|
577
|
+
<ul>{items().filter(filter => filter()).map(fn => <li>{String(fn)}</li>)}</ul>
|
|
578
|
+
</div>
|
|
579
|
+
)
|
|
580
|
+
}
|
|
581
|
+
`
|
|
582
|
+
const graph = buildComponentGraph(source, 'ListC.tsx')
|
|
583
|
+
const loop = graph.domBindings.find(d => d.type === 'loop')
|
|
584
|
+
expect(loop).toBeDefined()
|
|
585
|
+
expect(loop!.deps).not.toContain('filter')
|
|
586
|
+
})
|
|
587
|
+
|
|
505
588
|
test('opaque call in child-component prop is fallback (#944 concern 1)', () => {
|
|
506
589
|
// `<Card title={formatTitle(page)} />` — the #942 motivating example.
|
|
507
590
|
// The child-prop emitter wraps this in a reactive child-prop entry via
|
|
@@ -1646,3 +1729,225 @@ describe('prop reads are tracked deps (#2903)', () => {
|
|
|
1646
1729
|
expect(formatComponentGraph(graph)).toContain('~ <div title={pick()}> (no tracked deps)')
|
|
1647
1730
|
})
|
|
1648
1731
|
})
|
|
1732
|
+
|
|
1733
|
+
describe('async factories in the graph (#3167, spec/async.md §7.8)', () => {
|
|
1734
|
+
const source = `
|
|
1735
|
+
'use client'
|
|
1736
|
+
import { createQuery, createMutation, createMemo, createSignal, http } from '@barefootjs/client'
|
|
1737
|
+
type Post = { id: number; title: string }
|
|
1738
|
+
export function Feed(props: { posts: Post[] }) {
|
|
1739
|
+
const [page, setPage] = createSignal(1)
|
|
1740
|
+
const [posts, fetchPosts] = createQuery(() => http.get<Post[]>('/api/posts', { page: page() }), { initial: props.posts })
|
|
1741
|
+
const busy = createMemo(() => fetchPosts.isPending())
|
|
1742
|
+
const [saved, save] = createMutation(() => http.post('/api/likes', { body: { page: page() } }))
|
|
1743
|
+
return (
|
|
1744
|
+
<div aria-busy={fetchPosts.isPending()}>
|
|
1745
|
+
{fetchPosts.error() ? <p>Failed</p> : null}
|
|
1746
|
+
<ul>{posts().map((post) => <li key={post.id}>{post.title}</li>)}</ul>
|
|
1747
|
+
<button disabled={save.isPending()} onClick={() => { setPage(page() + 1); save() }}>next</button>
|
|
1748
|
+
<p>{busy() ? 'loading' : saved() ? 'liked' : 'idle'}</p>
|
|
1749
|
+
</div>
|
|
1750
|
+
)
|
|
1751
|
+
}
|
|
1752
|
+
`
|
|
1753
|
+
const graph = buildComponentGraph(source, 'Feed.tsx')
|
|
1754
|
+
|
|
1755
|
+
test('a query is a signal source with its request inputs; a mutation has none', () => {
|
|
1756
|
+
expect(graph.signals.find(s => s.name === 'posts')!.factory).toEqual({
|
|
1757
|
+
key: 'posts',
|
|
1758
|
+
kind: 'query',
|
|
1759
|
+
callee: 'createQuery',
|
|
1760
|
+
action: 'fetchPosts',
|
|
1761
|
+
value: 'posts',
|
|
1762
|
+
requestDeps: ['page'],
|
|
1763
|
+
})
|
|
1764
|
+
// The mutation's request function also reads `page`, but untracked.
|
|
1765
|
+
expect(graph.signals.find(s => s.name === 'saved')!.factory).toEqual({
|
|
1766
|
+
key: 'saved',
|
|
1767
|
+
kind: 'mutation',
|
|
1768
|
+
callee: 'createMutation',
|
|
1769
|
+
action: 'save',
|
|
1770
|
+
value: 'saved',
|
|
1771
|
+
requestDeps: [],
|
|
1772
|
+
})
|
|
1773
|
+
expect(graph.factories.map(f => f.key)).toEqual(['posts', 'saved'])
|
|
1774
|
+
expect(graph.signals.find(s => s.name === 'page')!.consumers).toContain('query:posts')
|
|
1775
|
+
expect(graph.signals.find(s => s.name === 'page')!.consumers).not.toContain('query:saved')
|
|
1776
|
+
})
|
|
1777
|
+
|
|
1778
|
+
test('accessor reads are nodes with DOM and memo consumers', () => {
|
|
1779
|
+
expect(graph.accessors.map(a => a.name)).toEqual([
|
|
1780
|
+
'fetchPosts.isPending',
|
|
1781
|
+
'fetchPosts.error',
|
|
1782
|
+
'save.isPending',
|
|
1783
|
+
'save.error',
|
|
1784
|
+
])
|
|
1785
|
+
const consumersOf = (name: string) => graph.accessors.find(a => a.name === name)!.consumers
|
|
1786
|
+
expect(consumersOf('fetchPosts.isPending')).toEqual(['memo:busy', 'dom:aria-busy'])
|
|
1787
|
+
expect(consumersOf('fetchPosts.error')).toEqual(['dom:conditional "s0"'])
|
|
1788
|
+
expect(consumersOf('save.isPending')).toEqual(['dom:disabled'])
|
|
1789
|
+
expect(consumersOf('save.error')).toEqual([])
|
|
1790
|
+
expect(graph.memos.find(m => m.name === 'busy')!.deps).toEqual(['fetchPosts.isPending'])
|
|
1791
|
+
// An accessor read is never mistaken for a prop read.
|
|
1792
|
+
expect(graph.props.map(p => p.name)).toEqual([])
|
|
1793
|
+
})
|
|
1794
|
+
|
|
1795
|
+
test('tracing a query input reaches the query value and accessor readers', () => {
|
|
1796
|
+
const path = traceUpdatePath(graph, 'page')!
|
|
1797
|
+
const query = path.dependents.find(d => d.name === 'posts')!
|
|
1798
|
+
expect(query.kind).toBe('query')
|
|
1799
|
+
expect(query.children.map(c => c.name)).toEqual(['loop "s2"', 'busy', 'aria-busy', 'conditional "s0"'])
|
|
1800
|
+
expect(traceUpdatePath(graph, 'save.isPending')!.dependents.map(d => d.name)).toEqual(['disabled'])
|
|
1801
|
+
})
|
|
1802
|
+
|
|
1803
|
+
test('the text format lists the factory, its accessors and their edges', () => {
|
|
1804
|
+
const text = formatComponentGraph(graph)
|
|
1805
|
+
expect(text).toContain('posts (initial: props.posts) [createQuery, action fetchPosts] <- page')
|
|
1806
|
+
expect(text).toContain('saved (initial: undefined) [createMutation, action save]')
|
|
1807
|
+
expect(text).toContain(' accessors:\n fetchPosts.isPending() (of posts)')
|
|
1808
|
+
expect(text).toContain(' page -> query:posts')
|
|
1809
|
+
expect(text).toContain(' save.isPending -> dom:disabled')
|
|
1810
|
+
expect(text).toContain(' busy <- fetchPosts.isPending')
|
|
1811
|
+
})
|
|
1812
|
+
|
|
1813
|
+
test('the JSON form carries the factory and the accessors', () => {
|
|
1814
|
+
const json = graphToJSON(graph) as {
|
|
1815
|
+
signals: { name: string; factory?: unknown }[]
|
|
1816
|
+
factories: unknown[]
|
|
1817
|
+
accessors: { name: string }[]
|
|
1818
|
+
}
|
|
1819
|
+
const saved = { key: 'saved', kind: 'mutation', callee: 'createMutation', action: 'save', value: 'saved', requestDeps: [] }
|
|
1820
|
+
expect(json.signals.find(s => s.name === 'saved')!.factory).toEqual(saved)
|
|
1821
|
+
expect(json.factories).toContainEqual(saved)
|
|
1822
|
+
expect(json.accessors.map(a => a.name)).toContain('save.error')
|
|
1823
|
+
})
|
|
1824
|
+
})
|
|
1825
|
+
|
|
1826
|
+
describe('async factories in the graph: edge cases (#3167 review)', () => {
|
|
1827
|
+
const source = `
|
|
1828
|
+
'use client'
|
|
1829
|
+
import { createQuery, createMutation, createMemo, createSignal, http } from '@barefootjs/client'
|
|
1830
|
+
export function Edge(props: { id: string }) {
|
|
1831
|
+
const [ttl, setTtl] = createSignal(1000)
|
|
1832
|
+
const [posts, fetchPosts] = createQuery(() => http.get('/api/posts/' + props.id), { ttl: ttl() })
|
|
1833
|
+
const [, save] = createMutation(() => http.post('/api/likes'))
|
|
1834
|
+
return (
|
|
1835
|
+
<div>
|
|
1836
|
+
<button disabled={save.isPending()} onClick={() => { save(); setTtl(1) }}>go</button>
|
|
1837
|
+
<ul>{[1, 2].map((fetchPosts) => <li>{fetchPosts.error() ? 'a' : 'b'}</li>)}</ul>
|
|
1838
|
+
<p>{posts() ? 'p' : 'n'}</p>
|
|
1839
|
+
</div>
|
|
1840
|
+
)
|
|
1841
|
+
}
|
|
1842
|
+
`
|
|
1843
|
+
const graph = buildComponentGraph(source, 'Edge.tsx')
|
|
1844
|
+
|
|
1845
|
+
test('only the request function is an input: a signal read in options re-sends nothing', () => {
|
|
1846
|
+
expect(graph.signals.find(s => s.name === 'posts')!.factory!.requestDeps).toEqual(['props.id'])
|
|
1847
|
+
expect(graph.signals.find(s => s.name === 'ttl')!.consumers).not.toContain('query:posts')
|
|
1848
|
+
})
|
|
1849
|
+
|
|
1850
|
+
test('a prop read in the request function is a query input with its own node', () => {
|
|
1851
|
+
expect(graph.props.find(p => p.name === 'props.id')!.consumers).toEqual(['query:posts'])
|
|
1852
|
+
const path = traceUpdatePath(graph, 'props.id')!
|
|
1853
|
+
expect(path.dependents.map(d => d.name)).toEqual(['posts'])
|
|
1854
|
+
})
|
|
1855
|
+
|
|
1856
|
+
test('an elided value ([, save]) is keyed by the action, never by the synthesized getter', () => {
|
|
1857
|
+
expect(graph.signals.map(s => s.name)).toEqual(['ttl', 'posts'])
|
|
1858
|
+
expect(graph.accessors.find(a => a.name === 'save.isPending')!.factory).toBe('save')
|
|
1859
|
+
expect(graph.factories.find(f => f.key === 'save')).toMatchObject({ value: null, callee: 'createMutation' })
|
|
1860
|
+
expect(formatComponentGraph(graph)).toContain('save.isPending() (createMutation, value not destructured)')
|
|
1861
|
+
expect(JSON.stringify(graphToJSON(graph))).not.toContain('__bfGet_')
|
|
1862
|
+
})
|
|
1863
|
+
|
|
1864
|
+
test('a loop param shadowing the action is not an accessor read', () => {
|
|
1865
|
+
expect(graph.accessors.find(a => a.name === 'fetchPosts.error')!.consumers).toEqual([])
|
|
1866
|
+
})
|
|
1867
|
+
|
|
1868
|
+
test('a binding that reads an accessor is proven reactive, not a fallback', () => {
|
|
1869
|
+
const disabled = graph.domBindings.find(d => d.label === 'disabled')!
|
|
1870
|
+
expect(disabled).toMatchObject({ deps: ['save.isPending'], classification: 'reactive' })
|
|
1871
|
+
})
|
|
1872
|
+
|
|
1873
|
+
test('why-update explains an accessor read by the handler that calls the action', () => {
|
|
1874
|
+
const result = buildWhyUpdate(source, 'Edge.tsx', 'disabled')!
|
|
1875
|
+
expect(result.deps).toEqual([
|
|
1876
|
+
{ name: 'save.isPending', kind: 'accessor', dependsOn: [], changedBy: [expect.objectContaining({ setter: 'save' })] },
|
|
1877
|
+
])
|
|
1878
|
+
expect(formatWhyUpdate(result)).toContain('save.isPending changes from:')
|
|
1879
|
+
})
|
|
1880
|
+
|
|
1881
|
+
test('why-update follows a query value to its request inputs', () => {
|
|
1882
|
+
const result = buildWhyUpdate(source, 'Edge.tsx', 's3')!
|
|
1883
|
+
expect(result.deps.find(d => d.name === 'posts')).toMatchObject({ kind: 'signal', dependsOn: ['props.id'] })
|
|
1884
|
+
expect(result.deps.map(d => d.name)).toContain('props.id')
|
|
1885
|
+
})
|
|
1886
|
+
})
|
|
1887
|
+
|
|
1888
|
+
describe('async factories in the graph: value not destructured, effects (#3167 review)', () => {
|
|
1889
|
+
const source = `
|
|
1890
|
+
'use client'
|
|
1891
|
+
import { createQuery, createEffect, createSignal, http } from '@barefootjs/client'
|
|
1892
|
+
export function Bare() {
|
|
1893
|
+
const [page, setPage] = createSignal(1)
|
|
1894
|
+
const [, fetchPosts] = createQuery(() => http.get('/api/posts', { page: page() }))
|
|
1895
|
+
createEffect(() => { if (fetchPosts.error()) console.warn('failed') })
|
|
1896
|
+
return (
|
|
1897
|
+
<div aria-busy={fetchPosts.isPending()}>
|
|
1898
|
+
<button onClick={() => setPage(page() + 1)}>next</button>
|
|
1899
|
+
</div>
|
|
1900
|
+
)
|
|
1901
|
+
}
|
|
1902
|
+
`
|
|
1903
|
+
const graph = buildComponentGraph(source, 'Bare.tsx')
|
|
1904
|
+
|
|
1905
|
+
test('an effect that reads an accessor is its consumer', () => {
|
|
1906
|
+
expect(graph.effects[0].deps).toContain('fetchPosts.error')
|
|
1907
|
+
expect(graph.accessors.find(a => a.name === 'fetchPosts.error')!.consumers).toEqual(['effect:e0'])
|
|
1908
|
+
})
|
|
1909
|
+
|
|
1910
|
+
test('why-update keeps a query\'s request inputs when its value is not destructured', () => {
|
|
1911
|
+
const result = buildWhyUpdate(source, 'Bare.tsx', 'aria-busy')!
|
|
1912
|
+
expect(result.deps.find(d => d.name === 'fetchPosts.isPending')).toMatchObject({ kind: 'accessor', dependsOn: ['page'] })
|
|
1913
|
+
expect(result.deps.find(d => d.name === 'page')!.changedBy[0]).toMatchObject({ setter: 'setPage' })
|
|
1914
|
+
})
|
|
1915
|
+
})
|
|
1916
|
+
|
|
1917
|
+
describe('async factories in the graph: reads in memo / effect bodies (#3227 review)', () => {
|
|
1918
|
+
const source = `
|
|
1919
|
+
'use client'
|
|
1920
|
+
import { createQuery, createEffect, createMemo, createSignal, http } from '@barefootjs/client'
|
|
1921
|
+
export function Bodies() {
|
|
1922
|
+
const [items] = createSignal([1, 2])
|
|
1923
|
+
const [, fetchPosts] = createQuery(() => http.get('/api/posts'))
|
|
1924
|
+
const busy = createMemo(() => { console.log('x'); return fetchPosts.isPending() })
|
|
1925
|
+
createEffect(() => { items().forEach((x) => { if (fetchPosts.error()) console.log(x) }) })
|
|
1926
|
+
return <p>{busy() ? 'loading' : 'idle'}</p>
|
|
1927
|
+
}
|
|
1928
|
+
`
|
|
1929
|
+
const graph = buildComponentGraph(source, 'Bodies.tsx')
|
|
1930
|
+
|
|
1931
|
+
test('a memo whose block body does not fold to one expression still records its accessor read', () => {
|
|
1932
|
+
expect(graph.memos.find(m => m.name === 'busy')!.deps).toContain('fetchPosts.isPending')
|
|
1933
|
+
})
|
|
1934
|
+
|
|
1935
|
+
test('an effect reading an accessor inside a block-bodied callback records the read', () => {
|
|
1936
|
+
expect(graph.effects[0].deps).toEqual(['items', 'fetchPosts.error'])
|
|
1937
|
+
expect(graph.accessors.find(a => a.name === 'fetchPosts.error')!.consumers).toEqual(['effect:e0'])
|
|
1938
|
+
})
|
|
1939
|
+
})
|
|
1940
|
+
|
|
1941
|
+
describe('consumer lists keep one entry per binding (#3227 review)', () => {
|
|
1942
|
+
test('two bindings with the same label are both listed, as before async factories', () => {
|
|
1943
|
+
const graph = buildComponentGraph(`
|
|
1944
|
+
'use client'
|
|
1945
|
+
import { createSignal } from '@barefootjs/client'
|
|
1946
|
+
export function Two() {
|
|
1947
|
+
const [busy, setBusy] = createSignal(false)
|
|
1948
|
+
return <div><button disabled={busy()} onClick={() => setBusy(true)}>a</button><button disabled={busy()}>b</button></div>
|
|
1949
|
+
}
|
|
1950
|
+
`, 'Two.tsx')
|
|
1951
|
+
expect(graph.signals.find(s => s.name === 'busy')!.consumers.filter(c => c === 'dom:disabled')).toHaveLength(2)
|
|
1952
|
+
})
|
|
1953
|
+
})
|
|
@@ -71,11 +71,13 @@
|
|
|
71
71
|
* once the mechanism is proven on more pages.
|
|
72
72
|
*/
|
|
73
73
|
|
|
74
|
-
import { describe, test, expect } from 'bun:test'
|
|
74
|
+
import { afterAll, beforeAll, describe, test, expect } from 'bun:test'
|
|
75
75
|
import { readFileSync } from 'node:fs'
|
|
76
76
|
import { resolve } from 'node:path'
|
|
77
|
+
import ts from 'typescript'
|
|
77
78
|
import { compileJSX } from '../compiler'
|
|
78
79
|
import { TestAdapter } from '../adapters/test-adapter'
|
|
80
|
+
import { createAnchoredProgram } from '../analyzer'
|
|
79
81
|
|
|
80
82
|
const DOCS_ROOT = resolve(__dirname, '../../../../docs')
|
|
81
83
|
|
|
@@ -290,6 +292,8 @@ const PAGES: PageSpec[] = [
|
|
|
290
292
|
{ path: 'core/reactivity/on-cleanup.md' },
|
|
291
293
|
{ path: 'core/reactivity/untrack.md' },
|
|
292
294
|
{ path: 'core/reactivity/batch.md' },
|
|
295
|
+
{ path: 'core/reactivity/create-query.md' },
|
|
296
|
+
{ path: 'core/reactivity/create-mutation.md' },
|
|
293
297
|
{ path: 'core/reactivity/props-reactivity.md' },
|
|
294
298
|
{ path: 'core/components/component-authoring.md' },
|
|
295
299
|
{ path: 'core/components/children-slots.md' },
|
|
@@ -319,8 +323,22 @@ const PAGES: PageSpec[] = [
|
|
|
319
323
|
|
|
320
324
|
const adapter = new TestAdapter()
|
|
321
325
|
|
|
326
|
+
/**
|
|
327
|
+
* Where the TypeChecker resolves `DocExample.tsx`'s imports from, so a
|
|
328
|
+
* `Reactive` accessor such as `fetchPosts.isPending()` types the same from
|
|
329
|
+
* the repo root (where CI runs) as from `packages/jsx` — see
|
|
330
|
+
* `createAnchoredProgram`. Resolution climbs to `packages/jsx/node_modules`.
|
|
331
|
+
* The brand guard at the bottom of this file fails if it stops resolving.
|
|
332
|
+
*/
|
|
333
|
+
const PROGRAM_ANCHOR = resolve(import.meta.dir, '__virtual__')
|
|
334
|
+
|
|
335
|
+
function compileDocExample(source: string) {
|
|
336
|
+
const program = createAnchoredProgram(source, 'DocExample.tsx', PROGRAM_ANCHOR)
|
|
337
|
+
return compileJSX(source, 'DocExample.tsx', { adapter, ...(program ? { program } : {}) })
|
|
338
|
+
}
|
|
339
|
+
|
|
322
340
|
function compileOnce(source: string) {
|
|
323
|
-
const result =
|
|
341
|
+
const result = compileDocExample(source)
|
|
324
342
|
const fatals = result.errors.filter(e => e.severity === 'error')
|
|
325
343
|
const clientJsFile = result.files.find(f => f.type === 'clientJs')
|
|
326
344
|
return { fatals, clientJs: clientJsFile?.content ?? null }
|
|
@@ -554,7 +572,7 @@ describe('docs/core/advanced/error-codes.md per-BFxxx matchers', () => {
|
|
|
554
572
|
const source = isExpression
|
|
555
573
|
? EXPRESSION_SCAFFOLD_HEADER + c.body + EXPRESSION_SCAFFOLD_FOOTER
|
|
556
574
|
: c.body + '\n'
|
|
557
|
-
const result =
|
|
575
|
+
const result = compileDocExample(source)
|
|
558
576
|
const matched = result.errors.find(e => e.code === c.code)
|
|
559
577
|
if (!matched) {
|
|
560
578
|
const got = result.errors.map(e => ` ${e.severity} ${e.code}: ${e.message}`).join('\n') || ' (no diagnostics)'
|
|
@@ -565,3 +583,48 @@ describe('docs/core/advanced/error-codes.md per-BFxxx matchers', () => {
|
|
|
565
583
|
})
|
|
566
584
|
}
|
|
567
585
|
})
|
|
586
|
+
|
|
587
|
+
// Guard for PROGRAM_ANCHOR (#3167), the twin of adapter-tests'
|
|
588
|
+
// `harness-program-resolves-brand.test.ts`: type resolution reads
|
|
589
|
+
// `@barefootjs/client`'s built `dist/*.d.ts`, so an unbuilt client (or a
|
|
590
|
+
// broken anchor) silently types every `Reactive<T>` accessor `any` and the
|
|
591
|
+
// snapshots above record non-reactive bindings with no red test. Runs from
|
|
592
|
+
// the repo root, where CI runs and where an unanchored compile sees `any`.
|
|
593
|
+
describe('doc-examples Program resolves @barefootjs/client types independent of cwd', () => {
|
|
594
|
+
const REPO_ROOT = resolve(import.meta.dir, '../../../..')
|
|
595
|
+
const SOURCE = `
|
|
596
|
+
'use client'
|
|
597
|
+
import { createQuery, http } from '@barefootjs/client'
|
|
598
|
+
export function Probe() {
|
|
599
|
+
const [, fetchProbe] = createQuery(() => http.get<number[]>('/api/probe'))
|
|
600
|
+
return <ul aria-busy={fetchProbe.isPending()}>{[1, 2].map(n => <li key={n}>{n}</li>)}</ul>
|
|
601
|
+
}
|
|
602
|
+
`
|
|
603
|
+
let savedCwd = ''
|
|
604
|
+
beforeAll(() => {
|
|
605
|
+
savedCwd = process.cwd()
|
|
606
|
+
process.chdir(REPO_ROOT)
|
|
607
|
+
})
|
|
608
|
+
afterAll(() => {
|
|
609
|
+
process.chdir(savedCwd)
|
|
610
|
+
})
|
|
611
|
+
|
|
612
|
+
test('a Reactive<T> accessor read is seen as its real type, not `any`', () => {
|
|
613
|
+
const program = createAnchoredProgram(SOURCE, 'DocExample.tsx', PROGRAM_ANCHOR)
|
|
614
|
+
const sourceFile = program?.getSourceFile('DocExample.tsx')
|
|
615
|
+
expect(sourceFile?.text).toBe(SOURCE)
|
|
616
|
+
if (!program || !sourceFile) return
|
|
617
|
+
const checker = program.getTypeChecker()
|
|
618
|
+
let typeAtCall = ''
|
|
619
|
+
const visit = (node: ts.Node): void => {
|
|
620
|
+
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === 'isPending') {
|
|
621
|
+
typeAtCall = checker.typeToString(checker.getTypeAtLocation(node))
|
|
622
|
+
}
|
|
623
|
+
ts.forEachChild(node, visit)
|
|
624
|
+
}
|
|
625
|
+
visit(sourceFile)
|
|
626
|
+
// Unresolved, this is `any`; `boolean` is what tells "the brand
|
|
627
|
+
// resolved" apart from "TypeScript gave up".
|
|
628
|
+
expect(typeAtCall).toBe('boolean')
|
|
629
|
+
})
|
|
630
|
+
})
|
|
@@ -92,6 +92,29 @@ describe('transparent fragment (Context Provider pattern)', () => {
|
|
|
92
92
|
}
|
|
93
93
|
})
|
|
94
94
|
|
|
95
|
+
test('does NOT mark <> {children} </> with single-line padding as transparent', () => {
|
|
96
|
+
// Unlike a text node split across a line break (cleans to ''), a
|
|
97
|
+
// same-line space around `{children}` is real, rendered text per
|
|
98
|
+
// `cleanJsxWhitespace` — so the fragment has three children, not one,
|
|
99
|
+
// and can't be a pure pass-through.
|
|
100
|
+
const source = `
|
|
101
|
+
'use client'
|
|
102
|
+
|
|
103
|
+
export function DialogRoot({ children }) {
|
|
104
|
+
return <> {children} </>
|
|
105
|
+
}
|
|
106
|
+
`
|
|
107
|
+
|
|
108
|
+
const ctx = analyzeComponent(source, 'DialogRoot.tsx')
|
|
109
|
+
const ir = jsxToIR(ctx)
|
|
110
|
+
|
|
111
|
+
expect(ir).not.toBeNull()
|
|
112
|
+
expect(ir!.type).toBe('fragment')
|
|
113
|
+
if (ir!.type === 'fragment') {
|
|
114
|
+
expect(ir!.transparent).toBeFalsy()
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
|
|
95
118
|
test('does NOT mark fragment with non-children expression as transparent', () => {
|
|
96
119
|
const source = `
|
|
97
120
|
'use client'
|