@barefootjs/jsx 0.40.1 → 0.41.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 (211) hide show
  1. package/dist/adapters/boolean-result.d.ts +32 -10
  2. package/dist/adapters/boolean-result.d.ts.map +1 -1
  3. package/dist/adapters/brace-delimiter-text.d.ts +18 -0
  4. package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
  5. package/dist/adapters/callback-method.d.ts +45 -5
  6. package/dist/adapters/callback-method.d.ts.map +1 -1
  7. package/dist/adapters/component-props.d.ts +3 -4
  8. package/dist/adapters/component-props.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 +24 -12
  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 +59 -0
  16. package/dist/adapters/loop-row-type.d.ts.map +1 -0
  17. package/dist/adapters/nil-reading.d.ts +63 -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 +80 -12
  26. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  27. package/dist/adapters/primitive-plan.d.ts +320 -0
  28. package/dist/adapters/primitive-plan.d.ts.map +1 -0
  29. package/dist/adapters/prop-classes.d.ts +5 -0
  30. package/dist/adapters/prop-classes.d.ts.map +1 -1
  31. package/dist/analyzer-context.d.ts +4 -0
  32. package/dist/analyzer-context.d.ts.map +1 -1
  33. package/dist/analyzer.d.ts.map +1 -1
  34. package/dist/augment-inherited-props.d.ts +63 -8
  35. package/dist/augment-inherited-props.d.ts.map +1 -1
  36. package/dist/compiler.d.ts.map +1 -1
  37. package/dist/event-handler-name.d.ts +12 -0
  38. package/dist/event-handler-name.d.ts.map +1 -1
  39. package/dist/expression-parser.d.ts +8 -0
  40. package/dist/expression-parser.d.ts.map +1 -1
  41. package/dist/index.d.ts +20 -11
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +4903 -2311
  44. package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
  45. package/dist/ir-to-client-js/child-props-object.d.ts +106 -0
  46. package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
  47. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  48. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  49. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  50. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  51. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +4 -2
  52. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
  53. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  54. package/dist/ir-to-client-js/imports.d.ts +2 -2
  55. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  56. package/dist/ir-to-client-js/index.d.ts +2 -6
  57. package/dist/ir-to-client-js/index.d.ts.map +1 -1
  58. package/dist/ir-to-client-js/phases/props-extraction.d.ts +3 -3
  59. package/dist/ir-to-client-js/plan/build-declaration-emit.d.ts.map +1 -1
  60. package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
  61. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +30 -18
  62. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
  63. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  64. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +3 -1
  65. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
  66. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
  67. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
  68. package/dist/jsx-to-ir.d.ts +1 -0
  69. package/dist/jsx-to-ir.d.ts.map +1 -1
  70. package/dist/nullable-signal-attr.d.ts +51 -34
  71. package/dist/nullable-signal-attr.d.ts.map +1 -1
  72. package/dist/prop-rewrite.d.ts +7 -0
  73. package/dist/prop-rewrite.d.ts.map +1 -1
  74. package/dist/props-binding.d.ts +58 -13
  75. package/dist/props-binding.d.ts.map +1 -1
  76. package/dist/props-object-whole-read.d.ts +15 -0
  77. package/dist/props-object-whole-read.d.ts.map +1 -0
  78. package/dist/rich-type-refusal.d.ts.map +1 -1
  79. package/dist/scope/binding-scope.d.ts +85 -10
  80. package/dist/scope/binding-scope.d.ts.map +1 -1
  81. package/dist/scope/lexical-scope-walk.d.ts +3 -2
  82. package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
  83. package/dist/scope/root-prop-alias.d.ts +10 -7
  84. package/dist/scope/root-prop-alias.d.ts.map +1 -1
  85. package/dist/ssr-defaults.d.ts +39 -3
  86. package/dist/ssr-defaults.d.ts.map +1 -1
  87. package/dist/strip-types.d.ts +13 -0
  88. package/dist/strip-types.d.ts.map +1 -1
  89. package/dist/template-position-walk.d.ts +21 -3
  90. package/dist/template-position-walk.d.ts.map +1 -1
  91. package/dist/types.d.ts +90 -0
  92. package/dist/types.d.ts.map +1 -1
  93. package/package.json +2 -2
  94. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +9 -9
  95. package/src/__tests__/binding-scope.test.ts +89 -1
  96. package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
  97. package/src/__tests__/body-destructured-prop-default-arrow.test.ts +2 -2
  98. package/src/__tests__/body-destructured-props-live.test.ts +5 -5
  99. package/src/__tests__/boolean-result.test.ts +22 -5
  100. package/src/__tests__/brace-delimiter-text.test.ts +26 -0
  101. package/src/__tests__/callback-method.test.ts +62 -2
  102. package/src/__tests__/child-components-in-map.test.ts +1 -1
  103. package/src/__tests__/client-js-generation.test.ts +3 -3
  104. package/src/__tests__/computed-const.test.ts +103 -0
  105. package/src/__tests__/destructure-default-reads-earlier-param.test.ts +7 -7
  106. package/src/__tests__/destructured-props-live.test.ts +1 -1
  107. package/src/__tests__/emit-to-string-call.test.ts +2 -1
  108. package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
  109. package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
  110. package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
  111. package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
  112. package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
  113. package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
  114. package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +2 -1
  115. package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
  116. package/src/__tests__/issue-3861-object-spread-child.test.ts +102 -0
  117. package/src/__tests__/lookup-literal-const.test.ts +9 -3
  118. package/src/__tests__/loop-index-reactivity.test.ts +2 -2
  119. package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
  120. package/src/__tests__/loop-plan.test.ts +1 -1
  121. package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
  122. package/src/__tests__/loop-row-type.test.ts +64 -0
  123. package/src/__tests__/lowering-value-position.test.ts +4 -0
  124. package/src/__tests__/memo-body-expr.test.ts +2 -2
  125. package/src/__tests__/module-const-destructure-default.test.ts +156 -0
  126. package/src/__tests__/nested-loop-index-param.test.ts +1 -1
  127. package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
  128. package/src/__tests__/nested-row-shadowing.test.ts +2 -2
  129. package/src/__tests__/nil-reading.test.ts +78 -0
  130. package/src/__tests__/nullable-signal-attr.test.ts +109 -48
  131. package/src/__tests__/primitive-plan.test.ts +494 -0
  132. package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
  133. package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
  134. package/src/__tests__/prop-default-undefined-only.test.ts +115 -0
  135. package/src/__tests__/prop-references.test.ts +2 -2
  136. package/src/__tests__/props-destructuring.test.ts +17 -0
  137. package/src/__tests__/props-object-whole-read.test.ts +40 -0
  138. package/src/__tests__/prune-unused-prop-extractions.test.ts +3 -3
  139. package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
  140. package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
  141. package/src/__tests__/renamed-body-destructure-template.test.ts +7 -7
  142. package/src/__tests__/rest-binding-gaps.test.ts +192 -0
  143. package/src/__tests__/rewrite-destructured-props.test.ts +1 -1
  144. package/src/__tests__/root-prop-alias.test.ts +53 -8
  145. package/src/__tests__/ssr-defaults.test.ts +113 -25
  146. package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
  147. package/src/__tests__/static-record-literal-array.test.ts +171 -0
  148. package/src/__tests__/string-concat-identifier.test.ts +40 -1
  149. package/src/__tests__/written-binding-substitution.test.ts +8 -8
  150. package/src/adapters/boolean-result.ts +72 -66
  151. package/src/adapters/brace-delimiter-text.ts +25 -0
  152. package/src/adapters/callback-method.ts +159 -27
  153. package/src/adapters/component-props.ts +3 -4
  154. package/src/adapters/computed-const.ts +238 -4
  155. package/src/adapters/dsl-template-adapter.ts +62 -26
  156. package/src/adapters/loop-bound-names.ts +9 -1
  157. package/src/adapters/loop-plan.ts +1 -1
  158. package/src/adapters/loop-row-type.ts +149 -0
  159. package/src/adapters/nil-reading.ts +295 -0
  160. package/src/adapters/non-primitive-equality.ts +92 -0
  161. package/src/adapters/number-value.ts +17 -4
  162. package/src/adapters/operand-typing.ts +522 -0
  163. package/src/adapters/parsed-expr-emitter.ts +183 -19
  164. package/src/adapters/primitive-plan.ts +765 -0
  165. package/src/adapters/prop-classes.ts +16 -3
  166. package/src/analyzer-context.ts +6 -0
  167. package/src/analyzer.ts +532 -26
  168. package/src/augment-inherited-props.ts +108 -10
  169. package/src/compiler.ts +17 -0
  170. package/src/event-handler-name.ts +15 -0
  171. package/src/expression-parser.ts +70 -1
  172. package/src/index.ts +35 -16
  173. package/src/ir-to-client-js/build-references.ts +69 -0
  174. package/src/ir-to-client-js/child-props-object.ts +240 -0
  175. package/src/ir-to-client-js/collect-elements.ts +31 -63
  176. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +8 -1
  177. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +31 -28
  178. package/src/ir-to-client-js/control-flow/shared.ts +20 -22
  179. package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +5 -3
  180. package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +2 -2
  181. package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +1 -1
  182. package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +2 -2
  183. package/src/ir-to-client-js/control-flow/stringify/loop.ts +1 -1
  184. package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +3 -3
  185. package/src/ir-to-client-js/html-template.ts +43 -17
  186. package/src/ir-to-client-js/imports.ts +2 -2
  187. package/src/ir-to-client-js/index.ts +53 -11
  188. package/src/ir-to-client-js/phases/props-extraction.ts +11 -7
  189. package/src/ir-to-client-js/plan/build-declaration-emit.ts +40 -7
  190. package/src/ir-to-client-js/plan/build-static-array-child-init.ts +31 -14
  191. package/src/ir-to-client-js/plan/static-array-child-init.ts +27 -18
  192. package/src/ir-to-client-js/prop-handling.ts +22 -3
  193. package/src/ir-to-client-js/prune-unused-prop-extractions.ts +58 -5
  194. package/src/ir-to-client-js/rewrite-destructured-props.ts +31 -1
  195. package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
  196. package/src/jsx-to-ir.ts +459 -37
  197. package/src/nullable-signal-attr.ts +200 -110
  198. package/src/prop-rewrite.ts +30 -6
  199. package/src/props-binding.ts +84 -19
  200. package/src/props-object-whole-read.ts +87 -0
  201. package/src/rich-type-refusal.ts +2 -1
  202. package/src/scope/binding-scope.ts +182 -18
  203. package/src/scope/lexical-scope-walk.ts +4 -3
  204. package/src/scope/root-prop-alias.ts +15 -8
  205. package/src/ssr-defaults.ts +100 -11
  206. package/src/strip-types.ts +31 -3
  207. package/src/template-position-walk.ts +57 -28
  208. package/src/types.ts +87 -0
  209. package/dist/adapters/ambiguous-nil-arithmetic.d.ts +0 -41
  210. package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +0 -1
  211. package/src/adapters/ambiguous-nil-arithmetic.ts +0 -94
@@ -210,6 +210,23 @@ describe('Destructured props keep their declared types (#2150)', () => {
210
210
  expect(typeOf(ctx, 'configs')).toEqual({ kind: 'unknown', raw: 'unknown' })
211
211
  })
212
212
 
213
+ test('resolves a structural member whose leaf is a nil union of a resolvable type; a top-level union still declines', () => {
214
+ const source = `
215
+ type Init = { label?: string | null; n: number | undefined }
216
+ type Props = { initial: Init; rows: { tag: string | null }[]; top: string | null }
217
+
218
+ export function Component({ initial, rows, top }: Props) {
219
+ return <div>{initial.label}{rows.length}{top}</div>
220
+ }
221
+ `
222
+
223
+ const ctx = analyzeComponent(source, 'Component.tsx')
224
+
225
+ expect(typeOf(ctx, 'initial')?.kind).toBe('interface')
226
+ expect(typeOf(ctx, 'rows')?.kind).toBe('array')
227
+ expect(typeOf(ctx, 'top')).toEqual({ kind: 'unknown', raw: 'unknown' })
228
+ })
229
+
213
230
  // A same-file object type referenced BY NAME resolves like its inline
214
231
  // spelling — the `props`-object form (`props: { initial: State }`) always
215
232
  // kept it; the destructured form declined it to `unknown`.
@@ -0,0 +1,40 @@
1
+ import { describe, expect, test } from 'bun:test'
2
+ import ts from 'typescript'
3
+ import { observesWholePropsObject } from '../props-object-whole-read'
4
+ import { analyzeComponent } from '../analyzer'
5
+ import { buildMetadata } from '../compiler'
6
+
7
+ const observes = (body: string): boolean => {
8
+ const sf = ts.createSourceFile('c.tsx', `function C(props: P) {\n${body}\n}`, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX)
9
+ const fn = sf.statements[0] as ts.FunctionDeclaration
10
+ return observesWholePropsObject(fn, 'props')
11
+ }
12
+
13
+ describe('observesWholePropsObject', () => {
14
+ test('member reads with a known key are not whole reads', () => {
15
+ expect(observes('return <b title={props.title} data-x={props["x"]} data-y={props?.y}>{props[0]}</b>')).toBe(false)
16
+ expect(observes('const { a, b: c } = props; return <b>{a}{c}</b>')).toBe(false)
17
+ expect(observes('const t: typeof props = null as any; return <b />')).toBe(false)
18
+ })
19
+
20
+ test('a spread, a dynamic key, a whole pass and a rest destructure are whole reads', () => {
21
+ expect(observes('return <b {...props} />')).toBe(true)
22
+ expect(observes('return <Inner {...props} />')).toBe(true)
23
+ expect(observes('const k = "a"; return <b>{props[k]}</b>')).toBe(true)
24
+ expect(observes('return <b>{Object.keys(props).join(",")}</b>')).toBe(true)
25
+ expect(observes('const { a, ...rest } = props; return <b {...rest}>{a}</b>')).toBe(true)
26
+ expect(observes('const o = { props }; return <b>{String(o)}</b>')).toBe(true)
27
+ })
28
+
29
+ test('a nested binding of the same name shadows the props object', () => {
30
+ expect(observes('const f = (props: any) => ({ ...props }); return <b>{String(f)}</b>')).toBe(false)
31
+ expect(observes('const xs = [1].map(n => { const props = { n }; return { ...props } }); return <b>{xs.length}</b>')).toBe(false)
32
+ expect(observes('const f = (other: any) => ({ ...props }); return <b>{String(f)}</b>')).toBe(true)
33
+ })
34
+
35
+ test('IR metadata carries the answer for a props-object component', () => {
36
+ const meta = (src: string) => buildMetadata(analyzeComponent(src, 'c.tsx'))
37
+ expect(meta('export function Kid(props: { label: string }) { return <b {...props}>{props.label}</b> }').propsObjectObservedWhole).toBe(true)
38
+ expect(meta('export function Kid(props: { label: string }) { return <b>{props.label}</b> }').propsObjectObservedWhole).toBe(false)
39
+ })
40
+ })
@@ -77,8 +77,8 @@ export { Widget }
77
77
  )
78
78
  expect(js).not.toContain('const config = ')
79
79
  expect(js).not.toContain('const items = ')
80
- expect(js).toContain('(_p.config ?? {}).startOpen')
81
- expect(js).toContain('(_p.items ?? []).length')
82
- expect(js).toContain('(_p.items ?? [])[0]')
80
+ expect(js).toContain('(_p.config === undefined ? {} : _p.config).startOpen')
81
+ expect(js).toContain('(_p.items === undefined ? [] : _p.items).length')
82
+ expect(js).toContain('(_p.items === undefined ? [] : _p.items)[0]')
83
83
  })
84
84
  })
@@ -232,7 +232,7 @@ describe('reactive attributes inside .map() callbacks', () => {
232
232
  // claimed 'text' slot writer now (slot unification A3), not a bare
233
233
  // `.textContent =` assignment.
234
234
  expect(js).toContain("setAttribute('class'")
235
- expect(js).toMatch(/__bfw_\w+\('s\d+', String\(item\.name\)\)/)
235
+ expect(js).toMatch(/__bfw_\w+\('s\d+', textOrNode\(item\.name\)\)/)
236
236
  // But there must be exactly one `items.forEach` call (not two).
237
237
  const forEachMatches = js.match(/items\.forEach\(/g) ?? []
238
238
  expect(forEachMatches.length).toBe(1)
@@ -203,9 +203,12 @@ describe('Reactive factory inlining (#931)', () => {
203
203
  `
204
204
 
205
205
  const result = compileJSX(source, 'Comp.tsx', { adapter })
206
- expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
207
206
  const bf110 = result.errors.find(e => e.code === 'BF110')
208
207
  expect(bf110).toBeUndefined()
208
+ // Not a factory call, so the destructure is an ordinary body destructure
209
+ // of a call, which no backend binds: refused with BF101 (#3854), where
210
+ // it used to compile clean with `x` left unbound.
211
+ expect(result.errors.filter(e => e.severity === 'error').map(e => e.code)).toEqual(['BF101'])
209
212
  })
210
213
  })
211
214
 
@@ -4,7 +4,7 @@
4
4
  * like the parameter form (#3579). A binding that no longer holds the prop
5
5
  * (a `let` reassigned before the return, a folded reassignment, a value
6
6
  * mutated in place) stays a local: the template leaves its slot empty and
7
- * defers a child that receives it, instead of reading `_p.b ?? d`.
7
+ * defers a child that receives it, instead of reading `_p.b` with its default.
8
8
  *
9
9
  * The reassigned shapes have no conformance fixture: the reference adapter
10
10
  * itself drops the reassignment of a destructured binding, so these pin the
@@ -42,7 +42,7 @@ function templateOf(body: string, jsx: string, propsType = '{ b?: string[] | nul
42
42
  describe('renamed body destructure binding in the client template (#3579)', () => {
43
43
  test('a default holding a callback reads the prop with its default', () => {
44
44
  const t = templateOf("const { b: local = ['y', ''].filter((x) => x !== '') } = props", '<p>{local.length}</p>')
45
- expect(t).toContain("_p.b ?? (['y', ''].filter((x) => x !== ''))")
45
+ expect(t).toContain("_p.b === undefined ? (['y', ''].filter((x) => x !== '')) : _p.b")
46
46
  expect(t).not.toContain("${''}")
47
47
  })
48
48
 
@@ -55,23 +55,23 @@ describe('renamed body destructure binding in the client template (#3579)', () =
55
55
  test('a let reassigned before the return keeps the child deferred', () => {
56
56
  const t = templateOf("let { b: local = null } = props\n local = ['changed']", '<p><Child xs={local} /></p>')
57
57
  expect(t).toContain('data-bf-ph')
58
- expect(t).not.toContain('_p.b ?? null')
58
+ expect(t).not.toContain('_p.b === undefined ? ')
59
59
  })
60
60
 
61
61
  test('a let reassigned before the return is not read as the prop in text or an attribute', () => {
62
62
  const t = templateOf("let { b: local = 'x' } = props\n local = local + '!'", '<p data-v={local}>{local}</p>', '{ b?: string }')
63
- expect(t).not.toContain("_p.b ?? 'x'")
63
+ expect(t).not.toContain('_p.b === undefined ? ')
64
64
  })
65
65
 
66
66
  test('a folded conditional reassignment is not read as the prop', () => {
67
67
  const t = templateOf("let { b: local = 'x' } = props\n if (!local) local = 'y'", '<p>{local}</p>', '{ b?: string }')
68
- expect(t).not.toContain("_p.b ?? 'x'")
68
+ expect(t).not.toContain('_p.b === undefined ? ')
69
69
  })
70
70
 
71
71
  test('an un-renamed let reassigned before the return keeps the child deferred too', () => {
72
72
  const t = templateOf("let { b = null } = props\n b = ['changed']", '<p><Child xs={b} /></p>')
73
73
  expect(t).toContain('data-bf-ph')
74
- expect(t).not.toContain('_p.b ?? null')
74
+ expect(t).not.toContain('_p.b === undefined ? ')
75
75
  })
76
76
 
77
77
  test('an un-renamed binding with a callback default renders the child in place', () => {
@@ -81,6 +81,6 @@ describe('renamed body destructure binding in the client template (#3579)', () =
81
81
 
82
82
  test('a value mutated in place is not read as the prop', () => {
83
83
  const t = templateOf("const { b: local = ['x'] } = props\n local.push('y')", '<p>{local.length}</p>', '{ b?: string[] }')
84
- expect(t).not.toContain("_p.b ?? ['x']")
84
+ expect(t).not.toContain('_p.b === undefined ? ')
85
85
  })
86
86
  })
@@ -0,0 +1,192 @@
1
+ /**
2
+ * Rest-binding and body-destructure shapes that used to compile without a
3
+ * diagnostic and render wrong (#3851, #3854). Each is either decided in
4
+ * Phase 1 so every backend renders it as JS does, or refused with a
5
+ * diagnostic. The rendered output is pinned by the conformance fixtures
6
+ * (`rest-spread-explicit-before-closed-rest`,
7
+ * `rest-binding-shadowed-by-loop-row`, `body-rest-member-read`, and the
8
+ * refused `body-destructure-*` / `body-rest-taken-key-read`); these tests
9
+ * pin the Phase 1 decisions and the refusals of the shapes no fixture
10
+ * renders.
11
+ */
12
+
13
+ import { describe, test, expect } from 'bun:test'
14
+ import { compileJSX } from '../compiler'
15
+ import { TestAdapter } from '../adapters/test-adapter'
16
+ import type { ComponentIR, IRNode } from '../types'
17
+
18
+ function compile(source: string) {
19
+ return compileJSX(source, 'Component.tsx', { adapter: new TestAdapter(), outputIR: true })
20
+ }
21
+
22
+ function codes(source: string): string[] {
23
+ return compile(source).errors.filter(e => e.severity === 'error').map(e => e.code)
24
+ }
25
+
26
+ function irOf(source: string): ComponentIR {
27
+ const file = compile(source).files.find(f => f.type === 'ir')
28
+ if (!file) throw new Error('no IR')
29
+ return JSON.parse(file.content) as ComponentIR
30
+ }
31
+
32
+ /** Every element of `node`'s tree with tag `tag`. */
33
+ function elements(node: IRNode, tag: string): Array<Extract<IRNode, { type: 'element' }>> {
34
+ const out: Array<Extract<IRNode, { type: 'element' }>> = []
35
+ const visit = (n: unknown): void => {
36
+ if (Array.isArray(n)) return n.forEach(visit)
37
+ if (n === null || typeof n !== 'object') return
38
+ const rec = n as { type?: string; tag?: string }
39
+ if (rec.type === 'element' && rec.tag === tag) out.push(n as Extract<IRNode, { type: 'element' }>)
40
+ for (const v of Object.values(n)) visit(v)
41
+ }
42
+ visit(node)
43
+ return out
44
+ }
45
+
46
+ function attrExpr(el: Extract<IRNode, { type: 'element' }>, name: string): string[] {
47
+ return el.attrs.filter(a => a.name === name).map(a => (a.value.kind === 'expression' ? a.value.expr : a.value.kind))
48
+ }
49
+
50
+ describe('an attribute before a closed rest spread (#3851)', () => {
51
+ test('the spread key falls back to the earlier value, which is dropped', () => {
52
+ const ir = irOf(`
53
+ export function P({ label, ...rest }: { label: string; title?: string }) {
54
+ return <p title="fixed" {...rest}>{label}</p>
55
+ }
56
+ `)
57
+ const [p] = elements(ir.root, 'p')
58
+ expect(attrExpr(p, 'title')).toEqual(['rest.title ?? ("fixed")'])
59
+ expect(p.attrs.find(a => a.name === 'title')?.restFallback).toBe('"fixed"')
60
+ })
61
+
62
+ test('a later attribute still wins over the spread, with no fallback', () => {
63
+ const ir = irOf(`
64
+ export function P({ label, ...rest }: { label: string; title?: string }) {
65
+ return <p {...rest} title="fixed">{label}</p>
66
+ }
67
+ `)
68
+ const [p] = elements(ir.root, 'p')
69
+ expect(attrExpr(p, 'title')).toEqual(['rest.title', 'literal'])
70
+ })
71
+
72
+ test('a member read before the spread is a fallback', () => {
73
+ expect(codes(`
74
+ export function P({ label, row, ...rest }: { label: string; row: { t: string }; title?: string }) {
75
+ return <p title={row.t} {...rest}>{label}</p>
76
+ }
77
+ `)).toEqual([])
78
+ })
79
+
80
+ test('a value that may have an effect is refused (BF021)', () => {
81
+ expect(codes(`
82
+ function mark(): string { return 'm' }
83
+ export function P({ label, ...rest }: { label: string; title?: string }) {
84
+ return <p title={mark()} {...rest}>{label}</p>
85
+ }
86
+ `)).toEqual(['BF021'])
87
+ })
88
+
89
+ test('a JSX element value is refused (BF021)', () => {
90
+ expect(codes(`
91
+ import { Card } from './card'
92
+ export function P({ ...rest }: { header?: unknown }) {
93
+ return <Card header={<b>h</b>} {...rest} />
94
+ }
95
+ `)).toEqual(['BF021'])
96
+ })
97
+ })
98
+
99
+ describe('a loop row named like the rest binding (#3851)', () => {
100
+ test('a spread of the row stays a spread of the row', () => {
101
+ const ir = irOf(`
102
+ export function P({ rows, ...rest }: { rows: { id: string }[]; tone?: string }) {
103
+ return <ul data-tone={rest.tone}>{rows.map(rest => <li key={rest.id} {...rest}>{rest.id}</li>)}</ul>
104
+ }
105
+ `)
106
+ const [li] = elements(ir.root, 'li')
107
+ const spread = li.attrs.find(a => a.name === '...')
108
+ expect(spread?.value.kind).toBe('spread')
109
+ expect(spread?.value.kind === 'spread' && spread.value.callerBag).toBeFalsy()
110
+ expect(li.attrs.some(a => a.name === 'tone')).toBe(false)
111
+ })
112
+ })
113
+
114
+ describe('a member read of a props-object body rest (#3854)', () => {
115
+ test('reads the props object', () => {
116
+ const ir = irOf(`
117
+ export function C(props: { label: string; id?: string }) {
118
+ const { label, ...rest } = props
119
+ return <i data-x={rest.id}>{label}</i>
120
+ }
121
+ `)
122
+ const [i] = elements(ir.root, 'i')
123
+ expect(attrExpr(i, 'data-x')).toEqual(['props.id'])
124
+ })
125
+
126
+ test('a row shadowing the rest keeps its own read', () => {
127
+ const ir = irOf(`
128
+ export function C(props: { label: string; id?: string; rows: { id: string }[] }) {
129
+ const { label, ...rest } = props
130
+ return <ul>{props.rows.map(rest => <li key={rest.id} data-x={rest.id}>{label}</li>)}</ul>
131
+ }
132
+ `)
133
+ const [li] = elements(ir.root, 'li')
134
+ expect(attrExpr(li, 'data-x')).toEqual(['rest.id'])
135
+ })
136
+
137
+ test('a key the props type does not declare stays a read of the rest', () => {
138
+ const ir = irOf(`
139
+ export function C(props: { label: string; [key: string]: unknown }) {
140
+ const { label, ...rest } = props
141
+ return <i data-x={rest.other}>{label}</i>
142
+ }
143
+ `)
144
+ const [i] = elements(ir.root, 'i')
145
+ expect(attrExpr(i, 'data-x')).toEqual(['rest.other'])
146
+ })
147
+
148
+ test('a read or spread under a row that shadows the props name is refused (BF101)', () => {
149
+ const src = (use: string) => `
150
+ export function C(props: { id?: string; rows: { key: string }[] }) {
151
+ const { rows, ...rest } = props
152
+ return <ul>{rows.map(props => <li key={props.key} ${use} />)}</ul>
153
+ }
154
+ `
155
+ expect(codes(src('data-x={rest.id}'))).toEqual(['BF101'])
156
+ expect(codes(src('{...rest}'))).toEqual(['BF101'])
157
+ })
158
+
159
+ test('a read of a taken key is refused (BF101)', () => {
160
+ expect(codes(`
161
+ export function C(props: { label: string; id?: string }) {
162
+ const { label, ...rest } = props
163
+ return <i>{label}{rest.label}</i>
164
+ }
165
+ `)).toEqual(['BF101'])
166
+ })
167
+ })
168
+
169
+ describe('a component-body destructure with no const form (#3854)', () => {
170
+ const wrap = (stmt: string, use: string) => `
171
+ const conf = { a: 'A', b: 'B' }
172
+ function getConf() { return conf }
173
+ export function C(props: { b: string; nested: { a: string } }) {
174
+ ${stmt}
175
+ return <i>{${use}}</i>
176
+ }
177
+ `
178
+
179
+ test.each([
180
+ ['a rest element', 'const { a, ...more } = conf', 'more.b'],
181
+ ['a non-literal default', 'const { a = props.b } = conf', 'a'],
182
+ ['a call source', 'const { a } = getConf()', 'a'],
183
+ ['a member source', 'const { a } = props.nested', 'a'],
184
+ ['a let destructure', 'let { a } = conf', 'a'],
185
+ ])('%s is refused (BF101)', (_name, stmt, use) => {
186
+ expect(codes(wrap(stmt, use))).toEqual(['BF101'])
187
+ })
188
+
189
+ test('a const destructure of a const object still compiles', () => {
190
+ expect(codes(wrap(`const { a, b: bee = 'x' } = conf`, 'a + bee'))).toEqual([])
191
+ })
192
+ })
@@ -107,7 +107,7 @@ function Comp({ base, tag = 'all', page }: { base: string; tag?: string; page: s
107
107
  export { Comp }
108
108
  `
109
109
  const js = compileClientJs(source)
110
- expect(js).toContain("{ tag: (_p.tag ?? 'all'), page: _p.page ? _p.page : '' }")
110
+ expect(js).toContain("{ tag: (_p.tag === undefined ? 'all' : _p.tag), page: _p.page ? _p.page : '' }")
111
111
  expect(js).not.toContain("(_p.tag ?? 'all'):")
112
112
  })
113
113
 
@@ -67,19 +67,19 @@ describe('the props object a binding named props shadows (#3693)', () => {
67
67
  const isBoundIn = (scope: BindingScope) => (name: string) => scope.isBound(name)
68
68
 
69
69
  test('props is the component props object until a binding of that name shadows it', () => {
70
- expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY))).toBe(true)
71
- expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY.enterLoopRow({ param: 'row' })))).toBe(true)
72
- expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY.enterLoopRow({ param: 'props' })))).toBe(false)
70
+ expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY), 'props')).toBe(true)
71
+ expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY.enterLoopRow({ param: 'row' })), 'props')).toBe(true)
72
+ expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY.enterLoopRow({ param: 'props' })), 'props')).toBe(false)
73
73
  const destructured = BindingScope.EMPTY.enterLoopRow({ param: '{ props }', paramBindings: [{ name: 'props' }] })
74
- expect(isRootPropsObject(props, isBoundIn(destructured))).toBe(false)
74
+ expect(isRootPropsObject(props, isBoundIn(destructured), 'props')).toBe(false)
75
75
  const preamble = BindingScope.EMPTY.enterLoopRow({ param: 'row', preamble: { declaredNames: ['props'] } })
76
- expect(isRootPropsObject(props, isBoundIn(preamble))).toBe(false)
77
- expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY.enterCallback(['props'])))).toBe(false)
76
+ expect(isRootPropsObject(props, isBoundIn(preamble), 'props')).toBe(false)
77
+ expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY.enterCallback(['props'])), 'props')).toBe(false)
78
78
  })
79
79
 
80
80
  test('only the bare identifier props reads the props object', () => {
81
- expect(isRootPropsObject({ kind: 'identifier', name: 'row' }, () => false)).toBe(false)
82
- expect(isRootPropsObject({ kind: 'member', object: props, property: 'a', computed: false, optional: false }, () => false)).toBe(false)
81
+ expect(isRootPropsObject({ kind: 'identifier', name: 'row' }, () => false, 'props')).toBe(false)
82
+ expect(isRootPropsObject({ kind: 'member', object: props, property: 'a', computed: false, optional: false }, () => false, 'props')).toBe(false)
83
83
  })
84
84
 
85
85
  test('a props.<name> read has no component typing while props is shadowed', () => {
@@ -93,3 +93,48 @@ describe('the props object a binding named props shadows (#3693)', () => {
93
93
  expect(row.isStringValueName('label', () => true)).toBe(true)
94
94
  })
95
95
  })
96
+
97
+ describe('a props object parameter not named props (#3729)', () => {
98
+ const p: ParsedExpr = { kind: 'identifier', name: 'p' }
99
+ const props: ParsedExpr = { kind: 'identifier', name: 'props' }
100
+ const component = BindingScope.forComponent('p')
101
+ const isBoundIn = (scope: BindingScope) => (name: string) => scope.isBound(name)
102
+
103
+ test('the props parameter name is the props object; props is not', () => {
104
+ expect(component.propsObject).toBe('p')
105
+ expect(isRootPropsObject(p, isBoundIn(component), component.propsObject)).toBe(true)
106
+ expect(isRootPropsObject(props, isBoundIn(component), component.propsObject)).toBe(false)
107
+ })
108
+
109
+ test('a destructured or props-named component keeps props', () => {
110
+ expect(BindingScope.forComponent(null).propsObject).toBe('props')
111
+ expect(BindingScope.forComponent('props').propsObject).toBe('props')
112
+ })
113
+
114
+ test('child scopes keep the name, and a binding of it shadows the props object', () => {
115
+ const row = component.enterLoopRow({ param: 'p' })
116
+ expect(row.propsObject).toBe('p')
117
+ expect(isRootPropsObject(p, isBoundIn(row), row.propsObject)).toBe(false)
118
+ expect(row.shadowsPropsObject()).toBe(true)
119
+ expect(component.enterLoopRow({ param: 'props' }).shadowsPropsObject()).toBe(false)
120
+ expect(component.enterCallback(['x']).propsObject).toBe('p')
121
+ // The typing callers flag only the literal receiver `props.<name>`: in a
122
+ // component whose props object is `p`, an unbound `props` is not it.
123
+ expect(component.isStringValueName('label', () => true, true)).toBe(false)
124
+ expect(row.isStringValueName('label', () => true, true)).toBe(false)
125
+ })
126
+
127
+ test('a row binding named props keeps its own member types when the props object is named p', () => {
128
+ const fields = {
129
+ Row: [
130
+ { name: 'value', type: { primitive: 'number' }, optional: false },
131
+ { name: 'label', type: { primitive: 'string' }, optional: false },
132
+ ],
133
+ }
134
+ const named = (name: string) => fields[name as keyof typeof fields]
135
+ const row = component.enterLoopRow({ param: 'props', rowType: { raw: 'Row' } })
136
+ // `outer` would type every component name as a string; the row binding wins.
137
+ expect(row.isStringValueName('value', () => true, true, [], named)).toBe(false)
138
+ expect(row.isStringValueName('label', () => false, true, [], named)).toBe(true)
139
+ })
140
+ })