@barefootjs/jsx 0.40.1 → 0.40.2

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 (176) hide show
  1. package/dist/adapters/ambiguous-nil-arithmetic.d.ts +4 -3
  2. package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -1
  3. package/dist/adapters/boolean-result.d.ts +32 -10
  4. package/dist/adapters/boolean-result.d.ts.map +1 -1
  5. package/dist/adapters/brace-delimiter-text.d.ts +18 -0
  6. package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
  7. package/dist/adapters/callback-method.d.ts +38 -5
  8. package/dist/adapters/callback-method.d.ts.map +1 -1
  9. package/dist/adapters/computed-const.d.ts +11 -1
  10. package/dist/adapters/computed-const.d.ts.map +1 -1
  11. package/dist/adapters/dsl-template-adapter.d.ts +21 -0
  12. package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
  13. package/dist/adapters/loop-bound-names.d.ts +9 -1
  14. package/dist/adapters/loop-bound-names.d.ts.map +1 -1
  15. package/dist/adapters/loop-row-type.d.ts +52 -0
  16. package/dist/adapters/loop-row-type.d.ts.map +1 -0
  17. package/dist/adapters/nil-reading.d.ts +68 -0
  18. package/dist/adapters/nil-reading.d.ts.map +1 -0
  19. package/dist/adapters/non-primitive-equality.d.ts +33 -0
  20. package/dist/adapters/non-primitive-equality.d.ts.map +1 -0
  21. package/dist/adapters/number-value.d.ts +5 -3
  22. package/dist/adapters/number-value.d.ts.map +1 -1
  23. package/dist/adapters/operand-typing.d.ts +126 -0
  24. package/dist/adapters/operand-typing.d.ts.map +1 -0
  25. package/dist/adapters/parsed-expr-emitter.d.ts +70 -12
  26. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  27. package/dist/adapters/primitive-plan.d.ts +291 -0
  28. package/dist/adapters/primitive-plan.d.ts.map +1 -0
  29. package/dist/analyzer-context.d.ts +4 -0
  30. package/dist/analyzer-context.d.ts.map +1 -1
  31. package/dist/analyzer.d.ts.map +1 -1
  32. package/dist/augment-inherited-props.d.ts +63 -8
  33. package/dist/augment-inherited-props.d.ts.map +1 -1
  34. package/dist/compiler.d.ts.map +1 -1
  35. package/dist/event-handler-name.d.ts +12 -0
  36. package/dist/event-handler-name.d.ts.map +1 -1
  37. package/dist/expression-parser.d.ts +8 -0
  38. package/dist/expression-parser.d.ts.map +1 -1
  39. package/dist/index.d.ts +19 -9
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +4628 -2198
  42. package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
  43. package/dist/ir-to-client-js/child-props-object.d.ts +64 -0
  44. package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
  45. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  46. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  47. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  48. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  49. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +4 -2
  50. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
  51. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  52. package/dist/ir-to-client-js/imports.d.ts +2 -2
  53. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  54. package/dist/ir-to-client-js/index.d.ts +2 -6
  55. package/dist/ir-to-client-js/index.d.ts.map +1 -1
  56. package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
  57. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +30 -18
  58. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
  59. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  60. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
  61. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
  62. package/dist/jsx-to-ir.d.ts +1 -0
  63. package/dist/jsx-to-ir.d.ts.map +1 -1
  64. package/dist/nullable-signal-attr.d.ts +68 -9
  65. package/dist/nullable-signal-attr.d.ts.map +1 -1
  66. package/dist/prop-rewrite.d.ts +7 -0
  67. package/dist/prop-rewrite.d.ts.map +1 -1
  68. package/dist/props-object-whole-read.d.ts +15 -0
  69. package/dist/props-object-whole-read.d.ts.map +1 -0
  70. package/dist/rich-type-refusal.d.ts.map +1 -1
  71. package/dist/scope/binding-scope.d.ts +85 -10
  72. package/dist/scope/binding-scope.d.ts.map +1 -1
  73. package/dist/scope/lexical-scope-walk.d.ts +3 -2
  74. package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
  75. package/dist/scope/root-prop-alias.d.ts +10 -7
  76. package/dist/scope/root-prop-alias.d.ts.map +1 -1
  77. package/dist/strip-types.d.ts +13 -0
  78. package/dist/strip-types.d.ts.map +1 -1
  79. package/dist/template-position-walk.d.ts +21 -3
  80. package/dist/template-position-walk.d.ts.map +1 -1
  81. package/dist/types.d.ts +90 -0
  82. package/dist/types.d.ts.map +1 -1
  83. package/package.json +2 -2
  84. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +3 -3
  85. package/src/__tests__/binding-scope.test.ts +89 -1
  86. package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
  87. package/src/__tests__/boolean-result.test.ts +22 -5
  88. package/src/__tests__/brace-delimiter-text.test.ts +26 -0
  89. package/src/__tests__/callback-method.test.ts +62 -2
  90. package/src/__tests__/child-components-in-map.test.ts +1 -1
  91. package/src/__tests__/computed-const.test.ts +103 -0
  92. package/src/__tests__/emit-to-string-call.test.ts +2 -1
  93. package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
  94. package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
  95. package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
  96. package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
  97. package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
  98. package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
  99. package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +2 -1
  100. package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
  101. package/src/__tests__/lookup-literal-const.test.ts +9 -3
  102. package/src/__tests__/loop-index-reactivity.test.ts +2 -2
  103. package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
  104. package/src/__tests__/loop-plan.test.ts +1 -1
  105. package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
  106. package/src/__tests__/loop-row-type.test.ts +64 -0
  107. package/src/__tests__/lowering-value-position.test.ts +4 -0
  108. package/src/__tests__/module-const-destructure-default.test.ts +156 -0
  109. package/src/__tests__/nested-loop-index-param.test.ts +1 -1
  110. package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
  111. package/src/__tests__/nested-row-shadowing.test.ts +2 -2
  112. package/src/__tests__/nil-reading.test.ts +77 -0
  113. package/src/__tests__/nullable-signal-attr.test.ts +134 -0
  114. package/src/__tests__/primitive-plan.test.ts +416 -0
  115. package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
  116. package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
  117. package/src/__tests__/props-object-whole-read.test.ts +40 -0
  118. package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
  119. package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
  120. package/src/__tests__/rest-binding-gaps.test.ts +192 -0
  121. package/src/__tests__/root-prop-alias.test.ts +53 -8
  122. package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
  123. package/src/__tests__/static-record-literal-array.test.ts +171 -0
  124. package/src/__tests__/string-concat-identifier.test.ts +40 -1
  125. package/src/adapters/ambiguous-nil-arithmetic.ts +22 -13
  126. package/src/adapters/boolean-result.ts +72 -66
  127. package/src/adapters/brace-delimiter-text.ts +25 -0
  128. package/src/adapters/callback-method.ts +151 -27
  129. package/src/adapters/computed-const.ts +238 -4
  130. package/src/adapters/dsl-template-adapter.ts +40 -2
  131. package/src/adapters/loop-bound-names.ts +9 -1
  132. package/src/adapters/loop-plan.ts +1 -1
  133. package/src/adapters/loop-row-type.ts +139 -0
  134. package/src/adapters/nil-reading.ts +297 -0
  135. package/src/adapters/non-primitive-equality.ts +92 -0
  136. package/src/adapters/number-value.ts +17 -4
  137. package/src/adapters/operand-typing.ts +522 -0
  138. package/src/adapters/parsed-expr-emitter.ts +154 -19
  139. package/src/adapters/primitive-plan.ts +696 -0
  140. package/src/analyzer-context.ts +6 -0
  141. package/src/analyzer.ts +505 -19
  142. package/src/augment-inherited-props.ts +108 -10
  143. package/src/compiler.ts +17 -0
  144. package/src/event-handler-name.ts +15 -0
  145. package/src/expression-parser.ts +70 -1
  146. package/src/index.ts +33 -7
  147. package/src/ir-to-client-js/build-references.ts +69 -0
  148. package/src/ir-to-client-js/child-props-object.ts +106 -0
  149. package/src/ir-to-client-js/collect-elements.ts +30 -55
  150. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +6 -1
  151. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +29 -28
  152. package/src/ir-to-client-js/control-flow/shared.ts +20 -22
  153. package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +5 -3
  154. package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +2 -2
  155. package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +1 -1
  156. package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +2 -2
  157. package/src/ir-to-client-js/control-flow/stringify/loop.ts +1 -1
  158. package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +3 -3
  159. package/src/ir-to-client-js/html-template.ts +25 -8
  160. package/src/ir-to-client-js/imports.ts +1 -1
  161. package/src/ir-to-client-js/index.ts +53 -11
  162. package/src/ir-to-client-js/plan/build-static-array-child-init.ts +31 -14
  163. package/src/ir-to-client-js/plan/static-array-child-init.ts +27 -18
  164. package/src/ir-to-client-js/prop-handling.ts +22 -3
  165. package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
  166. package/src/jsx-to-ir.ts +446 -37
  167. package/src/nullable-signal-attr.ts +246 -29
  168. package/src/prop-rewrite.ts +30 -6
  169. package/src/props-object-whole-read.ts +87 -0
  170. package/src/rich-type-refusal.ts +2 -1
  171. package/src/scope/binding-scope.ts +182 -18
  172. package/src/scope/lexical-scope-walk.ts +4 -3
  173. package/src/scope/root-prop-alias.ts +15 -8
  174. package/src/strip-types.ts +31 -3
  175. package/src/template-position-walk.ts +57 -28
  176. package/src/types.ts +87 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barefootjs/jsx",
3
- "version": "0.40.1",
3
+ "version": "0.40.2",
4
4
  "description": "JSX compiler for BarefootJS - transforms JSX to server HTML + client JS",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -49,7 +49,7 @@
49
49
  "directory": "packages/jsx"
50
50
  },
51
51
  "dependencies": {
52
- "@barefootjs/shared": "0.40.1"
52
+ "@barefootjs/shared": "0.40.2"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "@barefootjs/client": ">=0.2.0",
@@ -519,7 +519,7 @@ export function Example(_p, __bfKey) { return createComponent('Example', _p, __b
519
519
  `;
520
520
 
521
521
  exports[`docs/core/rendering/jsx-compatibility.md doc-examples L85 — ❌ BF101 on Go/Mojo; works on Hono 1`] = `
522
- "import { $, createComponent, createEffect, escapeAttr, escapeText, hydrate, lazySlots, withParentScope } from '@barefootjs/client/runtime'
522
+ "import { $, createComponent, createEffect, escapeAttr, escapeText, hydrate, lazySlots, textOrNode, withParentScope } from '@barefootjs/client/runtime'
523
523
 
524
524
 
525
525
  export function initReactionBar(__scope, _p = {}) {
@@ -546,8 +546,8 @@ export function initReactionBar(__scope, _p = {}) {
546
546
  }
547
547
  if (__iterEl) {
548
548
  const __bfw_s0 = lazySlots(__iterEl, [{ id: 's0', kind: 'text', path: [] }, { id: 's1', kind: 'text', path: [] }])
549
- createEffect(() => { __bfw_s0('s0', String(emoji)) })
550
- createEffect(() => { __bfw_s0('s1', String(String(users.length))) })
549
+ createEffect(() => { __bfw_s0('s0', textOrNode(emoji)) })
550
+ createEffect(() => { __bfw_s0('s1', textOrNode(String(users.length))) })
551
551
  }
552
552
  })
553
553
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
 
9
9
  import { describe, test, expect } from 'bun:test'
10
- import { BindingScope } from '../scope/binding-scope.ts'
10
+ import { arrayElementType, BindingScope, isSameLoopValueShape, loopValueTypeAt, unionOfLoopValueTypes } from '../scope/binding-scope.ts'
11
11
 
12
12
  describe('BindingScope', () => {
13
13
  test('EMPTY: nothing bound, lookup null, empty boundNames, shadow predicate false', () => {
@@ -265,3 +265,91 @@ describe('enterAliases (#3402)', () => {
265
265
  expect(BindingScope.EMPTY.enterAliases(new Map([['a', 'b']]))).toBe(BindingScope.EMPTY)
266
266
  })
267
267
  })
268
+
269
+ describe('row field string typing (#3762)', () => {
270
+ const fields = { Row: [{ name: 'id', type: { primitive: 'string' } }, { name: 'n', type: { primitive: 'number' } }] }
271
+ const named = (name: string) => fields[name as keyof typeof fields]
272
+ const rowType = { raw: 'Row' }
273
+
274
+ test('a row read resolves its field through the named type', () => {
275
+ const scope = BindingScope.EMPTY.enterLoopRow({ param: 'r', rowType })
276
+ expect(scope.isStringValueName('r', () => true, false, ['id'], named)).toBe(true)
277
+ expect(scope.isStringValueName('r', () => true, false, ['n'], named)).toBe(false)
278
+ expect(scope.isStringValueName('r', () => true, false, ['id'])).toBe(false)
279
+ expect(scope.isStringValueName('r', () => true)).toBe(false)
280
+ })
281
+
282
+ test('a destructured binding resolves its own path', () => {
283
+ const scope = BindingScope.EMPTY.enterLoopRow({
284
+ param: '{ id, n }',
285
+ rowType,
286
+ paramBindings: [
287
+ { name: 'id', segments: [{ kind: 'field', key: 'id', isIdent: true }] },
288
+ { name: 'n', segments: [{ kind: 'field', key: 'n', isIdent: true }] },
289
+ ],
290
+ })
291
+ expect(scope.isStringValueName('id', () => false, false, [], named)).toBe(true)
292
+ expect(scope.isStringValueName('n', () => true, false, [], named)).toBe(false)
293
+ })
294
+
295
+ test('an outer name types itself, not its members; a shadowing props row types props.<field>', () => {
296
+ expect(BindingScope.EMPTY.isStringValueName('label', () => true, false, ['length'])).toBe(false)
297
+ const props = BindingScope.EMPTY.enterLoopRow({ param: 'props', rowType })
298
+ expect(props.isStringValueName('id', () => false, true, [], named)).toBe(true)
299
+ expect(props.isStringValueName('n', () => true, true, [], named)).toBe(false)
300
+ })
301
+
302
+ test('a callback param shadows an outer string name', () => {
303
+ const scope = BindingScope.EMPTY.enterCallback(['label'])
304
+ expect(scope.isStringValueName('label', () => true)).toBe(false)
305
+ })
306
+ })
307
+
308
+ describe('optional row fields (#3762 review)', () => {
309
+ const fields = { Opt: [{ name: 'id', type: { primitive: 'string' }, optional: true }] }
310
+ const named = (name: string) => fields[name as keyof typeof fields]
311
+
312
+ test('an optional string field is not definitely a string, read or destructured', () => {
313
+ const row = BindingScope.EMPTY.enterLoopRow({ param: 'o', rowType: { raw: 'Opt' } })
314
+ expect(row.isStringValueName('o', () => true, false, ['id'], named)).toBe(false)
315
+ const destructured = BindingScope.EMPTY.enterLoopRow({
316
+ param: '{ id }',
317
+ rowType: { raw: 'Opt' },
318
+ paramBindings: [{ name: 'id', segments: [{ kind: 'field', key: 'id', isIdent: true }] }],
319
+ })
320
+ expect(destructured.isStringValueName('id', () => true, false, [], named)).toBe(false)
321
+ })
322
+ })
323
+
324
+ describe('arrayElementType and union normalisation (#3762 review)', () => {
325
+ const row = { kind: 'interface', raw: 'Row' }
326
+ const other = { kind: 'interface', raw: 'Other' }
327
+ const rows = { kind: 'array', raw: 'Row[]', elementType: row }
328
+
329
+ test('an array reads its element; a union of arrays reads the union of their elements', () => {
330
+ expect(arrayElementType(rows)).toBe(row)
331
+ const mixed = { kind: 'union', unionTypes: [rows, { kind: 'array', raw: 'Other[]', elementType: other }] }
332
+ expect(arrayElementType(mixed)?.unionTypes).toEqual([row, other])
333
+ })
334
+
335
+ test('a union with a non-array member, a scalar or nothing has no element type', () => {
336
+ expect(arrayElementType({ kind: 'union', unionTypes: [rows, { kind: 'primitive', primitive: 'undefined' }] })).toBeUndefined()
337
+ expect(arrayElementType({ kind: 'primitive', primitive: 'string' })).toBeUndefined()
338
+ expect(arrayElementType(null)).toBeUndefined()
339
+ })
340
+
341
+ test('structurally identical members collapse to one; different ones stay a union', () => {
342
+ const copy = { kind: 'array', raw: 'Row[]', elementType: { kind: 'interface', raw: 'Row' } }
343
+ expect(unionOfLoopValueTypes([rows, copy])).toBe(rows)
344
+ expect(unionOfLoopValueTypes([row])).toBe(row)
345
+ expect(unionOfLoopValueTypes([row, other]).unionTypes).toEqual([row, other])
346
+ expect(isSameLoopValueShape(row, other)).toBe(false)
347
+ })
348
+
349
+ test('a nested array field read across a union keeps its element type', () => {
350
+ const child = { kind: 'object', raw: '{ id: string }', properties: [{ name: 'id', type: { primitive: 'string' } }] }
351
+ const withChildren = (raw: string) => ({ kind: 'interface', raw, properties: [{ name: 'children', type: { kind: 'array', raw: 'C[]', elementType: child } }] })
352
+ const children = loopValueTypeAt(unionOfLoopValueTypes([withChildren('A'), withChildren('B')]), [{ kind: 'field', key: 'children', isIdent: true }])
353
+ expect(arrayElementType(children)).toEqual(child)
354
+ })
355
+ })
@@ -0,0 +1,123 @@
1
+ /**
2
+ * A body destructure whose initializer is a component-scope `const` alias of
3
+ * the props object (`const p = props; const { x } = p`) is a destructure of
4
+ * the props object: the analyzer expands it into one `props.<key>`
5
+ * `localConstants` entry per name, exactly like `const { x } = props`.
6
+ * Before, only the literal props identifier was recognized and the
7
+ * destructure was dropped with no diagnostic, so every read of `x` was an
8
+ * unbound name (Hono SSR threw `ReferenceError`).
9
+ *
10
+ * Shapes that are not provably the props object stay unrecognized: a `let`
11
+ * alias (a reassignment can repoint it), an alias declared after the
12
+ * destructure, a destructure in a nested block (a block-local binding
13
+ * can shadow the alias there), and any alias when the props parameter
14
+ * itself is reassigned (the alias keeps the object it captured).
15
+ */
16
+
17
+ import { describe, test, expect } from 'bun:test'
18
+ import { analyzeComponent } from '../analyzer'
19
+
20
+ function destructured(body: string) {
21
+ const source = `
22
+ type P = { value: number; label?: string; onPick?: () => void }
23
+ export function C(props: P) {
24
+ ${body}
25
+ return <div />
26
+ }
27
+ `
28
+ const ctx = analyzeComponent(source, 'test.tsx')
29
+ return ctx.localConstants
30
+ .filter(c => c.propsDestructure !== undefined)
31
+ .map(c => ({ name: c.name, value: c.value, sourceKey: c.propsDestructure?.sourceKey }))
32
+ }
33
+
34
+ const EXPANDED = [
35
+ { name: 'value', value: 'props.value', sourceKey: 'value' },
36
+ { name: 'label', value: "props.label ?? 'none'", sourceKey: 'label' },
37
+ { name: 'pick', value: 'props.onPick', sourceKey: 'onPick' },
38
+ ]
39
+
40
+ describe('body destructure of a const alias of the props object', () => {
41
+ test('the props object itself (baseline)', () => {
42
+ expect(destructured(`const { value, label = 'none', onPick: pick } = props`)).toEqual(EXPANDED)
43
+ })
44
+
45
+ test('a const alias expands like the props object', () => {
46
+ expect(destructured(`
47
+ const p = props
48
+ const { value, label = 'none', onPick: pick } = p
49
+ `)).toEqual(EXPANDED)
50
+ })
51
+
52
+ test('a chain of const aliases expands like the props object', () => {
53
+ expect(destructured(`
54
+ const p = props
55
+ const q = p
56
+ const { value, label = 'none', onPick: pick } = q
57
+ `)).toEqual(EXPANDED)
58
+ })
59
+
60
+ test('a let alias is not the props object', () => {
61
+ expect(destructured(`
62
+ let p = props
63
+ const { value } = p
64
+ `)).toEqual([])
65
+ })
66
+
67
+ test('an alias of a let alias is not the props object', () => {
68
+ expect(destructured(`
69
+ let p = props
70
+ const q = p
71
+ const { value } = q
72
+ `)).toEqual([])
73
+ })
74
+
75
+ test('an alias of another object is not the props object', () => {
76
+ expect(destructured(`
77
+ const other = { value: 1 }
78
+ const { value } = other
79
+ `)).toEqual([])
80
+ })
81
+
82
+ test('an alias declared after the destructure is not resolved', () => {
83
+ expect(destructured(`
84
+ const { value } = p
85
+ const p = props
86
+ `)).toEqual([])
87
+ })
88
+
89
+ for (const [label, reassign] of [
90
+ ['an assignment', `props = { value: 2 }`],
91
+ ['a destructuring assignment', `;({ props } = { props: { value: 2 } })`],
92
+ ['an assignment inside a handler', `const reset = () => { props = { value: 2 } }`],
93
+ ['a for-of loop head inside a handler', `const reset = () => { for (props of [{ value: 2 }]) {} }`],
94
+ ['a destructuring for-of loop head', `for ({ props } of [{ props: { value: 2 } }]) {}`],
95
+ ['a for-in loop head', `for (props in { a: 1 }) {}`],
96
+ ['an update expression', `props++`],
97
+ ] as const) {
98
+ test(`an alias is not the props object when the props parameter is reassigned by ${label}`, () => {
99
+ expect(destructured(`
100
+ const p = props
101
+ ${reassign}
102
+ const { value } = p
103
+ `)).toEqual([])
104
+ })
105
+ }
106
+
107
+ test('reassigning the props parameter still leaves a direct destructure of it alone', () => {
108
+ expect(destructured(`
109
+ props = { value: 2 }
110
+ const { value } = props
111
+ `)).toEqual([{ name: 'value', value: 'props.value', sourceKey: 'value' }])
112
+ })
113
+
114
+ test('a destructure in a nested block does not resolve the alias', () => {
115
+ expect(destructured(`
116
+ const p = props
117
+ if (p) {
118
+ const p = { value: 2 }
119
+ const { value } = p
120
+ }
121
+ `)).toEqual([])
122
+ })
123
+ })
@@ -1,10 +1,10 @@
1
1
  import { describe, test, expect } from 'bun:test'
2
- import { collectBooleanValueNames, isBooleanResultParsed, stringifyBooleanTernaryBranches } from '../adapters/boolean-result'
2
+ import { collectBooleanValueNames, isBooleanResultParsed, stringifyBooleanBranches } from '../adapters/boolean-result'
3
3
  import { exprToString, parseExpression } from '../expression-parser'
4
4
 
5
- const rewrite = (src: string) => exprToString(stringifyBooleanTernaryBranches(parseExpression(src)!))
5
+ const rewrite = (src: string) => exprToString(stringifyBooleanBranches(parseExpression(src)!))
6
6
 
7
- describe('stringifyBooleanTernaryBranches (#3349)', () => {
7
+ describe('stringifyBooleanBranches (#3349, #3789)', () => {
8
8
  test('a literal boolean branch next to a non-boolean branch becomes a string literal', () => {
9
9
  expect(rewrite("yes() ? false : s()")).toBe(exprToString(parseExpression("yes() ? 'false' : s()")!))
10
10
  })
@@ -17,12 +17,29 @@ describe('stringifyBooleanTernaryBranches (#3349)', () => {
17
17
  expect(rewrite("a() ? (b() ? 'x' : false) : 'y'")).toBe(exprToString(parseExpression("a() ? (b() ? 'x' : 'false') : 'y'")!))
18
18
  })
19
19
 
20
+ test('a comparison an && / || / ?? operand returns is stringified where it decides the result', () => {
21
+ expect(rewrite("n() > 0 && 'y'")).toBe(exprToString(parseExpression("n() > 0 ? 'y' : 'false'")!))
22
+ expect(rewrite("n() > 0 || 'y'")).toBe(exprToString(parseExpression("n() > 0 ? 'true' : 'y'")!))
23
+ expect(rewrite("!n() ?? 'y'")).toBe(exprToString(parseExpression("!n() ? 'true' : 'false'")!))
24
+ expect(rewrite("s() || n() > 0")).toBe(exprToString(parseExpression("s() || (n() > 0 ? 'true' : 'false')")!))
25
+ expect(rewrite("n() > 0 && (m() < 0 || 'y')")).toBe(exprToString(parseExpression("n() > 0 ? (m() < 0 ? 'true' : 'y') : 'false'")!))
26
+ const plain = parseExpression("s() && 'y'")!
27
+ expect(stringifyBooleanBranches(plain)).toBe(plain)
28
+ })
29
+
30
+ test('a mixed left operand that can yield a boolean keeps its truthiness test and yields the rewritten form', () => {
31
+ expect(rewrite("(n() > 0 && 'y') && 'z'")).toBe(exprToString(parseExpression("(n() > 0 && 'y') ? 'z' : (n() > 0 ? 'y' : 'false')")!))
32
+ expect(rewrite("(n() ? 'y' : false) && 'z'")).toBe(exprToString(parseExpression("(n() ? 'y' : false) ? 'z' : (n() ? 'y' : 'false')")!))
33
+ expect(rewrite("(n() ? 'y' : false) || 'z'")).toBe(exprToString(parseExpression("(n() ? 'y' : false) ? (n() ? 'y' : 'false') : 'z'")!))
34
+ expect(rewrite("((n() > 0) && 'y') ?? 'z'")).toBe(exprToString(parseExpression("(n() > 0 ? 'y' : 'false') ?? 'z'")!))
35
+ })
36
+
20
37
  test('a wholly boolean ternary and a non-ternary are left unchanged', () => {
21
38
  const whole = parseExpression('c() ? true : false')!
22
39
  expect(isBooleanResultParsed(whole)).toBe(true)
23
- expect(stringifyBooleanTernaryBranches(whole)).toBe(whole)
40
+ expect(stringifyBooleanBranches(whole)).toBe(whole)
24
41
  const plain = parseExpression('s()')!
25
- expect(stringifyBooleanTernaryBranches(plain)).toBe(plain)
42
+ expect(stringifyBooleanBranches(plain)).toBe(plain)
26
43
  })
27
44
  })
28
45
 
@@ -0,0 +1,26 @@
1
+ import { describe, test, expect } from 'bun:test'
2
+ import { escapeBraceDelimiters } from '../adapters/brace-delimiter-text'
3
+
4
+ describe('escapeBraceDelimiters (#3767)', () => {
5
+ const B = "{{ '{' }}"
6
+ const esc = (t: string) => escapeBraceDelimiters(t, '{%#', B)
7
+
8
+ test('rewrites a brace that opens a tag', () => {
9
+ expect(esc('{{ 1 }}')).toBe(`${B}{ 1 }}`)
10
+ expect(esc('{% if x %}')).toBe(`${B}% if x %}`)
11
+ expect(esc('{# c #}')).toBe(`${B}# c #}`)
12
+ })
13
+
14
+ test('rewrites every brace of a run, so none is left before another', () => {
15
+ expect(esc('{{{')).toBe(B + B + B)
16
+ })
17
+
18
+ test('rewrites a brace that ends the text', () => {
19
+ expect(esc('a{')).toBe(`a${B}`)
20
+ })
21
+
22
+ test('leaves a brace that cannot open a tag, and closing braces', () => {
23
+ expect(esc('{a} }} %} #}')).toBe('{a} }} %} #}')
24
+ expect(escapeBraceDelimiters('{%', '{', '{{"{"}}')).toBe('{%')
25
+ })
26
+ })
@@ -30,9 +30,9 @@ function callOf(source: string) {
30
30
  return call
31
31
  }
32
32
 
33
- const lower = (source: string, options = jinja) => {
33
+ const lower = (source: string, options = jinja, propsObject = 'props') => {
34
34
  const call = callOf(source)
35
- return lowerCallbackMethod(call.method, call.arrow, call.args, options)
35
+ return lowerCallbackMethod(call.method, call.arrow, call.args, options, propsObject)
36
36
  }
37
37
 
38
38
  const spelling: CallbackEvalSpelling = {
@@ -262,3 +262,63 @@ describe('unloweredCallRefusal (#3493)', () => {
262
262
  expect(refusal.suggestion).toContain('pass it as a prop')
263
263
  })
264
264
  })
265
+
266
+ describe('lowerCallbackMethod: props.X reads in an evaluator body', () => {
267
+ // A template has no `props` object, so the env binds each `props.X` read
268
+ // under a fresh name, spelled as the adapter spells `props.X` elsewhere.
269
+ const emitPropsRead = (e: ParsedExpr) =>
270
+ e.kind === 'member' && e.object.kind === 'identifier' && (e.object.name === 'props' || e.object.name === 'p')
271
+ ? `$${e.property}`
272
+ : emitIdentifier(e)
273
+ const renderProps = (source: string, propsObject = 'props') => {
274
+ const plan = lower(source, jinja, propsObject)
275
+ if (plan.kind !== 'eval') throw new Error(`expected an eval plan, got ${plan.kind}`)
276
+ return emitCallbackEval(plan, 'RECV', emitPropsRead, spelling)
277
+ }
278
+
279
+ test('a predicate reading props.n binds the read, not props', () => {
280
+ expect(renderProps('props.items.filter(i => i > props.n)')).toBe(
281
+ `bf.filter_eval(RECV, '{"kind":"binary","op":">","left":{"kind":"identifier","name":"i"},"right":{"kind":"identifier","name":"__bf_props_n"}}', 'i', {__bf_props_n=$n})`,
282
+ )
283
+ })
284
+
285
+ test('a param named like the prop keeps its own value', () => {
286
+ expect(renderProps('props.items.filter(n => n > props.n)')).toEndWith(`'n', {__bf_props_n=$n})`)
287
+ })
288
+
289
+ test('two reads of one prop share a name; other free names stay as they are', () => {
290
+ expect(renderProps('props.items.filter(i => i > props.n && i < props.n + limit)')).toEndWith(
291
+ `'i', {__bf_props_n=$n, limit=$limit})`,
292
+ )
293
+ })
294
+
295
+ test('the fresh name avoids every name the body uses', () => {
296
+ expect(renderProps('props.items.filter(__bf_props_n => __bf_props_n > props.n)')).toEndWith(
297
+ `'__bf_props_n', {__bf_props_n_=$n})`,
298
+ )
299
+ })
300
+
301
+ test('a param named props is not the props object', () => {
302
+ expect(renderProps('items.filter(props => props.n > 1)')).toEndWith(`'props', {})`)
303
+ })
304
+
305
+ test('a nested arrow that binds props shadows it in its body only', () => {
306
+ const out = renderProps('items.filter(i => i.tags.filter(props => props.on).length > props.n)')
307
+ expect(out).toContain('"object":{"kind":"identifier","name":"props"},"property":"on"')
308
+ expect(out).toEndWith(`'i', {__bf_props_n=$n})`)
309
+ })
310
+
311
+ test('sort, reduce and map bodies lift props.X reads too', () => {
312
+ expect(renderProps('props.items.toSorted((a, b) => props.desc ? b - a : a - b)')).toEndWith(
313
+ `{__bf_props_desc=$desc})`,
314
+ )
315
+ expect(renderProps('props.items.reduce((acc, i) => acc + i * props.k, 0)')).toEndWith(`{__bf_props_k=$k})`)
316
+ expect(renderProps('props.items.map(i => i * props.k)')).toEndWith(`{__bf_props_k=$k})`)
317
+ })
318
+
319
+ test('a props object named p lifts p.X reads, and props is then an ordinary name', () => {
320
+ expect(renderProps('p.items.filter(i => i > p.n)', 'p')).toEndWith(`'i', {__bf_props_n=$n})`)
321
+ expect(renderProps('p.items.some(p => p.on)', 'p')).toEndWith(`'p', {})`)
322
+ expect(renderProps('p.items.filter(i => i > props.n)', 'p')).toEndWith(`'i', {props=$props})`)
323
+ })
324
+ })
@@ -1230,7 +1230,7 @@ describe('child components inside .map() (#344)', () => {
1230
1230
  // Document-order zip shape — a flat cursor over the queried scopes
1231
1231
  // pairs each with its data item across the nested forEach. This shape
1232
1232
  // is fragment-root safe (no per-group wrapper element to index).
1233
- expect(content).toContain('qsaChildScopes')
1233
+ expect(content).toContain('childScopesBySlot')
1234
1234
  expect(content).toMatch(/__compScopes\[__ci\+\+\]/)
1235
1235
  // No element-offset addressing (`__outerEl = ...children[...]`) — that
1236
1236
  // would break for a fragment-rooted passthrough.
@@ -184,3 +184,106 @@ describe('only names the component scope binds shadow a module const (#3696)', (
184
184
  expect(inliner(source)('m')).toBe('props.n * 2')
185
185
  })
186
186
  })
187
+
188
+ describe('a read of an object const reads what the object holds (#3844)', () => {
189
+ const source = `
190
+ 'use client'
191
+ import { createSignal } from '@barefootjs/client'
192
+ const MODULE = { label: 'mod', inner: { mode: 'dark' } }
193
+ export function C(props: { n: number; kind: 'ok' | 'err'; other?: 'ok' | 'nope'; free: string; proto: 'ok' | 'toString' }) {
194
+ const [sel] = createSignal('ok')
195
+ const flags = { big: props.n > 1, label: 'L' + props.n }
196
+ const labels = { ok: 'Fine', err: 'Broken' }
197
+ const numbered = { 1: 'one', two: 'two' }
198
+ const mutated = { a: 1 }
199
+ mutated.a = 2
200
+ return <p>{flags.label}</p>
201
+ }
202
+ `
203
+
204
+ test('a member read is the value the literal gives the key, a missing key undefined', () => {
205
+ const inline = inliner(source)
206
+ expect(inline('flags.big ? "big" : "small"')).toBe('props.n > 1 ? "big" : "small"')
207
+ expect(inline("flags['label']")).toBe('"L" + props.n')
208
+ expect(inline('flags.missing')).toBe('undefined')
209
+ expect(inline('MODULE.inner.mode')).toBe('"dark"')
210
+ })
211
+
212
+ test('a read off a projected value keeps it as its object', () => {
213
+ const metadata = buildMetadata(analyzeComponent(source, 'test.tsx'))
214
+ const inlined = inlineComputedConsts(parseExpression('flags.label.length'), computedConstContext(metadata, () => false))
215
+ expect(inlined.kind === 'member' && inlined.property === 'length' && inlined.object.kind).toBe('binary')
216
+ })
217
+
218
+ test('an index typed as a union of string literals is the conditional over the keys it can hold', () => {
219
+ const inline = inliner(source)
220
+ expect(inline('labels[props.kind]')).toBe('props.kind === "ok" ? "Fine" : props.kind === "err" ? "Broken" : undefined')
221
+ expect(inline('labels[props.other]')).toBe('props.other === "ok" ? "Fine" : undefined')
222
+ expect(inline('numbered[props.kind]')).toBe('undefined')
223
+ })
224
+
225
+ test('an index whose keys are not all known reads the literal, for the adapter to refuse', () => {
226
+ const inline = inliner(source)
227
+ const literal = "({ ok: 'Fine', err: 'Broken' })"
228
+ expect(inline('labels[props.free]')).toBe(`${literal}[props.free]`)
229
+ expect(inline('labels[props.proto]')).toBe(`${literal}[props.proto]`)
230
+ expect(inline('labels[sel()]')).toBe(`${literal}[sel()]`)
231
+ expect(inline('labels[pick(props.kind)]')).toBe(`${literal}[pick(props.kind)]`)
232
+ expect(inline('rows.map(kind => labels[kind])')).toBe(`rows.map(kind => ${literal}[kind])`)
233
+ })
234
+
235
+ test('a whole read or an inherited key leaves the expression as written', () => {
236
+ const inline = inliner(source)
237
+ expect(inline('f(flags)')).toBe('f(flags)')
238
+ expect(inline('flags.label + flags.toString')).toBe('flags.label + flags.toString')
239
+ expect(inline('MODULE.inner')).toBe('MODULE.inner')
240
+ })
241
+
242
+ test('a mutated object, a shadowed name and a capturing callback param are left alone', () => {
243
+ expect(inliner(source)('mutated.a')).toBe('mutated.a')
244
+ expect(inliner(source, ['flags'])('flags.label')).toBe('flags.label')
245
+ expect(inliner(source)('rows.map(props => flags.label)')).toBe('rows.map(props => flags.label)')
246
+ expect(inliner(source)('rows.map(flags => flags.label)')).toBe('rows.map(flags => flags.label)')
247
+ })
248
+ })
249
+
250
+ describe('a body destructure of a const object is one const per binding (#3844)', () => {
251
+ const consts = (source: string) =>
252
+ Object.fromEntries(buildMetadata(analyzeComponent(source, 'test.tsx')).localConstants.filter(c => !c.isModule).map(c => [c.name, c.value]))
253
+
254
+ test('a local, a module and a rest source; renamed, nested and defaulted bindings', () => {
255
+ expect(consts(`
256
+ const MOD = { a: { b: 1 } }
257
+ export function C({ t, ...rest }: { t: string; tone: string }) {
258
+ const conf = { tone: 'info' }
259
+ const { tone: tn, missing = 'x' } = conf
260
+ const { a: { b } } = MOD
261
+ const { tone } = rest
262
+ return <p>{tn}{missing}{b}{tone}</p>
263
+ }
264
+ `)).toMatchObject({ tn: 'conf.tone', missing: 'conf.missing === undefined ? "x" : conf.missing', b: 'MOD.a.b', tone: 'rest.tone' })
265
+ })
266
+
267
+ test('a rest of a props-object body destructure reads the props object, a taken key its default', () => {
268
+ const metadata = buildMetadata(analyzeComponent(`
269
+ export function C(props: { label: string; id?: string }) {
270
+ const { label, ...rest } = props
271
+ const { id, label: gone = 'g' } = rest
272
+ return <p {...rest}>{label}{id}{gone}</p>
273
+ }
274
+ `, 'test.tsx'))
275
+ expect(metadata.restPropsName).toBe('rest')
276
+ expect(metadata.restPropsOmittedKeys).toEqual(['label'])
277
+ expect(metadata.restPropsExpandedKeys).toEqual(['id'])
278
+ const values = Object.fromEntries(metadata.localConstants.map(c => [c.name, c.value]))
279
+ expect(values).toMatchObject({ id: 'props.id', gone: '"g"' })
280
+ })
281
+
282
+ test('a source declared later, a let source, a rest element or a computed default is not collected', () => {
283
+ const names = (source: string) => buildMetadata(analyzeComponent(source, 'test.tsx')).localConstants.map(c => c.name)
284
+ expect(names(`export function C() { const { a } = later; const later = { a: 1 }; return <p>{a}</p> }`)).not.toContain('a')
285
+ expect(names(`export function C() { let conf = { a: 1 }; const { a } = conf; return <p>{a}</p> }`)).not.toContain('a')
286
+ expect(names(`export function C() { const conf = { a: 1, b: 2 }; const { a, ...more } = conf; return <p>{a}</p> }`)).not.toContain('a')
287
+ expect(names(`export function C({ d }: { d: number }) { const conf = { a: 1 }; const { a = d } = conf; return <p>{a}</p> }`)).not.toContain('a')
288
+ })
289
+ })
@@ -6,13 +6,14 @@
6
6
  */
7
7
 
8
8
  import { describe, test, expect } from 'bun:test'
9
- import { emitParsedExpr, parseExpression, type ParsedExpr, type ParsedExprEmitter } from '../index'
9
+ import { BindingScope, EMPTY_OPERAND_TYPING, emitParsedExpr, parseExpression, primitivesAt, type ParsedExpr, type ParsedExprEmitter } from '../index'
10
10
 
11
11
  // Spells every node as a JS-like string so the dispatched shape is visible.
12
12
  function makeEmitter(): ParsedExprEmitter {
13
13
  const callName = (callee: ParsedExpr, emit: (e: ParsedExpr) => string) =>
14
14
  callee.kind === 'identifier' ? callee.name : emit(callee)
15
15
  return {
16
+ primitives: primitivesAt(EMPTY_OPERAND_TYPING, BindingScope.EMPTY, {}),
16
17
  identifier: name => name,
17
18
  literal: value => JSON.stringify(value),
18
19
  call: (callee, args, emit) => `${callName(callee, emit)}(${args.map(emit).join(', ')})`,