@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.
Files changed (168) 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/env-signal.d.ts +0 -8
  4. package/dist/adapters/env-signal.d.ts.map +1 -1
  5. package/dist/adapters/interface.d.ts +12 -0
  6. package/dist/adapters/interface.d.ts.map +1 -1
  7. package/dist/adapters/jsx-adapter.d.ts.map +1 -1
  8. package/dist/analyzer.d.ts +61 -1
  9. package/dist/analyzer.d.ts.map +1 -1
  10. package/dist/async-action-refusal.d.ts +45 -0
  11. package/dist/async-action-refusal.d.ts.map +1 -0
  12. package/dist/builtin-lowering-plugins.d.ts.map +1 -1
  13. package/dist/compiler.d.ts.map +1 -1
  14. package/dist/component-root-scope-comment.d.ts +36 -0
  15. package/dist/component-root-scope-comment.d.ts.map +1 -0
  16. package/dist/debug.d.ts +61 -5
  17. package/dist/debug.d.ts.map +1 -1
  18. package/dist/errors.d.ts +7 -0
  19. package/dist/errors.d.ts.map +1 -1
  20. package/dist/event-handler-name.d.ts +13 -0
  21. package/dist/event-handler-name.d.ts.map +1 -0
  22. package/dist/expression-parser.d.ts +9 -0
  23. package/dist/expression-parser.d.ts.map +1 -1
  24. package/dist/format-date-refusal.d.ts +33 -0
  25. package/dist/format-date-refusal.d.ts.map +1 -0
  26. package/dist/index.d.ts +6 -4
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +3260 -1914
  29. package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
  30. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  31. package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
  32. package/dist/ir-to-client-js/compute-scope.d.ts.map +1 -1
  33. package/dist/ir-to-client-js/control-flow/plan/build-branch-loop.d.ts.map +1 -1
  34. package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
  35. package/dist/ir-to-client-js/control-flow/plan/build-composite-loop.d.ts.map +1 -1
  36. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  37. package/dist/ir-to-client-js/control-flow/plan/build-loop.d.ts.map +1 -1
  38. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -0
  39. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts.map +1 -1
  40. package/dist/ir-to-client-js/control-flow/plan/lazy-row-eligibility.d.ts +1 -1
  41. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +57 -12
  42. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
  43. package/dist/ir-to-client-js/control-flow/shared.d.ts +44 -3
  44. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  45. package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts +7 -5
  46. package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
  47. package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
  48. package/dist/ir-to-client-js/declaration-sort.d.ts +2 -1
  49. package/dist/ir-to-client-js/declaration-sort.d.ts.map +1 -1
  50. package/dist/ir-to-client-js/emit-module-level.d.ts.map +1 -1
  51. package/dist/ir-to-client-js/emit-reactive.d.ts +43 -2
  52. package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
  53. package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
  54. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  55. package/dist/ir-to-client-js/plan/declaration-emit.d.ts +7 -5
  56. package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
  57. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  58. package/dist/ir-to-client-js/reactivity.d.ts +9 -1
  59. package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
  60. package/dist/ir-to-client-js/source-map.d.ts.map +1 -1
  61. package/dist/ir-to-client-js/types.d.ts +4 -0
  62. package/dist/ir-to-client-js/types.d.ts.map +1 -1
  63. package/dist/ir-to-client-js/utils.d.ts +66 -1
  64. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  65. package/dist/jsx-to-ir.d.ts.map +1 -1
  66. package/dist/opaque-local-accessor.d.ts +27 -0
  67. package/dist/opaque-local-accessor.d.ts.map +1 -0
  68. package/dist/signal-initializer.d.ts +24 -0
  69. package/dist/signal-initializer.d.ts.map +1 -0
  70. package/dist/template-position-walk.d.ts +68 -0
  71. package/dist/template-position-walk.d.ts.map +1 -0
  72. package/dist/to-locale-date-lowering.d.ts +7 -4
  73. package/dist/to-locale-date-lowering.d.ts.map +1 -1
  74. package/dist/types.d.ts +104 -17
  75. package/dist/types.d.ts.map +1 -1
  76. package/package.json +2 -2
  77. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +878 -4528
  78. package/src/__tests__/action-accessor.test.ts +166 -0
  79. package/src/__tests__/binding-scope-ratchet.test.ts +21 -10
  80. package/src/__tests__/branch-local-in-raw-capture.test.ts +1 -1
  81. package/src/__tests__/branch-local-same-name-collision.test.ts +9 -9
  82. package/src/__tests__/client-js-generation.test.ts +175 -0
  83. package/src/__tests__/component-root-scope-comment.test.ts +106 -0
  84. package/src/__tests__/create-mutation.test.ts +220 -0
  85. package/src/__tests__/create-query.test.ts +644 -0
  86. package/src/__tests__/csr-fragment-root-scope-shape.test.ts +156 -0
  87. package/src/__tests__/debug.test.ts +222 -0
  88. package/src/__tests__/doc-examples.test.ts +78 -25
  89. package/src/__tests__/event-handler-name.test.ts +74 -0
  90. package/src/__tests__/flatmap-segments.test.ts +11 -9
  91. package/src/__tests__/format-date-refusal.test.ts +134 -0
  92. package/src/__tests__/fragment-wrapped-conditional-return-branch.test.ts +132 -0
  93. package/src/__tests__/hydrate-template.test.ts +9 -1
  94. package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +2 -2
  95. package/src/__tests__/issue-3009-loop-branch-ref.test.ts +2 -2
  96. package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +213 -0
  97. package/src/__tests__/issue-3113-init-fn-name-collision.test.ts +133 -0
  98. package/src/__tests__/jsx-return-type-assertion.test.ts +2 -2
  99. package/src/__tests__/lazy-row-eligibility.test.ts +5 -4
  100. package/src/__tests__/loop-row-child-children-reactive-attrs.test.ts +108 -0
  101. package/src/__tests__/loop-row-child-text-children-markers.test.ts +158 -0
  102. package/src/__tests__/opaque-local-accessor.test.ts +90 -0
  103. package/src/__tests__/profiler-e2e.test.ts +13 -8
  104. package/src/__tests__/props-destructuring.test.ts +79 -4
  105. package/src/__tests__/reactive-factory-arity.test.ts +311 -0
  106. package/src/__tests__/ref-attr-absent-at-ssr.test.ts +472 -0
  107. package/src/__tests__/ssr-portal-ref-callback.test.ts +219 -0
  108. package/src/action-accessor.ts +289 -0
  109. package/src/adapters/env-signal.ts +2 -21
  110. package/src/adapters/interface.ts +13 -0
  111. package/src/adapters/jsx-adapter.ts +17 -0
  112. package/src/analyzer.ts +589 -60
  113. package/src/async-action-refusal.ts +254 -0
  114. package/src/builtin-lowering-plugins.ts +0 -2
  115. package/src/compiler.ts +27 -0
  116. package/src/component-root-scope-comment.ts +59 -0
  117. package/src/debug.ts +318 -103
  118. package/src/errors.ts +83 -1
  119. package/src/event-handler-name.ts +14 -0
  120. package/src/expression-parser.ts +41 -17
  121. package/src/format-date-refusal.ts +147 -0
  122. package/src/index.ts +14 -3
  123. package/src/ir-to-client-js/build-references.ts +6 -2
  124. package/src/ir-to-client-js/child-components.ts +8 -3
  125. package/src/ir-to-client-js/collect-elements.ts +28 -13
  126. package/src/ir-to-client-js/compute-inlinability.ts +2 -1
  127. package/src/ir-to-client-js/compute-scope.ts +3 -1
  128. package/src/ir-to-client-js/control-flow/plan/build-branch-loop.ts +2 -4
  129. package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +67 -5
  130. package/src/ir-to-client-js/control-flow/plan/build-composite-loop.ts +3 -6
  131. package/src/ir-to-client-js/control-flow/plan/build-insert.ts +1 -0
  132. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +2 -1
  133. package/src/ir-to-client-js/control-flow/plan/build-loop.ts +23 -11
  134. package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -0
  135. package/src/ir-to-client-js/control-flow/plan/lazy-row-eligibility.ts +3 -3
  136. package/src/ir-to-client-js/control-flow/plan/loop.ts +51 -10
  137. package/src/ir-to-client-js/control-flow/shared.ts +78 -5
  138. package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +25 -12
  139. package/src/ir-to-client-js/control-flow/stringify/insert.ts +1 -1
  140. package/src/ir-to-client-js/csr-substitute.ts +11 -3
  141. package/src/ir-to-client-js/declaration-sort.ts +7 -3
  142. package/src/ir-to-client-js/emit-module-level.ts +7 -4
  143. package/src/ir-to-client-js/emit-reactive.ts +140 -10
  144. package/src/ir-to-client-js/emit-registration.ts +6 -11
  145. package/src/ir-to-client-js/generate-init.ts +2 -2
  146. package/src/ir-to-client-js/html-template.ts +41 -19
  147. package/src/ir-to-client-js/index.ts +9 -3
  148. package/src/ir-to-client-js/phases/ref-callbacks.ts +1 -1
  149. package/src/ir-to-client-js/plan/build-declaration-emit.ts +16 -15
  150. package/src/ir-to-client-js/plan/declaration-emit.ts +7 -5
  151. package/src/ir-to-client-js/prop-handling.ts +4 -0
  152. package/src/ir-to-client-js/reactivity.ts +21 -16
  153. package/src/ir-to-client-js/source-map.ts +2 -1
  154. package/src/ir-to-client-js/stringify/declaration-emit.ts +14 -7
  155. package/src/ir-to-client-js/types.ts +4 -0
  156. package/src/ir-to-client-js/utils.ts +89 -7
  157. package/src/jsx-to-ir.ts +772 -50
  158. package/src/opaque-local-accessor.ts +44 -0
  159. package/src/rich-type-refusal.ts +1 -1
  160. package/src/signal-initializer.ts +36 -0
  161. package/src/template-position-walk.ts +203 -0
  162. package/src/to-locale-date-lowering.ts +7 -4
  163. package/src/types.ts +103 -15
  164. package/dist/format-date-lowering.d.ts +0 -30
  165. package/dist/format-date-lowering.d.ts.map +0 -1
  166. package/src/__tests__/create-selector.test.ts +0 -124
  167. package/src/__tests__/format-date-lowering.test.ts +0 -125
  168. package/src/format-date-lowering.ts +0 -55
@@ -0,0 +1,220 @@
1
+ /**
2
+ * `createMutation` recognition (#3210): the value is collected as a signal
3
+ * seeded `undefined` (a mutation has no `initial`, spec/async.md §7.4), the
4
+ * request function is emitted into the client JS only and never wrapped in
5
+ * anything that tracks, and the action's `isPending()` / `error()` seed
6
+ * through the same gate as a query's (`action-accessor.ts`, #3166).
7
+ * Cross-adapter output lives in the `create-mutation-*` conformance fixtures;
8
+ * this file pins the compiler-internal decisions.
9
+ */
10
+
11
+ import { describe, test, expect } from 'bun:test'
12
+ import { compileJSX } from '../compiler'
13
+ import { TestAdapter } from '../adapters/test-adapter'
14
+ import { HonoAdapter } from '../../../adapter-hono/src/adapter/hono-adapter'
15
+ import { GoTemplateAdapter } from '../../../adapter-go-template/src/index'
16
+ import { BladeAdapter } from '../../../adapter-blade/src/index'
17
+ import { ErbAdapter } from '../../../adapter-erb/src/index'
18
+ import { JinjaAdapter } from '../../../adapter-jinja/src/index'
19
+ import { MojoAdapter } from '../../../adapter-mojolicious/src/index'
20
+ import { PebbleAdapter } from '../../../adapter-pebble/src/index'
21
+ import { MinijinjaAdapter } from '../../../adapter-rust/src/index'
22
+ import { TwigAdapter } from '../../../adapter-twig/src/index'
23
+ import { XslateAdapter } from '../../../adapter-xslate/src/index'
24
+ import { extractInitBody, extractTemplateBody } from './staged-ir/helpers'
25
+
26
+ function compile(source: string, options: { adapter?: 'test' | 'hono' | 'go' } = {}) {
27
+ const adapter =
28
+ options.adapter === 'hono' ? new HonoAdapter() : options.adapter === 'go' ? new GoTemplateAdapter() : new TestAdapter()
29
+ const result = compileJSX(source, 'Component.tsx', { adapter })
30
+ const clientJs = result.files.find((f) => f.type === 'clientJs')?.content ?? ''
31
+ const ssr = result.files.find((f) => f.type === 'markedTemplate')?.content ?? ''
32
+ return {
33
+ codes: result.errors.map((e) => e.code),
34
+ errors: result.errors,
35
+ clientJs,
36
+ initBody: extractInitBody(clientJs),
37
+ templateBody: extractTemplateBody(clientJs),
38
+ ssr,
39
+ }
40
+ }
41
+
42
+ const FORM = `
43
+ 'use client'
44
+ import { createMutation, createSignal, http } from '@barefootjs/client'
45
+ export function CommentForm(props: { postId: number }) {
46
+ const [text, setText] = createSignal('')
47
+ const [saved, save] = createMutation(() => http.post('/api/posts/' + props.postId + '/comments', { body: { text: text() } }), { invalidates: ['/api/posts'] })
48
+ return (
49
+ <form>
50
+ {save.error() ? <p>Failed</p> : null}
51
+ {saved() ? <p>Saved</p> : null}
52
+ <input value={text()} onInput={(e) => setText(e.target.value)} />
53
+ <button disabled={save.isPending()} onClick={() => save()}>Send</button>
54
+ </form>
55
+ )
56
+ }
57
+ `
58
+
59
+ describe('createMutation value and client JS (#3210)', () => {
60
+ test('emits the factory call verbatim with props read live and `invalidates` unchanged', () => {
61
+ const { codes, initBody } = compile(FORM)
62
+ expect(codes).toEqual([])
63
+ expect(initBody).toContain(
64
+ "const [saved, save] = createMutation(() => http.post('/api/posts/' + _p.postId + '/comments', { body: { text: text() } }), { invalidates: ['/api/posts'] })",
65
+ )
66
+ })
67
+
68
+ test('the request function is not wrapped in anything that tracks', () => {
69
+ const { initBody } = compile(FORM)
70
+ // The only occurrence of the request is the factory call's own argument:
71
+ // no effect, memo or signal initializer re-emits it.
72
+ expect(initBody.split("http.post('/api/posts/'").length - 1).toBe(1)
73
+ const effectBodies = initBody.split('createEffect(').slice(1)
74
+ for (const body of effectBodies) expect(body.split('\n')[0]).not.toContain('http.post')
75
+ expect(initBody).not.toContain('createSignal(undefined)')
76
+ })
77
+
78
+ test('the CSR template seeds the value undefined and never reads the request function', () => {
79
+ const { templateBody } = compile(FORM)
80
+ expect(templateBody).not.toContain('http.post')
81
+ expect(templateBody).toContain('${undefined ?')
82
+ })
83
+
84
+ test('Hono SSR seeds the value undefined, stubs the action, and never emits the request function', () => {
85
+ const { codes, ssr } = compile(FORM, { adapter: 'hono' })
86
+ expect(codes).toEqual([])
87
+ expect(ssr).toContain('const saved = () => undefined')
88
+ expect(ssr).toContain('const save: any = Object.assign(() => {}, { isPending: () => false, error: () => undefined })')
89
+ expect(ssr).not.toContain('http.post')
90
+ // A prop read only by the request function still reaches the client.
91
+ expect(ssr).toContain("__hydrateProps['postId'] = props.postId")
92
+ })
93
+
94
+ test('the value-elided form keeps just the action', () => {
95
+ const { codes, initBody } = compile(`
96
+ 'use client'
97
+ import { createMutation, http } from '@barefootjs/client'
98
+ export function C() {
99
+ const [, send] = createMutation(() => http.post('/api/ping'))
100
+ return <button onClick={() => send()}>ping</button>
101
+ }
102
+ `)
103
+ expect(codes).toEqual([])
104
+ expect(initBody).toContain("const [, send] = createMutation(() => http.post('/api/ping'))")
105
+ })
106
+ })
107
+
108
+ describe('createMutation action accessors (#3210, via #3166)', () => {
109
+ test('disabled={save.isPending()} and an error condition are seeded in DSL output', () => {
110
+ const { codes, ssr } = compile(FORM, { adapter: 'go' })
111
+ expect(codes).toEqual([])
112
+ expect(ssr).not.toContain('IsPending')
113
+ expect(ssr).not.toContain('.Save.')
114
+ expect(ssr).toContain('<button {{if false}}disabled{{end}}')
115
+ expect(ssr).toContain('{{if false}}<p bf-c="s0">Failed</p>')
116
+ })
117
+
118
+ // The shared fixture compares rendered HTML, and a DSL adapter that re-parses
119
+ // the raw `save.isPending()` reads an undeclared `save`, which every engine
120
+ // treats as falsy — the same HTML. So check the template text itself: no
121
+ // adapter may emit a reference to the action or its accessor.
122
+ test('every DSL adapter lowers the seed, never the accessor read', () => {
123
+ const adapters = [
124
+ new GoTemplateAdapter(),
125
+ new ErbAdapter(),
126
+ new MojoAdapter(),
127
+ new JinjaAdapter(),
128
+ new TwigAdapter(),
129
+ new XslateAdapter(),
130
+ new BladeAdapter(),
131
+ new MinijinjaAdapter(),
132
+ new PebbleAdapter(),
133
+ ]
134
+ for (const adapter of adapters) {
135
+ const result = compileJSX(FORM, 'Component.tsx', { adapter })
136
+ const ssr = result.files.find((f) => f.type === 'markedTemplate')?.content ?? ''
137
+ expect({ adapter: adapter.name, codes: result.errors.map((e) => e.code) }).toEqual({ adapter: adapter.name, codes: [] })
138
+ expect({ adapter: adapter.name, leaks: /isPending|IsPending|is_pending|\bsave\b|\bSave\b/.test(ssr) }).toEqual({
139
+ adapter: adapter.name,
140
+ leaks: false,
141
+ })
142
+ }
143
+ })
144
+
145
+ test('a read the gate does not admit still refuses with BF117', () => {
146
+ for (const markup of ['<p>{save.error()}</p>', '<p title={save.isPending()}>x</p>', '<button disabled={save.error()}>x</button>']) {
147
+ const { codes } = compile(`
148
+ 'use client'
149
+ import { createMutation, http } from '@barefootjs/client'
150
+ export function C() {
151
+ const [, save] = createMutation(() => http.post('/api/ping'))
152
+ return <div>${markup}</div>
153
+ }
154
+ `)
155
+ expect(codes).toContain('BF117')
156
+ }
157
+ })
158
+ })
159
+
160
+ describe('createMutation arity (BF115 / BF116)', () => {
161
+ test('a two-argument call and a two-element destructure are accepted', () => {
162
+ expect(compile(FORM).codes).toEqual([])
163
+ })
164
+
165
+ test('a three-element destructure is BF115', () => {
166
+ const { codes } = compile(`
167
+ 'use client'
168
+ import { createMutation, http } from '@barefootjs/client'
169
+ export function C() {
170
+ const [a, b, c] = createMutation(() => http.post('/api/items'))
171
+ return <p>{a() ? 'y' : 'n'}</p>
172
+ }
173
+ `)
174
+ expect(codes).toContain('BF115')
175
+ })
176
+
177
+ test('a third argument is BF116', () => {
178
+ const { codes } = compile(`
179
+ 'use client'
180
+ import { createMutation, http } from '@barefootjs/client'
181
+ export function C() {
182
+ const [a] = createMutation(() => http.post('/api/items'), {}, 'extra')
183
+ return <p>{a() ? 'y' : 'n'}</p>
184
+ }
185
+ `)
186
+ expect(codes).toContain('BF116')
187
+ })
188
+ })
189
+
190
+ describe('createMutation `initial` is refused (BF118)', () => {
191
+ const withOptions = (options: string, params = '') => `
192
+ 'use client'
193
+ import { createMutation, http } from '@barefootjs/client'
194
+ export function C(${params}) {
195
+ const [saved, save] = createMutation(() => http.post('/api/items'), ${options})
196
+ return <button onClick={() => save()}>{saved() ? 'saved' : 'save'}</button>
197
+ }
198
+ `
199
+
200
+ test('an `initial` key names createQuery as the factory that takes it', () => {
201
+ const { codes, errors } = compile(withOptions("{ initial: 'x', invalidates: ['/api'] }"))
202
+ expect(codes).toEqual(['BF118'])
203
+ expect(errors[0].message).toContain("Use 'createQuery'")
204
+ })
205
+
206
+ test('a shorthand `initial` is refused too', () => {
207
+ const { codes } = compile(withOptions('{ initial }', '{ initial }: { initial?: string }'))
208
+ expect(codes).toEqual(['BF118'])
209
+ })
210
+
211
+ test('options without `initial`, or that cannot be read structurally, are accepted', () => {
212
+ expect(compile(withOptions("{ invalidates: ['/api'] }")).codes).toEqual([])
213
+ expect(compile(withOptions('{ ...props.opts }', 'props: { opts: {} }')).codes).toEqual([])
214
+ })
215
+
216
+ test('the refused `initial` never seeds the value', () => {
217
+ const { ssr } = compile(withOptions("{ initial: 'x' }"), { adapter: 'hono' })
218
+ expect(ssr).toContain('const saved = () => undefined')
219
+ })
220
+ })