@barefootjs/jsx 0.38.0 → 0.39.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/dist/action-accessor.d.ts +159 -0
  2. package/dist/action-accessor.d.ts.map +1 -0
  3. package/dist/adapters/interface.d.ts +19 -0
  4. package/dist/adapters/interface.d.ts.map +1 -1
  5. package/dist/adapters/jsx-adapter.d.ts.map +1 -1
  6. package/dist/analyzer.d.ts +49 -1
  7. package/dist/analyzer.d.ts.map +1 -1
  8. package/dist/async-action-refusal.d.ts +45 -0
  9. package/dist/async-action-refusal.d.ts.map +1 -0
  10. package/dist/compiler.d.ts.map +1 -1
  11. package/dist/debug.d.ts +61 -5
  12. package/dist/debug.d.ts.map +1 -1
  13. package/dist/errors.d.ts +4 -0
  14. package/dist/errors.d.ts.map +1 -1
  15. package/dist/format-date-refusal.d.ts +2 -6
  16. package/dist/format-date-refusal.d.ts.map +1 -1
  17. package/dist/html-types.d.ts +5 -0
  18. package/dist/html-types.d.ts.map +1 -1
  19. package/dist/index.d.ts +3 -2
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +1261 -422
  22. package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
  23. package/dist/ir-to-client-js/collect-elements.d.ts +17 -1
  24. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  25. package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
  26. package/dist/ir-to-client-js/compute-scope.d.ts.map +1 -1
  27. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -0
  28. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts.map +1 -1
  29. package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
  30. package/dist/ir-to-client-js/declaration-sort.d.ts +2 -1
  31. package/dist/ir-to-client-js/declaration-sort.d.ts.map +1 -1
  32. package/dist/ir-to-client-js/emit-module-level.d.ts.map +1 -1
  33. package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
  34. package/dist/ir-to-client-js/plan/declaration-emit.d.ts +7 -5
  35. package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
  36. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  37. package/dist/ir-to-client-js/reactivity.d.ts +10 -2
  38. package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
  39. package/dist/ir-to-client-js/types.d.ts +4 -0
  40. package/dist/ir-to-client-js/types.d.ts.map +1 -1
  41. package/dist/ir-to-client-js/utils.d.ts +52 -9
  42. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  43. package/dist/jsx-to-ir.d.ts.map +1 -1
  44. package/dist/signal-initializer.d.ts +24 -0
  45. package/dist/signal-initializer.d.ts.map +1 -0
  46. package/dist/ssr-defaults.d.ts.map +1 -1
  47. package/dist/ssr-seed-plan.d.ts.map +1 -1
  48. package/dist/template-position-walk.d.ts +68 -0
  49. package/dist/template-position-walk.d.ts.map +1 -0
  50. package/dist/types.d.ts +95 -0
  51. package/dist/types.d.ts.map +1 -1
  52. package/package.json +2 -2
  53. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +194 -4
  54. package/src/__tests__/action-accessor.test.ts +166 -0
  55. package/src/__tests__/adapter-output.test.ts +142 -0
  56. package/src/__tests__/create-mutation.test.ts +250 -0
  57. package/src/__tests__/create-query.test.ts +696 -0
  58. package/src/__tests__/csr-fragment-root-scope-shape.test.ts +156 -0
  59. package/src/__tests__/debug.test.ts +305 -0
  60. package/src/__tests__/doc-examples.test.ts +66 -3
  61. package/src/__tests__/ir-fragment-transparency.test.ts +23 -0
  62. package/src/__tests__/issue-3240-nested-static-map-event-delegation.test.ts +245 -0
  63. package/src/__tests__/jsx-text-linebreak-whitespace.test.ts +80 -0
  64. package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +445 -0
  65. package/src/__tests__/props-destructuring.test.ts +79 -4
  66. package/src/__tests__/reactive-factory-arity.test.ts +311 -0
  67. package/src/__tests__/ssr-portal-ref-callback.test.ts +5 -3
  68. package/src/action-accessor.ts +289 -0
  69. package/src/adapters/interface.ts +19 -0
  70. package/src/adapters/jsx-adapter.ts +47 -20
  71. package/src/analyzer.ts +593 -44
  72. package/src/async-action-refusal.ts +254 -0
  73. package/src/compiler.ts +81 -46
  74. package/src/debug.ts +351 -104
  75. package/src/errors.ts +39 -0
  76. package/src/format-date-refusal.ts +6 -125
  77. package/src/html-types.ts +14 -0
  78. package/src/index.ts +3 -1
  79. package/src/ir-to-client-js/build-references.ts +6 -2
  80. package/src/ir-to-client-js/collect-elements.ts +65 -4
  81. package/src/ir-to-client-js/compute-inlinability.ts +2 -1
  82. package/src/ir-to-client-js/compute-scope.ts +3 -1
  83. package/src/ir-to-client-js/control-flow/plan/build-insert.ts +1 -0
  84. package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -0
  85. package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +112 -3
  86. package/src/ir-to-client-js/control-flow/stringify/insert.ts +1 -1
  87. package/src/ir-to-client-js/csr-substitute.ts +11 -3
  88. package/src/ir-to-client-js/declaration-sort.ts +7 -3
  89. package/src/ir-to-client-js/emit-module-level.ts +7 -4
  90. package/src/ir-to-client-js/emit-registration.ts +5 -10
  91. package/src/ir-to-client-js/index.ts +7 -2
  92. package/src/ir-to-client-js/phases/ref-callbacks.ts +1 -1
  93. package/src/ir-to-client-js/plan/build-declaration-emit.ts +16 -15
  94. package/src/ir-to-client-js/plan/declaration-emit.ts +7 -5
  95. package/src/ir-to-client-js/prop-handling.ts +4 -0
  96. package/src/ir-to-client-js/reactivity.ts +32 -1
  97. package/src/ir-to-client-js/stringify/declaration-emit.ts +14 -7
  98. package/src/ir-to-client-js/types.ts +4 -0
  99. package/src/ir-to-client-js/utils.ts +184 -21
  100. package/src/jsx-to-ir.ts +111 -14
  101. package/src/signal-initializer.ts +36 -0
  102. package/src/ssr-defaults.ts +10 -0
  103. package/src/ssr-seed-plan.ts +12 -1
  104. package/src/template-position-walk.ts +203 -0
  105. package/src/types.ts +99 -0
@@ -0,0 +1,696 @@
1
+ /**
2
+ * `createQuery` recognition (#3165): the value is collected as a signal
3
+ * seeded from `options.initial`, and the request function is emitted into
4
+ * the client JS only. Since #3166 (#3158-B), a DIRECT read of the action's
5
+ * `isPending()` / `error()` accessors — as the WHOLE expression of a
6
+ * condition, or (`isPending()` only) of an ARIA boolean-state attribute — is
7
+ * an ordinary seeded value (`false` / `undefined`, spec/async.md §7.3), not a
8
+ * refusal; a direct read anywhere else (where the seed would not render like
9
+ * Hono), calling the action itself, and any compound/transitive read
10
+ * (through a memo, constant or function) still refuse with BF117. Cross-adapter output lives in the `create-query-*`
11
+ * conformance fixtures; this file pins the compiler-internal decisions.
12
+ */
13
+
14
+ import { describe, test, expect } from 'bun:test'
15
+ import ts from 'typescript'
16
+ import path from 'path'
17
+ import { compileJSX } from '../compiler'
18
+ import { TestAdapter } from '../adapters/test-adapter'
19
+ import { HonoAdapter } from '../../../adapter-hono/src/adapter/hono-adapter'
20
+ import { GoTemplateAdapter } from '../../../adapter-go-template/src/index'
21
+ import { extractInitBody, extractTemplateBody } from './staged-ir/helpers'
22
+
23
+ function compile(source: string, options: { hono?: boolean; program?: ts.Program; fileName?: string } = {}) {
24
+ const fileName = options.fileName ?? 'Component.tsx'
25
+ const result = compileJSX(source, fileName, {
26
+ adapter: options.hono ? new HonoAdapter() : new TestAdapter(),
27
+ ...(options.program ? { program: options.program } : {}),
28
+ })
29
+ const clientJs = result.files.find((f) => f.type === 'clientJs')?.content ?? ''
30
+ const ssr = result.files.find((f) => f.type === 'markedTemplate')?.content ?? ''
31
+ return {
32
+ codes: result.errors.map((e) => e.code),
33
+ errors: result.errors,
34
+ clientJs,
35
+ initBody: extractInitBody(clientJs),
36
+ templateBody: extractTemplateBody(clientJs),
37
+ ssr,
38
+ }
39
+ }
40
+
41
+ /**
42
+ * A program whose `@barefootjs/client` resolves to the real sources, so the
43
+ * TypeChecker sees `createQuery`'s real signature (aliases, `Reactive` brands).
44
+ */
45
+ function programFor(source: string, componentPath: string): ts.Program {
46
+ const repo = path.resolve(__dirname, '../../../..')
47
+ const compilerOptions: ts.CompilerOptions = {
48
+ target: ts.ScriptTarget.Latest,
49
+ module: ts.ModuleKind.ESNext,
50
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
51
+ jsx: ts.JsxEmit.ReactJSX,
52
+ strict: true,
53
+ skipLibCheck: true,
54
+ noEmit: true,
55
+ allowImportingTsExtensions: true,
56
+ paths: {
57
+ '@barefootjs/client': [path.join(repo, 'packages/client/src/index.ts')],
58
+ '@barefootjs/client/async': [path.join(repo, 'packages/client/src/async.ts')],
59
+ '@barefootjs/client/reactive': [path.join(repo, 'packages/client/src/reactive.ts')],
60
+ },
61
+ }
62
+ const defaultHost = ts.createCompilerHost(compilerOptions)
63
+ const host: ts.CompilerHost = {
64
+ ...defaultHost,
65
+ getSourceFile(fileName, languageVersion) {
66
+ if (path.resolve(fileName) === componentPath) {
67
+ return ts.createSourceFile(fileName, source, languageVersion, true, ts.ScriptKind.TSX)
68
+ }
69
+ return defaultHost.getSourceFile(fileName, languageVersion)
70
+ },
71
+ fileExists(fileName) {
72
+ return path.resolve(fileName) === componentPath || defaultHost.fileExists(fileName)
73
+ },
74
+ readFile(fileName) {
75
+ return path.resolve(fileName) === componentPath ? source : defaultHost.readFile(fileName)
76
+ },
77
+ }
78
+ return ts.createProgram([componentPath], compilerOptions, host)
79
+ }
80
+
81
+ const MODE_A = `
82
+ 'use client'
83
+ import { createQuery, http } from '@barefootjs/client'
84
+ type Post = { id: number; title: string }
85
+ export function PostList(props: { posts: Post[]; userId: number }) {
86
+ const [posts, fetchPosts] = createQuery(() => http.get<Post[]>('/api/posts?user=' + props.userId), { initial: props.posts })
87
+ return (
88
+ <div>
89
+ <ul>{posts().map((post) => <li key={post.id}>{post.title}</li>)}</ul>
90
+ <button onClick={() => fetchPosts()}>reload</button>
91
+ </div>
92
+ )
93
+ }
94
+ `
95
+
96
+ describe('createQuery value and client JS (#3165)', () => {
97
+ test('emits the factory call with props read live, and no createSignal or prop sync for it', () => {
98
+ const { codes, initBody } = compile(MODE_A)
99
+ expect(codes).toEqual([])
100
+ expect(initBody).toContain(
101
+ "const [posts, fetchPosts] = createQuery(() => http.get('/api/posts?user=' + _p.userId), { initial: _p.posts })",
102
+ )
103
+ expect(initBody).not.toContain('createSignal(')
104
+ // `initial` is taken once: no controlled-prop sync effect writes the prop back.
105
+ expect(initBody).not.toContain('fetchPosts(__val)')
106
+ })
107
+
108
+ test('the CSR template reads `initial`, never the request function', () => {
109
+ const { templateBody } = compile(MODE_A)
110
+ expect(templateBody).toContain('(_p.posts).map(')
111
+ expect(templateBody).not.toContain('http.get')
112
+ })
113
+
114
+ test('Hono SSR seeds the value from `initial` and never emits the request function', () => {
115
+ const { codes, ssr } = compile(MODE_A, { hono: true })
116
+ expect(codes).toEqual([])
117
+ expect(ssr).toContain('const posts = () => props.posts')
118
+ expect(ssr).not.toContain('http.get')
119
+ // A prop read only by the request function still reaches the client.
120
+ expect(ssr).toContain("__hydrateProps['userId'] = props.userId")
121
+ })
122
+
123
+ test('an aliased import re-emits the call through the alias', () => {
124
+ // Aliases resolve through the TypeChecker, as for every reactive primitive.
125
+ const source = `
126
+ 'use client'
127
+ import { createQuery as query, http } from '@barefootjs/client'
128
+ export function C(props: { items: string[] }) {
129
+ const [items] = query(() => http.get<string[]>('/api/items'), { initial: props.items })
130
+ return <ul>{items().map((i) => <li key={i}>{i}</li>)}</ul>
131
+ }
132
+ `
133
+ const componentPath = path.join(__dirname, '_create-query-alias.tsx')
134
+ const { codes, initBody } = compile(source, { program: programFor(source, componentPath), fileName: componentPath })
135
+ expect(codes).toEqual([])
136
+ expect(initBody).toContain("const [items] = query(() => http.get('/api/items'), { initial: _p.items })")
137
+ })
138
+
139
+ test('the value-elided form keeps just the action', () => {
140
+ const { codes, initBody } = compile(`
141
+ 'use client'
142
+ import { createQuery, http } from '@barefootjs/client'
143
+ export function C() {
144
+ const [, fetchItems] = createQuery(() => http.get<string[]>('/api/items'))
145
+ return <button onClick={() => fetchItems()}>load</button>
146
+ }
147
+ `)
148
+ expect(codes).toEqual([])
149
+ expect(initBody).toContain("const [, fetchItems] = createQuery(() => http.get('/api/items'))")
150
+ })
151
+
152
+ // #3245: the value-elided form synthesizes an internal getter name
153
+ // (`__bfGet_<action>`, analyzer.ts's `collectFactorySignal`) that nothing in
154
+ // the source ever references. It must not leak into any adapter's
155
+ // server-facing output — the SSR module, the SSR-defaults manifest, or a
156
+ // generated props struct/constructor.
157
+ test('an elided value never leaks `__bfGet_` into SSR-facing output', () => {
158
+ const source = `
159
+ 'use client'
160
+ import { createQuery, http } from '@barefootjs/client'
161
+ export function C() {
162
+ const [, fetchItems] = createQuery(() => http.get<string[]>('/api/items'))
163
+ return <button disabled={fetchItems.isPending()} onClick={() => fetchItems()}>load</button>
164
+ }
165
+ `
166
+ const honoResult = compileJSX(source, 'C.tsx', { adapter: new HonoAdapter() })
167
+ expect(honoResult.errors).toEqual([])
168
+ const ssr = honoResult.files.find((f) => f.type === 'markedTemplate')?.content ?? ''
169
+ expect(ssr).not.toContain('__bfGet_')
170
+ expect(ssr).toContain('Object.assign(() => {}, { isPending: () => false, error: () => undefined })')
171
+ const honoSsrDefaults = honoResult.files.find((f) => f.type === 'ssrDefaults')?.content ?? ''
172
+ expect(honoSsrDefaults).not.toContain('__bfGet_')
173
+
174
+ const goResult = compileJSX(source, 'C.tsx', { adapter: new GoTemplateAdapter() })
175
+ expect(goResult.errors).toEqual([])
176
+ const goSsrDefaults = goResult.files.find((f) => f.type === 'ssrDefaults')?.content ?? ''
177
+ expect(goSsrDefaults).not.toContain('__bfGet_')
178
+ const goTypes = goResult.files.find((f) => f.type === 'types')?.content ?? ''
179
+ expect(goTypes).not.toContain('__bfGet_')
180
+ })
181
+
182
+ // #3245 follow-up (Pullfrog): an untyped object-array `initial` makes the
183
+ // Go adapter synthesize a struct for the elided value's item shape
184
+ // (`emitSynthStructs`) even though the field/ctor-init that would
185
+ // reference it is already skipped — the struct itself still leaked the
186
+ // internal `__bfGet_<action>` name into `.types`. The previous test's
187
+ // `initial`-less repro can't catch this, since no object-array shape means
188
+ // no struct is synthesized either way.
189
+ test('an elided value with an object-array `initial` does not leak `__bfGet_` into a Go synth struct', () => {
190
+ const source = `
191
+ 'use client'
192
+ import { createQuery, http } from '@barefootjs/client'
193
+ export function C() {
194
+ const [, fetchItems] = createQuery(() => http.get('/api/items'), { initial: [{ id: 1, name: 'a' }] })
195
+ return <button disabled={fetchItems.isPending()} onClick={() => fetchItems()}>load</button>
196
+ }
197
+ `
198
+ const goResult = compileJSX(source, 'C.tsx', { adapter: new GoTemplateAdapter() })
199
+ expect(goResult.errors).toEqual([])
200
+ const goTypes = goResult.files.find((f) => f.type === 'types')?.content ?? ''
201
+ expect(goTypes).not.toContain('__bfGet_')
202
+ })
203
+
204
+ test('`initial` absent seeds undefined; a shorthand `initial` is read structurally', () => {
205
+ const absent = compile(`
206
+ 'use client'
207
+ import { createQuery, http } from '@barefootjs/client'
208
+ export function C() {
209
+ const [items] = createQuery(() => http.get<string[]>('/api/items'))
210
+ return <p>{items() ? 'loaded' : 'loading'}</p>
211
+ }
212
+ `, { hono: true })
213
+ expect(absent.codes).toEqual([])
214
+ expect(absent.ssr).toContain('const items = () => undefined')
215
+
216
+ const shorthand = compile(`
217
+ 'use client'
218
+ import { createQuery, http } from '@barefootjs/client'
219
+ export function C({ initial }: { initial?: string[] }) {
220
+ const [items] = createQuery(() => http.get<string[]>('/api/items'), { initial })
221
+ return <p>{items() ? 'loaded' : 'loading'}</p>
222
+ }
223
+ `, { hono: true })
224
+ expect(shorthand.codes).toEqual([])
225
+ expect(shorthand.ssr).toContain('const items = () => initial')
226
+ })
227
+
228
+ test('options that cannot be read structurally seed from a member read of the options', () => {
229
+ const { codes, ssr } = compile(`
230
+ 'use client'
231
+ import { createQuery, http } from '@barefootjs/client'
232
+ export function C(props: { base: { initial?: string[] } }) {
233
+ const [items] = createQuery(() => http.get<string[]>('/api/items'), { ...props.base, ttl: 1000 })
234
+ return <p>{items() ? 'loaded' : 'loading'}</p>
235
+ }
236
+ `, { hono: true })
237
+ expect(codes).toEqual([])
238
+ expect(ssr).toContain('const items = () => ({ ...props.base, ttl: 1000 }).initial')
239
+ })
240
+
241
+ test('a forwarded action gets an SSR no-op', () => {
242
+ const { codes, ssr } = compile(`
243
+ 'use client'
244
+ import { createQuery, http } from '@barefootjs/client'
245
+ function Retry(props: { onRetry: () => void }) {
246
+ return <button onClick={() => props.onRetry()}>retry</button>
247
+ }
248
+ export function C(props: { items: string[] }) {
249
+ const [items, fetchItems] = createQuery(() => http.get<string[]>('/api/items'), { initial: props.items })
250
+ return <div><p>{items().length}</p><Retry onRetry={fetchItems} /></div>
251
+ }
252
+ `, { hono: true, fileName: 'C.tsx' })
253
+ expect(codes).toEqual([])
254
+ // #3166: the SSR no-op now carries working `isPending`/`error` methods
255
+ // too (spec/async.md §7.3's seed), not just a callable stub — a forwarded
256
+ // reference that reaches a template position through a child could call
257
+ // them (out of scope for THIS fixture, which only forwards the action as
258
+ // a value, but the stub must not assume the caller never will).
259
+ expect(ssr).toContain('const fetchItems: any = Object.assign(() => {}, { isPending: () => false, error: () => undefined })')
260
+ })
261
+ })
262
+
263
+ describe('createQuery arity (BF115 / BF116)', () => {
264
+ test('a two-argument call and a two-element destructure are accepted', () => {
265
+ expect(compile(MODE_A).codes).toEqual([])
266
+ })
267
+
268
+ test('a three-element destructure is BF115', () => {
269
+ const { codes } = compile(`
270
+ 'use client'
271
+ import { createQuery, http } from '@barefootjs/client'
272
+ export function C() {
273
+ const [a, b, c] = createQuery(() => http.get('/api/items'))
274
+ return <p>{a()}</p>
275
+ }
276
+ `)
277
+ expect(codes).toContain('BF115')
278
+ })
279
+
280
+ test('a third argument is BF116', () => {
281
+ const { codes } = compile(`
282
+ 'use client'
283
+ import { createQuery, http } from '@barefootjs/client'
284
+ export function C() {
285
+ const [a] = createQuery(() => http.get('/api/items'), {}, 'extra')
286
+ return <p>{a()}</p>
287
+ }
288
+ `)
289
+ expect(codes).toContain('BF116')
290
+ })
291
+ })
292
+
293
+ describe('createQuery action accessors seed as ordinary IR-visible values (#3166, #3158-B)', () => {
294
+ const SEEDED = `
295
+ 'use client'
296
+ import { createQuery, http } from '@barefootjs/client'
297
+ type Post = { id: number; title: string }
298
+ export function PostList(props: { posts: Post[] }) {
299
+ const [posts, fetchPosts] = createQuery(() => http.get<Post[]>('/api/posts'), { initial: props.posts })
300
+ return (
301
+ <div aria-busy={fetchPosts.isPending()}>
302
+ {fetchPosts.error() ? <p>Failed to load</p> : null}
303
+ <ul>{posts().map((post) => <li key={post.id}>{post.title}</li>)}</ul>
304
+ </div>
305
+ )
306
+ }
307
+ `
308
+
309
+ test('compiles clean on Hono, whose SSR stub carries working isPending/error methods', () => {
310
+ const { codes, ssr } = compile(SEEDED, { hono: true })
311
+ expect(codes).toEqual([])
312
+ // Hono's execution model runs the real JS text (unlike a DSL adapter's
313
+ // `parsedCondition`-based seed substitution below) — the read stays
314
+ // `fetchPosts.isPending()` verbatim, and the SSR no-op stub is what
315
+ // actually answers it (`false` / `undefined`, spec/async.md §7.3).
316
+ expect(ssr).toContain('fetchPosts.isPending()')
317
+ expect(ssr).toContain('fetchPosts.error()')
318
+ expect(ssr).toContain('const fetchPosts: any = Object.assign(() => {}, { isPending: () => false, error: () => undefined })')
319
+ })
320
+
321
+ test('the CSR path keeps the real accessor call — reactivity survives hydration', () => {
322
+ // A real `ts.Program` (as the real pipeline always supplies —
323
+ // `@barefootjs/vite`'s plugin, `packages/vite/src/plugin.ts`, calls
324
+ // `compileJSX` with `program: corpusProgram.programFor(...)` on every
325
+ // component, never without one): `isPending()` carries a `Reactive<T>`
326
+ // brand the TypeChecker resolves, which is what marks the attribute
327
+ // reactive enough to get a live DOM patch — without a checker, neither
328
+ // this accessor's brand nor a bare signal/props regex fallback fires, so
329
+ // this fixture's `aria-busy` would (mis-)compile as a static, one-shot
330
+ // value under the no-checker `compile()` default. See
331
+ // `shouldAutoDeferReactiveBrand`'s own checker-required gate for the
332
+ // same reason.
333
+ const componentPath = path.join(__dirname, '_create-query-seeded.tsx')
334
+ const { codes, initBody } = compile(SEEDED, { program: programFor(SEEDED, componentPath), fileName: componentPath })
335
+ expect(codes).toEqual([])
336
+ // The client must still call the REAL query accessor to patch the DOM
337
+ // when a request actually goes pending/fails — never a baked literal.
338
+ expect(initBody).toContain('fetchPosts.isPending()')
339
+ expect(initBody).toContain('fetchPosts.error()')
340
+ })
341
+
342
+ test('a direct read is structurally recognised via action-accessor.ts, not a name heuristic', () => {
343
+ // Same accessor name, same position role (`aria-busy`), same component:
344
+ // only the binding a real `createQuery` produced is seeded. `upload` is a
345
+ // different local whose value also has an `isPending()` method — a
346
+ // name-based match would seed it too (`{{false}}`); the structural one
347
+ // leaves it to the adapter's ordinary lowering.
348
+ const result = compileJSX(`
349
+ 'use client'
350
+ import { createQuery, http } from '@barefootjs/client'
351
+ type Tracker = { isPending: () => boolean }
352
+ export function C(props: { tracker: Tracker }) {
353
+ const [posts, fetchPosts] = createQuery(() => http.get<string[]>('/x'), { initial: [] as string[] })
354
+ const upload = props.tracker
355
+ return (
356
+ <div aria-busy={fetchPosts.isPending()}>
357
+ <span aria-busy={upload.isPending()}>{posts().length}</span>
358
+ </div>
359
+ )
360
+ }
361
+ `, 'C.tsx', { adapter: new GoTemplateAdapter() })
362
+ expect(result.errors.map((e) => e.code)).toEqual([])
363
+ const template = result.files.find((f) => f.type === 'markedTemplate')?.content ?? ''
364
+ expect(template).toContain('<div aria-busy="{{false}}"')
365
+ expect(template).toContain('<span aria-busy="{{.Tracker.IsPending}}"')
366
+ expect(template.match(/\{\{false\}\}/g)).toHaveLength(1)
367
+ expect(template).not.toContain('FetchPosts')
368
+ })
369
+ })
370
+
371
+ /**
372
+ * The seed is `false` (`isPending()`) / `undefined` (`error()`), rendered by
373
+ * DSL adapters as the literal `false`. It is admitted only where that renders
374
+ * exactly like Hono's real stub value (`action-accessor.ts`'s
375
+ * `seedableActionAccessorRead`); everywhere else the read still refuses with
376
+ * BF117, exactly as before #3166.
377
+ */
378
+ describe('createQuery action accessors seed only where the seed renders like Hono (#3166)', () => {
379
+ const withBody = (body: string) => `
380
+ 'use client'
381
+ import { createQuery, http } from '@barefootjs/client'
382
+ type Post = { id: number; title: string }
383
+ export function PostList(props: { posts: Post[] }) {
384
+ const [posts, fetchPosts] = createQuery(() => http.get<Post[]>('/api/posts'), { initial: props.posts })
385
+ return (
386
+ <div>
387
+ ${body}
388
+ <ul>{posts().map((post) => <li key={post.id}>{post.title}</li>)}</ul>
389
+ </div>
390
+ )
391
+ }
392
+ `
393
+
394
+ // Hono renders `{undefined}` / `{false}` as nothing, omits `title={undefined}`,
395
+ // and a DSL adapter renders the `false` literal as `false` (Mojolicious /
396
+ // Xslate: `0`) — so none of these has a sound seed.
397
+ const REFUSED: Array<[string, string]> = [
398
+ ['error() as a text child', '<span>{fetchPosts.error()}</span>'],
399
+ ['isPending() as a text child', '<b>{fetchPosts.isPending()}</b>'],
400
+ ['error() in a plain attribute', '<p title={fetchPosts.error()}>x</p>'],
401
+ ['error() in an aria-* attribute', '<p aria-label={fetchPosts.error()}>x</p>'],
402
+ ['error() in an ARIA boolean-state attribute', '<p aria-busy={fetchPosts.error()}>x</p>'],
403
+ ['isPending() in a plain attribute', '<p title={fetchPosts.isPending()}>x</p>'],
404
+ ['isPending() in a non-boolean aria-* attribute', '<p aria-label={fetchPosts.isPending()}>x</p>'],
405
+ ['isPending() in a data-* attribute', '<p data-pending={fetchPosts.isPending()}>x</p>'],
406
+ // A truthiness test, but the condition is raw template-part text every
407
+ // adapter lowers itself — nothing to substitute, so it is not lifted.
408
+ ['isPending() as a structured template-attribute ternary', "<p class={fetchPosts.isPending() ? 'busy' : 'idle'}>x</p>"],
409
+ ]
410
+
411
+ for (const [label, body] of REFUSED) {
412
+ test(`BF117 still refuses ${label}`, () => {
413
+ for (const hono of [false, true]) {
414
+ const { errors } = compile(withBody(body), { hono })
415
+ const bf117 = errors.filter((e) => e.code === 'BF117')
416
+ expect(bf117).toHaveLength(1)
417
+ expect(bf117[0].message).toContain("reads an async action's accessor")
418
+ }
419
+ })
420
+ }
421
+
422
+ const LIFTED: Array<[string, string, string]> = [
423
+ ['isPending() in aria-busy', '<p aria-busy={fetchPosts.isPending()}>x</p>', '<p aria-busy="{{false}}"'],
424
+ ['isPending() in another ARIA boolean-state attribute', '<p aria-hidden={fetchPosts.isPending()}>x</p>', '<p aria-hidden="{{false}}"'],
425
+ ['error() as a conditional test', '{fetchPosts.error() ? <p>failed</p> : null}', '{{if false}}<p'],
426
+ ['isPending() as a conditional test', '{fetchPosts.isPending() ? <p>loading</p> : null}', '{{if false}}<p'],
427
+ ]
428
+
429
+ for (const [label, body, seeded] of LIFTED) {
430
+ test(`lifts and seeds ${label}`, () => {
431
+ expect(compile(withBody(body)).codes).toEqual([])
432
+ expect(compile(withBody(body), { hono: true }).codes).toEqual([])
433
+ const result = compileJSX(withBody(body), 'PostList.tsx', { adapter: new GoTemplateAdapter() })
434
+ expect(result.errors).toEqual([])
435
+ const template = result.files.find((f) => f.type === 'markedTemplate')?.content ?? ''
436
+ expect(template).toContain(seeded)
437
+ expect(template).not.toContain('FetchPosts')
438
+ })
439
+ }
440
+
441
+ test('lifts and seeds an early-return if-statement condition', () => {
442
+ const source = `
443
+ 'use client'
444
+ import { createQuery, http } from '@barefootjs/client'
445
+ export function C() {
446
+ const [items, fetchItems] = createQuery(() => http.get<string[]>('/x'), { initial: [] as string[] })
447
+ if (fetchItems.error()) return <p>failed</p>
448
+ return <ul>{items().map((item) => <li key={item}>{item}</li>)}</ul>
449
+ }
450
+ `
451
+ expect(compile(source).codes).toEqual([])
452
+ const result = compileJSX(source, 'C.tsx', { adapter: new GoTemplateAdapter() })
453
+ expect(result.errors).toEqual([])
454
+ const template = result.files.find((f) => f.type === 'markedTemplate')?.content ?? ''
455
+ expect(template).toContain('{{if false}}<p')
456
+ expect(template).not.toContain('FetchItems')
457
+ // The CSR module-scope fallback template gets the JS seed text instead:
458
+ // `undefined` for `error()`, never the out-of-scope accessor call.
459
+ const { templateBody } = compile(source)
460
+ expect(templateBody).toContain('undefined ?')
461
+ expect(templateBody).not.toContain('fetchItems.error()')
462
+ })
463
+
464
+ test('a /* @client */ position is neither refused nor seeded', () => {
465
+ const result = compileJSX(withBody('{/* @client */ fetchPosts.error() ? <p>failed</p> : null}'), 'PostList.tsx', {
466
+ adapter: new GoTemplateAdapter(),
467
+ })
468
+ expect(result.errors).toEqual([])
469
+ const template = result.files.find((f) => f.type === 'markedTemplate')?.content ?? ''
470
+ expect(template).not.toContain('{{if false}}')
471
+ })
472
+ })
473
+
474
+ const ACTION_READS = `
475
+ 'use client'
476
+ import { createMemo, createQuery, http } from '@barefootjs/client'
477
+ type Post = { id: number; title: string }
478
+ export function PostList(props: { posts: Post[] }) {
479
+ const [posts, fetchPosts] = createQuery(() => http.get<Post[]>('/api/posts'), { initial: props.posts })
480
+ const busy = createMemo(() => fetchPosts.isPending())
481
+ return (
482
+ <div aria-busy={fetchPosts.isPending()}>
483
+ {fetchPosts.error() ? <p>failed</p> : null}
484
+ <span>{busy() ? 'loading' : 'ready'}</span>
485
+ <ul>{posts().map((post) => <li key={post.id}>{post.title}</li>)}</ul>
486
+ </div>
487
+ )
488
+ }
489
+ `
490
+
491
+ describe('BF117: query action reads in template positions', () => {
492
+ test('no longer refuses a direct accessor read in an attribute or a condition (#3166); still refuses a memo that reads the action', () => {
493
+ const { errors } = compile(ACTION_READS)
494
+ const bf117 = errors.filter((e) => e.code === 'BF117')
495
+ // Lines 9 (`aria-busy={fetchPosts.isPending()}`) and 10
496
+ // (`{fetchPosts.error() ? … : null}`) are direct, whole-expression reads
497
+ // — seeded (`false`/`undefined`, spec/async.md §7.3) rather than refused.
498
+ // Line 11 (`{busy() ? … : …}`) reads the action only THROUGH a memo, which
499
+ // this pass doesn't seed structurally (`action-accessor.ts`'s docstring),
500
+ // so it still refuses.
501
+ expect(bf117.map((e) => e.loc.start.line)).toEqual([11])
502
+ expect(bf117[0].message).toContain("memo 'busy' reads the async action 'fetchPosts'")
503
+ expect(bf117[0].suggestion?.escape).toEqual([{ kind: 'client-directive' }])
504
+ })
505
+
506
+ test('fires on Hono too, only for the memo-transitive read', () => {
507
+ expect(compile(ACTION_READS, { hono: true }).codes.filter((c) => c === 'BF117')).toHaveLength(1)
508
+ })
509
+
510
+ test('refuses calling the action in a template position', () => {
511
+ const { codes } = compile(`
512
+ 'use client'
513
+ import { createQuery, http } from '@barefootjs/client'
514
+ export function C() {
515
+ const [items, fetchItems] = createQuery(() => http.get<string[]>('/api/items'))
516
+ return <p>{String(fetchItems())}</p>
517
+ }
518
+ `)
519
+ expect(codes).toContain('BF117')
520
+ })
521
+
522
+ test('refuses reads through a local constant or a called local function', () => {
523
+ const viaConstant = compile(`
524
+ 'use client'
525
+ import { createQuery, http } from '@barefootjs/client'
526
+ export function C(props: { items: string[] }) {
527
+ const [items, fetchItems] = createQuery(() => http.get<string[]>('/api/items'), { initial: props.items })
528
+ const busy = fetchItems.isPending()
529
+ return <div aria-busy={busy}>{items().length}</div>
530
+ }
531
+ `, { hono: true })
532
+ expect(viaConstant.codes).toContain('BF117')
533
+ expect(viaConstant.errors.find((e) => e.code === 'BF117')!.message).toContain("constant 'busy' reads the async action 'fetchItems'")
534
+
535
+ const viaFunction = compile(`
536
+ 'use client'
537
+ import { createQuery, http } from '@barefootjs/client'
538
+ export function C(props: { items: string[] }) {
539
+ const [items, fetchItems] = createQuery(() => http.get<string[]>('/api/items'), { initial: props.items })
540
+ const status = () => (fetchItems.error() ? 'failed' : 'ok')
541
+ return <p>{status()} {items().length}</p>
542
+ }
543
+ `, { hono: true })
544
+ expect(viaFunction.codes).toContain('BF117')
545
+
546
+ const viaDeclaration = compile(`
547
+ 'use client'
548
+ import { createQuery, http } from '@barefootjs/client'
549
+ export function C(props: { items: string[] }) {
550
+ const [items, fetchItems] = createQuery(() => http.get<string[]>('/api/items'), { initial: props.items })
551
+ function status() {
552
+ return fetchItems.error() ? 'failed' : 'ok'
553
+ }
554
+ return <p>{status()} {items().length}</p>
555
+ }
556
+ `, { hono: true })
557
+ expect(viaDeclaration.codes).toContain('BF117')
558
+ })
559
+
560
+ test('follows reads transitively through memos, constants and functions', () => {
561
+ const { errors } = compile(`
562
+ 'use client'
563
+ import { createMemo, createQuery, http } from '@barefootjs/client'
564
+ export function C(props: { items: string[] }) {
565
+ const [items, fetchItems] = createQuery(() => http.get<string[]>('/api/items'), { initial: props.items })
566
+ const failed = () => fetchItems.error() !== undefined
567
+ const busy = createMemo(() => fetchItems.isPending() || failed())
568
+ const label = busy() ? 'loading' : 'ready'
569
+ return <p data-state={label}>{items().length}</p>
570
+ }
571
+ `)
572
+ const bf117 = errors.filter((e) => e.code === 'BF117')
573
+ expect(bf117).toHaveLength(1)
574
+ expect(bf117[0].message).toContain("constant 'label' reads the async action 'fetchItems'")
575
+ })
576
+
577
+ test('allows passing a function that reads the action as a value', () => {
578
+ const { codes } = compile(`
579
+ 'use client'
580
+ import { createQuery, http } from '@barefootjs/client'
581
+ function Retry(props: { run: () => void }) {
582
+ return <button onClick={() => props.run()}>retry</button>
583
+ }
584
+ export function C(props: { items: string[] }) {
585
+ const [items, fetchItems] = createQuery(() => http.get<string[]>('/api/items'), { initial: props.items })
586
+ const reload = () => fetchItems()
587
+ return (
588
+ <div>
589
+ <button onClick={reload}>{items().length}</button>
590
+ <Retry run={reload} />
591
+ </div>
592
+ )
593
+ }
594
+ `, { fileName: 'C.tsx' })
595
+ expect(codes).not.toContain('BF117')
596
+ })
597
+
598
+ test('treats a constant that aliases the action or a reader like its target', () => {
599
+ const accepted = compile(`
600
+ 'use client'
601
+ import { createQuery, http } from '@barefootjs/client'
602
+ function Retry(props: { run: () => void }) {
603
+ return <button onClick={() => props.run()}>retry</button>
604
+ }
605
+ export function C(props: { items: string[] }) {
606
+ const [items, fetchItems] = createQuery(() => http.get<string[]>('/api/items'), { initial: props.items })
607
+ const run = fetchItems
608
+ const reload = () => fetchItems()
609
+ const again = reload
610
+ return (
611
+ <div>
612
+ <Retry run={run} />
613
+ <Retry run={again} />
614
+ <p>{items().length}</p>
615
+ </div>
616
+ )
617
+ }
618
+ `, { fileName: 'C.tsx' })
619
+ expect(accepted.codes).not.toContain('BF117')
620
+
621
+ const refused = compile(`
622
+ 'use client'
623
+ import { createQuery, http } from '@barefootjs/client'
624
+ export function C(props: { items: string[] }) {
625
+ const [items, fetchItems] = createQuery(() => http.get<string[]>('/api/items'), { initial: props.items })
626
+ const run = fetchItems
627
+ const reload = () => run.error()
628
+ const again = reload
629
+ return (
630
+ <div aria-busy={run.isPending()}>
631
+ <p>{again() ? 'failed' : items().length}</p>
632
+ </div>
633
+ )
634
+ }
635
+ `, { fileName: 'C.tsx' })
636
+ expect(refused.codes.filter(c => c === 'BF117')).toHaveLength(2)
637
+ })
638
+
639
+ test('refuses an inline arrow reading the action outside an on* prop', () => {
640
+ const { codes } = compile(`
641
+ 'use client'
642
+ import { createQuery, http } from '@barefootjs/client'
643
+ function Retry(props: { run: () => void }) {
644
+ return <button onClick={() => props.run()}>retry</button>
645
+ }
646
+ export function C(props: { items: string[] }) {
647
+ const [items, fetchItems] = createQuery(() => http.get<string[]>('/api/items'), { initial: props.items })
648
+ return <div><Retry run={() => fetchItems()} />{items().length}</div>
649
+ }
650
+ `, { fileName: 'C.tsx' })
651
+ expect(codes).toContain('BF117')
652
+ })
653
+
654
+ test('allows /* @client */ reads, event handlers, and forwarding the action as a value', () => {
655
+ const { codes } = compile(`
656
+ 'use client'
657
+ import { createQuery, http } from '@barefootjs/client'
658
+ function Retry(props: { onRetry: () => void; run: () => void }) {
659
+ return <button onClick={() => props.onRetry()}>retry</button>
660
+ }
661
+ export function C(props: { items: string[] }) {
662
+ const [items, fetchItems] = createQuery(() => http.get<string[]>('/api/items'), { initial: props.items })
663
+ return (
664
+ <div aria-busy={/* @client */ fetchItems.isPending()}>
665
+ <button onClick={() => fetchItems()}>{items().length}</button>
666
+ <Retry onRetry={() => fetchItems()} run={fetchItems} />
667
+ </div>
668
+ )
669
+ }
670
+ `, { fileName: 'C.tsx' })
671
+ expect(codes).not.toContain('BF117')
672
+ })
673
+
674
+ test('with a TypeChecker, accessor reads are refused rather than auto-deferred to the client', () => {
675
+ const componentPath = path.join(__dirname, '_create-query-component.tsx')
676
+ const program = programFor(ACTION_READS, componentPath)
677
+ // Sanity: the checker sees the accessor as a reactive brand, which is
678
+ // what would auto-defer it without the async-action exclusion.
679
+ const checker = program.getTypeChecker()
680
+ const sourceFile = program.getSourceFile(componentPath)!
681
+ let accessorType = ''
682
+ const visit = (node: ts.Node): void => {
683
+ if (ts.isPropertyAccessExpression(node) && node.getText(sourceFile) === 'fetchPosts.isPending' && !accessorType) {
684
+ accessorType = checker.typeToString(checker.getTypeAtLocation(node))
685
+ }
686
+ ts.forEachChild(node, visit)
687
+ }
688
+ visit(sourceFile)
689
+ expect(accessorType).toStartWith('Reactive<')
690
+
691
+ const { codes } = compile(ACTION_READS, { program, fileName: componentPath })
692
+ // Only the memo-transitive read (line 11) still refuses — see the
693
+ // top-level describe's first test for why lines 9/10 no longer do.
694
+ expect(codes.filter((c) => c === 'BF117')).toHaveLength(1)
695
+ })
696
+ })