@barefootjs/jsx 0.39.6 → 0.39.8

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 (192) hide show
  1. package/dist/adapters/attr-plan.d.ts +8 -1
  2. package/dist/adapters/attr-plan.d.ts.map +1 -1
  3. package/dist/adapters/callback-method.d.ts +44 -0
  4. package/dist/adapters/callback-method.d.ts.map +1 -1
  5. package/dist/adapters/component-props.d.ts +80 -13
  6. package/dist/adapters/component-props.d.ts.map +1 -1
  7. package/dist/adapters/computed-const.d.ts +52 -0
  8. package/dist/adapters/computed-const.d.ts.map +1 -0
  9. package/dist/adapters/dangerous-inner-html.d.ts +10 -0
  10. package/dist/adapters/dangerous-inner-html.d.ts.map +1 -1
  11. package/dist/adapters/dsl-template-adapter.d.ts +151 -0
  12. package/dist/adapters/dsl-template-adapter.d.ts.map +1 -0
  13. package/dist/adapters/element-shell.d.ts +44 -0
  14. package/dist/adapters/element-shell.d.ts.map +1 -0
  15. package/dist/adapters/interface.d.ts +2 -0
  16. package/dist/adapters/interface.d.ts.map +1 -1
  17. package/dist/adapters/jsx-adapter.d.ts +4 -0
  18. package/dist/adapters/jsx-adapter.d.ts.map +1 -1
  19. package/dist/adapters/literal-value.d.ts +2 -2
  20. package/dist/adapters/literal-value.d.ts.map +1 -1
  21. package/dist/adapters/parsed-expr-emitter.d.ts +27 -0
  22. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  23. package/dist/adapters/style-object.d.ts +33 -0
  24. package/dist/adapters/style-object.d.ts.map +1 -0
  25. package/dist/analyzer.d.ts.map +1 -1
  26. package/dist/augment-inherited-props.d.ts +7 -0
  27. package/dist/augment-inherited-props.d.ts.map +1 -1
  28. package/dist/children-nullish.d.ts +22 -0
  29. package/dist/children-nullish.d.ts.map +1 -0
  30. package/dist/errors.d.ts +1 -0
  31. package/dist/errors.d.ts.map +1 -1
  32. package/dist/expression-parser.d.ts +9 -19
  33. package/dist/expression-parser.d.ts.map +1 -1
  34. package/dist/fold-render-reassignments.d.ts +43 -0
  35. package/dist/fold-render-reassignments.d.ts.map +1 -0
  36. package/dist/free-refs.d.ts +15 -0
  37. package/dist/free-refs.d.ts.map +1 -1
  38. package/dist/index.d.ts +21 -14
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +24620 -23233
  41. package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
  42. package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts +2 -1
  43. package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts.map +1 -1
  44. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -2
  45. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts.map +1 -1
  46. package/dist/ir-to-client-js/control-flow/stringify/event-delegation.d.ts.map +1 -1
  47. package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +10 -9
  48. package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
  49. package/dist/ir-to-client-js/csr-substitute.d.ts +0 -13
  50. package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
  51. package/dist/ir-to-client-js/emit-reactive.d.ts +1 -1
  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 +10 -8
  54. package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
  55. package/dist/ir-to-client-js/generate-init.d.ts.map +1 -1
  56. package/dist/ir-to-client-js/html-template.d.ts +9 -19
  57. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  58. package/dist/ir-to-client-js/identifiers.d.ts +6 -8
  59. package/dist/ir-to-client-js/identifiers.d.ts.map +1 -1
  60. package/dist/ir-to-client-js/imports.d.ts +23 -2
  61. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  62. package/dist/ir-to-client-js/index.d.ts.map +1 -1
  63. package/dist/ir-to-client-js/phases/props-extraction.d.ts +3 -2
  64. package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
  65. package/dist/ir-to-client-js/plan/declaration-emit.d.ts +5 -0
  66. package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
  67. package/dist/ir-to-client-js/prop-handling.d.ts +11 -12
  68. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  69. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +1 -1
  70. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
  71. package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
  72. package/dist/ir-to-client-js/rewrite-destructured-props.d.ts +1 -1
  73. package/dist/ir-to-client-js/rewrite-destructured-props.d.ts.map +1 -1
  74. package/dist/ir-to-client-js/safe-html.d.ts +20 -2
  75. package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
  76. package/dist/ir-to-client-js/types.d.ts +3 -3
  77. package/dist/ir-to-client-js/types.d.ts.map +1 -1
  78. package/dist/ir-to-client-js/utils.d.ts +15 -11
  79. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  80. package/dist/jsx-to-ir.d.ts.map +1 -1
  81. package/dist/nullable-signal-attr.d.ts +43 -12
  82. package/dist/nullable-signal-attr.d.ts.map +1 -1
  83. package/dist/prop-rewrite.d.ts +18 -0
  84. package/dist/prop-rewrite.d.ts.map +1 -1
  85. package/dist/props-binding.d.ts +43 -6
  86. package/dist/props-binding.d.ts.map +1 -1
  87. package/dist/relocate.d.ts +16 -0
  88. package/dist/relocate.d.ts.map +1 -1
  89. package/dist/scanner/js-scanner.d.ts.map +1 -1
  90. package/dist/scanner/js-scanner.js +30 -2
  91. package/dist/signal-prop-name-collision.d.ts +6 -5
  92. package/dist/signal-prop-name-collision.d.ts.map +1 -1
  93. package/dist/ssr-defaults.d.ts +28 -0
  94. package/dist/ssr-defaults.d.ts.map +1 -1
  95. package/dist/ssr-seed-plan.d.ts +37 -18
  96. package/dist/ssr-seed-plan.d.ts.map +1 -1
  97. package/dist/types.d.ts +162 -20
  98. package/dist/types.d.ts.map +1 -1
  99. package/dist/value-references.d.ts +21 -0
  100. package/dist/value-references.d.ts.map +1 -1
  101. package/package.json +2 -2
  102. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +18 -18
  103. package/src/__tests__/adapter-output.test.ts +5 -3
  104. package/src/__tests__/analyzer.test.ts +33 -0
  105. package/src/__tests__/arithmetic-lowering.test.ts +44 -0
  106. package/src/__tests__/block-memo-type.test.ts +51 -0
  107. package/src/__tests__/callback-method.test.ts +75 -0
  108. package/src/__tests__/children-read.test.ts +226 -0
  109. package/src/__tests__/client-only-date-lowering.test.ts +2 -2
  110. package/src/__tests__/client-template-escape-soundness.test.ts +31 -1
  111. package/src/__tests__/component-props.test.ts +68 -1
  112. package/src/__tests__/computed-const.test.ts +101 -0
  113. package/src/__tests__/constant-inlining-tree.test.ts +136 -0
  114. package/src/__tests__/destructure-default-reads-earlier-param.test.ts +138 -0
  115. package/src/__tests__/dsl-template-adapter.test.ts +173 -0
  116. package/src/__tests__/element-shell.test.ts +78 -0
  117. package/src/__tests__/event-handler-verbatim.test.ts +73 -0
  118. package/src/__tests__/expression-parser.test.ts +1 -44
  119. package/src/__tests__/fold-render-reassignments.test.ts +96 -0
  120. package/src/__tests__/inlinable-local-method-call.test.ts +81 -0
  121. package/src/__tests__/memo-body-expr.test.ts +120 -0
  122. package/src/__tests__/nullable-signal-attr.test.ts +67 -3
  123. package/src/__tests__/prop-handling.test.ts +35 -16
  124. package/src/__tests__/props-alias-const.test.ts +65 -0
  125. package/src/__tests__/reference-graph-identifiers.test.ts +161 -0
  126. package/src/__tests__/runtime-import-detection.test.ts +134 -0
  127. package/src/__tests__/signal-prop-name-collision.test.ts +30 -0
  128. package/src/__tests__/signal-prop-seed.test.ts +90 -0
  129. package/src/__tests__/ssr-defaults.test.ts +19 -1
  130. package/src/__tests__/ssr-seed-plan.test.ts +173 -1
  131. package/src/__tests__/static-template-reachability.test.ts +137 -0
  132. package/src/__tests__/style-object-plan.test.ts +54 -0
  133. package/src/__tests__/zero-param-helper-inline.test.ts +57 -0
  134. package/src/adapters/attr-plan.ts +29 -1
  135. package/src/adapters/callback-method.ts +85 -0
  136. package/src/adapters/component-props.ts +130 -12
  137. package/src/adapters/computed-const.ts +185 -0
  138. package/src/adapters/dangerous-inner-html.ts +33 -0
  139. package/src/adapters/dsl-template-adapter.ts +248 -0
  140. package/src/adapters/element-shell.ts +160 -0
  141. package/src/adapters/interface.ts +8 -0
  142. package/src/adapters/jsx-adapter.ts +12 -6
  143. package/src/adapters/literal-value.ts +2 -2
  144. package/src/adapters/parsed-expr-emitter.ts +34 -0
  145. package/src/adapters/style-object.ts +61 -0
  146. package/src/analyzer.ts +382 -94
  147. package/src/augment-inherited-props.ts +12 -0
  148. package/src/children-nullish.ts +33 -0
  149. package/src/debug.ts +2 -2
  150. package/src/errors.ts +12 -0
  151. package/src/expression-parser.ts +56 -53
  152. package/src/fold-render-reassignments.ts +314 -0
  153. package/src/free-refs.ts +77 -17
  154. package/src/index.ts +27 -10
  155. package/src/ir-to-client-js/compute-inlinability.ts +122 -19
  156. package/src/ir-to-client-js/control-flow/plan/event-collision.ts +2 -1
  157. package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -2
  158. package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +3 -2
  159. package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +13 -12
  160. package/src/ir-to-client-js/csr-substitute.ts +25 -37
  161. package/src/ir-to-client-js/emit-reactive.ts +2 -2
  162. package/src/ir-to-client-js/emit-registration.ts +31 -59
  163. package/src/ir-to-client-js/generate-init.ts +17 -1
  164. package/src/ir-to-client-js/html-template.ts +56 -120
  165. package/src/ir-to-client-js/identifiers.ts +75 -24
  166. package/src/ir-to-client-js/imports.ts +56 -19
  167. package/src/ir-to-client-js/index.ts +5 -3
  168. package/src/ir-to-client-js/init-declarations.ts +1 -1
  169. package/src/ir-to-client-js/phases/event-handlers.ts +2 -2
  170. package/src/ir-to-client-js/phases/props-extraction.ts +16 -15
  171. package/src/ir-to-client-js/plan/build-declaration-emit.ts +26 -52
  172. package/src/ir-to-client-js/plan/declaration-emit.ts +5 -0
  173. package/src/ir-to-client-js/prop-handling.ts +13 -54
  174. package/src/ir-to-client-js/prune-unused-prop-extractions.ts +7 -4
  175. package/src/ir-to-client-js/reactivity.ts +2 -2
  176. package/src/ir-to-client-js/rewrite-destructured-props.ts +27 -9
  177. package/src/ir-to-client-js/safe-html.ts +29 -35
  178. package/src/ir-to-client-js/stringify/declaration-emit.ts +1 -1
  179. package/src/ir-to-client-js/types.ts +3 -2
  180. package/src/ir-to-client-js/utils.ts +13 -26
  181. package/src/jsx-to-ir.ts +318 -130
  182. package/src/nullable-signal-attr.ts +156 -37
  183. package/src/prop-rewrite.ts +41 -0
  184. package/src/props-binding.ts +117 -7
  185. package/src/relocate.ts +40 -8
  186. package/src/scanner/__tests__/js-scanner.test.ts +37 -0
  187. package/src/scanner/js-scanner.ts +64 -4
  188. package/src/signal-prop-name-collision.ts +50 -24
  189. package/src/ssr-defaults.ts +47 -20
  190. package/src/ssr-seed-plan.ts +121 -29
  191. package/src/types.ts +165 -21
  192. package/src/value-references.ts +132 -0
@@ -370,9 +370,9 @@ export function initExample(__scope, _p = {}) {
370
370
 
371
371
  const [_s0, _s1, _s2] = $(__scope, 's0', 's1', 's2')
372
372
 
373
- if (_s0) _s0.addEventListener('click', () => { setCount(n => n + 1) })
374
- if (_s1) _s1.addEventListener('input', (e) => { setText((e.target).value) })
375
- if (_s2) _s2.addEventListener('keydown', (e) => { e.key === 'Enter' && handleSubmit() })
373
+ if (_s0) _s0.addEventListener('click', () => setCount(n => n + 1))
374
+ if (_s1) _s1.addEventListener('input', (e) => setText((e.target).value))
375
+ if (_s2) _s2.addEventListener('keydown', (e) => e.key === 'Enter' && handleSubmit())
376
376
  }
377
377
 
378
378
  hydrate('Example', { init: initExample, template: (_p) => \`<div><button bf="s0">+1</button><input bf="s1" /><input bf="s2" /></div>\` })
@@ -1137,7 +1137,7 @@ export function initPostList(__scope, _p = {}) {
1137
1137
  }
1138
1138
  })
1139
1139
 
1140
- if (_s3) _s3.addEventListener('click', () => { setPage(page() + 1) })
1140
+ if (_s3) _s3.addEventListener('click', () => setPage(page() + 1))
1141
1141
  const __tpl_l0 = document.createElement('template')
1142
1142
  __tpl_l0.innerHTML = \`<li data-key=""><!--bf:s1--><!--/--></li>\`
1143
1143
  const __lzp_l0 = [[0]]
@@ -1285,8 +1285,8 @@ export function initCommentForm(__scope, _p = {}) {
1285
1285
  }
1286
1286
  })
1287
1287
 
1288
- if (_s1) _s1.addEventListener('input', (e) => { setText(e.target.value) })
1289
- if (_s2) _s2.addEventListener('click', () => { save() })
1288
+ if (_s1) _s1.addEventListener('input', (e) => setText(e.target.value))
1289
+ if (_s2) _s2.addEventListener('click', () => save())
1290
1290
  }
1291
1291
 
1292
1292
  hydrate('CommentForm', { init: initCommentForm, template: (_p) => \`<form bf="s4">\${undefined ? \`<p bf-c="s0" role="alert">Failed to save</p>\` : \`<!--bf-cond-start:s0--><!--bf-cond-end:s0-->\`}<input \${(('')) != null ? 'value="' + escapeAttr(('')) + '"' : ''} bf="s1" /><button type="button" \${false ? 'disabled' : ''} bf="s2">Post</button>\${(undefined) ? \`<p bf-c="s3">Saved</p>\` : \`<!--bf-cond-start:s3--><!--bf-cond-end:s3-->\`}</form>\` })
@@ -1354,7 +1354,7 @@ export function initCounter(__scope, _p = {}) {
1354
1354
  __bfw_s0('s0', escapeTextOrNode(__val))
1355
1355
  })
1356
1356
 
1357
- if (_s1) _s1.addEventListener('click', () => { setCount(n => n + 1) })
1357
+ if (_s1) _s1.addEventListener('click', () => setCount(n => n + 1))
1358
1358
  }
1359
1359
 
1360
1360
  hydrate('Counter', { init: initCounter, template: (_p) => \`<button bf="s1">Count: <!--bf:s0-->\${escapeTextOrMarkup((0))}<!--/--></button>\` })
@@ -1418,10 +1418,10 @@ export function initCounter(__scope, _p = {}) {
1418
1418
  __bfw_s0('s0', escapeTextOrNode(__val))
1419
1419
  })
1420
1420
 
1421
- if (_s1) _s1.addEventListener('click', () => { setCount(n => n + 1) })
1421
+ if (_s1) _s1.addEventListener('click', () => setCount(n => n + 1))
1422
1422
  }
1423
1423
 
1424
- hydrate('Counter', { init: initCounter, template: (_p) => \`<button bf="s1"><!--bf:s0-->\${escapeTextOrMarkup((_p.initial))}<!--/--></button>\` })
1424
+ hydrate('Counter', { init: initCounter, template: (_p) => \`<button bf="s1"><!--bf:s0-->\${escapeTextOrMarkup(((_p.initial ?? 0)))}<!--/--></button>\` })
1425
1425
  export function Counter(_p, __bfKey) { return createComponent('Counter', _p, __bfKey) }"
1426
1426
  `;
1427
1427
 
@@ -1843,8 +1843,8 @@ export function initTooltip(__scope, _p = {}) {
1843
1843
  __bfw_s1('s1', escapeTextOrNode(__val))
1844
1844
  })
1845
1845
 
1846
- if (_s0) _s0.addEventListener('mouseenter', () => { setVisible(true) })
1847
- if (_s0) _s0.addEventListener('mouseleave', () => { setVisible(false) })
1846
+ if (_s0) _s0.addEventListener('mouseenter', () => setVisible(true))
1847
+ if (_s0) _s0.addEventListener('mouseleave', () => setVisible(false))
1848
1848
  if (_s2) { (handleMount)(_s2); if (__scopeId) _s2.setAttribute('bf-po', __scopeId) }
1849
1849
  }
1850
1850
 
@@ -1888,7 +1888,7 @@ export function initCounter(__scope, _p = {}) {
1888
1888
  __bfw_s0('s0', escapeTextOrNode(__val))
1889
1889
  })
1890
1890
 
1891
- if (_s1) _s1.addEventListener('click', () => { setCount(n => n + 1) })
1891
+ if (_s1) _s1.addEventListener('click', () => setCount(n => n + 1))
1892
1892
  }
1893
1893
 
1894
1894
  hydrate('Counter', { init: initCounter, template: (_p) => \`<button bf="s1">Count: <!--bf:s0-->\${escapeTextOrMarkup((0))}<!--/--></button>\` })
@@ -1917,7 +1917,7 @@ export function initCounter(__scope, _p = {}) {
1917
1917
  __bfw_s0('s0', escapeTextOrNode(__val))
1918
1918
  })
1919
1919
 
1920
- if (_s2) _s2.addEventListener('click', () => { setCount(n => n + 1) })
1920
+ if (_s2) _s2.addEventListener('click', () => setCount(n => n + 1))
1921
1921
  }
1922
1922
 
1923
1923
  hydrate('Counter', { init: initCounter, template: (_p) => \`<div><span bf="s1">Count: <!--bf:s0-->\${escapeTextOrMarkup((_p.initial ?? 0))}<!--/--></span><button bf="s2">+1</button></div>\` })
@@ -1966,7 +1966,7 @@ export function initCounter(__scope, _p = {}) {
1966
1966
  __bfw_s0('s0', escapeTextOrNode(__val))
1967
1967
  })
1968
1968
 
1969
- if (_s2) _s2.addEventListener('click', () => { setCount(n => n + 1) })
1969
+ if (_s2) _s2.addEventListener('click', () => setCount(n => n + 1))
1970
1970
  }
1971
1971
 
1972
1972
  hydrate('Counter', { init: initCounter, template: (_p) => \`<div><span bf="s1">Count: <!--bf:s0-->\${escapeTextOrMarkup((_p.initial ?? 0))}<!--/--></span><button bf="s2">+1</button></div>\` })
@@ -1995,10 +1995,10 @@ export function initCounter(__scope, _p = {}) {
1995
1995
  __bfw_s0('s0', escapeTextOrNode(__val))
1996
1996
  })
1997
1997
 
1998
- if (_s2) _s2.addEventListener('click', () => { setCount(n => n + 1) })
1998
+ if (_s2) _s2.addEventListener('click', () => setCount(n => n + 1))
1999
1999
  }
2000
2000
 
2001
- hydrate('Counter', { init: initCounter, template: (_p) => \`<div><p bf="s1"><!--bf:s0-->\${escapeTextOrMarkup((_p.initial))}<!--/--></p><button bf="s2">+1</button></div>\` })
2001
+ hydrate('Counter', { init: initCounter, template: (_p) => \`<div><p bf="s1"><!--bf:s0-->\${escapeTextOrMarkup(((_p.initial ?? 0)))}<!--/--></p><button bf="s2">+1</button></div>\` })
2002
2002
  export function Counter(_p, __bfKey) { return createComponent('Counter', _p, __bfKey) }"
2003
2003
  `;
2004
2004
 
@@ -2027,7 +2027,7 @@ export function initCounter(__scope, _p = {}) {
2027
2027
  __bfw_s1('s1', escapeTextOrNode(__val))
2028
2028
  })
2029
2029
 
2030
- if (_s3) _s3.addEventListener('click', () => { setCount(n => n + 1) })
2030
+ if (_s3) _s3.addEventListener('click', () => setCount(n => n + 1))
2031
2031
  }
2032
2032
 
2033
2033
  hydrate('Counter', { init: initCounter, template: (_p) => \`<div><p bf="s2"><!--bf:s0-->\${escapeTextOrMarkup((0))}<!--/--> doubled is <!--bf:s1-->\${escapeTextOrMarkup(((0) * 2))}<!--/--></p><button bf="s3">+1</button></div>\` })
@@ -2052,7 +2052,7 @@ export function initCounter(__scope, _p = {}) {
2052
2052
  __bfw_s0('s0', escapeTextOrNode(__val))
2053
2053
  })
2054
2054
 
2055
- if (_s1) _s1.addEventListener('click', () => { setCount(n => n + 1) })
2055
+ if (_s1) _s1.addEventListener('click', () => setCount(n => n + 1))
2056
2056
  }
2057
2057
 
2058
2058
  hydrate('Counter', { init: initCounter, template: (_p) => \`<button bf="s1">Count: <!--bf:s0-->\${escapeTextOrMarkup((0))}<!--/--></button>\` })
@@ -454,15 +454,17 @@ describe('Adapter output', () => {
454
454
 
455
455
  test('exported arrow function body is preserved in module exports', () => {
456
456
  const honoAdapter = new HonoAdapter()
457
+ // The helper is not named `formatDate`: that is a runtime helper the
458
+ // client JS calls, so a module-level binding of it is BF057 (#3537).
457
459
  const source = `
458
460
  'use client'
459
461
  import { createSignal } from '@barefootjs/client'
460
462
 
461
- export const formatDate = (d) => d.toISOString().split('T')[0]
463
+ export const toIsoDay = (d) => d.toISOString().split('T')[0]
462
464
 
463
465
  export function DatePicker() {
464
466
  const [date, setDate] = createSignal(new Date())
465
- return <span>{formatDate(date())}</span>
467
+ return <span>{toIsoDay(date())}</span>
466
468
  }
467
469
  `
468
470
 
@@ -471,7 +473,7 @@ describe('Adapter output', () => {
471
473
 
472
474
  const template = result.files.find(f => f.type === 'markedTemplate')!
473
475
  expect(template).toBeDefined()
474
- expect(template.content).toContain("export const formatDate = (d) => d.toISOString().split('T')[0]")
476
+ expect(template.content).toContain("export const toIsoDay = (d) => d.toISOString().split('T')[0]")
475
477
  })
476
478
  })
477
479
 
@@ -50,6 +50,39 @@ describe('analyzeComponent', () => {
50
50
  expect(ctx.typeDefinitions[0].name).toBe('CounterProps')
51
51
  })
52
52
 
53
+ test('records string-literal union members in any quoting or layout (#3542)', () => {
54
+ const source = `
55
+ export type Mode =
56
+ | 'light'
57
+ | 'dark'
58
+ type Tone = "warm" | "cool"
59
+ type One = ('only')
60
+ type Paren = 'a' | ('b')
61
+ type Nested = (('x') | ('y' | 'z'))
62
+ type Size = 'auto' | number
63
+ type ParenMixed = 'a' | (1)
64
+ type Point = { x: number }
65
+
66
+ export function Shapes(props: { mode: Mode; tone: Tone; one: One; paren: Paren; nested: Nested; size: Size; parenMixed: ParenMixed; point: Point }) {
67
+ return <div>{props.mode}</div>
68
+ }
69
+ `
70
+
71
+ const ctx = analyzeComponent(source, 'Shapes.tsx')
72
+ const members = Object.fromEntries(ctx.typeDefinitions.map(td => [td.name, td.stringLiteralUnion]))
73
+
74
+ expect(members).toEqual({
75
+ Mode: ['light', 'dark'],
76
+ Tone: ['warm', 'cool'],
77
+ One: ['only'],
78
+ Paren: ['a', 'b'],
79
+ Nested: ['x', 'y', 'z'],
80
+ Size: undefined,
81
+ ParenMixed: undefined,
82
+ Point: undefined,
83
+ })
84
+ })
85
+
53
86
  test('extracts memos', () => {
54
87
  const source = `
55
88
  'use client'
@@ -0,0 +1,44 @@
1
+ import { describe, test, expect } from 'bun:test'
2
+ import { parseExpression } from '../expression-parser'
3
+ import { arithmeticHelper, groupBinaryOperand, groupUnaryOperand } from '../adapters/parsed-expr-emitter'
4
+
5
+ describe('arithmeticHelper (#3474)', () => {
6
+ test('`%` lowers to the runtime\'s JS remainder', () => {
7
+ expect(arithmeticHelper('%')).toBe('mod')
8
+ })
9
+
10
+ test('the other operators are spelled by the adapter', () => {
11
+ for (const op of ['+', '-', '*', '/', '**', '===', '<']) expect(arithmeticHelper(op)).toBeNull()
12
+ })
13
+ })
14
+
15
+ describe('groupUnaryOperand (#3474)', () => {
16
+ const group = (src: string) => {
17
+ const parsed = parseExpression(src)
18
+ if (parsed.kind !== 'unary') throw new Error(`not a unary: ${src}`)
19
+ return groupUnaryOperand(parsed.argument, 'X')
20
+ }
21
+
22
+ test('a compound operand is grouped, as under a binary operator', () => {
23
+ expect(group('-(a + b)')).toBe('(X)')
24
+ expect(group('-(a || b)')).toBe('(X)')
25
+ expect(group('-(a ? b : c)')).toBe('(X)')
26
+ })
27
+
28
+ test('a nested unary operand is grouped, so `--x` never forms', () => {
29
+ expect(group('-(-a)')).toBe('(X)')
30
+ })
31
+
32
+ test('a leaf operand stays as it is', () => {
33
+ expect(group('-a')).toBe('X')
34
+ expect(group('-a.b')).toBe('X')
35
+ expect(group('-f()')).toBe('X')
36
+ })
37
+
38
+ test('agrees with groupBinaryOperand on everything but a unary operand', () => {
39
+ for (const src of ['a + b', 'a && b', 'a ? b : c', 'a', 'a.b', 'f()', '1']) {
40
+ const parsed = parseExpression(src)
41
+ expect(groupUnaryOperand(parsed, 'X')).toBe(groupBinaryOperand(parsed, 'X'))
42
+ }
43
+ })
44
+ })
@@ -0,0 +1,51 @@
1
+ /**
2
+ * #3523: a block-bodied memo with no type argument is typed from the
3
+ * analyzer's fold of its statements, not from the block's text, where `{`
4
+ * reads as an object (Go Template then declared a `map[string]interface{}`
5
+ * field and rendered `map[]`). A block that doesn't fold keeps the text
6
+ * heuristic.
7
+ */
8
+
9
+ import { describe, test, expect } from 'bun:test'
10
+ import { analyzeComponent } from '../analyzer'
11
+
12
+ function memoType(callback: string, typeArg = '') {
13
+ const source = `
14
+ 'use client'
15
+ import { createSignal, createMemo } from '@barefootjs/client'
16
+ export function C({ label }: { label?: string }) {
17
+ const [count] = createSignal(2)
18
+ const m = createMemo${typeArg}(${callback})
19
+ return <p>{String(m())}</p>
20
+ }
21
+ `
22
+ const memo = analyzeComponent(source, 'C.tsx').memos.find((x) => x.name === 'm')
23
+ if (!memo) throw new Error('memo not collected')
24
+ return memo.type.kind
25
+ }
26
+
27
+ describe('block memo type from its fold (#3523)', () => {
28
+ test.each([
29
+ "() => { if (count() > 5) return 'big'\n return 'small' }",
30
+ "() => { if (!label) return 'none'\n return label }",
31
+ '() => { const k = count()\n return k * 3 }',
32
+ ])('%s is not typed as an object', (callback) => {
33
+ expect(memoType(callback)).not.toBe('object')
34
+ })
35
+
36
+ test('a logical fold is unknown, not the fallback type', () => {
37
+ expect(memoType('() => { const k = count()\n return k ?? false }')).toBe('unknown')
38
+ })
39
+
40
+ test('a block returning an object literal is still an object', () => {
41
+ expect(memoType('() => { const k = count()\n return { k } }')).toBe('object')
42
+ })
43
+
44
+ test('a block that does not fold keeps the text heuristic', () => {
45
+ expect(memoType('() => { let n = 0\n for (const x of [1, 2]) n += x\n return n }')).toBe('object')
46
+ })
47
+
48
+ test('an explicit type argument wins', () => {
49
+ expect(memoType('() => { const k = count()\n return k * 3 }', '<number>')).toBe('primitive')
50
+ })
51
+ })
@@ -8,6 +8,8 @@ import { describe, test, expect } from 'bun:test'
8
8
  import {
9
9
  emitCallbackEval,
10
10
  lowerCallbackMethod,
11
+ isUnloweredCall,
12
+ unloweredCallRefusal,
11
13
  type CallbackEvalSpelling,
12
14
  type CallbackLoweringOptions,
13
15
  } from '../adapters/callback-method'
@@ -187,3 +189,76 @@ describe('unboundCallbackParamRead: the index and array params', () => {
187
189
  expect(isSupported(parseExpression('rows.map((r, i) => r.slice(i)).map(r => r.length)')).supported).toBe(false)
188
190
  })
189
191
  })
192
+
193
+ describe('isUnloweredCall (#3493)', () => {
194
+ function call(source: string): Extract<ParsedExpr, { kind: 'call' }> {
195
+ const parsed = parseExpression(source)
196
+ if (parsed.kind !== 'call') throw new Error(`expected a call: ${source}`)
197
+ return parsed
198
+ }
199
+
200
+ const noGlobals = () => false
201
+
202
+ test('a method call with arguments outside the catalogue has no lowering', () => {
203
+ for (const source of ["s().localeCompare('m')", "props.label.localeCompare('m')", 'label.codePointAt(0)']) {
204
+ const c = call(source)
205
+ expect(isUnloweredCall(c.callee, c.args, 'props', noGlobals)).toBe(true)
206
+ }
207
+ })
208
+
209
+ test('a getter read or a function-valued prop keeps the callee read', () => {
210
+ for (const source of ['props.value()', 'props.value.v()', 'props.renderNode(node)']) {
211
+ const c = call(source)
212
+ expect(isUnloweredCall(c.callee, c.args, 'props', noGlobals)).toBe(false)
213
+ }
214
+ // The props parameter's own name, whatever it is.
215
+ const named = call('p.renderNode(node)')
216
+ expect(isUnloweredCall(named.callee, named.args, 'p', noGlobals)).toBe(false)
217
+ expect(isUnloweredCall(named.callee, named.args, null, noGlobals)).toBe(true)
218
+ })
219
+
220
+ test('a zero-arg call rooted at a global has no lowering (#3505)', () => {
221
+ const isGlobal = (name: string) => ['Date', 'Math', 'window', 'location', 'globalThis'].includes(name)
222
+ for (const source of [
223
+ 'Date.now()',
224
+ 'Math.random()',
225
+ 'window.performance.now()',
226
+ 'location.toString()',
227
+ 'Date.now.call()',
228
+ 'Date[method]()',
229
+ 'globalThis[name].now()',
230
+ ]) {
231
+ const c = call(source)
232
+ expect(isUnloweredCall(c.callee, c.args, 'props', isGlobal)).toBe(true)
233
+ }
234
+ })
235
+
236
+ test('a zero-arg call on a bound name keeps the callee read, even when it is named like a global', () => {
237
+ // `isGlobal` is false for a prop, a local (`const navigator = useContext(Nav)`) or a loop binding.
238
+ for (const source of ['ctx.open()', 'navigator.current()', 'document.title()', 'item.label()', 'props.getters[key]()']) {
239
+ const c = call(source)
240
+ expect(isUnloweredCall(c.callee, c.args, 'props', noGlobals)).toBe(false)
241
+ }
242
+ })
243
+ })
244
+
245
+ describe('unloweredCallRefusal (#3493)', () => {
246
+ test('a zero-arg call on a global names the global, not missing arguments (#3505)', () => {
247
+ const call = parseExpression('Date.now()')
248
+ if (call.kind !== 'call') throw new Error('expected a call')
249
+ expect(unloweredCallRefusal(call.callee, call.args, 'Jinja').message).toBe(
250
+ "Call 'Date.now()' reads a JavaScript global, which has no Jinja template binding; read as a value it would drop the call",
251
+ )
252
+ })
253
+
254
+ test('names the whole call and the adapter, and offers both escapes', () => {
255
+ const call = parseExpression("s().localeCompare('m')")
256
+ if (call.kind !== 'call') throw new Error('expected a call')
257
+ const refusal = unloweredCallRefusal(call.callee, call.args, 'Jinja')
258
+ expect(refusal.message).toBe(
259
+ "Call 's().localeCompare(...)' has no Jinja template lowering; read as a value it would drop the call and its arguments",
260
+ )
261
+ expect(refusal.suggestion).toContain('/* @client */')
262
+ expect(refusal.suggestion).toContain('pass it as a prop')
263
+ })
264
+ })
@@ -0,0 +1,226 @@
1
+ /**
2
+ * `IRExpression.childrenRead` (#3539): which expression children read the
3
+ * component's own `children` prop, decided from the binding in Phase 1.
4
+ */
5
+ import { describe, test, expect } from 'bun:test'
6
+ import { compileJSX } from '../compiler'
7
+ import { fallbackChainParts, nullishChainParts } from '../children-nullish'
8
+ import { parseExpression, stringifyParsedExpr } from '../expression-parser'
9
+ import { TestAdapter } from '../adapters/test-adapter'
10
+
11
+ /** `childrenRead` of every expression child, in document order. */
12
+ function childrenReads(source: string): (string | undefined)[] {
13
+ const result = compileJSX(source.trimStart(), 'C.tsx', { adapter: new TestAdapter(), outputIR: true })
14
+ const ir = JSON.parse(result.files.find(f => f.type === 'ir')!.content)
15
+ const out: (string | undefined)[] = []
16
+ const walk = (node: unknown): void => {
17
+ if (!node || typeof node !== 'object') return
18
+ const n = node as Record<string, unknown>
19
+ if (n.type === 'expression') out.push(n.childrenRead as string | undefined)
20
+ for (const value of Object.values(n)) {
21
+ if (Array.isArray(value)) value.forEach(walk)
22
+ else if (value && typeof value === 'object') walk(value)
23
+ }
24
+ }
25
+ walk(ir.root)
26
+ return out
27
+ }
28
+
29
+ describe('nullishChainParts', () => {
30
+ test('a left-nested chain reads its head, else the remaining fallbacks', () => {
31
+ const parts = nullishChainParts(parseExpression("(c ?? '<b>x</b>') ?? ''"))!
32
+ expect(stringifyParsedExpr(parts.head)).toBe('c')
33
+ expect(stringifyParsedExpr(parts.fallback)).toBe('"<b>x</b>" ?? ""')
34
+ })
35
+
36
+ test('a non-nullish expression has no parts', () => {
37
+ expect(nullishChainParts(parseExpression('c || d'))).toBeNull()
38
+ })
39
+ })
40
+
41
+ describe('fallbackChainParts', () => {
42
+ test('a left-nested || chain reads its head, else the remaining fallbacks', () => {
43
+ const parts = fallbackChainParts(parseExpression("(c || '<b>x</b>') || ''"), '||')!
44
+ expect(stringifyParsedExpr(parts.head)).toBe('c')
45
+ expect(stringifyParsedExpr(parts.fallback)).toBe('"<b>x</b>" || ""')
46
+ })
47
+
48
+ test('a chain of the other operator has no parts', () => {
49
+ expect(fallbackChainParts(parseExpression('c ?? d'), '||')).toBeNull()
50
+ expect(fallbackChainParts(parseExpression('(c ?? d) || e'), '??')).toBeNull()
51
+ })
52
+ })
53
+
54
+ describe('childrenRead', () => {
55
+ test('a chained fallback stays a fallback, not an empty read', () => {
56
+ expect(childrenReads(`
57
+ export function Box(props: { children?: unknown }) {
58
+ return <div>{(props.children ?? '<b>fallback</b>') ?? ''}{(props.children ?? '') ?? ''}</div>
59
+ }
60
+ `)).toEqual(['nullish-fallback', 'nullish-empty'])
61
+ })
62
+
63
+ test('an || fallback over the children prop is an or-fallback read', () => {
64
+ expect(childrenReads(`
65
+ export function Box({ children, label }: { children?: unknown; label?: string }) {
66
+ return <div>{children || '<b>x</b>'}{(children || label) || ''}{label || children}{(children ?? label) || ''}</div>
67
+ }
68
+ `)).toEqual(['or-fallback', 'or-fallback', undefined, undefined])
69
+ })
70
+
71
+ test('a loop preamble local named children shadows the prop', () => {
72
+ expect(childrenReads(`
73
+ export function List({ children, rows }: { children?: unknown; rows: { label: string }[] }) {
74
+ return (
75
+ <div>
76
+ {children}
77
+ <ul>{rows.map(r => { const children = r.label; return <li key={r.label}>{children}</li> })}</ul>
78
+ </div>
79
+ )
80
+ }
81
+ `)).toEqual(['bare', undefined])
82
+ })
83
+ })
84
+
85
+ /** The client JS `compileJSX` emits for `source`. */
86
+ function clientJs(source: string): string {
87
+ const result = compileJSX(source.trimStart(), 'C.tsx', { adapter: new TestAdapter() })
88
+ return result.files.find(f => f.type === 'clientJs')?.content ?? ''
89
+ }
90
+
91
+ describe('children prop renamed in the parameter destructure (#3572)', () => {
92
+ test('is a children read, not a reactive prop slot', () => {
93
+ const source = `
94
+ 'use client'
95
+ export function Box({ children: kids }: { children?: unknown }) {
96
+ return <div><p>{kids}</p><p>{kids ?? '<b>x</b>'}</p><p>{kids || '<b>y</b>'}</p></div>
97
+ }
98
+ `
99
+ expect(childrenReads(source)).toEqual(['bare', 'nullish-fallback', 'or-fallback'])
100
+ const js = clientJs(source)
101
+ expect(js).not.toContain('createEffect')
102
+ expect(js).not.toContain('escapeTextOrMarkup(_p.children')
103
+ expect(js).toContain('markupOrEmpty(_p.children)')
104
+ })
105
+
106
+ test('compiles to the same client JS as the unrenamed binding', () => {
107
+ const body = `{label}{KIDS}{KIDS ?? '<b>x</b>'}{KIDS ? <i>{KIDS}</i> : null}`
108
+ const renamed = clientJs(`
109
+ 'use client'
110
+ export function Box({ children: kids, label }: { children?: unknown; label?: string }) {
111
+ return <div>${body.replaceAll('KIDS', 'kids')}</div>
112
+ }
113
+ `)
114
+ const plain = clientJs(`
115
+ 'use client'
116
+ export function Box({ children, label }: { children?: unknown; label?: string }) {
117
+ return <div>${body.replaceAll('KIDS', 'children')}</div>
118
+ }
119
+ `)
120
+ expect(renamed).toBe(plain)
121
+ })
122
+
123
+ test('a handler read binds the renamed local once from _p.children', () => {
124
+ const js = clientJs(`
125
+ 'use client'
126
+ export function Box({ children: kids }: { children?: unknown }) {
127
+ return <button onClick={() => console.log(kids)}>{kids}</button>
128
+ }
129
+ `)
130
+ expect(js).toContain('const kids = _p.children')
131
+ expect(js).toContain('console.log(kids)')
132
+ })
133
+
134
+ test('a template-only read leaves no eager _p.children read in init', () => {
135
+ const js = clientJs(`
136
+ 'use client'
137
+ import { createSignal } from '@barefootjs/client'
138
+ export function Box({ children: kids }: { children?: unknown }) {
139
+ const [n, setN] = createSignal(0)
140
+ return <button onClick={() => setN(n() + 1)}>{n()}{kids}</button>
141
+ }
142
+ `)
143
+ expect(js).toContain('function initBox')
144
+ expect(js).not.toContain('const kids')
145
+ })
146
+
147
+ test('a local named children that binds another prop is that prop', () => {
148
+ const js = clientJs(`
149
+ 'use client'
150
+ export function Box({ label: children }: { label?: string }) {
151
+ return <button onClick={() => console.log(children)}>{children}</button>
152
+ }
153
+ `)
154
+ expect(js).not.toContain('_p.children')
155
+ expect(js).toContain('console.log(_p.label)')
156
+ expect(js).toContain('createEffect')
157
+ })
158
+
159
+ test('a const alias hop of the children prop is a children read', () => {
160
+ expect(childrenReads(`
161
+ export function Box({ children, rows }: { children?: unknown; rows: string[] }) {
162
+ const c = children
163
+ const d = c
164
+ return <div>{c}{d ?? '<b>x</b>'}<ul>{rows.map(c => <li key={c}>{c}</li>)}</ul></div>
165
+ }
166
+ `)).toEqual(['bare', 'nullish-fallback', undefined])
167
+ expect(childrenReads(`
168
+ export function Box(props: { children?: unknown }) {
169
+ const p = props
170
+ const q = p
171
+ const c = q.children
172
+ return <div>{p.children}{q.children || '<b>x</b>'}{c}</div>
173
+ }
174
+ `)).toEqual(['bare', 'or-fallback', 'bare'])
175
+ })
176
+
177
+ test('a loop row shadowing a name an outer alias resolves through does not change the alias', () => {
178
+ // The initializer `props.children` and the hop through `a` resolve in
179
+ // the component scope; only the name read at the use site is checked
180
+ // against the loop rows.
181
+ expect(childrenReads(`
182
+ export function Box(props: { children?: unknown; rows: string[][] }) {
183
+ const { children: c } = props
184
+ return <ul>{props.rows.map((props, i) => <li key={i}>{c}{props.map((props, j) => <i key={j}>{c}</i>)}</li>)}</ul>
185
+ }
186
+ `)).toEqual(['bare', 'bare'])
187
+ expect(childrenReads(`
188
+ export function Box({ children, rows }: { children?: unknown; rows: string[] }) {
189
+ const a = children
190
+ const b = a
191
+ return <ul>{rows.map(a => <li key={a}>{b}{a}</li>)}{rows.map((children, i) => <li key={i}>{b}{children}</li>)}</ul>
192
+ }
193
+ `)).toEqual(['bare', undefined, 'bare', undefined])
194
+ })
195
+
196
+ test('a let alias of the children prop is not a children read', () => {
197
+ expect(childrenReads(`
198
+ export function Box({ children }: { children?: unknown }) {
199
+ let c = children
200
+ return <div>{c}</div>
201
+ }
202
+ `)).toEqual([undefined])
203
+ })
204
+
205
+ test('a read through a const alias of the props object is not a reactive slot', () => {
206
+ const js = clientJs(`
207
+ 'use client'
208
+ export function Box(props: { children?: unknown }) {
209
+ const p = props
210
+ return <div>{p.children}</div>
211
+ }
212
+ `)
213
+ expect(js).not.toContain('createEffect')
214
+ expect(js).toContain('markupOrEmpty((_p).children)')
215
+ })
216
+
217
+ test('a fragment root reading only the renamed binding is transparent', () => {
218
+ const js = clientJs(`
219
+ 'use client'
220
+ export function Pass({ children: kids }: { children?: unknown }) {
221
+ return <>{kids}</>
222
+ }
223
+ `)
224
+ expect(js).toContain('transparent: true')
225
+ })
226
+ })
@@ -186,8 +186,8 @@ describe('#2641 — reactive attribute bindings route catalogued calls through t
186
186
 
187
187
  describe('#2645 — /* @client */ text expressions elide in the static/CSR template like SSR', () => {
188
188
  // `irToComponentTemplateWithOpts`'s 'expression' case never checked
189
- // `node.clientOnly` — a bare-destructured-prop receiver (`isSimplePropExpression`
190
- // treats `createdAt.toISOString()` as "simple") routed to this static
189
+ // `node.clientOnly` — a bare-destructured-prop receiver
190
+ // (`createdAt.toISOString()` reads only a prop) routed to this static
191
191
  // template builder, which had no clientOnly awareness and inlined the
192
192
  // (possibly lowered) value directly, breaking SSR/CSR byte parity: SSR
193
193
  // renders the @client region empty, this builder rendered it populated.
@@ -180,12 +180,37 @@ export function Box(props: { children?: unknown }) {
180
180
  pin: (js) => expect(js).toContain('markupOrEmpty('),
181
181
  },
182
182
  {
183
+ // #3554 — present children stay markup; only the fallback is escaped.
183
184
  id: 'children-nullish-text-fallback',
184
185
  source: `
185
186
  export function Box(props: { children?: unknown }) {
186
187
  return <div>{props.children ?? '<b>text</b>'}</div>
187
188
  }`,
188
- pin: (js) => expect(js).toContain('escapeText('),
189
+ pin: (js) =>
190
+ expect(js).toContain(
191
+ "${_p.children == null ? escapeText(_p.children ?? '<b>text</b>') : markupOrEmpty(_p.children)}",
192
+ ),
193
+ },
194
+ {
195
+ // #3554 — a destructured children reads `_p.children` in the template.
196
+ id: 'children-nullish-text-fallback-destructured',
197
+ source: `
198
+ export function Box({ children }: { children?: unknown }) {
199
+ return <div>{(children ?? '<b>text</b>') ?? ''}</div>
200
+ }`,
201
+ pin: (js) =>
202
+ expect(js).toContain(
203
+ "${_p.children == null ? escapeText((_p.children ?? '<b>text</b>') ?? '') : markupOrEmpty(_p.children)}",
204
+ ),
205
+ },
206
+ {
207
+ id: 'children-or-text-fallback',
208
+ source: `
209
+ export function Box(props: { children?: unknown }) {
210
+ return <div>{props.children || '<b>text</b>'}</div>
211
+ }`,
212
+ pin: (js) =>
213
+ expect(js).toContain("${!_p.children ? escapeText(_p.children || '<b>text</b>') : markupOrEmpty(_p.children)}"),
189
214
  },
190
215
  {
191
216
  // The author-facing escape hatch, by name.
@@ -386,6 +411,11 @@ function classifyHole(expr: ts.Expression, nested: ts.TemplateExpression[]): str
386
411
  }
387
412
  return 'conditional-markup'
388
413
  }
414
+ // `absent ? escapeText(fallback) : markupOrEmpty(children)` —
415
+ // childrenMarkupOrEscapedFallback (#3554): each branch is an escaping or
416
+ // trusted call on its own.
417
+ const branchForms = [e.whenTrue, e.whenFalse].map(b => (ts.isCallExpression(strip(b)) ? classifyHole(b, nested) : null))
418
+ if (branchForms.every(f => f !== null && (f.startsWith('escape:') || f.startsWith('trusted:')))) return 'children-or-fallback'
389
419
  // `x ? 'attr' : ''` / `x != null ? 'attr="' + escapeAttr(x) + '"' : ''`.
390
420
  if (isAttrConcat(e.whenTrue) && (isEmptyString(e.whenFalse) || ts.isStringLiteral(strip(e.whenFalse)))) return 'attr'
391
421
  return null