@barefootjs/jsx 0.41.1 → 0.41.3
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/adapters/attr-plan.d.ts +8 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +19 -0
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +2 -1
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/inherited-const-server-read.d.ts +37 -0
- package/dist/adapters/inherited-const-server-read.d.ts.map +1 -0
- package/dist/adapters/loop-row-type.d.ts +18 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -1
- package/dist/adapters/non-primitive-equality.d.ts +8 -2
- package/dist/adapters/non-primitive-equality.d.ts.map +1 -1
- package/dist/adapters/unapplied-prop-default.d.ts +34 -0
- package/dist/adapters/unapplied-prop-default.d.ts.map +1 -0
- package/dist/adapters/uninlinable-seed-call.d.ts +29 -0
- package/dist/adapters/uninlinable-seed-call.d.ts.map +1 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +4 -22
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/caller-value-reach.d.ts +57 -0
- package/dist/caller-value-reach.d.ts.map +1 -0
- package/dist/const-identity.d.ts +38 -0
- package/dist/const-identity.d.ts.map +1 -0
- package/dist/index.d.ts +10 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2685 -1381
- package/dist/ir-to-client-js/child-props-object.d.ts +39 -41
- package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -1
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/component-scope.d.ts +13 -2
- package/dist/ir-to-client-js/component-scope.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/local-call-body.d.ts +112 -0
- package/dist/local-call-body.d.ts.map +1 -0
- package/dist/nullable-signal-attr.d.ts +10 -0
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-default-resolution.d.ts +52 -0
- package/dist/prop-default-resolution.d.ts.map +1 -0
- package/dist/props-binding.d.ts +15 -0
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/server-read-walk.d.ts +66 -0
- package/dist/server-read-walk.d.ts.map +1 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +24 -1
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/static-const-member.d.ts +92 -0
- package/dist/static-const-member.d.ts.map +1 -0
- package/dist/static-literal.d.ts +21 -1
- package/dist/static-literal.d.ts.map +1 -1
- package/dist/types.d.ts +63 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/computed-const.test.ts +7 -1
- package/src/__tests__/const-identity-child-prop.test.ts +127 -0
- package/src/__tests__/inherited-const-server-read.test.ts +116 -0
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +8 -3
- package/src/__tests__/issue-3714-open-rest-spread-child.test.ts +188 -0
- package/src/__tests__/local-call-body.test.ts +101 -0
- package/src/__tests__/loop-branch-bare-expression-reactive-text.test.ts +212 -56
- package/src/__tests__/nested-loop-conditional.test.ts +16 -16
- package/src/__tests__/prop-default-resolution.test.ts +185 -0
- package/src/__tests__/props-spread-shadowed.test.ts +113 -0
- package/src/__tests__/server-read-walk.test.ts +125 -0
- package/src/__tests__/ssr-defaults.test.ts +67 -7
- package/src/__tests__/static-literal.test.ts +47 -2
- package/src/__tests__/static-record-literal-array.test.ts +82 -11
- package/src/adapters/attr-plan.ts +12 -1
- package/src/adapters/component-props.ts +2 -1
- package/src/adapters/computed-const.ts +108 -12
- package/src/adapters/dsl-template-adapter.ts +17 -4
- package/src/adapters/inherited-const-server-read.ts +174 -0
- package/src/adapters/loop-row-type.ts +69 -2
- package/src/adapters/non-primitive-equality.ts +17 -2
- package/src/adapters/unapplied-prop-default.ts +83 -0
- package/src/adapters/uninlinable-seed-call.ts +62 -0
- package/src/analyzer.ts +43 -3
- package/src/augment-inherited-props.ts +4 -49
- package/src/caller-value-reach.ts +436 -0
- package/src/compiler.ts +4 -4
- package/src/const-identity.ts +189 -0
- package/src/index.ts +10 -5
- package/src/ir-to-client-js/child-props-object.ts +94 -62
- package/src/ir-to-client-js/collect-elements.ts +53 -68
- package/src/ir-to-client-js/component-scope.ts +16 -2
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +1 -0
- package/src/ir-to-client-js/html-template.ts +24 -4
- package/src/ir-to-client-js/index.ts +2 -2
- package/src/ir-to-client-js/prop-handling.ts +4 -0
- package/src/jsx-to-ir.ts +40 -2
- package/src/local-call-body.ts +191 -0
- package/src/nullable-signal-attr.ts +39 -17
- package/src/prop-default-resolution.ts +195 -0
- package/src/props-binding.ts +16 -0
- package/src/server-read-walk.ts +174 -0
- package/src/ssr-defaults.ts +93 -10
- package/src/ssr-seed-plan.ts +104 -18
- package/src/static-const-member.ts +243 -0
- package/src/static-literal.ts +55 -1
- package/src/types.ts +63 -1
|
@@ -1,32 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Emission pin for the loop-branch
|
|
2
|
+
* Emission pin for the text effect of a loop-row conditional branch that is
|
|
3
|
+
* one bare expression (no wrapping element).
|
|
3
4
|
*
|
|
4
|
-
* `
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* when its condition is unchanged — branch-internal updates are the effect
|
|
12
|
-
* system's job).
|
|
5
|
+
* `insert()` (runtime/insert.ts) leaves a branch alone while its condition
|
|
6
|
+
* is unchanged, so a keyed row reused with a new item only rewrites such a
|
|
7
|
+
* branch through an effect inside the arm. `summarizeLoopChildBranch`
|
|
8
|
+
* (ir-to-client-js/collect-elements.ts) takes the branch's text slots from
|
|
9
|
+
* `collectLoopChildReactiveTexts`, the collector every other row text goes
|
|
10
|
+
* through, so a branch gets the effect when it reads the row or a reactive
|
|
11
|
+
* source, or contains a call.
|
|
13
12
|
*
|
|
14
|
-
*
|
|
15
|
-
* `
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
13
|
+
* A branch with a call in it used to be left out whatever it called
|
|
14
|
+
* (#3894): `cond ? fmt(row.wait) : '0s'` kept the value the row was built
|
|
15
|
+
* with, while the same call directly in the row, or in a conditional
|
|
16
|
+
* outside a loop, was re-evaluated. The one branch still left out is the
|
|
17
|
+
* one Phase 1 marks `IRExpression.readsCallerValue`: it reads something the
|
|
18
|
+
* caller supplied that may be or build a live element, which the row keeps
|
|
19
|
+
* (`caller-value-reach.ts`; `nested-loop-conditional.test.ts` and
|
|
20
|
+
* `packages/client/__tests__/runtime/loop-branch-live-element.test.ts`).
|
|
22
21
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* reference), so even with the collect-elements.ts skip narrowed there was
|
|
27
|
-
* nothing for the new effect — or the `<!--bf:sN-->` marker
|
|
28
|
-
* `irToHtmlTemplate` emits for it — to attach to. Both are exercised here
|
|
29
|
-
* together because neither half alone reproduces the fix.
|
|
22
|
+
* `transformConditionalBranch` (jsx-to-ir.ts) gives a bare branch reading
|
|
23
|
+
* the loop item its `slotId`, which the effect claims and which puts the
|
|
24
|
+
* `<!--bf:sN-->` marker in the template.
|
|
30
25
|
*/
|
|
31
26
|
import { describe, test, expect } from 'bun:test'
|
|
32
27
|
import { compileJSX } from '../compiler'
|
|
@@ -34,14 +29,33 @@ import { TestAdapter } from '../adapters/test-adapter'
|
|
|
34
29
|
|
|
35
30
|
const adapter = new TestAdapter()
|
|
36
31
|
|
|
37
|
-
|
|
32
|
+
const PROPS_TYPE = `{
|
|
33
|
+
format?: (s: string) => string
|
|
34
|
+
label?: (s: string) => string | number | null
|
|
35
|
+
count: number
|
|
36
|
+
rows2: Row[]
|
|
37
|
+
render?: (row: Row) => unknown
|
|
38
|
+
cell?: (row: Row) => JSX.Element
|
|
39
|
+
untyped?: any
|
|
40
|
+
children?: (row: Row) => unknown
|
|
41
|
+
slots: { cell: (row: Row) => unknown }
|
|
42
|
+
cols: Col[]
|
|
43
|
+
}`
|
|
44
|
+
|
|
45
|
+
/** Compile a keyed loop whose row is `row.done ? <branchTrue> : 'pending'`. */
|
|
46
|
+
function clientJsFor(branchTrue: string, opts: { params?: string; body?: string } = {}): string {
|
|
38
47
|
const source = `
|
|
39
48
|
'use client'
|
|
40
49
|
import { createSignal } from '@barefootjs/client'
|
|
41
|
-
|
|
50
|
+
import { createMemo } from '@barefootjs/client'
|
|
42
51
|
type Row = { id: number; label: string; done: boolean }
|
|
43
|
-
|
|
52
|
+
interface Col { key: string; cell?: (row: Row) => unknown }
|
|
53
|
+
type Props = ${PROPS_TYPE}
|
|
54
|
+
const fmt = (s: string) => s.trim()
|
|
55
|
+
const apply = (f: any, ...args: any[]) => f(...args)
|
|
56
|
+
export function C(${opts.params ?? '_p: Props'}) {
|
|
44
57
|
const [rows] = createSignal<Row[]>([])
|
|
58
|
+
${opts.body ?? ''}
|
|
45
59
|
return <ul>{rows().map(row => <li key={row.id}>{row.done ? ${branchTrue} : 'pending'}</li>)}</ul>
|
|
46
60
|
}
|
|
47
61
|
`
|
|
@@ -51,48 +65,190 @@ function clientJsFor(branchTrue: string): string {
|
|
|
51
65
|
return result.files.find(f => f.type === 'clientJs')!.content
|
|
52
66
|
}
|
|
53
67
|
|
|
54
|
-
|
|
55
|
-
|
|
68
|
+
/** The arm's text effects, as the expression each one writes. */
|
|
69
|
+
function armEffects(js: string): string[] {
|
|
70
|
+
const out: string[] = []
|
|
71
|
+
const open = "createDisposableEffect(() => { __bfw_"
|
|
72
|
+
for (let at = js.indexOf(open); at !== -1; at = js.indexOf(open, at + 1)) {
|
|
73
|
+
const start = js.indexOf('escapeTextOrNode(', at) + 'escapeTextOrNode('.length
|
|
74
|
+
out.push(js.slice(start, js.indexOf(')) })', start)))
|
|
75
|
+
}
|
|
76
|
+
return out
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const ARM_WRITER = /lazySlots\(__branchScope, \[\{ id: 's\d+', kind: 'markup', path: \[\] \}\]\)/
|
|
80
|
+
|
|
81
|
+
describe('loop-row conditional: a bare-expression branch gets its own text effect', () => {
|
|
82
|
+
test('a bare property read', () => {
|
|
56
83
|
const js = clientJsFor('row.label')
|
|
57
|
-
expect(js).toContain("escapeTextOrNode(row().label)")
|
|
58
|
-
|
|
59
|
-
// `refsLoopParam` half of the fix, without which the effect above would
|
|
60
|
-
// have nothing to `lazySlots(...)` claim.
|
|
61
|
-
expect(js).toMatch(/lazySlots\(__branchScope, \[\{ id: 's\d+', kind: 'markup', path: \[\] \}\]\)/)
|
|
84
|
+
expect(js).toContain("createDisposableEffect(() => { __bfw_s1('s1', escapeTextOrNode(row().label)) })")
|
|
85
|
+
expect(js).toMatch(ARM_WRITER)
|
|
62
86
|
})
|
|
63
87
|
|
|
64
|
-
test('a template
|
|
88
|
+
test('a template literal built from property reads', () => {
|
|
65
89
|
const js = clientJsFor('`[${row.label}]`')
|
|
66
90
|
expect(js).toContain('escapeTextOrNode(`[${row().label}]`)')
|
|
67
91
|
})
|
|
68
92
|
|
|
69
|
-
test('a
|
|
70
|
-
const js = clientJsFor('
|
|
71
|
-
expect(js).
|
|
72
|
-
|
|
73
|
-
|
|
93
|
+
test('a call to a module-level function (#3894)', () => {
|
|
94
|
+
const js = clientJsFor('fmt(row.label)')
|
|
95
|
+
expect(js).toContain("createDisposableEffect(() => { __bfw_s1('s1', escapeTextOrNode(fmt(row().label))) })")
|
|
96
|
+
expect(js).toMatch(ARM_WRITER)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('a call nested inside a template literal', () => {
|
|
100
|
+
const js = clientJsFor('`${fmt(row.label)}!`')
|
|
101
|
+
expect(js).toContain('escapeTextOrNode(`${fmt(row().label)}!`)')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('a method call on the row value and a global call', () => {
|
|
105
|
+
expect(clientJsFor('row.label.toUpperCase()')).toContain('escapeTextOrNode(row().label.toUpperCase())')
|
|
106
|
+
expect(clientJsFor('String(row.id)')).toContain('escapeTextOrNode(String(row().id))')
|
|
74
107
|
})
|
|
75
108
|
|
|
76
|
-
test('a
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
109
|
+
test('a call to a callback prop declared to return only primitives', () => {
|
|
110
|
+
expect(armEffects(clientJsFor('_p.format(row.label)'))).toEqual(['_p.format(row().label)'])
|
|
111
|
+
expect(armEffects(clientJsFor('_p.label(row.label)'))).toEqual(['_p.label(row().label)'])
|
|
112
|
+
// A destructured prop, through its declared type too.
|
|
113
|
+
const destructured = clientJsFor('format(row.label)', { params: '{ format }: Props' })
|
|
114
|
+
expect(armEffects(destructured)).toHaveLength(1)
|
|
115
|
+
expect(armEffects(destructured)[0]).toContain('format(row().label)')
|
|
81
116
|
})
|
|
82
117
|
|
|
83
|
-
test('a
|
|
84
|
-
const js = clientJsFor("row.id > 0 ?
|
|
85
|
-
// The
|
|
86
|
-
//
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
// nested-ternary expression text.
|
|
91
|
-
expect(js).not.toContain('escapeTextOrNode(row.id > 0 ? _p.format(row().label) : row().label)')
|
|
118
|
+
test('a call in the leaf of a nested ternary binds inside the nested arm', () => {
|
|
119
|
+
const js = clientJsFor("row.id > 0 ? fmt(row.label) : row.label")
|
|
120
|
+
// The nested conditional has its own `insert()`; each of its leaves
|
|
121
|
+
// gets its own effect there, not one effect over the whole ternary.
|
|
122
|
+
expect(js).toContain('escapeTextOrNode(fmt(row().label))')
|
|
123
|
+
expect(js).toContain('escapeTextOrNode(row().label)')
|
|
124
|
+
expect(js).not.toContain('escapeTextOrNode(row().id > 0 ?')
|
|
92
125
|
})
|
|
93
126
|
|
|
94
|
-
test('
|
|
127
|
+
test('a literal branch (no row read, no call) gets no slot and no effect', () => {
|
|
95
128
|
const js = clientJsFor("'yes'")
|
|
96
129
|
expect(js).not.toContain("escapeTextOrNode('yes')")
|
|
130
|
+
expect(js).not.toMatch(ARM_WRITER)
|
|
131
|
+
})
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
type Case = [name: string, branch: string, opts?: { params?: string; body?: string }]
|
|
135
|
+
|
|
136
|
+
describe('loop-row conditional: a branch that can reach a caller-supplied element gets no text effect', () => {
|
|
137
|
+
const NO_EFFECT: Case[] = [
|
|
138
|
+
// The callee is the prop, in any spelling.
|
|
139
|
+
['a callback prop returning unknown', '_p.render(row)'],
|
|
140
|
+
['a callback prop returning an element', '_p.cell(row)'],
|
|
141
|
+
['a prop with no function type', '_p.untyped(row)'],
|
|
142
|
+
['children called as a function', '_p.children(row)'],
|
|
143
|
+
['a bracket access', "_p['render'](row)"],
|
|
144
|
+
['a computed bracket access', '_p[row.label](row)'],
|
|
145
|
+
['an optional call', '_p.render?.(row)'],
|
|
146
|
+
['a non-null assertion on the callee', '_p.render!(row)'],
|
|
147
|
+
['a parenthesized, cast call', '(_p.render(row) as string)'],
|
|
148
|
+
['`.call` on the prop', '_p.render.call(null, row)'],
|
|
149
|
+
['a deeper member', '_p.slots.cell(row)'],
|
|
150
|
+
['a member of a typed row object holding a callback', '_p.cols[0].cell(row)'],
|
|
151
|
+
// Anywhere in the branch, not only as its value.
|
|
152
|
+
['the value side of `&&`', 'row.id > 0 && _p.render(row)'],
|
|
153
|
+
['either side of `??`', '_p.render(row) ?? row.label'],
|
|
154
|
+
['inside a template literal', '`${_p.render(row)}!`'],
|
|
155
|
+
['as an operand', "_p.render(row) + ''"],
|
|
156
|
+
// The prop is handed to something else.
|
|
157
|
+
['a prop passed as an argument', 'apply(_p.render, row)'],
|
|
158
|
+
['the whole props object passed as an argument', 'apply(_p, row)'],
|
|
159
|
+
['a prop inside an argument', 'fmt(String(_p.render(row)))'],
|
|
160
|
+
// A component declaration that reads it.
|
|
161
|
+
['a local wrapper arrow', 'draw(row)', { body: 'const draw = (r: Row) => _p.render(r)' }],
|
|
162
|
+
['a local wrapper function declaration', 'draw(row)', { body: 'function draw(r: Row) { return _p.render(r) }' }],
|
|
163
|
+
['a wrapper calling a wrapper', 'outer(row)', { body: 'const inner = (r: Row) => _p.render(r)\n const outer = (r: Row) => inner(r)' }],
|
|
164
|
+
['a wrapper declared after its user', 'outer(row)', { body: 'const outer = (r: Row) => inner(r)\n const inner = (r: Row) => _p.render(r)' }],
|
|
165
|
+
['a callback stored in a const object', 'o.draw(row)', { body: 'const o = { draw: _p.render }' }],
|
|
166
|
+
['a const alias of a callback prop', 'draw(row)', { body: 'const draw = _p.render' }],
|
|
167
|
+
['a const alias of the props object', 'p.render(row)', { body: 'const p = _p' }],
|
|
168
|
+
['a body destructure of a callback prop', 'render(row)', { body: 'const { render } = _p' }],
|
|
169
|
+
['a nested pattern in a body destructure', 'cell(row)', { body: 'const { slots: { cell } } = _p' }],
|
|
170
|
+
['a memo holding the prop', 'draw()(row)', { body: 'const draw = createMemo(() => _p.render)' }],
|
|
171
|
+
['a signal seeded from the prop', 'draw()(row)', { body: 'const [draw] = createSignal(_p.render)' }],
|
|
172
|
+
['a `let`', 'draw(row)', { body: 'let draw = fmt' }],
|
|
173
|
+
// A destructured props parameter.
|
|
174
|
+
['a destructured callback prop', 'render(row)', { params: '{ render }: Props' }],
|
|
175
|
+
['a renamed destructured callback prop', 'draw(row)', { params: '{ render: draw }: Props' }],
|
|
176
|
+
['a rest bag', 'rest.render(row)', { params: '{ count, ...rest }: Props' }],
|
|
177
|
+
['a nested pattern in the parameter', 'cell(row)', { params: '{ slots: { cell } }: Props' }],
|
|
178
|
+
['a wrapper over a destructured callback prop', 'draw(row)', { params: '{ render }: Props', body: 'const draw = (r: Row) => render(r)' }],
|
|
179
|
+
]
|
|
180
|
+
for (const [name, branch, opts] of NO_EFFECT) {
|
|
181
|
+
test(name, () => {
|
|
182
|
+
expect(armEffects(clientJsFor(branch, opts))).toEqual([])
|
|
183
|
+
})
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
test('the callback is still called where the branch is built', () => {
|
|
187
|
+
const js = clientJsFor('_p.render(row)')
|
|
188
|
+
expect(js).toContain('__bfSlot(_p.render(row()), __slots)')
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
test('a `.map()` preamble local is not known safe', () => {
|
|
192
|
+
const source = `
|
|
193
|
+
'use client'
|
|
194
|
+
import { createSignal } from '@barefootjs/client'
|
|
195
|
+
type Row = { id: number; label: string; done: boolean }
|
|
196
|
+
const fmt = (s: string) => s.trim()
|
|
197
|
+
export function C(props: { render: (row: Row) => unknown }) {
|
|
198
|
+
const [rows] = createSignal<Row[]>([])
|
|
199
|
+
return <ul>{rows().map(row => { const draw = props.render; return <li key={row.id}>{row.done ? draw(row) : 'pending'}</li> })}</ul>
|
|
200
|
+
}
|
|
201
|
+
`
|
|
202
|
+
const result = compileJSX(source, 'C.tsx', { adapter })
|
|
203
|
+
const js = result.files.find(f => f.type === 'clientJs')!.content
|
|
204
|
+
expect(armEffects(js)).toEqual([])
|
|
205
|
+
})
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
describe('loop-row conditional: a branch the caller cannot reach with an element keeps its text effect', () => {
|
|
209
|
+
const EFFECT: Case[] = [
|
|
210
|
+
['a module function', 'fmt(row.label)'],
|
|
211
|
+
['an imported function', 'String(createMemo(() => row.label)())'],
|
|
212
|
+
['a method and a global', 'String(row.label.toUpperCase())'],
|
|
213
|
+
['a callback prop returning a string, by bracket access', "_p['format'](row.label)"],
|
|
214
|
+
['a callback prop returning a string, passed as an argument', 'apply(_p.format, row.label)'],
|
|
215
|
+
['a data prop read as an argument', 'fmt(String(_p.count + row.id))'],
|
|
216
|
+
['a prop array of a data-only row type', 'fmt(_p.rows2.find(r => r.id === row.id)?.label ?? row.label)'],
|
|
217
|
+
['a const alias of the props object, for a data prop', 'p.format(row.label)', { body: 'const p = _p' }],
|
|
218
|
+
['a body destructure of a data prop', 'format(row.label)', { body: 'const { format } = _p' }],
|
|
219
|
+
['a local function that reads no prop', 'tidy(row.label)', { body: 'const tidy = (s: string) => fmt(s)' }],
|
|
220
|
+
['a local wrapper over a string-returning callback prop', 'tidy(row.label)', { body: 'const tidy = (s: string) => _p.format(s)' }],
|
|
221
|
+
['a const computed from a data prop', 'fmt(String(twice + row.id))', { body: 'const twice = _p.count * 2' }],
|
|
222
|
+
['a signal seeded from a data prop', 'fmt(String(n() + row.id))', { body: 'const [n] = createSignal(_p.count)' }],
|
|
223
|
+
['a memo over a data prop', 'fmt(String(n() + row.id))', { body: 'const n = createMemo(() => _p.count * 2)' }],
|
|
224
|
+
['a destructured data prop with a default', 'format(row.label)', { params: '{ format = fmt }: Props' }],
|
|
225
|
+
['a nested arrow whose parameter is named like the props object', 'fmt([row.label].map(_p => _p.trim()).join())'],
|
|
226
|
+
]
|
|
227
|
+
for (const [name, branch, opts] of EFFECT) {
|
|
228
|
+
test(name, () => {
|
|
229
|
+
expect(armEffects(clientJsFor(branch, opts))).toHaveLength(1)
|
|
230
|
+
})
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
test('a loop row named like the props object or a prop shadows it', () => {
|
|
234
|
+
const source = `
|
|
235
|
+
'use client'
|
|
236
|
+
import { createSignal } from '@barefootjs/client'
|
|
237
|
+
type Cell = { id: number; done: boolean; render: (n: number) => string }
|
|
238
|
+
export function C(props: { render?: (c: Cell) => unknown }) {
|
|
239
|
+
const [cells] = createSignal<Cell[]>([])
|
|
240
|
+
return (
|
|
241
|
+
<div>
|
|
242
|
+
<ul>{cells().map(props => <li key={props.id}>{props.done ? props.render(props.id) : 'pending'}</li>)}</ul>
|
|
243
|
+
<ol>{cells().map(({ id, done, render }) => <li key={id}>{done ? render(id) : 'pending'}</li>)}</ol>
|
|
244
|
+
</div>
|
|
245
|
+
)
|
|
246
|
+
}
|
|
247
|
+
`
|
|
248
|
+
const result = compileJSX(source, 'C.tsx', { adapter })
|
|
249
|
+
expect(result.errors.filter(e => e.severity === 'error')).toEqual([])
|
|
250
|
+
const js = result.files.find(f => f.type === 'clientJs')!.content
|
|
251
|
+
// Both rows call the row's own `render`, so both branches get the effect.
|
|
252
|
+
expect(armEffects(js)).toHaveLength(2)
|
|
97
253
|
})
|
|
98
254
|
})
|
|
@@ -505,19 +505,17 @@ describe('per-item conditional wrapping a dynamic attr/event element binds exact
|
|
|
505
505
|
})
|
|
506
506
|
|
|
507
507
|
test('a JSX-callback prop call as the WHOLE branch content does not get a nested text effect', () => {
|
|
508
|
-
//
|
|
509
|
-
//
|
|
510
|
-
//
|
|
511
|
-
//
|
|
512
|
-
//
|
|
513
|
-
//
|
|
514
|
-
//
|
|
515
|
-
//
|
|
516
|
-
//
|
|
517
|
-
//
|
|
518
|
-
//
|
|
519
|
-
// first instead of replacing it (surfaced via barefootjs-xyflow's
|
|
520
|
-
// renderNode reference page in CI).
|
|
508
|
+
// A per-item conditional whose branch is one bare `expression` calling
|
|
509
|
+
// a callback prop (a hoisted JSX-callback prop,
|
|
510
|
+
// `renderNode={(n) => <Pill/>}`, #1211/#1213). The value is an element
|
|
511
|
+
// the caller built, spliced via `__bfSlot` whenever insert() mounts the
|
|
512
|
+
// branch. An effect in the arm would call the callback again on the
|
|
513
|
+
// first run and on every change of the row's item, replacing the live
|
|
514
|
+
// element (and its state) each time; the xyflow `<Flow>` renderer
|
|
515
|
+
// would rebuild every custom node on each drag frame. Phase 1 marks the
|
|
516
|
+
// branch `readsCallerValue` and the arm gets no text effect for it.
|
|
517
|
+
// A branch calling anything else does get one (#3894, pinned in
|
|
518
|
+
// `loop-branch-bare-expression-reactive-text.test.ts`).
|
|
521
519
|
const source = `
|
|
522
520
|
'use client'
|
|
523
521
|
import { createSignal } from '@barefootjs/client'
|
|
@@ -541,8 +539,10 @@ describe('per-item conditional wrapping a dynamic attr/event element binds exact
|
|
|
541
539
|
expect(result.errors).toHaveLength(0)
|
|
542
540
|
const content = result.files.find(f => f.type === 'clientJs')!.content
|
|
543
541
|
|
|
544
|
-
//
|
|
545
|
-
//
|
|
546
|
-
expect(content).
|
|
542
|
+
// The callback is called where the branch is built (the row template
|
|
543
|
+
// and the arm's `template()`), and in no effect.
|
|
544
|
+
expect(content).toContain('__bfSlot(_p.renderNode(n()), __slots)')
|
|
545
|
+
expect(content).not.toMatch(/create(Disposable)?Effect\(\(\) => \{[^}]*_p\.renderNode\(/)
|
|
546
|
+
expect(content).not.toContain('escapeTextOrNode(_p.renderNode(')
|
|
547
547
|
})
|
|
548
548
|
})
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// What a computed destructure default stands for once the declarations it
|
|
2
|
+
// reads are resolved (`appliedPropDefaults`), the SSR seed plan's prop steps
|
|
3
|
+
// built from it, and the refusal of a default no template can apply
|
|
4
|
+
// (`refuseUnappliedPropDefaults`), against IR built by the real pipeline.
|
|
5
|
+
// The rendered output is pinned by the conformance fixtures
|
|
6
|
+
// `destructure-default-local-const*`, `destructure-default-param-module-scope`
|
|
7
|
+
// and `destructure-default-reads-signal`.
|
|
8
|
+
|
|
9
|
+
import { describe, test, expect } from 'bun:test'
|
|
10
|
+
import { analyzeComponent } from '../analyzer'
|
|
11
|
+
import { buildMetadata } from '../compiler'
|
|
12
|
+
import { stringifyParsedExpr } from '../expression-parser'
|
|
13
|
+
import { jsxToIR } from '../jsx-to-ir'
|
|
14
|
+
import { appliedPropDefaults, computeSsrSeedPlan } from '../ssr-seed-plan'
|
|
15
|
+
import { refuseUnappliedPropDefaults } from '../adapters/unapplied-prop-default'
|
|
16
|
+
import type { CompilerError, ComponentIR } from '../types'
|
|
17
|
+
|
|
18
|
+
function irFor(body: string, signature = 'props: { a?: string; b?: string; c?: string }', preamble = ''): ComponentIR {
|
|
19
|
+
const ctx = analyzeComponent(`
|
|
20
|
+
'use client'
|
|
21
|
+
import { createSignal, createMemo } from '@barefootjs/client'
|
|
22
|
+
function Child(props: { v?: string; onDone?: () => void }) { return <b>{props.v}</b> }
|
|
23
|
+
${preamble}
|
|
24
|
+
export function C(${signature}) {
|
|
25
|
+
${body}
|
|
26
|
+
}
|
|
27
|
+
`, 'C.tsx', 'C')
|
|
28
|
+
return { version: '0.1', metadata: buildMetadata(ctx), root: jsxToIR(ctx)!, errors: [] }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Each applied default as `name = <resolved>`, with `!names` for what blocks it, in application order. */
|
|
32
|
+
function defaults(ir: ComponentIR): string[] {
|
|
33
|
+
const { order, defaults } = appliedPropDefaults(ir.metadata)
|
|
34
|
+
return order.flatMap((p) => {
|
|
35
|
+
const d = defaults.get(p.name)
|
|
36
|
+
if (!d) return []
|
|
37
|
+
return [`${p.name} = ${stringifyParsedExpr(d.expr)}${d.blocked.length ? ` !${d.blocked.join(',')}` : ''}`]
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The seed plan's prop steps, as their expression text. */
|
|
42
|
+
function propSteps(ir: ComponentIR): string[] {
|
|
43
|
+
return computeSsrSeedPlan(ir.metadata).steps.flatMap(s => (s.kind === 'derived' && s.origin === 'prop' ? [s.expr] : []))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function refusals(ir: ComponentIR): string[] {
|
|
47
|
+
const errors: CompilerError[] = []
|
|
48
|
+
refuseUnappliedPropDefaults(ir, { errors, componentName: 'C' })
|
|
49
|
+
return errors.map(e => `${e.code} ${/^The default of prop '(\w+)' reads ([^,]+(?:, '[^']+')*), which/.exec(e.message)?.slice(1).join(' <- ')}`)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
describe('appliedPropDefaults', () => {
|
|
53
|
+
test('a body destructure default reads a component const, through a method, a const of a const and a function', () => {
|
|
54
|
+
const ir = irFor(`
|
|
55
|
+
const DEF = 'z'
|
|
56
|
+
const SUFFIX = DEF + '!'
|
|
57
|
+
const label = () => DEF + '?'
|
|
58
|
+
const { a = DEF.toUpperCase(), b = SUFFIX, c = label() } = props
|
|
59
|
+
return <p>{a}{b}{c}</p>`)
|
|
60
|
+
expect(defaults(ir)).toEqual(['a = "z".toUpperCase()', 'b = "z" + "!"', 'c = "z" + "?"'])
|
|
61
|
+
expect(propSteps(ir)).toEqual(['a ?? "z".toUpperCase()', 'b ?? "z" + "!"', 'c ?? "z" + "?"'])
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('a component const hides a module const of the same name in a body destructure default', () => {
|
|
65
|
+
const ir = irFor(`
|
|
66
|
+
const DEF = 'z'
|
|
67
|
+
const { a = DEF.toUpperCase(), b = DEF } = props
|
|
68
|
+
return <p>{a}{b}</p>`, undefined, `const DEF = 'm'`)
|
|
69
|
+
expect(defaults(ir)).toEqual(['a = "z".toUpperCase()', 'b = "z"'])
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('a parameter default reads the module const, whatever the body declares under that name', () => {
|
|
73
|
+
const ir = irFor(`
|
|
74
|
+
const DEF = 'local'
|
|
75
|
+
return <p>{a}{b}</p>`, `{ a = DEF.toUpperCase(), b = NEXT }: { a?: string; b?: number }`, `const DEF = 'm'\nconst START = 3\nconst NEXT = START + 1`)
|
|
76
|
+
expect(defaults(ir)).toEqual(['a = "m".toUpperCase()', 'b = 3 + 1'])
|
|
77
|
+
expect(propSteps(ir)).toEqual(['a ?? "m".toUpperCase()', 'b ?? 3 + 1'])
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('a module const reads the module scope where a component const has a name it reads', () => {
|
|
81
|
+
const ir = irFor(`
|
|
82
|
+
const BASE = 'local'
|
|
83
|
+
const { a = TAG } = props
|
|
84
|
+
return <p>{a}{BASE}</p>`, undefined, `const BASE = 'm'\nconst TAG = BASE + '!'`)
|
|
85
|
+
expect(defaults(ir)).toEqual(['a = "m" + "!"'])
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('a prop named like a module const is the prop', () => {
|
|
89
|
+
const ir = irFor(`return <p>{DEF}{b}</p>`, `{ DEF = 'p', b = DEF + '!' }: { DEF?: string; b?: string }`, `const DEF = 'm'`)
|
|
90
|
+
expect(defaults(ir)).toEqual(['b = DEF + "!"'])
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
test('a default reading an earlier prop through a const is applied after that prop', () => {
|
|
94
|
+
const ir = irFor(`
|
|
95
|
+
const { a = 'x'.toUpperCase() } = props
|
|
96
|
+
const L = a + '!'
|
|
97
|
+
const { b = L } = props
|
|
98
|
+
return <p>{a}{b}</p>`, 'props: { b?: string; a?: string }')
|
|
99
|
+
expect(defaults(ir)).toEqual(['a = "x".toUpperCase()', 'b = a + "!"'])
|
|
100
|
+
expect(propSteps(ir)).toEqual(['a ?? "x".toUpperCase()', 'b ?? a + "!"'])
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
test('a const under a callback param of a name it reads is not resolved into the callback', () => {
|
|
104
|
+
const ir = irFor(`
|
|
105
|
+
const { a = 'x'.toUpperCase() } = props
|
|
106
|
+
const L = a + '!'
|
|
107
|
+
const { b = ['p'].map(a => a + L).join('') } = props
|
|
108
|
+
return <p>{a}{b}</p>`)
|
|
109
|
+
expect(defaults(ir)[1]).toEndWith(' !L')
|
|
110
|
+
expect(propSteps(ir)).toEqual(['a ?? "x".toUpperCase()'])
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('a const declared after the destructure is not initialized when the default runs', () => {
|
|
114
|
+
const ir = irFor(`
|
|
115
|
+
const { a = DEF, b = EARLY } = props
|
|
116
|
+
const DEF = 'z'
|
|
117
|
+
return <p>{a}{b}</p>`, undefined, `const DEF = 'm'\nconst EARLY = 'e'`)
|
|
118
|
+
expect(defaults(ir)).toEqual(['a = DEF !DEF', 'b = "e"'])
|
|
119
|
+
expect(propSteps(ir)).toEqual(['b ?? "e"'])
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
test('a signal, a memo, a let, a reassigned const and a function with a parameter block the default', () => {
|
|
123
|
+
const ir = irFor(`
|
|
124
|
+
const [s] = createSignal('q')
|
|
125
|
+
const m = createMemo(() => s() + '!')
|
|
126
|
+
let n = 'n'
|
|
127
|
+
function fmt(x: string) { return x }
|
|
128
|
+
const L = s() + '!'
|
|
129
|
+
const { a = L, b = m() + n, c = fmt('x') } = props
|
|
130
|
+
return <p>{a}{b}{c}</p>`)
|
|
131
|
+
expect(defaults(ir)).toEqual(['a = s() + "!" !s', 'b = m() + n !m,n', 'c = fmt("x") !fmt'])
|
|
132
|
+
expect(propSteps(ir)).toEqual([])
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
describe('refuseUnappliedPropDefaults', () => {
|
|
137
|
+
const BLOCKED = `
|
|
138
|
+
const [s] = createSignal('q')
|
|
139
|
+
const L = s() + '!'
|
|
140
|
+
const { a = L, b = a } = props`
|
|
141
|
+
|
|
142
|
+
test('a blocked default is refused where a position reads the prop', () => {
|
|
143
|
+
expect(refusals(irFor(`${BLOCKED}
|
|
144
|
+
return <p>{a}</p>`))).toEqual([`BF101 a <- 's'`])
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('through a signal seed, a memo, a child prop and another prop default', () => {
|
|
148
|
+
expect(refusals(irFor(`${BLOCKED}
|
|
149
|
+
const [n] = createSignal(a)
|
|
150
|
+
return <p>{n()}</p>`))).toEqual([`BF101 a <- 's'`])
|
|
151
|
+
expect(refusals(irFor(`${BLOCKED}
|
|
152
|
+
const m = createMemo(() => a + '?')
|
|
153
|
+
return <p>{m()}</p>`))).toEqual([`BF101 a <- 's'`])
|
|
154
|
+
expect(refusals(irFor(`${BLOCKED}
|
|
155
|
+
return <Child v={a} />`))).toEqual([`BF101 a <- 's'`])
|
|
156
|
+
expect(refusals(irFor(`${BLOCKED}
|
|
157
|
+
return <p>{b}</p>`))).toEqual([`BF101 a <- 's'`])
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
test('not where only the client reads the prop', () => {
|
|
161
|
+
expect(refusals(irFor(`${BLOCKED}
|
|
162
|
+
return <p>{/* @client */ a}</p>`))).toEqual([])
|
|
163
|
+
expect(refusals(irFor(`${BLOCKED}
|
|
164
|
+
return <button onClick={() => console.log(a)}>x</button>`))).toEqual([])
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
test('claims the /* @client */ escape unless a child prop reads the prop', () => {
|
|
168
|
+
const claims = (body: string) => {
|
|
169
|
+
const errors: CompilerError[] = []
|
|
170
|
+
refuseUnappliedPropDefaults(irFor(body), { errors, componentName: 'C' })
|
|
171
|
+
return errors.map(e => e.suggestion?.escape?.map(x => x.kind) ?? [])
|
|
172
|
+
}
|
|
173
|
+
expect(claims(`${BLOCKED}
|
|
174
|
+
return <p>{a}</p>`)).toEqual([['client-directive']])
|
|
175
|
+
expect(claims(`${BLOCKED}
|
|
176
|
+
return <p>{a}<Child v={b} /></p>`)).toEqual([[]])
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
test('a handler default naming a component function is not refused', () => {
|
|
180
|
+
expect(refusals(irFor(`
|
|
181
|
+
function handleDone() {}
|
|
182
|
+
const { onDone = handleDone } = props
|
|
183
|
+
return <div><button onClick={onDone}>x</button><Child onDone={onDone} /></div>`, 'props: { onDone?: () => void }'))).toEqual([])
|
|
184
|
+
})
|
|
185
|
+
})
|