@barefootjs/jsx 0.35.3 → 0.35.5
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.
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/debug.d.ts +33 -3
- package/dist/debug.d.ts.map +1 -1
- package/dist/index.js +267 -73
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/compute-inlinability.d.ts +8 -0
- package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
- package/dist/ir-to-client-js/generate-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +2 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/ir-to-client-js/types.d.ts +25 -0
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +12 -1
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/types.d.ts +31 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/debug.test.ts +246 -0
- package/src/__tests__/issue-2897-static-loop-conditional.test.ts +340 -0
- package/src/__tests__/issue-2910-mutated-const-not-inlined.test.ts +118 -0
- package/src/__tests__/issue-2910-own-scope-id-emission.test.ts +64 -0
- package/src/__tests__/lazy-conditional.test.ts +1 -0
- package/src/analyzer.ts +157 -22
- package/src/debug.ts +214 -84
- package/src/ir-to-client-js/collect-elements.ts +83 -3
- package/src/ir-to-client-js/compute-inlinability.ts +8 -1
- package/src/ir-to-client-js/emit-registration.ts +2 -2
- package/src/ir-to-client-js/generate-init.ts +16 -2
- package/src/ir-to-client-js/imports.ts +6 -0
- package/src/ir-to-client-js/index.ts +6 -0
- package/src/ir-to-client-js/prop-handling.ts +9 -0
- package/src/ir-to-client-js/types.ts +37 -1
- package/src/ir-to-client-js/utils.ts +15 -1
- package/src/jsx-to-ir.ts +14 -0
- package/src/types.ts +31 -0
|
@@ -1400,3 +1400,249 @@ describe('formatComponentSummary', () => {
|
|
|
1400
1400
|
expect(output).toContain('dynamic text bindings:')
|
|
1401
1401
|
})
|
|
1402
1402
|
})
|
|
1403
|
+
|
|
1404
|
+
describe('prop reads are tracked deps (#2903)', () => {
|
|
1405
|
+
// The issue's own repro: `props.value` read in an attribute AND a
|
|
1406
|
+
// conditional. The IR marks both `reactive: true`; the graph must not
|
|
1407
|
+
// print `(no tracked deps)` for a read the compiler itself proved reactive.
|
|
1408
|
+
const reproChildSource = `
|
|
1409
|
+
'use client'
|
|
1410
|
+
|
|
1411
|
+
export interface ReproChildProps {
|
|
1412
|
+
value: boolean
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
export function ReproChild(props: ReproChildProps) {
|
|
1416
|
+
return <button disabled={props.value}>{props.value ? 'busy' : 'idle'}</button>
|
|
1417
|
+
}
|
|
1418
|
+
`
|
|
1419
|
+
|
|
1420
|
+
test('props.value in attribute and conditional lists props.value as a dep', () => {
|
|
1421
|
+
const graph = buildComponentGraph(reproChildSource, 'ReproChild.tsx')
|
|
1422
|
+
const attr = graph.domBindings.find(d => d.label === 'disabled')
|
|
1423
|
+
const cond = graph.domBindings.find(d => d.type === 'conditional')
|
|
1424
|
+
expect(attr).toBeDefined()
|
|
1425
|
+
expect(cond).toBeDefined()
|
|
1426
|
+
expect(attr!.classification).toBe('reactive')
|
|
1427
|
+
expect(cond!.classification).toBe('reactive')
|
|
1428
|
+
expect(attr!.deps).toEqual(['props.value'])
|
|
1429
|
+
expect(cond!.deps).toEqual(['props.value'])
|
|
1430
|
+
})
|
|
1431
|
+
|
|
1432
|
+
test('formatComponentGraph shows the prop as the source, never (no tracked deps)', () => {
|
|
1433
|
+
const output = formatComponentGraph(buildComponentGraph(reproChildSource, 'ReproChild.tsx'))
|
|
1434
|
+
expect(output).toContain('props.value -> <button disabled={props.value}>')
|
|
1435
|
+
expect(output).toContain('props.value -> {props.value ? ... : ...}')
|
|
1436
|
+
expect(output).not.toContain('(no tracked deps)')
|
|
1437
|
+
// Props get their own node section and dependency-graph edges, exactly
|
|
1438
|
+
// like signals and memos do.
|
|
1439
|
+
expect(output).toContain(' props:\n props.value')
|
|
1440
|
+
expect(output).toContain('dependency graph:')
|
|
1441
|
+
expect(output).toContain('props.value -> dom:disabled')
|
|
1442
|
+
expect(output).toContain('props.value -> dom:conditional "s0"')
|
|
1443
|
+
})
|
|
1444
|
+
|
|
1445
|
+
test('graph.props carries one node per read prop with consumers; JSON mirrors it', () => {
|
|
1446
|
+
const graph = buildComponentGraph(reproChildSource, 'ReproChild.tsx')
|
|
1447
|
+
expect(graph.props).toHaveLength(1)
|
|
1448
|
+
expect(graph.props[0].kind).toBe('prop')
|
|
1449
|
+
expect(graph.props[0].name).toBe('props.value')
|
|
1450
|
+
expect(graph.props[0].consumers).toEqual(['dom:disabled', 'dom:conditional "s0"'])
|
|
1451
|
+
const json = graphToJSON(graph) as { props: Array<{ name: string; consumers: string[] }> }
|
|
1452
|
+
expect(json.props).toEqual([{ name: 'props.value', consumers: ['dom:disabled', 'dom:conditional "s0"'] }])
|
|
1453
|
+
})
|
|
1454
|
+
|
|
1455
|
+
test('a reactive non-event binding never has an empty deps list for a prop read', () => {
|
|
1456
|
+
// The invariant the issue is really about: `classification: 'reactive'`
|
|
1457
|
+
// with `deps: []` is what rendered as `(no tracked deps)`.
|
|
1458
|
+
const graph = buildComponentGraph(reproChildSource, 'ReproChild.tsx')
|
|
1459
|
+
for (const d of graph.domBindings) {
|
|
1460
|
+
if (d.type === 'event') continue
|
|
1461
|
+
if (d.classification === 'reactive') expect(d.deps.length).toBeGreaterThan(0)
|
|
1462
|
+
}
|
|
1463
|
+
})
|
|
1464
|
+
|
|
1465
|
+
test('destructured props are deps by name in text, attribute, conditional and child prop', () => {
|
|
1466
|
+
const source = `
|
|
1467
|
+
'use client'
|
|
1468
|
+
import { Card } from './Card'
|
|
1469
|
+
|
|
1470
|
+
export function A({ title, count, children }: { title: string; count: number; children?: any }) {
|
|
1471
|
+
return (
|
|
1472
|
+
<div data-n={count}>
|
|
1473
|
+
<Card title={title} />
|
|
1474
|
+
<span>{title}</span>
|
|
1475
|
+
{count > 0 ? 'y' : 'n'}
|
|
1476
|
+
{children}
|
|
1477
|
+
</div>
|
|
1478
|
+
)
|
|
1479
|
+
}
|
|
1480
|
+
`
|
|
1481
|
+
const graph = buildComponentGraph(source, 'A.tsx')
|
|
1482
|
+
const byLabel = (l: string) => graph.domBindings.find(d => d.label === l)!
|
|
1483
|
+
expect(byLabel('data-n').deps).toEqual(['count'])
|
|
1484
|
+
expect(byLabel('Card.title').deps).toEqual(['title'])
|
|
1485
|
+
expect(graph.domBindings.find(d => d.type === 'text')!.deps).toEqual(['title'])
|
|
1486
|
+
expect(graph.domBindings.find(d => d.type === 'conditional')!.deps).toEqual(['count'])
|
|
1487
|
+
// `children` is server-rendered, never a reactive dep — and never a node.
|
|
1488
|
+
expect(graph.props.map(p => p.name).sort()).toEqual(['count', 'title'])
|
|
1489
|
+
expect(graph.domBindings.every(d => !d.deps.includes('children'))).toBe(true)
|
|
1490
|
+
})
|
|
1491
|
+
|
|
1492
|
+
test('a prop read through a prop-derived local const reports the underlying props, not the const', () => {
|
|
1493
|
+
// The #1863 Slot-composed-button shape: `class={classes}` where `classes`
|
|
1494
|
+
// captures `props.variant` / `props.className`. The const is not a reactive
|
|
1495
|
+
// source; the props it captures are.
|
|
1496
|
+
const source = `
|
|
1497
|
+
'use client'
|
|
1498
|
+
|
|
1499
|
+
export function B(props: { title: string; variant: 'a' | 'b'; className?: string }) {
|
|
1500
|
+
const label = props.title + '!'
|
|
1501
|
+
const classes = \`btn-\${props.variant} \${props.className ?? ''}\`
|
|
1502
|
+
return <div class={classes}>{label}</div>
|
|
1503
|
+
}
|
|
1504
|
+
`
|
|
1505
|
+
const graph = buildComponentGraph(source, 'B.tsx')
|
|
1506
|
+
const cls = graph.domBindings.find(d => d.label === 'class')!
|
|
1507
|
+
expect(cls.classification).toBe('reactive')
|
|
1508
|
+
expect(cls.deps).toEqual(['props.variant', 'props.className'])
|
|
1509
|
+
const text = graph.domBindings.find(d => d.type === 'text')!
|
|
1510
|
+
expect(text.deps).toEqual(['props.title'])
|
|
1511
|
+
expect(graph.domBindings.every(d => !d.deps.includes('classes') && !d.deps.includes('label'))).toBe(true)
|
|
1512
|
+
})
|
|
1513
|
+
|
|
1514
|
+
test('props.children, bare props, and props.x inside a string literal are not deps', () => {
|
|
1515
|
+
const source = `
|
|
1516
|
+
'use client'
|
|
1517
|
+
|
|
1518
|
+
export function C(props: { title: string; children?: any }) {
|
|
1519
|
+
return <div title="props.title" data-x={props}>{props.children}</div>
|
|
1520
|
+
}
|
|
1521
|
+
`
|
|
1522
|
+
const graph = buildComponentGraph(source, 'C.tsx')
|
|
1523
|
+
expect(graph.props).toHaveLength(0)
|
|
1524
|
+
for (const d of graph.domBindings) {
|
|
1525
|
+
expect(d.deps).toHaveLength(0)
|
|
1526
|
+
}
|
|
1527
|
+
})
|
|
1528
|
+
|
|
1529
|
+
test('signal, memo and prop deps coexist in one binding, in that order', () => {
|
|
1530
|
+
const source = `
|
|
1531
|
+
'use client'
|
|
1532
|
+
import { createSignal, createMemo } from '@barefootjs/client'
|
|
1533
|
+
|
|
1534
|
+
export function M(props: { base: number }) {
|
|
1535
|
+
const [n, setN] = createSignal(0)
|
|
1536
|
+
const total = createMemo(() => n() + props.base)
|
|
1537
|
+
return (
|
|
1538
|
+
<button onClick={() => setN(v => v + 1)} aria-label={\`\${props.base}/\${n()}\`}>
|
|
1539
|
+
{total()}
|
|
1540
|
+
</button>
|
|
1541
|
+
)
|
|
1542
|
+
}
|
|
1543
|
+
`
|
|
1544
|
+
const graph = buildComponentGraph(source, 'M.tsx')
|
|
1545
|
+
const aria = graph.domBindings.find(d => d.label === 'aria-label')!
|
|
1546
|
+
expect(aria.deps).toEqual(['n', 'props.base'])
|
|
1547
|
+
// The memo's own `props.base` read is memo metadata, not a DOM binding —
|
|
1548
|
+
// so the prop node's consumers are the DOM sites only.
|
|
1549
|
+
expect(graph.props).toEqual([{ kind: 'prop', name: 'props.base', consumers: ['dom:aria-label'] }])
|
|
1550
|
+
const output = formatComponentGraph(graph)
|
|
1551
|
+
expect(output).toContain('n, props.base -> <button aria-label=')
|
|
1552
|
+
expect(output).toContain('props.base -> dom:aria-label')
|
|
1553
|
+
})
|
|
1554
|
+
|
|
1555
|
+
test('buildWhyUpdate reports a prop dep with kind "prop"', () => {
|
|
1556
|
+
const result = buildWhyUpdate(reproChildSource, 'ReproChild.tsx', 'disabled')!
|
|
1557
|
+
expect(result).not.toBeNull()
|
|
1558
|
+
expect(result.deps).toEqual([{ name: 'props.value', kind: 'prop', dependsOn: [], changedBy: [] }])
|
|
1559
|
+
const output = formatWhyUpdate(result)
|
|
1560
|
+
expect(output).toContain('disabled updates because:')
|
|
1561
|
+
expect(output).toContain('props.value changes from:')
|
|
1562
|
+
expect(output).toContain('(prop — the parent component passes a new value)')
|
|
1563
|
+
})
|
|
1564
|
+
|
|
1565
|
+
test('traceUpdatePath accepts a prop name and lists its DOM consumers', () => {
|
|
1566
|
+
const graph = buildComponentGraph(reproChildSource, 'ReproChild.tsx')
|
|
1567
|
+
const path = traceUpdatePath(graph, 'props.value')
|
|
1568
|
+
expect(path).not.toBeNull()
|
|
1569
|
+
expect(path!.kind).toBe('prop')
|
|
1570
|
+
expect(path!.dependents.map(d => d.name)).toEqual(['disabled', 'conditional "s0"'])
|
|
1571
|
+
expect(formatUpdatePath(path!)).toContain('props.value (prop)')
|
|
1572
|
+
})
|
|
1573
|
+
|
|
1574
|
+
test('a prop shadowed by a .map() row binding of the same name is not a prop dep', () => {
|
|
1575
|
+
// `item` inside the row is the loop item, not the outer prop — the
|
|
1576
|
+
// shadow guard (`BindingScope.isBound`) keeps it out of `deps` / `props`.
|
|
1577
|
+
const source = `
|
|
1578
|
+
'use client'
|
|
1579
|
+
|
|
1580
|
+
export function S({ item, rows }: { item: string; rows: string[] }) {
|
|
1581
|
+
return (
|
|
1582
|
+
<div>
|
|
1583
|
+
<span>{item}</span>
|
|
1584
|
+
<ul>{rows.map(item => <li title={item}>{item}</li>)}</ul>
|
|
1585
|
+
</div>
|
|
1586
|
+
)
|
|
1587
|
+
}
|
|
1588
|
+
`
|
|
1589
|
+
const graph = buildComponentGraph(source, 'S.tsx')
|
|
1590
|
+
// (`rows` feeds the loop's array, which is a loop binding, not a prop
|
|
1591
|
+
// dep — loop-array sources are outside this fix.)
|
|
1592
|
+
expect(graph.props.map(p => p.name)).toEqual(['item'])
|
|
1593
|
+
const outer = graph.props[0]
|
|
1594
|
+
// Only the outer `<span>{item}</span>` consumes the prop.
|
|
1595
|
+
expect(outer.consumers).toHaveLength(1)
|
|
1596
|
+
expect(outer.consumers[0]).toMatch(/^dom:text/)
|
|
1597
|
+
const rowTitle = graph.domBindings.find(d => d.label === 'title')!
|
|
1598
|
+
expect(rowTitle.classification).toBe('reactive')
|
|
1599
|
+
expect(rowTitle.deps).not.toContain('item')
|
|
1600
|
+
})
|
|
1601
|
+
|
|
1602
|
+
test('a .map() row param literally named like the props object shadows props.x reads', () => {
|
|
1603
|
+
// The props-object branch of `propDepsOf` needs the same shadow guard as
|
|
1604
|
+
// the destructured / const branches: inside `rows.map(props => …)` the
|
|
1605
|
+
// name `props` is the row item, so `props.label` is not an outer-prop
|
|
1606
|
+
// read — neither as a text binding nor as a forwarded child prop.
|
|
1607
|
+
const source = `
|
|
1608
|
+
'use client'
|
|
1609
|
+
import { Card } from './Card'
|
|
1610
|
+
|
|
1611
|
+
export function S(props: { title: string; rows: Array<{ label: string }> }) {
|
|
1612
|
+
return (
|
|
1613
|
+
<div>
|
|
1614
|
+
<span>{props.title}</span>
|
|
1615
|
+
<ul>{props.rows.map(props => <li title={props.label}><Card label={props.label} />{props.label}</li>)}</ul>
|
|
1616
|
+
</div>
|
|
1617
|
+
)
|
|
1618
|
+
}
|
|
1619
|
+
`
|
|
1620
|
+
const graph = buildComponentGraph(source, 'S.tsx')
|
|
1621
|
+
expect(graph.props.map(p => p.name)).toEqual(['props.title'])
|
|
1622
|
+
expect(graph.props[0].consumers).toHaveLength(1)
|
|
1623
|
+
for (const d of graph.domBindings) {
|
|
1624
|
+
expect(d.deps).not.toContain('props.label')
|
|
1625
|
+
}
|
|
1626
|
+
})
|
|
1627
|
+
|
|
1628
|
+
test('a fallback binding with no proven source still reads (no tracked deps)', () => {
|
|
1629
|
+
// Guard the other direction: the fix must not invent a prop dep for an
|
|
1630
|
+
// opaque call the analyzer genuinely cannot see through.
|
|
1631
|
+
const source = `
|
|
1632
|
+
'use client'
|
|
1633
|
+
import { createSignal } from '@barefootjs/client'
|
|
1634
|
+
import { pick } from './pick'
|
|
1635
|
+
|
|
1636
|
+
export function F(props: { id: string }) {
|
|
1637
|
+
const [, setFoo] = createSignal(0)
|
|
1638
|
+
return <div onClick={() => setFoo(1)} title={pick()}>x</div>
|
|
1639
|
+
}
|
|
1640
|
+
`
|
|
1641
|
+
const graph = buildComponentGraph(source, 'F.tsx')
|
|
1642
|
+
const title = graph.domBindings.find(d => d.label === 'title')!
|
|
1643
|
+
expect(title.classification).toBe('fallback')
|
|
1644
|
+
expect(title.deps).toEqual([])
|
|
1645
|
+
expect(graph.props).toHaveLength(0)
|
|
1646
|
+
expect(formatComponentGraph(graph)).toContain('~ <div title={pick()}> (no tracked deps)')
|
|
1647
|
+
})
|
|
1648
|
+
})
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #2897 — a conditional (`{cond ? A : B}`) inside a STATIC (non-signal)
|
|
3
|
+
* array's `.map()` row was never wired reactively: neither the top-level
|
|
4
|
+
* static forEach bake nor the depth-1 `inner-loop-nested` clone-and-wire
|
|
5
|
+
* architecture (#2798) has any conditional-handling machinery, so the
|
|
6
|
+
* condition's INITIAL value baked into the per-row template at compile
|
|
7
|
+
* time and never updated again.
|
|
8
|
+
*
|
|
9
|
+
* `elem.bodyIsItemConditional` (#1665) already carved out ONE narrow shape
|
|
10
|
+
* of this — the row's entire body is a single conditional with at least one
|
|
11
|
+
* empty branch — by routing it through the plain dynamic path regardless of
|
|
12
|
+
* `isStaticArray`. This fix widens the same idea: a derived client-side
|
|
13
|
+
* `isStaticArray` (`collect-elements.ts`) goes false whenever a conditional
|
|
14
|
+
* is present anywhere in the row tree (this row's own bindings, or any
|
|
15
|
+
* depth-1 inner loop's), falling through to the plain/composite dynamic
|
|
16
|
+
* path — which already wires a conditional's live branch-swap correctly on
|
|
17
|
+
* a signal-backed array — instead of the static fast path that has no
|
|
18
|
+
* conditional support to add.
|
|
19
|
+
*
|
|
20
|
+
* Only ROUTING/EMISSION is pinned here (compiler-unit speed); the actual
|
|
21
|
+
* reactive behavior (does the branch really swap after a click) is a
|
|
22
|
+
* `fixture-hydrate.playwright.ts` fixture, since CSR conformance only
|
|
23
|
+
* observes the initial mount — which already matches SSR's baked branch
|
|
24
|
+
* either way, so it can't see this bug.
|
|
25
|
+
*/
|
|
26
|
+
import { describe, test, expect } from 'bun:test'
|
|
27
|
+
import { compileJSX } from '../compiler'
|
|
28
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
29
|
+
|
|
30
|
+
const adapter = new TestAdapter()
|
|
31
|
+
|
|
32
|
+
function compile(source: string): string {
|
|
33
|
+
const result = compileJSX(source.trimStart(), 'C.tsx', { adapter })
|
|
34
|
+
expect(result.errors).toHaveLength(0)
|
|
35
|
+
return result.files.find((f) => f.type === 'clientJs')!.content
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const usesStaticForEach = (js: string) => /\.forEach\(\(?\w+, __idx\)? =>/.test(js)
|
|
39
|
+
const usesReconciliation = (js: string) => js.includes('mapArray(') || js.includes('mapArrayLazy(') || js.includes('mapArrayAnchored(')
|
|
40
|
+
|
|
41
|
+
describe('#2897 — top-level: static array with a conditional alongside static siblings', () => {
|
|
42
|
+
const source = `
|
|
43
|
+
'use client'
|
|
44
|
+
import { createSignal } from '@barefootjs/client'
|
|
45
|
+
export function C() {
|
|
46
|
+
const items = [{ id: 1, label: 'Alpha' }]
|
|
47
|
+
const [flag] = createSignal(true)
|
|
48
|
+
return (
|
|
49
|
+
<ul>
|
|
50
|
+
{items.map(item => (
|
|
51
|
+
<li key={item.id}>
|
|
52
|
+
<span>{item.label}</span>
|
|
53
|
+
{flag() ? <b>Yes</b> : <i>No</i>}
|
|
54
|
+
</li>
|
|
55
|
+
))}
|
|
56
|
+
</ul>
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
`
|
|
60
|
+
|
|
61
|
+
test('routes through reconciliation (mapArray family), not the static forEach bake', () => {
|
|
62
|
+
const js = compile(source)
|
|
63
|
+
expect(usesReconciliation(js)).toBe(true)
|
|
64
|
+
expect(usesStaticForEach(js)).toBe(false)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('createRow/applyOuter read flag() live, not just the SSR-matching bootstrap template', () => {
|
|
68
|
+
const js = compile(source)
|
|
69
|
+
// The `hydrate(...).template` bootstrap legitimately bakes the signal's
|
|
70
|
+
// INITIAL value (it only needs to match SSR's first render — the same
|
|
71
|
+
// shape the dynamic-array control produces, see the module docstring).
|
|
72
|
+
// What must NOT happen is EVERY occurrence being baked: `applyOuter`'s
|
|
73
|
+
// own re-evaluation (`flag()` called bare, to track the dependency) is
|
|
74
|
+
// the actual regression pin — pre-fix, this function never existed at
|
|
75
|
+
// all for a static array.
|
|
76
|
+
expect(js).toContain('applyOuter')
|
|
77
|
+
expect(js).toMatch(/applyOuter:\s*\(__es, __seed\) => {\s*flag\(\)/)
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
describe('#2897 — top-level: static array, whole-row ternary between two elements (both arms render)', () => {
|
|
82
|
+
// `bodyIsItemConditional` (#1665) only carves out a WHOLE-row conditional
|
|
83
|
+
// with at least one EMPTY branch — a ternary where BOTH arms render an
|
|
84
|
+
// element evades that narrower gate too, and needs the same widened fix.
|
|
85
|
+
const source = `
|
|
86
|
+
'use client'
|
|
87
|
+
import { createSignal } from '@barefootjs/client'
|
|
88
|
+
export function C() {
|
|
89
|
+
const items = [{ id: 1 }]
|
|
90
|
+
const [flag] = createSignal(true)
|
|
91
|
+
return (
|
|
92
|
+
<ul>
|
|
93
|
+
{items.map(item => flag() ? <li key={item.id}>Active</li> : <li key={item.id}>Inactive</li>)}
|
|
94
|
+
</ul>
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
`
|
|
98
|
+
|
|
99
|
+
test('routes through reconciliation, not the static forEach bake', () => {
|
|
100
|
+
const js = compile(source)
|
|
101
|
+
expect(usesReconciliation(js)).toBe(true)
|
|
102
|
+
expect(usesStaticForEach(js)).toBe(false)
|
|
103
|
+
})
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
describe('#2897 — nested: static outer array + depth-1 inner loop whose row has a conditional', () => {
|
|
107
|
+
const source = `
|
|
108
|
+
'use client'
|
|
109
|
+
import { createSignal } from '@barefootjs/client'
|
|
110
|
+
export function C() {
|
|
111
|
+
const items = [{ id: 1, children: [{ id: 11 }] }]
|
|
112
|
+
const [flag] = createSignal(true)
|
|
113
|
+
return (
|
|
114
|
+
<ul>
|
|
115
|
+
{items.map(item => (
|
|
116
|
+
<li key={item.id}>
|
|
117
|
+
{item.children.map(child => (
|
|
118
|
+
<span key={child.id}>{flag() ? <b>Yes</b> : <i>No</i>}</span>
|
|
119
|
+
))}
|
|
120
|
+
</li>
|
|
121
|
+
))}
|
|
122
|
+
</ul>
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
`
|
|
126
|
+
|
|
127
|
+
test('escalates to composite element reconciliation for BOTH loops, not the #2798 static clone-and-wire path', () => {
|
|
128
|
+
const js = compile(source)
|
|
129
|
+
expect(usesReconciliation(js)).toBe(true)
|
|
130
|
+
expect(usesStaticForEach(js)).toBe(false)
|
|
131
|
+
// The #2798 static-nested machinery's own signature (a plain
|
|
132
|
+
// `_sN.children[__idx]` walk feeding `createEffect`, no `insert()`) must
|
|
133
|
+
// not appear alongside the escalated path.
|
|
134
|
+
expect(js).not.toContain('Initialize inner-loop components in static array')
|
|
135
|
+
})
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
describe('#2897 follow-up — static array, own-row conditional escalates the row, sibling inner loop has a ref binding', () => {
|
|
139
|
+
// Pullfrog review on #2899: an own-row conditional escalates
|
|
140
|
+
// `clientIsStaticArray` to false regardless of any inner loop, but
|
|
141
|
+
// `useElementReconciliation` previously escalated ONLY on an
|
|
142
|
+
// inner-loop's OWN conditional (`staticInnerLoopHasConditional`). A row
|
|
143
|
+
// whose OWN conditional escalates it, with a SIBLING inner loop that has
|
|
144
|
+
// no conditional of its own but DOES have a ref/reactive-attr/reactive-text
|
|
145
|
+
// binding, fell through to `buildPlainLoopPlan` — which never reads
|
|
146
|
+
// `TopLevelLoop.innerLoops` at all — silently dropping that inner loop's
|
|
147
|
+
// binding with no compile error. Confirmed by compiling this exact shape
|
|
148
|
+
// pre-fix: the `ref` callback never appeared anywhere in the emitted JS.
|
|
149
|
+
const source = `
|
|
150
|
+
'use client'
|
|
151
|
+
import { createSignal } from '@barefootjs/client'
|
|
152
|
+
export function C() {
|
|
153
|
+
const items = [{ id: 1, children: [{ id: 11 }] }]
|
|
154
|
+
const [flag] = createSignal(true)
|
|
155
|
+
return (
|
|
156
|
+
<ul>
|
|
157
|
+
{items.map(item => (
|
|
158
|
+
<li key={item.id}>
|
|
159
|
+
{flag() ? <b>Yes</b> : <i>No</i>}
|
|
160
|
+
{item.children.map(child => (
|
|
161
|
+
<span key={child.id} ref={(el) => console.log(el, child.id)}>{child.id}</span>
|
|
162
|
+
))}
|
|
163
|
+
</li>
|
|
164
|
+
))}
|
|
165
|
+
</ul>
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
`
|
|
169
|
+
|
|
170
|
+
test('escalates to composite element reconciliation so the inner loop\'s ref binding is not dropped', () => {
|
|
171
|
+
const js = compile(source)
|
|
172
|
+
expect(usesReconciliation(js)).toBe(true)
|
|
173
|
+
expect(usesStaticForEach(js)).toBe(false)
|
|
174
|
+
// The regression itself: the ref callback's body must appear somewhere
|
|
175
|
+
// in the emitted JS, wired to the inner loop's row element.
|
|
176
|
+
expect(js).toContain('console.log(el')
|
|
177
|
+
})
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
describe('#2897 follow-up — static array, own-row conditional reading ONLY the loop item, child-component branch', () => {
|
|
181
|
+
// `SocialShell` (site/ui) shape: `item.key === 'messages'` is a
|
|
182
|
+
// `classifyReactivity` `'loop-param'` verdict (every loop-param-referencing
|
|
183
|
+
// condition is, regardless of array static-ness — see `reactiveBeyondLoopScope`'s
|
|
184
|
+
// doc comment on `LoopChildConditional`), so `bindings.conditionals` is
|
|
185
|
+
// non-empty here too. For a STATIC array the row item never changes after
|
|
186
|
+
// first render, so this decision is exactly as compile-time-bakeable as a
|
|
187
|
+
// bare-element conditional — escalating it is unnecessary and (via the
|
|
188
|
+
// composite/`insert()` path) previously double-mounted the child component.
|
|
189
|
+
const source = `
|
|
190
|
+
'use client'
|
|
191
|
+
import { createSignal } from '@barefootjs/client'
|
|
192
|
+
function Badge() { return <span className="badge">B</span> }
|
|
193
|
+
export function C() {
|
|
194
|
+
const items = [{ id: 1, key: 'other' }, { id: 2, key: 'messages' }]
|
|
195
|
+
return (
|
|
196
|
+
<ul>
|
|
197
|
+
{items.map(item => (
|
|
198
|
+
<li key={item.id}>
|
|
199
|
+
{item.key === 'messages' ? <Badge /> : null}
|
|
200
|
+
</li>
|
|
201
|
+
))}
|
|
202
|
+
</ul>
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
`
|
|
206
|
+
|
|
207
|
+
test('stays on the static forEach + initChild path, no reconciliation', () => {
|
|
208
|
+
const js = compile(source)
|
|
209
|
+
expect(usesStaticForEach(js)).toBe(true)
|
|
210
|
+
expect(usesReconciliation(js)).toBe(false)
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
describe('#2897 follow-up — static array, own-row conditional whose CONDITION reads a signal, child-component branch', () => {
|
|
215
|
+
// Same branch shape as above, but the condition itself (`flag()`) can
|
|
216
|
+
// change after first render — this is the case #2897 actually fixes, and
|
|
217
|
+
// must still escalate regardless of what the branches contain.
|
|
218
|
+
const source = `
|
|
219
|
+
'use client'
|
|
220
|
+
import { createSignal } from '@barefootjs/client'
|
|
221
|
+
function Badge() { return <span className="badge">B</span> }
|
|
222
|
+
export function C() {
|
|
223
|
+
const items = [{ id: 1 }]
|
|
224
|
+
const [flag] = createSignal(true)
|
|
225
|
+
return (
|
|
226
|
+
<ul>
|
|
227
|
+
{items.map(item => (
|
|
228
|
+
<li key={item.id}>
|
|
229
|
+
{flag() ? <Badge /> : null}
|
|
230
|
+
</li>
|
|
231
|
+
))}
|
|
232
|
+
</ul>
|
|
233
|
+
)
|
|
234
|
+
}
|
|
235
|
+
`
|
|
236
|
+
|
|
237
|
+
test('routes through reconciliation, not the static forEach bake', () => {
|
|
238
|
+
const js = compile(source)
|
|
239
|
+
expect(usesReconciliation(js)).toBe(true)
|
|
240
|
+
expect(usesStaticForEach(js)).toBe(false)
|
|
241
|
+
})
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
describe('#2897 follow-up — static array, own-row condition mixing an item read and a signal read', () => {
|
|
245
|
+
// `classifyReactivity` reports `'loop-param'` (not `'signal-or-memo-or-prop'`)
|
|
246
|
+
// for `item.x && flag()` because it checks loop-bound names FIRST — that
|
|
247
|
+
// precedence is deliberate and must not change (two other, typed callers
|
|
248
|
+
// depend on it). `reactiveBeyondLoopScope` must still escalate this,
|
|
249
|
+
// independently of that `kind`, since the value CAN change after first
|
|
250
|
+
// render (`flag()`).
|
|
251
|
+
const source = `
|
|
252
|
+
'use client'
|
|
253
|
+
import { createSignal } from '@barefootjs/client'
|
|
254
|
+
export function C() {
|
|
255
|
+
const items = [{ id: 1, x: true }]
|
|
256
|
+
const [flag] = createSignal(true)
|
|
257
|
+
return (
|
|
258
|
+
<ul>
|
|
259
|
+
{items.map(item => (
|
|
260
|
+
<li key={item.id}>
|
|
261
|
+
{item.x && flag() ? <b>Yes</b> : <i>No</i>}
|
|
262
|
+
</li>
|
|
263
|
+
))}
|
|
264
|
+
</ul>
|
|
265
|
+
)
|
|
266
|
+
}
|
|
267
|
+
`
|
|
268
|
+
|
|
269
|
+
test('routes through reconciliation, not the static forEach bake', () => {
|
|
270
|
+
const js = compile(source)
|
|
271
|
+
expect(usesReconciliation(js)).toBe(true)
|
|
272
|
+
expect(usesStaticForEach(js)).toBe(false)
|
|
273
|
+
})
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
describe('#2897 follow-up — static array, own-row conditional reading ONLY the loop item, bare-element branches', () => {
|
|
277
|
+
// The item-only-condition control for a BARE-element branch (as opposed
|
|
278
|
+
// to the child-component branch above) — both must stay static; content
|
|
279
|
+
// of the branches plays no part in the escalation decision, only whether
|
|
280
|
+
// the condition itself can change. `ref` gives the row #2798's own
|
|
281
|
+
// static-forEach wiring to check against (a fully inert row — no
|
|
282
|
+
// ref/text/attr/childComponent needing a runtime touch at all — emits NO
|
|
283
|
+
// loop wiring in the first place, which would make `usesStaticForEach`
|
|
284
|
+
// vacuously false here regardless of this fix; a click handler alone
|
|
285
|
+
// doesn't count either — events use container-level delegation, not
|
|
286
|
+
// per-row forEach).
|
|
287
|
+
const source = `
|
|
288
|
+
'use client'
|
|
289
|
+
export function C() {
|
|
290
|
+
const items = [{ id: 1, done: true }, { id: 2, done: false }]
|
|
291
|
+
return (
|
|
292
|
+
<ul>
|
|
293
|
+
{items.map(item => (
|
|
294
|
+
<li key={item.id} ref={(el) => console.log(el, item.id)}>
|
|
295
|
+
{item.done ? <b>Yes</b> : <i>No</i>}
|
|
296
|
+
</li>
|
|
297
|
+
))}
|
|
298
|
+
</ul>
|
|
299
|
+
)
|
|
300
|
+
}
|
|
301
|
+
`
|
|
302
|
+
|
|
303
|
+
test('stays on the static forEach + createEffect path, no reconciliation', () => {
|
|
304
|
+
const js = compile(source)
|
|
305
|
+
expect(usesStaticForEach(js)).toBe(true)
|
|
306
|
+
expect(usesReconciliation(js)).toBe(false)
|
|
307
|
+
})
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
describe('#2897 — control: static outer + depth-1 inner loop with NO conditional stays on the #2798 fast path', () => {
|
|
311
|
+
// Regression armor: the escalation must be conditional-triggered only —
|
|
312
|
+
// a nested static loop with ordinary ref/text/attr bindings and no
|
|
313
|
+
// conditional anywhere must keep taking #2798's static clone-and-wire
|
|
314
|
+
// path, not silently lose that optimization.
|
|
315
|
+
const source = `
|
|
316
|
+
'use client'
|
|
317
|
+
import { createSignal } from '@barefootjs/client'
|
|
318
|
+
export function C() {
|
|
319
|
+
const items = [{ id: 1, children: [{ id: 11 }] }]
|
|
320
|
+
const [count] = createSignal(0)
|
|
321
|
+
return (
|
|
322
|
+
<ul>
|
|
323
|
+
{items.map(item => (
|
|
324
|
+
<li key={item.id}>
|
|
325
|
+
{item.children.map(child => (
|
|
326
|
+
<span key={child.id}>{child.id}:{count()}</span>
|
|
327
|
+
))}
|
|
328
|
+
</li>
|
|
329
|
+
))}
|
|
330
|
+
</ul>
|
|
331
|
+
)
|
|
332
|
+
}
|
|
333
|
+
`
|
|
334
|
+
|
|
335
|
+
test('stays on the static forEach + createEffect path, no reconciliation', () => {
|
|
336
|
+
const js = compile(source)
|
|
337
|
+
expect(usesStaticForEach(js)).toBe(true)
|
|
338
|
+
expect(usesReconciliation(js)).toBe(false)
|
|
339
|
+
})
|
|
340
|
+
})
|