@barefootjs/jsx 0.37.0 → 0.38.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 (128) hide show
  1. package/dist/adapters/env-signal.d.ts +0 -8
  2. package/dist/adapters/env-signal.d.ts.map +1 -1
  3. package/dist/adapters/interface.d.ts +12 -0
  4. package/dist/adapters/interface.d.ts.map +1 -1
  5. package/dist/analyzer.d.ts +40 -0
  6. package/dist/analyzer.d.ts.map +1 -1
  7. package/dist/builtin-lowering-plugins.d.ts.map +1 -1
  8. package/dist/compiler.d.ts.map +1 -1
  9. package/dist/component-root-scope-comment.d.ts +36 -0
  10. package/dist/component-root-scope-comment.d.ts.map +1 -0
  11. package/dist/errors.d.ts +3 -0
  12. package/dist/errors.d.ts.map +1 -1
  13. package/dist/event-handler-name.d.ts +13 -0
  14. package/dist/event-handler-name.d.ts.map +1 -0
  15. package/dist/expression-parser.d.ts +9 -0
  16. package/dist/expression-parser.d.ts.map +1 -1
  17. package/dist/format-date-refusal.d.ts +37 -0
  18. package/dist/format-date-refusal.d.ts.map +1 -0
  19. package/dist/index.d.ts +5 -3
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +2973 -2310
  22. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  23. package/dist/ir-to-client-js/control-flow/plan/build-branch-loop.d.ts.map +1 -1
  24. package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
  25. package/dist/ir-to-client-js/control-flow/plan/build-composite-loop.d.ts.map +1 -1
  26. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts +13 -2
  27. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  28. package/dist/ir-to-client-js/control-flow/plan/build-loop.d.ts.map +1 -1
  29. package/dist/ir-to-client-js/control-flow/plan/build-reactive-effects.d.ts.map +1 -1
  30. package/dist/ir-to-client-js/control-flow/plan/lazy-conditional.d.ts.map +1 -1
  31. package/dist/ir-to-client-js/control-flow/plan/lazy-row-eligibility.d.ts +1 -1
  32. package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts +9 -0
  33. package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts.map +1 -1
  34. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +57 -12
  35. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
  36. package/dist/ir-to-client-js/control-flow/shared.d.ts +44 -3
  37. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  38. package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts +7 -5
  39. package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
  40. package/dist/ir-to-client-js/control-flow/stringify/loop-child-arm.d.ts +9 -1
  41. package/dist/ir-to-client-js/control-flow/stringify/loop-child-arm.d.ts.map +1 -1
  42. package/dist/ir-to-client-js/emit-reactive.d.ts +43 -2
  43. package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
  44. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  45. package/dist/ir-to-client-js/reactivity.d.ts +16 -1
  46. package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
  47. package/dist/ir-to-client-js/source-map.d.ts.map +1 -1
  48. package/dist/ir-to-client-js/types.d.ts +2 -0
  49. package/dist/ir-to-client-js/types.d.ts.map +1 -1
  50. package/dist/ir-to-client-js/utils.d.ts +26 -0
  51. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  52. package/dist/jsx-to-ir.d.ts.map +1 -1
  53. package/dist/opaque-local-accessor.d.ts +27 -0
  54. package/dist/opaque-local-accessor.d.ts.map +1 -0
  55. package/dist/to-locale-date-lowering.d.ts +7 -4
  56. package/dist/to-locale-date-lowering.d.ts.map +1 -1
  57. package/dist/types.d.ts +53 -17
  58. package/dist/types.d.ts.map +1 -1
  59. package/package.json +2 -2
  60. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +724 -4564
  61. package/src/__tests__/binding-scope-ratchet.test.ts +21 -10
  62. package/src/__tests__/branch-local-in-raw-capture.test.ts +1 -1
  63. package/src/__tests__/branch-local-same-name-collision.test.ts +9 -9
  64. package/src/__tests__/client-js-generation.test.ts +175 -0
  65. package/src/__tests__/component-root-scope-comment.test.ts +106 -0
  66. package/src/__tests__/doc-examples.test.ts +12 -22
  67. package/src/__tests__/event-handler-name.test.ts +74 -0
  68. package/src/__tests__/flatmap-segments.test.ts +11 -9
  69. package/src/__tests__/format-date-refusal.test.ts +134 -0
  70. package/src/__tests__/fragment-wrapped-conditional-return-branch.test.ts +132 -0
  71. package/src/__tests__/hydrate-template.test.ts +9 -1
  72. package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +2 -2
  73. package/src/__tests__/issue-3009-loop-branch-ref.test.ts +80 -0
  74. package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +213 -0
  75. package/src/__tests__/issue-3113-init-fn-name-collision.test.ts +133 -0
  76. package/src/__tests__/jsx-return-type-assertion.test.ts +2 -2
  77. package/src/__tests__/lazy-row-eligibility.test.ts +5 -4
  78. package/src/__tests__/loop-row-child-children-reactive-attrs.test.ts +108 -0
  79. package/src/__tests__/loop-row-child-text-children-markers.test.ts +158 -0
  80. package/src/__tests__/opaque-local-accessor.test.ts +90 -0
  81. package/src/__tests__/profiler-e2e.test.ts +13 -8
  82. package/src/__tests__/ref-attr-absent-at-ssr.test.ts +472 -0
  83. package/src/__tests__/ssr-portal-ref-callback.test.ts +217 -0
  84. package/src/adapters/env-signal.ts +2 -21
  85. package/src/adapters/interface.ts +13 -0
  86. package/src/analyzer.ts +40 -28
  87. package/src/builtin-lowering-plugins.ts +0 -2
  88. package/src/compiler.ts +24 -0
  89. package/src/component-root-scope-comment.ts +59 -0
  90. package/src/errors.ts +44 -1
  91. package/src/event-handler-name.ts +14 -0
  92. package/src/expression-parser.ts +41 -17
  93. package/src/format-date-refusal.ts +266 -0
  94. package/src/index.ts +13 -3
  95. package/src/ir-to-client-js/child-components.ts +8 -3
  96. package/src/ir-to-client-js/collect-elements.ts +29 -13
  97. package/src/ir-to-client-js/control-flow/plan/build-branch-loop.ts +2 -4
  98. package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +67 -5
  99. package/src/ir-to-client-js/control-flow/plan/build-composite-loop.ts +3 -6
  100. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +31 -1
  101. package/src/ir-to-client-js/control-flow/plan/build-loop.ts +23 -11
  102. package/src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts +5 -0
  103. package/src/ir-to-client-js/control-flow/plan/lazy-conditional.ts +1 -0
  104. package/src/ir-to-client-js/control-flow/plan/lazy-row-eligibility.ts +3 -3
  105. package/src/ir-to-client-js/control-flow/plan/loop-child-arm.ts +11 -0
  106. package/src/ir-to-client-js/control-flow/plan/loop.ts +51 -10
  107. package/src/ir-to-client-js/control-flow/shared.ts +78 -5
  108. package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +25 -12
  109. package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +22 -1
  110. package/src/ir-to-client-js/emit-reactive.ts +140 -10
  111. package/src/ir-to-client-js/emit-registration.ts +2 -2
  112. package/src/ir-to-client-js/generate-init.ts +2 -2
  113. package/src/ir-to-client-js/html-template.ts +41 -19
  114. package/src/ir-to-client-js/index.ts +4 -3
  115. package/src/ir-to-client-js/reactivity.ts +28 -17
  116. package/src/ir-to-client-js/source-map.ts +2 -1
  117. package/src/ir-to-client-js/types.ts +2 -0
  118. package/src/ir-to-client-js/utils.ts +34 -0
  119. package/src/jsx-to-ir.ts +737 -49
  120. package/src/opaque-local-accessor.ts +44 -0
  121. package/src/rich-type-refusal.ts +1 -1
  122. package/src/to-locale-date-lowering.ts +7 -4
  123. package/src/types.ts +51 -15
  124. package/dist/format-date-lowering.d.ts +0 -30
  125. package/dist/format-date-lowering.d.ts.map +0 -1
  126. package/src/__tests__/create-selector.test.ts +0 -124
  127. package/src/__tests__/format-date-lowering.test.ts +0 -125
  128. package/src/format-date-lowering.ts +0 -55
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Authored `formatDate` call refusal (BF056, #3089).
3
+ *
4
+ * `formatDate` is compiler ABI — the lowering target of the
5
+ * `.toLocaleDateString(locale, { timeZone, ... })` sugar — never an
6
+ * authored API. `checkAuthoredFormatDateCalls` (format-date-refusal.ts) is
7
+ * wired into `compileJSX`, so these tests go through it directly, same as
8
+ * `rich-type-method-refusal.test.ts`.
9
+ */
10
+
11
+ import { describe, test, expect } from 'bun:test'
12
+ import { compileJSX } from '../compiler'
13
+ import { ErrorCodes } from '../errors'
14
+ import { TestAdapter } from '../adapters/test-adapter'
15
+
16
+ const adapter = new TestAdapter()
17
+
18
+ function bf056(source: string, filePath = 'Test.tsx') {
19
+ const result = compileJSX(source, filePath, { adapter })
20
+ return result.errors.filter((e) => e.code === ErrorCodes.FORMAT_DATE_AUTHORED_CALL)
21
+ }
22
+
23
+ describe('authored formatDate call refusal — fires (BF056)', () => {
24
+ test('text-position call', () => {
25
+ const errors = bf056(`
26
+ import { formatDate } from '@barefootjs/client'
27
+ export function Foo({ createdAt }: { createdAt: Date }) {
28
+ return <div>{formatDate(createdAt, 'YYYY-MM-DD')}</div>
29
+ }
30
+ `)
31
+ expect(errors.length).toBe(1)
32
+ })
33
+
34
+ test('attribute-position call', () => {
35
+ const errors = bf056(`
36
+ import { formatDate } from '@barefootjs/client'
37
+ export function Foo({ createdAt }: { createdAt: Date }) {
38
+ return <div title={formatDate(createdAt, 'YYYY-MM-DD')} />
39
+ }
40
+ `)
41
+ expect(errors.length).toBe(1)
42
+ })
43
+
44
+ test('ternary-consequent inside a structured template attribute', () => {
45
+ const errors = bf056(`
46
+ import { formatDate } from '@barefootjs/client'
47
+ export function Foo({ ok, createdAt }: { ok: boolean; createdAt: Date }) {
48
+ return <div title={ok ? formatDate(createdAt, 'YYYY-MM-DD') : 'n/a'} />
49
+ }
50
+ `)
51
+ expect(errors.length).toBe(1)
52
+ })
53
+
54
+ test('imported via the /runtime subpath', () => {
55
+ const errors = bf056(`
56
+ import { formatDate } from '@barefootjs/client/runtime'
57
+ export function Foo({ createdAt }: { createdAt: Date }) {
58
+ return <div>{formatDate(createdAt, 'YYYY-MM-DD')}</div>
59
+ }
60
+ `)
61
+ expect(errors.length).toBe(1)
62
+ })
63
+
64
+ test('aliased import', () => {
65
+ const errors = bf056(`
66
+ import { formatDate as fd } from '@barefootjs/client'
67
+ export function Foo({ createdAt }: { createdAt: Date }) {
68
+ return <div>{fd(createdAt, 'YYYY-MM-DD')}</div>
69
+ }
70
+ `)
71
+ expect(errors.length).toBe(1)
72
+ })
73
+
74
+ test('nested inside a loop row', () => {
75
+ const errors = bf056(`
76
+ import { formatDate } from '@barefootjs/client'
77
+ export function Foo({ items }: { items: Date[] }) {
78
+ return <ul>{items.map((d, i) => <li key={i}>{formatDate(d, 'YYYY-MM-DD')}</li>)}</ul>
79
+ }
80
+ `)
81
+ expect(errors.length).toBe(1)
82
+ })
83
+
84
+ test('two distinct call sites both fire (dedup is per-location, not per-file)', () => {
85
+ const errors = bf056(`
86
+ import { formatDate } from '@barefootjs/client'
87
+ export function Foo({ a, b }: { a: Date; b: Date }) {
88
+ return <div>{formatDate(a, 'YYYY-MM-DD')}{formatDate(b, 'YYYY-MM-DD')}</div>
89
+ }
90
+ `)
91
+ expect(errors.length).toBe(2)
92
+ })
93
+ })
94
+
95
+ describe('authored formatDate call refusal — silent (no BF056)', () => {
96
+ test('/* @client */ defers the read', () => {
97
+ const errors = bf056(`
98
+ 'use client'
99
+ import { formatDate } from '@barefootjs/client'
100
+ export function Foo({ createdAt }: { createdAt: Date }) {
101
+ return <div>{/* @client */ formatDate(createdAt, 'YYYY-MM-DD')}</div>
102
+ }
103
+ `)
104
+ expect(errors.length).toBe(0)
105
+ })
106
+
107
+ test('a same-named local helper NOT imported from @barefootjs/client is untouched', () => {
108
+ const errors = bf056(`
109
+ function formatDate(d: Date, p: string) { return d.toISOString() }
110
+ export function Foo({ createdAt }: { createdAt: Date }) {
111
+ return <div>{formatDate(createdAt, 'YYYY-MM-DD')}</div>
112
+ }
113
+ `)
114
+ expect(errors.length).toBe(0)
115
+ })
116
+
117
+ test('the sanctioned toLocaleDateString sugar is untouched', () => {
118
+ const errors = bf056(`
119
+ export function Foo({ createdAt }: { createdAt: Date }) {
120
+ return <div>{createdAt.toLocaleDateString('en-US', { timeZone: 'UTC' })}</div>
121
+ }
122
+ `)
123
+ expect(errors.length).toBe(0)
124
+ })
125
+
126
+ test('no formatDate import at all', () => {
127
+ const errors = bf056(`
128
+ export function Foo({ createdAt }: { createdAt: Date }) {
129
+ return <div>{createdAt.toISOString()}</div>
130
+ }
131
+ `)
132
+ expect(errors.length).toBe(0)
133
+ })
134
+ })
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Diagnostic tests for BF029 (#3063).
3
+ *
4
+ * A `'use client'` component with an early-return branch whose OTHER
5
+ * branch is wrapped in a bare fragment (`return <>…</>`) is never fully
6
+ * hydrated: `ComponentDef`'s `comment` / `fragmentRoot` flags
7
+ * (`emit-registration.ts`) are decided once per component from
8
+ * `ir.root.type === 'if-statement'`, which can't say "branch A needs the
9
+ * comment-scope boundary, branch B doesn't" — so hydration never claims
10
+ * the fragment-wrapped branch's root (SSR and a fresh CSR mount are both
11
+ * correct; only claiming existing SSR markup during hydrate misses it).
12
+ * Fail loud at compile time instead of shipping a branch whose events
13
+ * silently never bind, with a `/* @client *\/` escape for the case an author
14
+ * accepts the known gap.
15
+ */
16
+
17
+ import { describe, test, expect } from 'bun:test'
18
+ import { compileJSX } from '../compiler'
19
+ import { TestAdapter } from '../adapters/test-adapter'
20
+ import { ErrorCodes } from '../errors'
21
+
22
+ const adapter = new TestAdapter()
23
+
24
+ function findBF029(errors: ReturnType<typeof compileJSX>['errors']) {
25
+ return errors.filter(e => e.code === ErrorCodes.FRAGMENT_WRAPPED_CONDITIONAL_RETURN_BRANCH)
26
+ }
27
+
28
+ describe('BF029 — fragment-wrapped branch of a conditional return', () => {
29
+ test('fires when the final (else) branch is a bare fragment', () => {
30
+ const source = `
31
+ 'use client'
32
+ import { createSignal } from '@barefootjs/client'
33
+
34
+ export function ConditionalReturnFragmentBranch(props: { x: boolean }) {
35
+ const [count, setCount] = createSignal(0)
36
+ if (props.x) return <a>link</a>
37
+ return <><button onClick={() => setCount(count() + 1)}>button: {count()}</button></>
38
+ }
39
+ `
40
+ const result = compileJSX(source, 'ConditionalReturnFragmentBranch.tsx', { adapter })
41
+ const matches = findBF029(result.errors)
42
+ expect(matches).toHaveLength(1)
43
+ expect(matches[0].severity).toBe('error')
44
+ expect(matches[0].message).toContain('/* @client */')
45
+ })
46
+
47
+ test('fires when a non-final (if) branch is a bare fragment', () => {
48
+ const source = `
49
+ 'use client'
50
+ import { createSignal } from '@barefootjs/client'
51
+
52
+ export function IfBranchFragment(props: { x: boolean }) {
53
+ const [count, setCount] = createSignal(0)
54
+ if (props.x) {
55
+ return <><button onClick={() => setCount(count() + 1)}>button: {count()}</button></>
56
+ }
57
+ return <a>link</a>
58
+ }
59
+ `
60
+ const result = compileJSX(source, 'IfBranchFragment.tsx', { adapter })
61
+ expect(findBF029(result.errors)).toHaveLength(1)
62
+ })
63
+
64
+ test('does not fire when every branch is a bare element (no fragment)', () => {
65
+ const source = `
66
+ 'use client'
67
+ import { createSignal } from '@barefootjs/client'
68
+
69
+ export function AllElements(props: { x: boolean }) {
70
+ const [count, setCount] = createSignal(0)
71
+ if (props.x) return <a>link</a>
72
+ return <button onClick={() => setCount(count() + 1)}>button: {count()}</button>
73
+ }
74
+ `
75
+ const result = compileJSX(source, 'AllElements.tsx', { adapter })
76
+ expect(findBF029(result.errors)).toHaveLength(0)
77
+ })
78
+
79
+ test('does not fire for a non-"use client" component whose fragment branch renders no child component', () => {
80
+ const source = `
81
+ export function StaticFragmentBranch(props: { x: boolean }) {
82
+ if (props.x) return <a>link</a>
83
+ return <><span>static</span></>
84
+ }
85
+ `
86
+ const result = compileJSX(source, 'StaticFragmentBranch.tsx', { adapter })
87
+ expect(findBF029(result.errors)).toHaveLength(0)
88
+ })
89
+
90
+ test('fires for a non-"use client" component whose fragment branch renders a child component', () => {
91
+ // The parent still gets `needsInit` and an `initChild` for the child,
92
+ // which only runs if hydration claims this branch's scope.
93
+ const source = `
94
+ import { Counter } from './counter'
95
+
96
+ export function ChildInFragmentBranch(props: { x: boolean }) {
97
+ if (props.x) return <a>link</a>
98
+ return <><Counter /><span>x</span></>
99
+ }
100
+ `
101
+ const result = compileJSX(source, 'ChildInFragmentBranch.tsx', { adapter })
102
+ expect(findBF029(result.errors)).toHaveLength(1)
103
+ })
104
+
105
+ test('does not fire for a non-"use client" component whose child component sits only in a non-fragment branch', () => {
106
+ const source = `
107
+ import { Counter } from './counter'
108
+
109
+ export function ChildInElementBranch(props: { x: boolean }) {
110
+ if (props.x) return <div><Counter /></div>
111
+ return <><span>static</span></>
112
+ }
113
+ `
114
+ const result = compileJSX(source, 'ChildInElementBranch.tsx', { adapter })
115
+ expect(findBF029(result.errors)).toHaveLength(0)
116
+ })
117
+
118
+ test('/* @client */ immediately before the fragment escapes the refusal', () => {
119
+ const source = `
120
+ 'use client'
121
+ import { createSignal } from '@barefootjs/client'
122
+
123
+ export function EscapedFragmentBranch(props: { x: boolean }) {
124
+ const [count, setCount] = createSignal(0)
125
+ if (props.x) return <a>link</a>
126
+ return /* @client */ <><button onClick={() => setCount(count() + 1)}>button: {count()}</button></>
127
+ }
128
+ `
129
+ const result = compileJSX(source, 'EscapedFragmentBranch.tsx', { adapter })
130
+ expect(findBF029(result.errors)).toHaveLength(0)
131
+ })
132
+ })
@@ -90,7 +90,15 @@ describe('hydrate() template generation for signal-bearing components', () => {
90
90
  // Stateless Child gets a static template (always useful).
91
91
  // Non-exported helpers are file-scoped (`Child__<hash>`) so they
92
92
  // cannot collide with same-named components in other modules.
93
- expect(content).toMatch(/hydrate\('Child(?:__[a-f0-9]+)?', \{ init: initChild, template:/)
93
+ //
94
+ // The init function is `initChild$`, not `initChild` (#3113):
95
+ // `Child` reactively mirrors the parent's signal into a slot, so this
96
+ // module also imports the RUNTIME's own `initChild` (to dispatch props
97
+ // into a mounted child); a bare `init${name}` here would declare a
98
+ // SECOND top-level `initChild` binding in the same module, which is a
99
+ // hard `SyntaxError` under real ES module semantics — see
100
+ // `initFunctionName`'s docstring (`ir-to-client-js/utils.ts`).
101
+ expect(content).toMatch(/hydrate\('Child(?:__[a-f0-9]+)?', \{ init: initChild\$, template:/)
94
102
 
95
103
  // Parent also gets CSR fallback template for cross-file conditional use
96
104
  expect(content).toMatch(/hydrate\('Parent',.*template:/)
@@ -37,7 +37,7 @@ export function StaticRefCase() {
37
37
  return (
38
38
  <ul>
39
39
  {items.map(item => (
40
- <li key={item.id} ref={trackMount}>{item.id}</li>
40
+ <li key={item.id} data-tracked="0" ref={trackMount}>{item.id}</li>
41
41
  ))}
42
42
  </ul>
43
43
  )
@@ -58,7 +58,7 @@ export function StaticNestedRefCase() {
58
58
  {items.map(item => (
59
59
  <li key={item.id}>
60
60
  {item.children.map(child => (
61
- <span key={child.id} ref={trackMount}>{child.id}</span>
61
+ <span key={child.id} data-tracked="0" ref={trackMount}>{child.id}</span>
62
62
  ))}
63
63
  </li>
64
64
  ))}
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Emission pin for #3009: a `ref` on an element inside a keyed loop row's
3
+ * conditional branch must land on that branch's own `insert()` bindEvents,
4
+ * not the row-level `mapArray` renderItem body (which only fires once, at
5
+ * row creation) — see `reactivity.ts`'s `collectLoopChildRefs` for the fix.
6
+ */
7
+ import { describe, test, expect } from 'bun:test'
8
+ import { compileJSX } from '../compiler'
9
+ import { TestAdapter } from '../adapters/test-adapter'
10
+
11
+ const adapter = new TestAdapter()
12
+
13
+ function clientJsFor(source: string): string {
14
+ const result = compileJSX(source, 'C.tsx', { adapter })
15
+ const errors = result.errors.filter(e => e.severity === 'error')
16
+ if (errors.length > 0) throw new Error(errors.map(e => e.message).join('\n'))
17
+ return result.files.find(f => f.type === 'clientJs')!.content
18
+ }
19
+
20
+ const BRANCH_REF_SOURCE = `
21
+ 'use client'
22
+ import { createSignal } from '@barefootjs/client'
23
+ type Row = { key: string; kind: 'a' | 'b'; label: string }
24
+ export function C() {
25
+ const [rows] = createSignal<Row[]>([])
26
+ return (
27
+ <ul>{rows().map(row => (
28
+ <li key={row.key}>
29
+ {row.kind === 'a' ? (
30
+ <span ref={el => { el.dataset.mounted = 'yes' }} data-mounted="yes">{row.label}</span>
31
+ ) : (
32
+ <span>placeholder</span>
33
+ )}
34
+ </li>
35
+ ))}</ul>
36
+ )
37
+ }
38
+ `
39
+
40
+ describe('issue-3009 — loop-row conditional branch ref emission', () => {
41
+ test('the ref call is emitted inside the branch bindEvents, using __branchScope', () => {
42
+ const js = clientJsFor(BRANCH_REF_SOURCE)
43
+ expect(js).toContain("el.dataset.mounted = 'yes'")
44
+
45
+ // Find the specific line the ref call landed on and confirm it looks
46
+ // up its element off __branchScope (the insert()-mounted branch node)
47
+ // via qsa — not off the row's own __el/__existing clone.
48
+ const refLine = js.split('\n').find(l => l.includes("el.dataset.mounted = 'yes'"))
49
+ expect(refLine).toBeDefined()
50
+ expect(js).toContain("qsa(__branchScope, '[bf=")
51
+ })
52
+
53
+ test('no ref lookup is hoisted into the mapArray renderItem body (row-level)', () => {
54
+ const js = clientJsFor(BRANCH_REF_SOURCE)
55
+ // The row-level hoist (the #3009 bug) emitted a `__rf_<slot>` lookup
56
+ // keyed off the item element var, unconditionally, once per renderItem.
57
+ // With the fix, the only ref lookup for this fixture is the branch-scoped
58
+ // one asserted above (qsa(__branchScope, ...)) — no `__rf_` variable at
59
+ // row level.
60
+ expect(js).not.toMatch(/const __rf_\w+ = (qsa|qsaItem)\(__el,/)
61
+ })
62
+
63
+ test('a ref on a plain (non-conditional) loop row element is unaffected — still row-level', () => {
64
+ const js = clientJsFor(`
65
+ 'use client'
66
+ import { createSignal } from '@barefootjs/client'
67
+ type Row = { key: string; label: string }
68
+ export function C() {
69
+ const [rows] = createSignal<Row[]>([])
70
+ return <ul>{rows().map(row => (
71
+ <li key={row.key} ref={el => { el.dataset.mounted = 'yes' }} data-mounted="yes">{row.label}</li>
72
+ ))}</ul>
73
+ }
74
+ `)
75
+ expect(js).toContain("el.dataset.mounted = 'yes'")
76
+ // This ref has no conditional to hide behind — it must still be the
77
+ // ordinary row-level per-mount emission.
78
+ expect(js).toMatch(/\{ const __rf_\w+ = (qsa|qsaItem|__p)/)
79
+ })
80
+ })
@@ -0,0 +1,213 @@
1
+ /**
2
+ * #3044 — a client component receiving a REACTIVE prop that is an OBJECT
3
+ * (`{ data }: { data: { items: T[] } }`), whose body maps a nested array
4
+ * property off that prop (`data.items.map(...)`), stayed on the static
5
+ * SSR-row-bind path instead of `mapArray`. `isArrayExprDirectPropRef`
6
+ * (jsx-to-ir.ts) recognized:
7
+ * (a) a bare identifier that is itself a destructured prop (`items.map(...)`
8
+ * when `items` is the prop), and
9
+ * (b) a property access rooted at the WHOLE props object (`props.items`,
10
+ * one member access deep).
11
+ * Neither covered a property access rooted at a destructured OBJECT-shaped
12
+ * prop (`data.items`, where `data` — not `items` — is the prop): the
13
+ * `.expression` of `data.items` is the identifier `data`, which is a
14
+ * destructured prop binding rather than the whole props object, so case (b)
15
+ * (gated on `propsObjName`, which is null in destructured-prop mode) never
16
+ * matched. The loop was classified `isStaticArray: true`, so when the
17
+ * parent signal driving `data` went from `{ items: [] }` (SSR) to
18
+ * `{ items: [...] }` (e.g. in `onMount`), the child's static `forEach`-bind
19
+ * init only ran once against the empty SSR row set and never inserted rows.
20
+ *
21
+ * Fix: `isArrayExprDirectPropRef`'s property-access branch now walks to the
22
+ * ROOT identifier of the (possibly chained) member-access expression and
23
+ * accepts it either as the whole props object OR as a destructured prop
24
+ * binding (via the same `isDirectPropBindingName` terminal (a) already
25
+ * uses), through the same alias-hop chain as before.
26
+ */
27
+
28
+ import { describe, test, expect } from 'bun:test'
29
+ import { compileJSX } from '../compiler'
30
+ import { analyzeComponent } from '../analyzer'
31
+ import { jsxToIR } from '../jsx-to-ir'
32
+ import { TestAdapter } from '../adapters/test-adapter'
33
+ import type { IRNode } from '../types'
34
+
35
+ const adapter = new TestAdapter()
36
+
37
+ function getClientJs(source: string, filename: string): string {
38
+ const result = compileJSX(source, filename, { adapter })
39
+ expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
40
+ const clientJs = result.files.find(f => f.type === 'clientJs')
41
+ expect(clientJs).toBeDefined()
42
+ return clientJs!.content
43
+ }
44
+
45
+ /** First `IRLoop` node found in a DFS of the component IR. */
46
+ function findLoop(node: IRNode | null, predicate: (n: any) => boolean = () => true): any {
47
+ if (!node || typeof node !== 'object') return undefined
48
+ if ((node as any).type === 'loop' && predicate(node)) return node
49
+ for (const key of Object.keys(node)) {
50
+ const v = (node as any)[key]
51
+ if (Array.isArray(v)) {
52
+ for (const c of v) {
53
+ const found = findLoop(c, predicate)
54
+ if (found) return found
55
+ }
56
+ } else if (v && typeof v === 'object') {
57
+ const found = findLoop(v, predicate)
58
+ if (found) return found
59
+ }
60
+ }
61
+ return undefined
62
+ }
63
+
64
+ function loopFlags(source: string, filename: string, predicate?: (n: any) => boolean) {
65
+ const ctx = analyzeComponent(source, filename, 'ReproChild')
66
+ const ir = jsxToIR(ctx)
67
+ expect(ir).not.toBeNull()
68
+ const loop = findLoop(ir, predicate)
69
+ expect(loop).toBeDefined()
70
+ return { isStaticArray: loop.isStaticArray as boolean, isPropDerivedArray: loop.isPropDerivedArray as boolean | undefined }
71
+ }
72
+
73
+ describe('#3044 — nested array off a destructured object prop uses mapArray, not the static path', () => {
74
+ test('headline repro: `data.items.map(...)` where `data` is a destructured object prop uses mapArray', () => {
75
+ const clientJs = getClientJs(`
76
+ 'use client'
77
+ export type ReproData = { items: { id: string; label: string }[] }
78
+ export function ReproChild({ data }: { data: ReproData }) {
79
+ return (
80
+ <ul aria-label="items">
81
+ {data.items.map((item) => <li key={item.id}>{item.label}</li>)}
82
+ </ul>
83
+ )
84
+ }
85
+ `, 'ReproChild.tsx')
86
+ expect(clientJs).toMatch(/\bmapArray(Lazy)?\s*\(/)
87
+ expect(clientJs).not.toMatch(/qsaChildScopes\(/)
88
+ })
89
+
90
+ test('flags: `data.items.map(...)` is classified isPropDerivedArray / non-static', () => {
91
+ const flags = loopFlags(`
92
+ 'use client'
93
+ export type ReproData = { items: { id: string; label: string }[] }
94
+ export function ReproChild({ data }: { data: ReproData }) {
95
+ return (
96
+ <ul aria-label="items">
97
+ {data.items.map((item) => <li key={item.id}>{item.label}</li>)}
98
+ </ul>
99
+ )
100
+ }
101
+ `, 'ReproChildFlags.tsx')
102
+ expect(flags.isStaticArray).toBe(false)
103
+ expect(flags.isPropDerivedArray).toBe(true)
104
+ })
105
+
106
+ test('an alias hop of the destructured object prop also uses mapArray (`const d = data; d.items.map(...)`)', () => {
107
+ const flags = loopFlags(`
108
+ 'use client'
109
+ export type ReproData = { items: { id: string; label: string }[] }
110
+ export function ReproChild({ data }: { data: ReproData }) {
111
+ const d = data
112
+ return <ul>{d.items.map((item) => <li key={item.id}>{item.label}</li>)}</ul>
113
+ }
114
+ `, 'AliasedObjectProp.tsx')
115
+ expect(flags.isStaticArray).toBe(false)
116
+ expect(flags.isPropDerivedArray).toBe(true)
117
+ })
118
+
119
+ test('whole-props form, two-level chain (`props.data.items.map(...)`) also uses mapArray', () => {
120
+ const flags = loopFlags(`
121
+ 'use client'
122
+ export type ReproData = { items: { id: string; label: string }[] }
123
+ export function ReproChild(props: { data: ReproData }) {
124
+ return <ul>{props.data.items.map((item) => <li key={item.id}>{item.label}</li>)}</ul>
125
+ }
126
+ `, 'WholePropsTwoLevel.tsx')
127
+ expect(flags.isStaticArray).toBe(false)
128
+ expect(flags.isPropDerivedArray).toBe(true)
129
+ })
130
+
131
+ test('false-positive guard: a nested array off an unrelated local object (not a prop) stays static', () => {
132
+ const flags = loopFlags(`
133
+ 'use client'
134
+ export function ReproChild({ data }: { data: { items: { id: string; label: string }[] } }) {
135
+ const state = { items: [{ id: 'z', label: 'z' }] }
136
+ return <ul>{state.items.map((item) => <li key={item.id}>{item.label}</li>)}</ul>
137
+ }
138
+ `, 'NameCollisionObjectProp.tsx')
139
+ expect(flags.isStaticArray).toBe(true)
140
+ expect(flags.isPropDerivedArray).toBeUndefined()
141
+ })
142
+
143
+ // Pullfrog review findings on the initial fix (both confirmed against the
144
+ // real compiler before being fixed):
145
+
146
+ test('pullfrog review finding: a local const aliasing `data.items` (one hop before .map()) also uses mapArray', () => {
147
+ // `const items = data.items; items.map(...)` — the extremely common
148
+ // "alias the array to a local const before mapping" pattern. The
149
+ // initial fix only widened `isArrayExprDirectPropRef`'s OWN property-
150
+ // access branch (the `.map()` array expression itself); it left
151
+ // `isDirectPropBindingName`'s member-parsed check — used to resolve a
152
+ // local const's value — still single-level (`parsed.object.kind ===
153
+ // 'identifier' && parsed.object.name === propsObjName`), so this shape
154
+ // reproduced #3044's exact silent-freeze bug one alias hop away.
155
+ const flags = loopFlags(`
156
+ 'use client'
157
+ export type ReproData = { items: { id: string; label: string }[] }
158
+ export function ReproChild({ data }: { data: ReproData }) {
159
+ const items = data.items
160
+ return <ul>{items.map((item) => <li key={item.id}>{item.label}</li>)}</ul>
161
+ }
162
+ `, 'ConstAliasOfObjectProp.tsx')
163
+ expect(flags.isStaticArray).toBe(false)
164
+ expect(flags.isPropDerivedArray).toBe(true)
165
+ })
166
+
167
+ test('pullfrog review finding: a local const aliasing the whole-props two-level chain also uses mapArray', () => {
168
+ // `const items = props.data.items; items.map(...)` — same gap as above,
169
+ // reached through the whole-props (non-destructured) form instead.
170
+ const flags = loopFlags(`
171
+ 'use client'
172
+ export type ReproData = { items: { id: string; label: string }[] }
173
+ export function ReproChild(props: { data: ReproData }) {
174
+ const items = props.data.items
175
+ return <ul>{items.map((item) => <li key={item.id}>{item.label}</li>)}</ul>
176
+ }
177
+ `, 'ConstAliasOfWholePropsChain.tsx')
178
+ expect(flags.isStaticArray).toBe(false)
179
+ expect(flags.isPropDerivedArray).toBe(true)
180
+ })
181
+
182
+ test('pullfrog review finding: a loop-row parameter shadowing the outer props-object name is not misclassified', () => {
183
+ // `props.rows.map((props) => props.items.map(...))` — the inner `props`
184
+ // is an arbitrary row value bound by the OUTER `.map()`, unrelated to
185
+ // the real props object it happens to share a name with. The initial
186
+ // fix's new property-access branch checked `name === propsObjName`
187
+ // without the shadow guard `isDirectPropBindingName` uses right next to
188
+ // it, and widened that unguarded check from a single member access
189
+ // (`props.foo`) to an arbitrarily deep chain — increasing the blast
190
+ // radius of a pre-existing gap. `isPropDerivedArray` feeds real
191
+ // Go-adapter codegen decisions for nested components, so a false
192
+ // positive here is a genuine misclassification risk, not just a
193
+ // redundant `mapArray`.
194
+ const ctx = analyzeComponent(`
195
+ 'use client'
196
+ type Row = { items: { id: string; label: string }[] }
197
+ export function Foo(props: { rows: Row[] }) {
198
+ return (
199
+ <div>
200
+ {props.rows.map((props) => (
201
+ <ul>{props.items.map((item) => <li key={item.id}>{item.label}</li>)}</ul>
202
+ ))}
203
+ </div>
204
+ )
205
+ }
206
+ `, 'ShadowedPropsParam.tsx', 'Foo')
207
+ const ir = jsxToIR(ctx)
208
+ const inner = findLoop(ir, (n: any) => n.array === 'props.items')
209
+ expect(inner).toBeDefined()
210
+ expect(inner.isStaticArray).toBe(true)
211
+ expect(inner.isPropDerivedArray).toBeUndefined()
212
+ })
213
+ })