@barefootjs/jsx 0.38.0 → 0.39.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/action-accessor.d.ts +159 -0
- package/dist/action-accessor.d.ts.map +1 -0
- package/dist/adapters/jsx-adapter.d.ts.map +1 -1
- package/dist/analyzer.d.ts +30 -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/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1056 -344
- package/dist/ir-to-client-js/build-references.d.ts.map +1 -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.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 +41 -2
- 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/template-position-walk.d.ts +68 -0
- package/dist/template-position-walk.d.ts.map +1 -0
- package/dist/types.d.ts +51 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +191 -1
- package/src/__tests__/action-accessor.test.ts +166 -0
- package/src/__tests__/create-mutation.test.ts +220 -0
- package/src/__tests__/create-query.test.ts +644 -0
- package/src/__tests__/csr-fragment-root-scope-shape.test.ts +156 -0
- package/src/__tests__/debug.test.ts +222 -0
- package/src/__tests__/doc-examples.test.ts +66 -3
- 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/jsx-adapter.ts +17 -0
- package/src/analyzer.ts +558 -33
- package/src/async-action-refusal.ts +254 -0
- package/src/compiler.ts +3 -0
- package/src/debug.ts +318 -103
- package/src/errors.ts +39 -0
- package/src/format-date-refusal.ts +6 -125
- package/src/index.ts +2 -1
- package/src/ir-to-client-js/build-references.ts +6 -2
- package/src/ir-to-client-js/collect-elements.ts +1 -0
- 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/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 +1 -0
- 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 +55 -7
- package/src/jsx-to-ir.ts +36 -2
- package/src/signal-initializer.ts +36 -0
- package/src/template-position-walk.ts +203 -0
- package/src/types.ts +52 -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
|
+
})
|
|
@@ -1646,3 +1646,225 @@ describe('prop reads are tracked deps (#2903)', () => {
|
|
|
1646
1646
|
expect(formatComponentGraph(graph)).toContain('~ <div title={pick()}> (no tracked deps)')
|
|
1647
1647
|
})
|
|
1648
1648
|
})
|
|
1649
|
+
|
|
1650
|
+
describe('async factories in the graph (#3167, spec/async.md §7.8)', () => {
|
|
1651
|
+
const source = `
|
|
1652
|
+
'use client'
|
|
1653
|
+
import { createQuery, createMutation, createMemo, createSignal, http } from '@barefootjs/client'
|
|
1654
|
+
type Post = { id: number; title: string }
|
|
1655
|
+
export function Feed(props: { posts: Post[] }) {
|
|
1656
|
+
const [page, setPage] = createSignal(1)
|
|
1657
|
+
const [posts, fetchPosts] = createQuery(() => http.get<Post[]>('/api/posts', { page: page() }), { initial: props.posts })
|
|
1658
|
+
const busy = createMemo(() => fetchPosts.isPending())
|
|
1659
|
+
const [saved, save] = createMutation(() => http.post('/api/likes', { body: { page: page() } }))
|
|
1660
|
+
return (
|
|
1661
|
+
<div aria-busy={fetchPosts.isPending()}>
|
|
1662
|
+
{fetchPosts.error() ? <p>Failed</p> : null}
|
|
1663
|
+
<ul>{posts().map((post) => <li key={post.id}>{post.title}</li>)}</ul>
|
|
1664
|
+
<button disabled={save.isPending()} onClick={() => { setPage(page() + 1); save() }}>next</button>
|
|
1665
|
+
<p>{busy() ? 'loading' : saved() ? 'liked' : 'idle'}</p>
|
|
1666
|
+
</div>
|
|
1667
|
+
)
|
|
1668
|
+
}
|
|
1669
|
+
`
|
|
1670
|
+
const graph = buildComponentGraph(source, 'Feed.tsx')
|
|
1671
|
+
|
|
1672
|
+
test('a query is a signal source with its request inputs; a mutation has none', () => {
|
|
1673
|
+
expect(graph.signals.find(s => s.name === 'posts')!.factory).toEqual({
|
|
1674
|
+
key: 'posts',
|
|
1675
|
+
kind: 'query',
|
|
1676
|
+
callee: 'createQuery',
|
|
1677
|
+
action: 'fetchPosts',
|
|
1678
|
+
value: 'posts',
|
|
1679
|
+
requestDeps: ['page'],
|
|
1680
|
+
})
|
|
1681
|
+
// The mutation's request function also reads `page`, but untracked.
|
|
1682
|
+
expect(graph.signals.find(s => s.name === 'saved')!.factory).toEqual({
|
|
1683
|
+
key: 'saved',
|
|
1684
|
+
kind: 'mutation',
|
|
1685
|
+
callee: 'createMutation',
|
|
1686
|
+
action: 'save',
|
|
1687
|
+
value: 'saved',
|
|
1688
|
+
requestDeps: [],
|
|
1689
|
+
})
|
|
1690
|
+
expect(graph.factories.map(f => f.key)).toEqual(['posts', 'saved'])
|
|
1691
|
+
expect(graph.signals.find(s => s.name === 'page')!.consumers).toContain('query:posts')
|
|
1692
|
+
expect(graph.signals.find(s => s.name === 'page')!.consumers).not.toContain('query:saved')
|
|
1693
|
+
})
|
|
1694
|
+
|
|
1695
|
+
test('accessor reads are nodes with DOM and memo consumers', () => {
|
|
1696
|
+
expect(graph.accessors.map(a => a.name)).toEqual([
|
|
1697
|
+
'fetchPosts.isPending',
|
|
1698
|
+
'fetchPosts.error',
|
|
1699
|
+
'save.isPending',
|
|
1700
|
+
'save.error',
|
|
1701
|
+
])
|
|
1702
|
+
const consumersOf = (name: string) => graph.accessors.find(a => a.name === name)!.consumers
|
|
1703
|
+
expect(consumersOf('fetchPosts.isPending')).toEqual(['memo:busy', 'dom:aria-busy'])
|
|
1704
|
+
expect(consumersOf('fetchPosts.error')).toEqual(['dom:conditional "s0"'])
|
|
1705
|
+
expect(consumersOf('save.isPending')).toEqual(['dom:disabled'])
|
|
1706
|
+
expect(consumersOf('save.error')).toEqual([])
|
|
1707
|
+
expect(graph.memos.find(m => m.name === 'busy')!.deps).toEqual(['fetchPosts.isPending'])
|
|
1708
|
+
// An accessor read is never mistaken for a prop read.
|
|
1709
|
+
expect(graph.props.map(p => p.name)).toEqual([])
|
|
1710
|
+
})
|
|
1711
|
+
|
|
1712
|
+
test('tracing a query input reaches the query value and accessor readers', () => {
|
|
1713
|
+
const path = traceUpdatePath(graph, 'page')!
|
|
1714
|
+
const query = path.dependents.find(d => d.name === 'posts')!
|
|
1715
|
+
expect(query.kind).toBe('query')
|
|
1716
|
+
expect(query.children.map(c => c.name)).toEqual(['loop "s2"', 'busy', 'aria-busy', 'conditional "s0"'])
|
|
1717
|
+
expect(traceUpdatePath(graph, 'save.isPending')!.dependents.map(d => d.name)).toEqual(['disabled'])
|
|
1718
|
+
})
|
|
1719
|
+
|
|
1720
|
+
test('the text format lists the factory, its accessors and their edges', () => {
|
|
1721
|
+
const text = formatComponentGraph(graph)
|
|
1722
|
+
expect(text).toContain('posts (initial: props.posts) [createQuery, action fetchPosts] <- page')
|
|
1723
|
+
expect(text).toContain('saved (initial: undefined) [createMutation, action save]')
|
|
1724
|
+
expect(text).toContain(' accessors:\n fetchPosts.isPending() (of posts)')
|
|
1725
|
+
expect(text).toContain(' page -> query:posts')
|
|
1726
|
+
expect(text).toContain(' save.isPending -> dom:disabled')
|
|
1727
|
+
expect(text).toContain(' busy <- fetchPosts.isPending')
|
|
1728
|
+
})
|
|
1729
|
+
|
|
1730
|
+
test('the JSON form carries the factory and the accessors', () => {
|
|
1731
|
+
const json = graphToJSON(graph) as {
|
|
1732
|
+
signals: { name: string; factory?: unknown }[]
|
|
1733
|
+
factories: unknown[]
|
|
1734
|
+
accessors: { name: string }[]
|
|
1735
|
+
}
|
|
1736
|
+
const saved = { key: 'saved', kind: 'mutation', callee: 'createMutation', action: 'save', value: 'saved', requestDeps: [] }
|
|
1737
|
+
expect(json.signals.find(s => s.name === 'saved')!.factory).toEqual(saved)
|
|
1738
|
+
expect(json.factories).toContainEqual(saved)
|
|
1739
|
+
expect(json.accessors.map(a => a.name)).toContain('save.error')
|
|
1740
|
+
})
|
|
1741
|
+
})
|
|
1742
|
+
|
|
1743
|
+
describe('async factories in the graph: edge cases (#3167 review)', () => {
|
|
1744
|
+
const source = `
|
|
1745
|
+
'use client'
|
|
1746
|
+
import { createQuery, createMutation, createMemo, createSignal, http } from '@barefootjs/client'
|
|
1747
|
+
export function Edge(props: { id: string }) {
|
|
1748
|
+
const [ttl, setTtl] = createSignal(1000)
|
|
1749
|
+
const [posts, fetchPosts] = createQuery(() => http.get('/api/posts/' + props.id), { ttl: ttl() })
|
|
1750
|
+
const [, save] = createMutation(() => http.post('/api/likes'))
|
|
1751
|
+
return (
|
|
1752
|
+
<div>
|
|
1753
|
+
<button disabled={save.isPending()} onClick={() => { save(); setTtl(1) }}>go</button>
|
|
1754
|
+
<ul>{[1, 2].map((fetchPosts) => <li>{fetchPosts.error() ? 'a' : 'b'}</li>)}</ul>
|
|
1755
|
+
<p>{posts() ? 'p' : 'n'}</p>
|
|
1756
|
+
</div>
|
|
1757
|
+
)
|
|
1758
|
+
}
|
|
1759
|
+
`
|
|
1760
|
+
const graph = buildComponentGraph(source, 'Edge.tsx')
|
|
1761
|
+
|
|
1762
|
+
test('only the request function is an input: a signal read in options re-sends nothing', () => {
|
|
1763
|
+
expect(graph.signals.find(s => s.name === 'posts')!.factory!.requestDeps).toEqual(['props.id'])
|
|
1764
|
+
expect(graph.signals.find(s => s.name === 'ttl')!.consumers).not.toContain('query:posts')
|
|
1765
|
+
})
|
|
1766
|
+
|
|
1767
|
+
test('a prop read in the request function is a query input with its own node', () => {
|
|
1768
|
+
expect(graph.props.find(p => p.name === 'props.id')!.consumers).toEqual(['query:posts'])
|
|
1769
|
+
const path = traceUpdatePath(graph, 'props.id')!
|
|
1770
|
+
expect(path.dependents.map(d => d.name)).toEqual(['posts'])
|
|
1771
|
+
})
|
|
1772
|
+
|
|
1773
|
+
test('an elided value ([, save]) is keyed by the action, never by the synthesized getter', () => {
|
|
1774
|
+
expect(graph.signals.map(s => s.name)).toEqual(['ttl', 'posts'])
|
|
1775
|
+
expect(graph.accessors.find(a => a.name === 'save.isPending')!.factory).toBe('save')
|
|
1776
|
+
expect(graph.factories.find(f => f.key === 'save')).toMatchObject({ value: null, callee: 'createMutation' })
|
|
1777
|
+
expect(formatComponentGraph(graph)).toContain('save.isPending() (createMutation, value not destructured)')
|
|
1778
|
+
expect(JSON.stringify(graphToJSON(graph))).not.toContain('__bfGet_')
|
|
1779
|
+
})
|
|
1780
|
+
|
|
1781
|
+
test('a loop param shadowing the action is not an accessor read', () => {
|
|
1782
|
+
expect(graph.accessors.find(a => a.name === 'fetchPosts.error')!.consumers).toEqual([])
|
|
1783
|
+
})
|
|
1784
|
+
|
|
1785
|
+
test('a binding that reads an accessor is proven reactive, not a fallback', () => {
|
|
1786
|
+
const disabled = graph.domBindings.find(d => d.label === 'disabled')!
|
|
1787
|
+
expect(disabled).toMatchObject({ deps: ['save.isPending'], classification: 'reactive' })
|
|
1788
|
+
})
|
|
1789
|
+
|
|
1790
|
+
test('why-update explains an accessor read by the handler that calls the action', () => {
|
|
1791
|
+
const result = buildWhyUpdate(source, 'Edge.tsx', 'disabled')!
|
|
1792
|
+
expect(result.deps).toEqual([
|
|
1793
|
+
{ name: 'save.isPending', kind: 'accessor', dependsOn: [], changedBy: [expect.objectContaining({ setter: 'save' })] },
|
|
1794
|
+
])
|
|
1795
|
+
expect(formatWhyUpdate(result)).toContain('save.isPending changes from:')
|
|
1796
|
+
})
|
|
1797
|
+
|
|
1798
|
+
test('why-update follows a query value to its request inputs', () => {
|
|
1799
|
+
const result = buildWhyUpdate(source, 'Edge.tsx', 's3')!
|
|
1800
|
+
expect(result.deps.find(d => d.name === 'posts')).toMatchObject({ kind: 'signal', dependsOn: ['props.id'] })
|
|
1801
|
+
expect(result.deps.map(d => d.name)).toContain('props.id')
|
|
1802
|
+
})
|
|
1803
|
+
})
|
|
1804
|
+
|
|
1805
|
+
describe('async factories in the graph: value not destructured, effects (#3167 review)', () => {
|
|
1806
|
+
const source = `
|
|
1807
|
+
'use client'
|
|
1808
|
+
import { createQuery, createEffect, createSignal, http } from '@barefootjs/client'
|
|
1809
|
+
export function Bare() {
|
|
1810
|
+
const [page, setPage] = createSignal(1)
|
|
1811
|
+
const [, fetchPosts] = createQuery(() => http.get('/api/posts', { page: page() }))
|
|
1812
|
+
createEffect(() => { if (fetchPosts.error()) console.warn('failed') })
|
|
1813
|
+
return (
|
|
1814
|
+
<div aria-busy={fetchPosts.isPending()}>
|
|
1815
|
+
<button onClick={() => setPage(page() + 1)}>next</button>
|
|
1816
|
+
</div>
|
|
1817
|
+
)
|
|
1818
|
+
}
|
|
1819
|
+
`
|
|
1820
|
+
const graph = buildComponentGraph(source, 'Bare.tsx')
|
|
1821
|
+
|
|
1822
|
+
test('an effect that reads an accessor is its consumer', () => {
|
|
1823
|
+
expect(graph.effects[0].deps).toContain('fetchPosts.error')
|
|
1824
|
+
expect(graph.accessors.find(a => a.name === 'fetchPosts.error')!.consumers).toEqual(['effect:e0'])
|
|
1825
|
+
})
|
|
1826
|
+
|
|
1827
|
+
test('why-update keeps a query\'s request inputs when its value is not destructured', () => {
|
|
1828
|
+
const result = buildWhyUpdate(source, 'Bare.tsx', 'aria-busy')!
|
|
1829
|
+
expect(result.deps.find(d => d.name === 'fetchPosts.isPending')).toMatchObject({ kind: 'accessor', dependsOn: ['page'] })
|
|
1830
|
+
expect(result.deps.find(d => d.name === 'page')!.changedBy[0]).toMatchObject({ setter: 'setPage' })
|
|
1831
|
+
})
|
|
1832
|
+
})
|
|
1833
|
+
|
|
1834
|
+
describe('async factories in the graph: reads in memo / effect bodies (#3227 review)', () => {
|
|
1835
|
+
const source = `
|
|
1836
|
+
'use client'
|
|
1837
|
+
import { createQuery, createEffect, createMemo, createSignal, http } from '@barefootjs/client'
|
|
1838
|
+
export function Bodies() {
|
|
1839
|
+
const [items] = createSignal([1, 2])
|
|
1840
|
+
const [, fetchPosts] = createQuery(() => http.get('/api/posts'))
|
|
1841
|
+
const busy = createMemo(() => { console.log('x'); return fetchPosts.isPending() })
|
|
1842
|
+
createEffect(() => { items().forEach((x) => { if (fetchPosts.error()) console.log(x) }) })
|
|
1843
|
+
return <p>{busy() ? 'loading' : 'idle'}</p>
|
|
1844
|
+
}
|
|
1845
|
+
`
|
|
1846
|
+
const graph = buildComponentGraph(source, 'Bodies.tsx')
|
|
1847
|
+
|
|
1848
|
+
test('a memo whose block body does not fold to one expression still records its accessor read', () => {
|
|
1849
|
+
expect(graph.memos.find(m => m.name === 'busy')!.deps).toContain('fetchPosts.isPending')
|
|
1850
|
+
})
|
|
1851
|
+
|
|
1852
|
+
test('an effect reading an accessor inside a block-bodied callback records the read', () => {
|
|
1853
|
+
expect(graph.effects[0].deps).toEqual(['items', 'fetchPosts.error'])
|
|
1854
|
+
expect(graph.accessors.find(a => a.name === 'fetchPosts.error')!.consumers).toEqual(['effect:e0'])
|
|
1855
|
+
})
|
|
1856
|
+
})
|
|
1857
|
+
|
|
1858
|
+
describe('consumer lists keep one entry per binding (#3227 review)', () => {
|
|
1859
|
+
test('two bindings with the same label are both listed, as before async factories', () => {
|
|
1860
|
+
const graph = buildComponentGraph(`
|
|
1861
|
+
'use client'
|
|
1862
|
+
import { createSignal } from '@barefootjs/client'
|
|
1863
|
+
export function Two() {
|
|
1864
|
+
const [busy, setBusy] = createSignal(false)
|
|
1865
|
+
return <div><button disabled={busy()} onClick={() => setBusy(true)}>a</button><button disabled={busy()}>b</button></div>
|
|
1866
|
+
}
|
|
1867
|
+
`, 'Two.tsx')
|
|
1868
|
+
expect(graph.signals.find(s => s.name === 'busy')!.consumers.filter(c => c === 'dom:disabled')).toHaveLength(2)
|
|
1869
|
+
})
|
|
1870
|
+
})
|
|
@@ -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
|
+
})
|
|
@@ -117,12 +117,13 @@ describe('Destructured props keep their declared types (#2150)', () => {
|
|
|
117
117
|
})
|
|
118
118
|
})
|
|
119
119
|
|
|
120
|
-
// A non-primitive optional
|
|
121
|
-
//
|
|
122
|
-
//
|
|
120
|
+
// A non-primitive optional whose type doesn't resolve (here: an imported
|
|
121
|
+
// element type) keeps `unknown` (interface{}-based lowering) but still
|
|
122
|
+
// reports the type's `?` — the adversarial catalogue derives absent
|
|
123
|
+
// points from `optional` alone.
|
|
123
124
|
test('marks OPTIONAL non-primitive members optional while keeping unknown type (#2259)', () => {
|
|
124
125
|
const source = `
|
|
125
|
-
type Todo
|
|
126
|
+
import type { Todo } from './todo'
|
|
126
127
|
type Props = { items?: Todo[] }
|
|
127
128
|
|
|
128
129
|
export function Component({ items }: Props) {
|
|
@@ -208,4 +209,78 @@ describe('Destructured props keep their declared types (#2150)', () => {
|
|
|
208
209
|
expect(typeOf(ctx, 'handlers')).toEqual({ kind: 'unknown', raw: 'unknown' })
|
|
209
210
|
expect(typeOf(ctx, 'configs')).toEqual({ kind: 'unknown', raw: 'unknown' })
|
|
210
211
|
})
|
|
212
|
+
|
|
213
|
+
// A same-file object type referenced BY NAME resolves like its inline
|
|
214
|
+
// spelling — the `props`-object form (`props: { initial: State }`) always
|
|
215
|
+
// kept it; the destructured form declined it to `unknown`.
|
|
216
|
+
test('resolves a member typed by a same-file object type, named element types included', () => {
|
|
217
|
+
const source = `
|
|
218
|
+
type Item = { id: string; label: string }
|
|
219
|
+
interface State { items: Item[]; nextId: number }
|
|
220
|
+
|
|
221
|
+
export function Component({ initial, rows }: { initial: State; rows: Item[] }) {
|
|
222
|
+
return <div>{initial.nextId}{rows.length}</div>
|
|
223
|
+
}
|
|
224
|
+
`
|
|
225
|
+
|
|
226
|
+
const ctx = analyzeComponent(source, 'Component.tsx')
|
|
227
|
+
|
|
228
|
+
expect(typeOf(ctx, 'initial')).toEqual({ kind: 'interface', raw: 'State' })
|
|
229
|
+
expect(typeOf(ctx, 'rows')).toEqual({
|
|
230
|
+
kind: 'array',
|
|
231
|
+
raw: 'Item[]',
|
|
232
|
+
elementType: { kind: 'interface', raw: 'Item' },
|
|
233
|
+
})
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
// The named type is checked through its declaration's members, so the
|
|
237
|
+
// same all-or-nothing rule applies: a union/function leaf, a generic, an
|
|
238
|
+
// `extends` clause, a non-property member, an import, or a recursive
|
|
239
|
+
// reference still declines the whole member.
|
|
240
|
+
test('declines a named type that is not a plain, fully-resolvable same-file object type', () => {
|
|
241
|
+
const source = `
|
|
242
|
+
import type { External } from './external'
|
|
243
|
+
type WithUnion = { kind: 'a' | 'b' }
|
|
244
|
+
type Generic<T> = { value: T }
|
|
245
|
+
interface Base { id: string }
|
|
246
|
+
interface Derived extends Base { label: string }
|
|
247
|
+
interface WithMethod { run(): void }
|
|
248
|
+
type Tree = { children: Tree[] }
|
|
249
|
+
|
|
250
|
+
export function Component({ a, b, c, d, e, f }: {
|
|
251
|
+
a: WithUnion; b: Generic<string>; c: Derived; d: WithMethod; e: External; f: Tree
|
|
252
|
+
}) {
|
|
253
|
+
return <div />
|
|
254
|
+
}
|
|
255
|
+
`
|
|
256
|
+
|
|
257
|
+
const ctx = analyzeComponent(source, 'Component.tsx')
|
|
258
|
+
|
|
259
|
+
for (const name of ['a', 'b', 'c', 'd', 'e', 'f']) {
|
|
260
|
+
expect(typeOf(ctx, name)).toEqual({ kind: 'unknown', raw: 'unknown' })
|
|
261
|
+
}
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
// Interface declaration merging: the type is the union of both blocks,
|
|
265
|
+
// so either half alone can't vouch for it. Declines whichever order the
|
|
266
|
+
// halves come in, rather than admitting on whichever one a walk keeps.
|
|
267
|
+
test('declines a named type declared more than once (interface merging)', () => {
|
|
268
|
+
const merged = (first: string, second: string) => `
|
|
269
|
+
interface State { ${first} }
|
|
270
|
+
interface State { ${second} }
|
|
271
|
+
|
|
272
|
+
export function Component({ initial }: { initial: State }) {
|
|
273
|
+
return <div />
|
|
274
|
+
}
|
|
275
|
+
`
|
|
276
|
+
|
|
277
|
+
for (const source of [
|
|
278
|
+
merged('label: string', "kind: 'a' | 'b'"),
|
|
279
|
+
merged("kind: 'a' | 'b'", 'label: string'),
|
|
280
|
+
merged('label: string', 'count: number'),
|
|
281
|
+
]) {
|
|
282
|
+
const ctx = analyzeComponent(source, 'Component.tsx')
|
|
283
|
+
expect(typeOf(ctx, 'initial')).toEqual({ kind: 'unknown', raw: 'unknown' })
|
|
284
|
+
}
|
|
285
|
+
})
|
|
211
286
|
})
|