@barefootjs/jsx 0.37.1 → 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/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/adapters/jsx-adapter.d.ts.map +1 -1
- package/dist/analyzer.d.ts +61 -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/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/debug.d.ts +61 -5
- package/dist/debug.d.ts.map +1 -1
- package/dist/errors.d.ts +7 -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 +33 -0
- package/dist/format-date-refusal.d.ts.map +1 -0
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3260 -1914
- 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/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.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/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/control-flow/plan/lazy-row-eligibility.d.ts +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/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-reactive.d.ts +43 -2
- package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.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 +9 -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 +4 -0
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +66 -1
- 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/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/to-locale-date-lowering.d.ts +7 -4
- package/dist/to-locale-date-lowering.d.ts.map +1 -1
- package/dist/types.d.ts +104 -17
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +878 -4528
- package/src/__tests__/action-accessor.test.ts +166 -0
- 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__/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 +78 -25
- 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 +2 -2
- 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__/props-destructuring.test.ts +79 -4
- package/src/__tests__/reactive-factory-arity.test.ts +311 -0
- package/src/__tests__/ref-attr-absent-at-ssr.test.ts +472 -0
- package/src/__tests__/ssr-portal-ref-callback.test.ts +219 -0
- package/src/action-accessor.ts +289 -0
- package/src/adapters/env-signal.ts +2 -21
- package/src/adapters/interface.ts +13 -0
- package/src/adapters/jsx-adapter.ts +17 -0
- package/src/analyzer.ts +589 -60
- package/src/async-action-refusal.ts +254 -0
- package/src/builtin-lowering-plugins.ts +0 -2
- package/src/compiler.ts +27 -0
- package/src/component-root-scope-comment.ts +59 -0
- package/src/debug.ts +318 -103
- package/src/errors.ts +83 -1
- package/src/event-handler-name.ts +14 -0
- package/src/expression-parser.ts +41 -17
- package/src/format-date-refusal.ts +147 -0
- package/src/index.ts +14 -3
- package/src/ir-to-client-js/build-references.ts +6 -2
- package/src/ir-to-client-js/child-components.ts +8 -3
- package/src/ir-to-client-js/collect-elements.ts +28 -13
- 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-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-insert.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +2 -1
- package/src/ir-to-client-js/control-flow/plan/build-loop.ts +23 -11
- package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -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.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/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-reactive.ts +140 -10
- package/src/ir-to-client-js/emit-registration.ts +6 -11
- 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 +9 -3
- 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 +21 -16
- package/src/ir-to-client-js/source-map.ts +2 -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 +89 -7
- package/src/jsx-to-ir.ts +772 -50
- package/src/opaque-local-accessor.ts +44 -0
- package/src/rich-type-refusal.ts +1 -1
- package/src/signal-initializer.ts +36 -0
- package/src/template-position-walk.ts +203 -0
- package/src/to-locale-date-lowering.ts +7 -4
- package/src/types.ts +103 -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,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
|
|
|
@@ -283,55 +285,60 @@ const PAGES: PageSpec[] = [
|
|
|
283
285
|
return undefined
|
|
284
286
|
},
|
|
285
287
|
},
|
|
286
|
-
{ path: 'core/rendering/fragment.md' },
|
|
287
288
|
{ path: 'core/reactivity/create-signal.md' },
|
|
288
289
|
{ path: 'core/reactivity/create-effect.md' },
|
|
289
290
|
{ path: 'core/reactivity/create-memo.md' },
|
|
290
291
|
{ path: 'core/reactivity/on-mount.md' },
|
|
291
292
|
{ path: 'core/reactivity/on-cleanup.md' },
|
|
292
293
|
{ path: 'core/reactivity/untrack.md' },
|
|
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
|
-
{ path: 'core/components/component-authoring.
|
|
298
|
+
{ path: 'core/components/component-authoring.md' },
|
|
295
299
|
{ path: 'core/components/children-slots.md' },
|
|
296
|
-
{
|
|
300
|
+
{
|
|
301
|
+
path: 'core/components/context-api.md',
|
|
302
|
+
pageSkip: (body: string) => {
|
|
303
|
+
if (/\bapp\.get\b/.test(body)) return 'server route example (not a component)'
|
|
304
|
+
return undefined
|
|
305
|
+
},
|
|
306
|
+
},
|
|
297
307
|
{ path: 'core/components/portals.md' },
|
|
298
|
-
{ path: 'core/components/props-type-safety.md' },
|
|
299
308
|
{ path: 'core/components/styling.md' },
|
|
300
|
-
{ path: 'core/
|
|
301
|
-
{ path: 'core/
|
|
302
|
-
{ path: 'core/core-concepts/mpa-style.md' },
|
|
303
|
-
{ path: 'core/core-concepts/ai-native.md' },
|
|
309
|
+
{ path: 'core/how-it-works.mdx' },
|
|
310
|
+
{ path: 'core/advanced/testing-and-cli.md' },
|
|
304
311
|
{ path: 'core/adapters/hono-adapter.md' },
|
|
305
312
|
{ path: 'core/adapters/go-template-adapter.md' },
|
|
306
313
|
{ path: 'core/adapters/custom-adapter.md' },
|
|
307
|
-
// `core/advanced/code-splitting.md` is deliberately absent: it documents
|
|
308
|
-
// stock Vite/Rollup build config (`manualChunks`) and carries no
|
|
309
|
-
// component code, while this extractor only reads ```tsx fences. Adding
|
|
310
|
-
// a token component there purely to keep the page listed here would
|
|
311
|
-
// hollow out the check rather than extend it.
|
|
312
|
-
{ path: 'core/advanced/compiler-internals.md' },
|
|
313
314
|
// `core/advanced/error-codes.md` is handled by the per-BFxxx
|
|
314
315
|
// matcher (see bottom of file) rather than the general extractor:
|
|
315
316
|
// its `negative-all-adapters` strictness ("BFxxx must be the ONLY
|
|
316
317
|
// fatal") doesn't fit a reference page whose minimal reproductions
|
|
317
318
|
// routinely trip unrelated checks, and the per-section walker can
|
|
318
319
|
// tie each ❌ snippet to its parent `### BFxxx —` H3.
|
|
319
|
-
{ path: 'core/advanced/performance.md' },
|
|
320
320
|
{ path: 'core/reactivity.md' },
|
|
321
|
-
{
|
|
322
|
-
path: 'core/reactivity/shared-state.md',
|
|
323
|
-
pageSkip: (body: string) => {
|
|
324
|
-
if (/\bapp\.get\b/.test(body)) return 'server route example (not a component)'
|
|
325
|
-
return undefined
|
|
326
|
-
},
|
|
327
|
-
},
|
|
328
321
|
{ path: 'core/introduction.mdx' },
|
|
329
322
|
]
|
|
330
323
|
|
|
331
324
|
const adapter = new TestAdapter()
|
|
332
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
|
+
|
|
333
340
|
function compileOnce(source: string) {
|
|
334
|
-
const result =
|
|
341
|
+
const result = compileDocExample(source)
|
|
335
342
|
const fatals = result.errors.filter(e => e.severity === 'error')
|
|
336
343
|
const clientJsFile = result.files.find(f => f.type === 'clientJs')
|
|
337
344
|
return { fatals, clientJs: clientJsFile?.content ?? null }
|
|
@@ -458,6 +465,7 @@ interface ErrorCodeContract {
|
|
|
458
465
|
const ERROR_CODES_DOC_TOO_MINIMAL: Record<string, string> = {
|
|
459
466
|
BF021: 'BF021 has multiple ❌ snippets in placeholder form (`.map(...)`) — covered concretely by `jsx-compatibility.md` tests; the doc-form snippets here remain placeholder',
|
|
460
467
|
BF044: 'BF044 fires only when a real signal getter is bound. The doc snippet `<Child count={count} />` references an undeclared `count`, so the check is silent — doc snippet needs enrichment',
|
|
468
|
+
BF102: 'BF102 is raised only by the Go adapter, from a boolean-test position its template grammar cannot host (an `else if` predicate, a module-scope helper in an `if` condition) — `TestAdapter` is JS-runtime-style and executes the condition verbatim, so the check is correctly silent here. Covered by the Go adapter\'s conformance pins (`module-helper-boolcontext-call`).',
|
|
461
469
|
BF101: 'BF101 is raised by the non-JS template adapters\' own per-adapter checks (Go/Mojo/Xslate/… `renderLoop` / callback lowering), not by the shared `isSupported` gate every adapter shares — `TestAdapter` here is JS-runtime-style like Hono and executes both doc snippets verbatim, so the check is correctly silent. Covered concretely (compiled against the real 8 template adapters, with verbatim diagnostic text) by `jsx-compatibility.md`\'s doc-examples coverage and the `filter-nested-callback-predicate` / `filter-nested-find-predicate` / `static-array-from-props` adapter-conformance fixtures.',
|
|
462
470
|
}
|
|
463
471
|
|
|
@@ -564,7 +572,7 @@ describe('docs/core/advanced/error-codes.md per-BFxxx matchers', () => {
|
|
|
564
572
|
const source = isExpression
|
|
565
573
|
? EXPRESSION_SCAFFOLD_HEADER + c.body + EXPRESSION_SCAFFOLD_FOOTER
|
|
566
574
|
: c.body + '\n'
|
|
567
|
-
const result =
|
|
575
|
+
const result = compileDocExample(source)
|
|
568
576
|
const matched = result.errors.find(e => e.code === c.code)
|
|
569
577
|
if (!matched) {
|
|
570
578
|
const got = result.errors.map(e => ` ${e.severity} ${e.code}: ${e.message}`).join('\n') || ' (no diagnostics)'
|
|
@@ -575,3 +583,48 @@ describe('docs/core/advanced/error-codes.md per-BFxxx matchers', () => {
|
|
|
575
583
|
})
|
|
576
584
|
}
|
|
577
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
|
+
})
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `isEventHandlerName` is the one event-handler classification (`on[A-Z]`)
|
|
3
|
+
* shared by the IR builder, client-JS prop wiring and the SSR adapters. A
|
|
4
|
+
* prop merely starting with `on` (`only`, `once`) is data.
|
|
5
|
+
*/
|
|
6
|
+
import { describe, test, expect } from 'bun:test'
|
|
7
|
+
import { analyzeComponent, buildMetadata, jsxToIR, isEventHandlerName } from '../index'
|
|
8
|
+
import { compileJSX } from '../compiler'
|
|
9
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
10
|
+
import type { IRLoop, IRNode } from '../types'
|
|
11
|
+
|
|
12
|
+
describe('isEventHandlerName', () => {
|
|
13
|
+
test('on + uppercase letter is an event handler', () => {
|
|
14
|
+
for (const name of ['onClick', 'onValueChange', 'onX']) expect(isEventHandlerName(name)).toBe(true)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
test('names that merely start with `on` are data', () => {
|
|
18
|
+
for (const name of ['on', 'only', 'once', 'online', 'onclick', 'on-x', 'tone']) {
|
|
19
|
+
expect(isEventHandlerName(name)).toBe(false)
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test("a loop child component's `isEventHandler` flag uses it", () => {
|
|
24
|
+
const source = `
|
|
25
|
+
'use client'
|
|
26
|
+
function Mark(props: { once?: boolean; onPick?: () => void }) { return <em>x</em> }
|
|
27
|
+
export function Row() {
|
|
28
|
+
const items = [{ id: 'a', flag: true }]
|
|
29
|
+
return <ul>{items.map(o => <Mark key={o.id} once={o.flag} onPick={() => {}} />)}</ul>
|
|
30
|
+
}
|
|
31
|
+
`
|
|
32
|
+
const ctx = analyzeComponent(source, 'test.tsx', 'Row')
|
|
33
|
+
buildMetadata(ctx)
|
|
34
|
+
const root = jsxToIR(ctx)!
|
|
35
|
+
const findLoop = (n: IRNode): IRLoop | null => {
|
|
36
|
+
if (n.type === 'loop') return n as IRLoop
|
|
37
|
+
for (const c of (n as { children?: IRNode[] }).children ?? []) {
|
|
38
|
+
const hit = findLoop(c)
|
|
39
|
+
if (hit) return hit
|
|
40
|
+
}
|
|
41
|
+
return null
|
|
42
|
+
}
|
|
43
|
+
const props = findLoop(root)!.childComponent!.props
|
|
44
|
+
expect(props.find(p => p.name === 'once')!.isEventHandler).toBe(false)
|
|
45
|
+
expect(props.find(p => p.name === 'onPick')!.isEventHandler).toBe(true)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
// Client-JS consequence: a loop row's child component receives an
|
|
49
|
+
// `on`-prefixed DATA prop as a live getter like any other reactive prop,
|
|
50
|
+
// not as a one-shot value the way a handler is passed. `onPick` stays a
|
|
51
|
+
// plain handler entry.
|
|
52
|
+
test('a loop child component receives `only`/`once` as reactive getters, not handler values', () => {
|
|
53
|
+
const source = `
|
|
54
|
+
'use client'
|
|
55
|
+
import { createSignal } from '@barefootjs/client'
|
|
56
|
+
function Child(props: { only?: string; once?: boolean; onPick?: () => void }) {
|
|
57
|
+
return <span data-only={props.only}>c</span>
|
|
58
|
+
}
|
|
59
|
+
export function Rows() {
|
|
60
|
+
const [sel, setSel] = createSignal('a')
|
|
61
|
+
const [flag] = createSignal(false)
|
|
62
|
+
const items = [{ id: 'x' }, { id: 'y' }]
|
|
63
|
+
return <ul>{items.map(it => <li key={it.id}><Child only={sel()} once={flag()} onPick={() => setSel(it.id)} /></li>)}</ul>
|
|
64
|
+
}
|
|
65
|
+
`
|
|
66
|
+
const result = compileJSX(source, 'Rows.tsx', { adapter: new TestAdapter() })
|
|
67
|
+
expect(result.errors).toHaveLength(0)
|
|
68
|
+
const clientJs = result.files.find(f => f.type === 'clientJs')!.content
|
|
69
|
+
expect(clientJs).toContain('get only() { return sel() }')
|
|
70
|
+
expect(clientJs).toContain('get once() { return flag() }')
|
|
71
|
+
expect(clientJs).not.toContain('only: sel()')
|
|
72
|
+
expect(clientJs).toContain('onPick: () => setSel(it.id)')
|
|
73
|
+
})
|
|
74
|
+
})
|
|
@@ -8,12 +8,13 @@
|
|
|
8
8
|
* - TS type annotations in the block body are stripped from the client
|
|
9
9
|
* bundle (the old raw-slice carrier spliced them verbatim — invalid JS).
|
|
10
10
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
11
|
+
* The leaf `data-key` rides BOTH renders of a flatMap leaf: the CSR string
|
|
12
|
+
* template (`h:` descriptor HTML, `escapeAttr`-wrapped) and the Hono rawBody
|
|
13
|
+
* SSR (the leaf's `key` attribute is rewritten to `data-key={String(key)}`
|
|
14
|
+
* in the branded source text). Hydration therefore finds the attribute
|
|
15
|
+
* already in place instead of adding it, which is what the browser oracle
|
|
16
|
+
* (`tag-cloud` fixture) pins. Slot comment markers inside flatMap leaves
|
|
17
|
+
* remain client-side only.
|
|
17
18
|
*/
|
|
18
19
|
|
|
19
20
|
import { describe, test, expect } from 'bun:test'
|
|
@@ -165,7 +166,7 @@ export function F() {
|
|
|
165
166
|
const cj = r.files.find(f => f.type === 'clientJs')!.content
|
|
166
167
|
// Flattened source with descriptor leaves…
|
|
167
168
|
expect(cj).toMatch(/mapArray\(\(\) => \(todos\(\)\)\.flatMap\(\(t\) => \{/)
|
|
168
|
-
expect(cj).toMatch(/\(\{ k: \(`\$\{t\.id\}:\$\{tag\}`\), h: `<li>/)
|
|
169
|
+
expect(cj).toMatch(/\(\{ k: \(`\$\{t\.id\}:\$\{tag\}`\), h: `<li data-key="\$\{escapeAttr\(String\(\(`\$\{t\.id\}:\$\{tag\}`\)\)\)\}">/)
|
|
169
170
|
// …keyed on the leaf key with index fallback…
|
|
170
171
|
expect(cj).toMatch(/\(__bfD, __bfI\) => String\(__bfD\.k \?\? __bfI\)/)
|
|
171
172
|
// …renderItem builds from the descriptor HTML and patches on change…
|
|
@@ -174,8 +175,9 @@ export function F() {
|
|
|
174
175
|
// …and the statements-before-return are NOT duplicated as a renderItem
|
|
175
176
|
// preamble (the segments carrier is the single door).
|
|
176
177
|
expect(cj).not.toMatch(/return \[\];* if \(__existing\)/)
|
|
177
|
-
//
|
|
178
|
-
|
|
178
|
+
// The leaf key is rendered as an escaped `data-key` in the descriptor HTML
|
|
179
|
+
// (SSR emits the same attribute, so hydration never has to add it).
|
|
180
|
+
expect(cj).toMatch(/data-key="\$\{escapeAttr\(/)
|
|
179
181
|
})
|
|
180
182
|
|
|
181
183
|
test('a leaf with an event handler refuses loudly (no silent dead DOM)', () => {
|