@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,133 @@
1
+ /**
2
+ * Regression test for #3113: a component literally named `Child` compiled
3
+ * a top-level `export function initChild(...)` — the compiler's own
4
+ * `init<Name>` naming convention (`generate-init.ts`) — into the SAME
5
+ * module that also `import { initChild } from '@barefootjs/client/runtime'`
6
+ * (the runtime dispatcher a parent calls to hand a live prop getter to a
7
+ * mounted child, `registry.ts`'s `initChild`). Two top-level declarations
8
+ * of the same identifier in one ES module is a hard `SyntaxError` under
9
+ * real module semantics — verified against a raw `<script type="module">`
10
+ * load, the same unbundled-ESM shape `bf build` ships (see
11
+ * `initFunctionName`'s docstring, `ir-to-client-js/utils.ts`) — so the
12
+ * ENTIRE hydration script failed to parse, breaking every action on the
13
+ * page, not just the child's own reactive text. `initFunctionName` now
14
+ * disambiguates the generated declaration (`initChild$`) whenever it would
15
+ * collide with a `RUNTIME_IMPORT_CANDIDATES` entry, and every emission
16
+ * site (`generate-init.ts`, `emit-registration.ts`, `ir-to-client-js/
17
+ * index.ts`'s static-template stub path, `source-map.ts`) computes the
18
+ * SAME name so the declaration and its `hydrate(...)` registration always
19
+ * agree.
20
+ *
21
+ * The bounded state-space exploration scenario
22
+ * (`packages/adapter-tests/explore/scenarios/child-prop-slots.ts`) is the
23
+ * end-to-end regression armor (real browser, real ES module load); this
24
+ * file pins the compiler-unit half: the generated code must neither carry
25
+ * a literal `export function initChild(` declaration nor otherwise emit
26
+ * a duplicate top-level `initChild` binding, whenever the component named
27
+ * `Child` needs BOTH its own compiled init (reactive prop-derived content)
28
+ * AND the runtime's `initChild` (because some ancestor mounts a further
29
+ * child by name).
30
+ */
31
+
32
+ import { describe, test, expect } from 'bun:test'
33
+ import { compileJSX } from '../compiler'
34
+ import { TestAdapter } from '../adapters/test-adapter'
35
+ import ts from 'typescript'
36
+
37
+ const adapter = new TestAdapter()
38
+
39
+ function getClientJs(source: string, filename: string): string {
40
+ const result = compileJSX(source, filename, { adapter })
41
+ expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
42
+ const clientJs = result.files.find(f => f.type === 'clientJs')
43
+ expect(clientJs).toBeDefined()
44
+ return clientJs!.content
45
+ }
46
+
47
+ /** No top-level identifier is declared more than once (mirrors real ESM semantics). */
48
+ function assertNoDuplicateTopLevelDeclarations(code: string, filename: string): void {
49
+ const sourceFile = ts.createSourceFile(filename, code, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)
50
+ const seen = new Set<string>()
51
+ const dupes = new Set<string>()
52
+ const noteName = (name: string) => {
53
+ if (seen.has(name)) dupes.add(name)
54
+ seen.add(name)
55
+ }
56
+ for (const stmt of sourceFile.statements) {
57
+ if (ts.isImportDeclaration(stmt) && stmt.importClause?.namedBindings && ts.isNamedImports(stmt.importClause.namedBindings)) {
58
+ for (const spec of stmt.importClause.namedBindings.elements) noteName(spec.name.text)
59
+ } else if (ts.isFunctionDeclaration(stmt) && stmt.name) {
60
+ noteName(stmt.name.text)
61
+ }
62
+ }
63
+ expect([...dupes]).toEqual([])
64
+ }
65
+
66
+ describe('#3113: init<Name> disambiguated against runtime import names', () => {
67
+ test('a component literally named `Child` with a reactive prop-derived text child does not collide with the imported runtime initChild', () => {
68
+ // Mirrors the issue's exploration scenario: an inlined (non-"use
69
+ // client") `Child` reads a destructured object prop's field as text,
70
+ // and the "use client" parent passes it a live signal-derived value.
71
+ const source = `
72
+ 'use client'
73
+ import { createSignal } from '@barefootjs/client'
74
+
75
+ type Data = { label: string }
76
+
77
+ function Child({ data }: { data: Data }) {
78
+ return <p aria-label="label">{data.label}</p>
79
+ }
80
+
81
+ export function ChildPropSlots({ initial }: { initial: { data: Data } }) {
82
+ const [data, setData] = createSignal<Data>(initial.data)
83
+ return (
84
+ <div>
85
+ <Child data={data()} />
86
+ <button onClick={() => setData({ label: 'loaded' })}>load</button>
87
+ </div>
88
+ )
89
+ }
90
+ `
91
+ const clientJs = getClientJs(source, 'ChildPropSlots.tsx')
92
+
93
+ // The exact defect: a bare `export function initChild(` declaration
94
+ // sharing a module with `import { ..., initChild, ... }`.
95
+ expect(clientJs).not.toMatch(/\bexport function initChild\(/)
96
+ // The disambiguated name is used consistently for both the
97
+ // declaration and its `hydrate(...)` registration.
98
+ expect(clientJs).toContain('export function initChild$(')
99
+ expect(clientJs).toMatch(/hydrate\('Child[^']*',\s*\{\s*init:\s*initChild\$,/)
100
+ // The runtime dispatcher call (a DIFFERENT `initChild` — the import —
101
+ // invoked from the parent's own init to deliver live props) is
102
+ // untouched.
103
+ expect(clientJs).toMatch(/\binitChild\('Child[_a-f0-9]*',/)
104
+
105
+ assertNoDuplicateTopLevelDeclarations(clientJs, 'ChildPropSlots.client.js')
106
+ })
107
+
108
+ test('a component named anything else keeps the plain init<Name> convention (no needless disambiguation)', () => {
109
+ const source = `
110
+ 'use client'
111
+ import { createSignal } from '@barefootjs/client'
112
+
113
+ type Data = { label: string }
114
+
115
+ function Leaf({ data }: { data: Data }) {
116
+ return <p aria-label="label">{data.label}</p>
117
+ }
118
+
119
+ export function Parent({ initial }: { initial: { data: Data } }) {
120
+ const [data, setData] = createSignal<Data>(initial.data)
121
+ return (
122
+ <div>
123
+ <Leaf data={data()} />
124
+ <button onClick={() => setData({ label: 'loaded' })}>load</button>
125
+ </div>
126
+ )
127
+ }
128
+ `
129
+ const clientJs = getClientJs(source, 'Parent.tsx')
130
+ expect(clientJs).toContain('export function initLeaf(')
131
+ expect(clientJs).not.toContain('initLeaf$')
132
+ })
133
+ })
@@ -52,13 +52,13 @@ describe("JSX return through TypeScript type assertion (#1405)", () => {
52
52
 
53
53
  if (isSmall) {
54
54
  return (
55
- <div ref={attachHost}>
55
+ <div ref={attachHost} data-attached="true">
56
56
  <div>small: {count()}</div>
57
57
  </div>
58
58
  ) as unknown as HTMLElement
59
59
  }
60
60
  return (
61
- <div ref={attachHost}>
61
+ <div ref={attachHost} data-attached="true">
62
62
  <div>large: {count()}</div>
63
63
  </div>
64
64
  ) as unknown as HTMLElement
@@ -341,12 +341,13 @@ describe('classifyLazyBinding — fail-safe', () => {
341
341
  })
342
342
 
343
343
  test('an import, a local function, and a non-literal const are all OPAQUE, not inert', () => {
344
- // A reactive accessor hides behind ordinary-looking names — the
345
- // `createSelector` const is the canonical case. Guessing "inert" here
346
- // would emit an applyOuter effect that reads them non-reactively.
344
+ // A reactive accessor hides behind ordinary-looking names — a local
345
+ // `const isSelected = someFactory(selected)` is the canonical case.
346
+ // Guessing "inert" here would emit an applyOuter effect that reads them
347
+ // non-reactively.
347
348
  const selScope = makeScope({
348
349
  signals: scope.signals,
349
- constants: new Map<string, ReadonlySet<string> | null>([['isSelected', new Set(['createSelector', 'selected'])]]),
350
+ constants: new Map<string, ReadonlySet<string> | null>([['isSelected', new Set(['someFactory', 'selected'])]]),
350
351
  inert: new Set(['clsx']),
351
352
  })
352
353
  const c = classifyLazyBinding({
@@ -0,0 +1,108 @@
1
+ /**
2
+ * BarefootJS Compiler — reactive attrs on an element forwarded as a
3
+ * loop-row child component's `children` (#3143).
4
+ *
5
+ * `{items.map(item => <Chip><a href={...}>...</a></Chip>)}` used to compile
6
+ * clean but silently drop any effect for the `<a>`'s own reactive
7
+ * attributes — no diagnostic, the DOM just never patched after the first
8
+ * render. Two distinct loop-plan shapes hit this, both fixed here:
9
+ *
10
+ * - a DYNAMIC (signal-derived) array whose row root is a component
11
+ * (`buildComponentLoopPlan`, `control-flow/plan/build-component-loop.ts`)
12
+ * — `elem.bindings.reactiveAttrs`/`.reactiveTexts` were collected but
13
+ * never read by that builder, AND the "simple" (no nested child
14
+ * components) stringifier path returned before ever looking at the
15
+ * reactive-effects plan at all.
16
+ * - a STATIC array (a plain literal, not a signal) whose row root is a
17
+ * component (`buildStaticLoopPlan`, `control-flow/plan/build-loop.ts`)
18
+ * — `attrsBySlot` was unconditionally skipped whenever the row root was
19
+ * a child component, a migration-era artifact (#1253) that never
20
+ * applied to the equivalent `texts` collection a few lines below it.
21
+ *
22
+ * The static-array shape is also the real-browser regression fixture
23
+ * `loop-row-child-children-attrs` (`packages/adapter-tests/fixtures/
24
+ * loop-row-child-children-attrs.ts`, its `interactions` — quarantine
25
+ * removed by this same change) and the known-limitation registry entry
26
+ * `loop-row-child-children-attrs-frozen` (narrowed by this change to the
27
+ * unrelated go-template SSR-construction bug the same fixture also caught).
28
+ */
29
+
30
+ import { describe, test, expect } from 'bun:test'
31
+ import { compileJSX } from '../compiler'
32
+ import { TestAdapter } from '../adapters/test-adapter'
33
+
34
+ const adapter = new TestAdapter()
35
+
36
+ describe('loop-row child component forwarded-children reactive attrs (#3143)', () => {
37
+ test('dynamic (signal-derived) array + component-root loop: forwarded <a> attrs get a createEffect', () => {
38
+ const source = `
39
+ 'use client'
40
+ import { createSignal } from '@barefootjs/client'
41
+
42
+ function Chip({ children }: { children?: any }) {
43
+ return <span>{children}</span>
44
+ }
45
+
46
+ export function Nav() {
47
+ const [active, setActive] = createSignal('a')
48
+ const [opts] = createSignal(['a', 'b'])
49
+ return (
50
+ <div>
51
+ {opts().map(opt => (
52
+ <Chip key={opt}>
53
+ <a
54
+ href={active() === opt ? '/current' : \`/other/\${opt}\`}
55
+ data-current={active() === opt ? 'true' : 'false'}
56
+ >{opt}</a>
57
+ </Chip>
58
+ ))}
59
+ </div>
60
+ )
61
+ }
62
+ `
63
+ const result = compileJSX(source, 'Nav.tsx', { adapter })
64
+ expect(result.errors).toHaveLength(0)
65
+ const content = result.files.find((f) => f.type === 'clientJs')!.content
66
+
67
+ // The forwarded <a>'s own reactive attrs must reach a createEffect —
68
+ // not just be baked once into the `children` props getter.
69
+ expect(content).toContain("setAttribute('href'")
70
+ expect(content).toContain("setAttribute('data-current'")
71
+ expect(content).toMatch(/createEffect\(\(\) => \{[\s\S]*?setAttribute\('href'/)
72
+ })
73
+
74
+ test('static array (plain literal) + component-root loop: forwarded <a> attrs get a createEffect', () => {
75
+ const source = `
76
+ 'use client'
77
+ import { createSignal } from '@barefootjs/client'
78
+
79
+ function Chip({ children }: { children?: any }) {
80
+ return <span>{children}</span>
81
+ }
82
+
83
+ export function Nav() {
84
+ const [active, setActive] = createSignal('a')
85
+ const opts = ['a', 'b']
86
+ return (
87
+ <div>
88
+ {opts.map(opt => (
89
+ <Chip key={opt}>
90
+ <a
91
+ href={active() === opt ? '/current' : \`/other/\${opt}\`}
92
+ data-current={active() === opt ? 'true' : 'false'}
93
+ >{opt}</a>
94
+ </Chip>
95
+ ))}
96
+ </div>
97
+ )
98
+ }
99
+ `
100
+ const result = compileJSX(source, 'Nav.tsx', { adapter })
101
+ expect(result.errors).toHaveLength(0)
102
+ const content = result.files.find((f) => f.type === 'clientJs')!.content
103
+
104
+ expect(content).toContain("setAttribute('href'")
105
+ expect(content).toContain("setAttribute('data-current'")
106
+ expect(content).toMatch(/createEffect\(\(\) => \{[\s\S]*?setAttribute\('href'/)
107
+ })
108
+ })
@@ -0,0 +1,158 @@
1
+ /**
2
+ * BarefootJS Compiler — keyed loop row: forwarded text children of a child
3
+ * component keep their SSR slot markers on hydration (#3064).
4
+ *
5
+ * Given a keyed `.map()` row whose child component call receives
6
+ * parent-owned reactive text as `children`
7
+ * (`<TableRow key={...}><TableCell>{payment.id}</TableCell></TableRow>`),
8
+ * the row's hydration effect used to rewrite each forwarded cell with
9
+ * `__c.textContent = ...` on every run — including its OWN first run, which
10
+ * fires immediately even when nothing has changed yet — discarding the
11
+ * `<!--bf:^sN-->…<!--/-->` slot marker pair the server emitted around the
12
+ * forwarded text. The fix patches through the marker (`$t`'s resolved Text
13
+ * node, `.data =`) instead of clobbering the whole child root, mirroring
14
+ * how a parent-owned text patches everywhere else in the compiler
15
+ * (`packages/client/src/runtime/query.ts`'s `$t`/`textNodeAfterComment`).
16
+ *
17
+ * Real-browser regression pin: the shared `data-table` fixture
18
+ * (`packages/adapter-tests/fixtures/data-table.ts`, `site/ui/components/
19
+ * data-table-demo.tsx`'s `DataTablePreviewDemo` — see `oracle-quarantine.ts`,
20
+ * quarantine removed by this same change) and the known-limitation entry
21
+ * `loop-row-child-text-children-markers-dropped` (deleted by this change).
22
+ */
23
+
24
+ import { describe, test, expect } from 'bun:test'
25
+ import { compileJSX } from '../compiler'
26
+ import { TestAdapter } from '../adapters/test-adapter'
27
+
28
+ const adapter = new TestAdapter()
29
+
30
+ describe('keyed loop row: forwarded text children keep their SSR markers (#3064)', () => {
31
+ test('a single bare-expression child patches through $t, not textContent', () => {
32
+ const source = `
33
+ 'use client'
34
+ import { createSignal } from '@barefootjs/client'
35
+
36
+ function Cell({ children }: { children?: any }) {
37
+ return <td>{children}</td>
38
+ }
39
+ function Row({ children }: { children?: any }) {
40
+ return <tr>{children}</tr>
41
+ }
42
+
43
+ type Payment = { id: string }
44
+
45
+ export function Table() {
46
+ const [payments] = createSignal<Payment[]>([])
47
+ return (
48
+ <table>
49
+ <tbody>
50
+ {payments().map(payment => (
51
+ <Row key={payment.id}>
52
+ <Cell>{payment.id}</Cell>
53
+ </Row>
54
+ ))}
55
+ </tbody>
56
+ </table>
57
+ )
58
+ }
59
+ `
60
+ const result = compileJSX(source, 'Table.tsx', { adapter })
61
+ expect(result.errors).toHaveLength(0)
62
+ const content = result.files.find((f) => f.type === 'clientJs')!.content
63
+
64
+ // Must patch through the marker-aware $t primitive, not blindly
65
+ // overwrite the whole child root's textContent (which would discard
66
+ // the SSR `<!--bf:^sN-->…<!--/-->` marker pair around `payment.id`).
67
+ expect(content).toContain('$t(__c,')
68
+ expect(content).not.toMatch(/__c\.textContent = Array\.isArray\(__v\) \? __v\.join/)
69
+ // The resolved Text node's `.data` is written, not innerHTML/textContent.
70
+ expect(content).toMatch(/\.data = String\(payment\(\)\.id/)
71
+ })
72
+
73
+ test('mixed static text + expression child (e.g. "$" + amount) only patches the expression slot', () => {
74
+ const source = `
75
+ 'use client'
76
+ import { createSignal } from '@barefootjs/client'
77
+
78
+ function Cell({ children }: { children?: any }) {
79
+ return <td>{children}</td>
80
+ }
81
+ function Row({ children }: { children?: any }) {
82
+ return <tr>{children}</tr>
83
+ }
84
+
85
+ type Payment = { id: string; amount: number }
86
+
87
+ export function Table() {
88
+ const [payments] = createSignal<Payment[]>([])
89
+ return (
90
+ <table>
91
+ <tbody>
92
+ {payments().map(payment => (
93
+ <Row key={payment.id}>
94
+ <Cell>\${payment.amount.toFixed(2)}</Cell>
95
+ </Row>
96
+ ))}
97
+ </tbody>
98
+ </table>
99
+ )
100
+ }
101
+ `
102
+ const result = compileJSX(source, 'Table.tsx', { adapter })
103
+ expect(result.errors).toHaveLength(0)
104
+ const content = result.files.find((f) => f.type === 'clientJs')!.content
105
+
106
+ expect(content).toContain('$t(__c,')
107
+ expect(content).toMatch(/\.data = String\(payment\(\)\.amount\.toFixed\(2\)/)
108
+ // The static "$" text must never be part of a patch expression — only
109
+ // the reactive part gets its own marker/slot.
110
+ expect(content).not.toMatch(/\.data = String\(\["\$"/)
111
+ })
112
+
113
+ test('composite loop (plain-element row + nested component + inner loop) patches through $t too', () => {
114
+ // `emitComponentAndEventSetup` (control-flow/shared.ts) is the SAME
115
+ // decision + emission `stringify/component-loop.ts`'s `emitNestedInit`
116
+ // used to duplicate, for the composite-loop shape (a plain-element loop
117
+ // row with both a nested child component AND an inner `.map()`, which
118
+ // routes through `upsertChild` instead of `initChild`+`createComponent`).
119
+ // #3064's fix lives in the ONE shared `buildChildrenTextEffect`/
120
+ // `stringifyChildrenTextEffect` pair both call, so this shape must be
121
+ // covered by its own fixture, not just inferred from the component-root
122
+ // case above.
123
+ const source = `
124
+ 'use client'
125
+ import { createSignal } from '@barefootjs/client'
126
+
127
+ function Badge({ children }: { children?: any }) {
128
+ return <span>{children}</span>
129
+ }
130
+
131
+ type Item = { id: number; name: string; tags: string[] }
132
+
133
+ export function List() {
134
+ const [items] = createSignal<Item[]>([])
135
+ return (
136
+ <ul>
137
+ {items().map(item => (
138
+ <li key={item.id}>
139
+ {item.tags.map(tag => <em key={tag}>{tag}</em>)}
140
+ <Badge>{item.name}</Badge>
141
+ </li>
142
+ ))}
143
+ </ul>
144
+ )
145
+ }
146
+ `
147
+ const result = compileJSX(source, 'List.tsx', { adapter })
148
+ expect(result.errors).toHaveLength(0)
149
+ const content = result.files.find((f) => f.type === 'clientJs')!.content
150
+
151
+ // upsertChild confirms this row is on the composite (not component-root)
152
+ // path — the shape `emitComponentAndEventSetup` handles.
153
+ expect(content).toContain('upsertChild(')
154
+ expect(content).toContain('$t(__c,')
155
+ expect(content).toMatch(/\.data = String\(item\(\)\.name/)
156
+ expect(content).not.toMatch(/__c\.textContent = Array\.isArray\(__v\) \? __v\.join/)
157
+ })
158
+ })
@@ -0,0 +1,90 @@
1
+ /**
2
+ * `isOpaqueLocalAccessorName` (#3144) — the shared "is this a component-body
3
+ * local bound to an opaque call" check every non-JS-runtime adapter's
4
+ * `call()` fallback consults before treating a zero-arg identifier call as
5
+ * a signal getter. See `opaque-local-accessor.ts`'s docstring.
6
+ *
7
+ * The real end-to-end behavior (the refusal firing, with the right BF101
8
+ * diagnostic, through each adapter's actual `call()` method) is covered by
9
+ * the `opaque-local-accessor-call` corpus fixture's `conformancePins`
10
+ * entries, exercised against real PHP/Blade, Ruby/ERB, Python/Jinja2,
11
+ * Rust/minijinja, Perl/Mojolicious, Java/Pebble, PHP/Twig, Perl/Xslate and
12
+ * Go rendering. This file pins the pure classification function itself,
13
+ * built from the real analyzer's `ConstantInfo[]` (via `compileJSX`) rather
14
+ * than hand-built fixtures, so the shape this function actually receives in
15
+ * production is what gets tested.
16
+ */
17
+
18
+ import { describe, test, expect } from 'bun:test'
19
+ import { compileJSX } from '../compiler.ts'
20
+ import { TestAdapter } from '../adapters/test-adapter.ts'
21
+ import { isOpaqueLocalAccessorName } from '../opaque-local-accessor.ts'
22
+ import type { ComponentIR } from '../types.ts'
23
+
24
+ function localConstantsFor(source: string, filePath: string, componentName: string): ComponentIR['metadata']['localConstants'] {
25
+ const result = compileJSX(source, filePath, { adapter: new TestAdapter(), outputIR: true })
26
+ expect(result.errors).toEqual([])
27
+ const irFile = result.files.find(
28
+ (f) => f.type === 'ir' && f.path.includes(`.${componentName}.ir.json`),
29
+ ) ?? result.files.find((f) => f.type === 'ir')
30
+ expect(irFile).toBeDefined()
31
+ const ir = JSON.parse(irFile!.content) as ComponentIR
32
+ return ir.metadata.localConstants
33
+ }
34
+
35
+ describe('isOpaqueLocalAccessorName', () => {
36
+ test('true for a component-body const bound to an opaque call', () => {
37
+ const localConstants = localConstantsFor(
38
+ `
39
+ 'use client'
40
+ import { createSignal } from '@barefootjs/client'
41
+ function makeLabel() { return () => 'ready' }
42
+ export function Widget() {
43
+ const [count] = createSignal(0)
44
+ const label = makeLabel()
45
+ return <div><span>{label()}</span><button>{count()}</button></div>
46
+ }
47
+ `,
48
+ 'Widget.tsx',
49
+ 'Widget',
50
+ )
51
+ expect(isOpaqueLocalAccessorName('label', localConstants)).toBe(true)
52
+ })
53
+
54
+ test('false for a plain literal-valued local', () => {
55
+ const localConstants = localConstantsFor(
56
+ `
57
+ export function Widget({ label }: { label: string }) {
58
+ const greeting = 'hi'
59
+ return <div>{greeting}: {label}</div>
60
+ }
61
+ `,
62
+ 'Widget.tsx',
63
+ 'Widget',
64
+ )
65
+ expect(isOpaqueLocalAccessorName('greeting', localConstants)).toBe(false)
66
+ })
67
+
68
+ test('false for a name that names no local constant at all (e.g. a real signal getter)', () => {
69
+ expect(isOpaqueLocalAccessorName('count', [])).toBe(false)
70
+ expect(isOpaqueLocalAccessorName('count', undefined)).toBe(false)
71
+ })
72
+
73
+ test('false for a MODULE-scope const bound to an opaque call (a different, already-handled shape)', () => {
74
+ const localConstants = localConstantsFor(
75
+ `
76
+ 'use client'
77
+ import { createSignal } from '@barefootjs/client'
78
+ function makeLabel() { return () => 'ready' }
79
+ const label = makeLabel()
80
+ export function Widget() {
81
+ const [count] = createSignal(0)
82
+ return <div><span>{label()}</span><button>{count()}</button></div>
83
+ }
84
+ `,
85
+ 'Widget.tsx',
86
+ 'Widget',
87
+ )
88
+ expect(isOpaqueLocalAccessorName('label', localConstants)).toBe(false)
89
+ })
90
+ })
@@ -9,8 +9,10 @@
9
9
  * `Cart#handler:s1:click`). It then joins to the real IR graph (SR4) and runs
10
10
  * the v1 analyses, asserting the end-to-end story holds:
11
11
  *
12
- * an unbatched 3-write click re-runs `total` 5×/turn; a `batch()` would
13
- * collapse 14 effect runs to 5.
12
+ * an unbatched 3-write click re-runs `total` 3×/turn (once per write —
13
+ * glitch-free propagation recomputes it once even though it reads
14
+ * `subtotal` both directly and through `tax`); a `batch()` would collapse
15
+ * 12 effect runs to 5.
14
16
  *
15
17
  * This is the executable proof of the pipeline until the `--scenario` run
16
18
  * driver lands; it also pins the mount-vs-interaction metric split surfaced by
@@ -76,11 +78,12 @@ describe('profiler end-to-end (real substrate)', () => {
76
78
  expect(total).toBeDefined()
77
79
  expect(total.kind).toBe('memo')
78
80
  expect(total.loc!.file).toBe('Cart.tsx')
79
- // The unbatched 3-write turn re-runs `total` 5× — and mount (1 run) is
80
- // excluded, so the per-turn figure is 5.0, not the diluted 3.0.
81
+ // The unbatched 3-write turn re-runs `total` 3× (once per write) — and
82
+ // mount (1 run) is excluded, so the per-turn figure is 3.0, not the
83
+ // diluted 2.0.
81
84
  expect(total.mountRuns).toBe(1)
82
85
  expect(total.turns).toBe(1)
83
- expect(total.runsPerTurn).toBe(5)
86
+ expect(total.runsPerTurn).toBe(3)
84
87
  expect(total.hot).toBe(true)
85
88
 
86
89
  // Every hot subscriber resolved to a real source line.
@@ -94,11 +97,13 @@ describe('profiler end-to-end (real substrate)', () => {
94
97
  const batch = analyzeBatchAdvisor(events)
95
98
  const cand = batch.candidates.find(c => c.turn === 'Cart#handler:s1:click')!
96
99
  expect(cand).toBeDefined()
97
- // 3 unbatched writes cascade the memo chain: 14 effect runs, 5 distinct.
100
+ // 3 unbatched writes cascade the memo chain: qty and price each re-run
101
+ // subtotal, tax, total and both effects (5 each), coupon re-runs total and
102
+ // its effect (2) — 12 runs over 5 distinct subscribers.
98
103
  expect(cand.writes).toBe(3)
99
- expect(cand.totalRuns).toBe(14)
104
+ expect(cand.totalRuns).toBe(12)
100
105
  expect(cand.distinctSubscribers).toBe(5)
101
- expect(cand.savings).toBe(9)
106
+ expect(cand.savings).toBe(7)
102
107
  expect(cand.safety).toBe('unverified')
103
108
  })
104
109
  })