@barefootjs/jsx 0.39.8 → 0.39.9
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/callback-method.d.ts +19 -7
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +2 -0
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-plan.d.ts +133 -9
- package/dist/adapters/loop-plan.d.ts.map +1 -1
- package/dist/adapters/parsed-expr-emitter.d.ts +20 -3
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/assignment-target.d.ts +26 -0
- package/dist/assignment-target.d.ts.map +1 -0
- package/dist/errors.d.ts +1 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1913 -1457
- package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +24 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts +16 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts +6 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/csr-substitute.d.ts +13 -2
- package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-extraction.d.ts +5 -3
- package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +8 -0
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/rewrite-props-object.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +1 -0
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +17 -8
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +57 -0
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +30 -2
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/lexical-scope-walk.d.ts +26 -0
- package/dist/scope/lexical-scope-walk.d.ts.map +1 -0
- package/dist/types.d.ts +25 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/arithmetic-lowering.test.ts +5 -1
- package/src/__tests__/binding-scope.test.ts +29 -0
- package/src/__tests__/create-query.test.ts +1 -1
- package/src/__tests__/emit-to-string-call.test.ts +58 -0
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +90 -31
- package/src/__tests__/loop-plan.test.ts +131 -1
- package/src/__tests__/loop-row-child-children-handler.test.ts +129 -0
- package/src/__tests__/loop-row-child-children-inner-loop-shadow.test.ts +74 -0
- package/src/__tests__/loop-row-child-children-ref.test.ts +117 -0
- package/src/__tests__/props-alias-const.test.ts +60 -1
- package/src/__tests__/props-named-callback-param.test.ts +256 -0
- package/src/__tests__/renamed-body-destructure-template.test.ts +86 -0
- package/src/__tests__/rewrite-props-object.test.ts +90 -0
- package/src/__tests__/written-binding-substitution.test.ts +430 -0
- package/src/adapters/callback-method.ts +25 -10
- package/src/adapters/dsl-template-adapter.ts +11 -0
- package/src/adapters/loop-plan.ts +201 -13
- package/src/adapters/parsed-expr-emitter.ts +37 -4
- package/src/analyzer.ts +283 -2
- package/src/assignment-target.ts +79 -0
- package/src/errors.ts +11 -0
- package/src/index.ts +13 -2
- package/src/ir-to-client-js/compute-inlinability.ts +6 -3
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +21 -5
- package/src/ir-to-client-js/control-flow/plan/loop.ts +25 -1
- package/src/ir-to-client-js/control-flow/shared.ts +41 -3
- package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +23 -3
- package/src/ir-to-client-js/csr-substitute.ts +70 -22
- package/src/ir-to-client-js/emit-reactive.ts +2 -2
- package/src/ir-to-client-js/emit-registration.ts +6 -1
- package/src/ir-to-client-js/html-template.ts +18 -7
- package/src/ir-to-client-js/phases/props-extraction.ts +26 -5
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +7 -0
- package/src/ir-to-client-js/plan/static-array-child-init.ts +8 -0
- package/src/ir-to-client-js/rewrite-destructured-props.ts +2 -2
- package/src/ir-to-client-js/rewrite-props-object.ts +14 -8
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +4 -1
- package/src/ir-to-client-js/utils.ts +40 -96
- package/src/jsx-to-ir.ts +31 -12
- package/src/prop-rewrite.ts +31 -72
- package/src/props-binding.ts +73 -1
- package/src/scope/binding-scope.ts +34 -3
- package/src/scope/lexical-scope-walk.ts +167 -0
- package/src/types.ts +25 -1
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Callback parameters spelled like the props object in the CSR client JS.
|
|
3
|
+
*
|
|
4
|
+
* The init body reads the component's props through `_p`, and a
|
|
5
|
+
* destructured prop read compiles to `_p.<prop>`. The props-object rename
|
|
6
|
+
* (`rewritePropsObjectRef`) used to rename EVERY `props` identifier in the
|
|
7
|
+
* init body, binders included, so a `.map((props) => …)` row, a handler
|
|
8
|
+
* `(props) => …` arrow, a `.filter((props) => …)` callback or a nested
|
|
9
|
+
* `function f(props)` became a `_p` binding that captured the outer
|
|
10
|
+
* `_p.<prop>` reads inside it: a client-created row lost its `suffix`, a
|
|
11
|
+
* forwarded child prop read the row accessor, and `props.by * step` became
|
|
12
|
+
* `NaN`. Fixture: `loop-param-named-props`.
|
|
13
|
+
*/
|
|
14
|
+
import { describe, test, expect } from 'bun:test'
|
|
15
|
+
import { compileJSX } from '../compiler'
|
|
16
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
17
|
+
import { rewriteScopedValueRefs } from '../prop-rewrite'
|
|
18
|
+
|
|
19
|
+
const adapter = new TestAdapter()
|
|
20
|
+
|
|
21
|
+
function clientJs(source: string): string {
|
|
22
|
+
const result = compileJSX(source, 'Repro.tsx', { adapter })
|
|
23
|
+
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
24
|
+
return result.files.find(f => f.type === 'clientJs')!.content
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** An `init<Name>` function body (excludes the module-level template lambda). */
|
|
28
|
+
function initBody(content: string, name = ''): string {
|
|
29
|
+
const start = content.indexOf(`export function init${name}`)
|
|
30
|
+
const end = content.indexOf('\nhydrate(', start)
|
|
31
|
+
return content.slice(start, end)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
describe('a callback param named `props` keeps its binding in the client JS', () => {
|
|
35
|
+
test('destructured props: a reactive loop row reads the outer prop through `_p`', () => {
|
|
36
|
+
const body = initBody(clientJs(`
|
|
37
|
+
"use client";
|
|
38
|
+
import { createSignal } from '@barefootjs/client'
|
|
39
|
+
export function Rows({ suffix }: { suffix: string }) {
|
|
40
|
+
const [items, setItems] = createSignal([{ id: 1, label: 'a' }])
|
|
41
|
+
return (
|
|
42
|
+
<div>
|
|
43
|
+
<ul>{items().map((props) => <li key={props.id}>{props.label}{suffix}</li>)}</ul>
|
|
44
|
+
<button onClick={() => setItems([...items(), { id: 2, label: 'b' }])}>add</button>
|
|
45
|
+
</div>
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
`))
|
|
49
|
+
expect(body).toContain('const props = () => __e.item')
|
|
50
|
+
expect(body).toContain('props().label')
|
|
51
|
+
expect(body).toContain('_p.suffix')
|
|
52
|
+
expect(body).not.toMatch(/const _p = /)
|
|
53
|
+
expect(body).not.toContain('_p().')
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('a child component forwarded inside the row reads the outer prop, not the row', () => {
|
|
57
|
+
const body = initBody(clientJs(`
|
|
58
|
+
"use client";
|
|
59
|
+
import { createSignal } from '@barefootjs/client'
|
|
60
|
+
function Item(props: { label: string; mark: string }) { return <li>{props.label}{props.mark}</li> }
|
|
61
|
+
export function List({ mark }: { mark: string }) {
|
|
62
|
+
const [items, setItems] = createSignal([{ label: 'a' }])
|
|
63
|
+
return (
|
|
64
|
+
<div>
|
|
65
|
+
<ul>{items().map((props) => <Item key={props.label} label={props.label} mark={mark} />)}</ul>
|
|
66
|
+
<button onClick={() => setItems([...items(), { label: 'b' }])}>add</button>
|
|
67
|
+
</div>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
`), 'List')
|
|
71
|
+
expect(body).toContain('get mark() { return _p.mark }')
|
|
72
|
+
expect(body).toContain('get label() { return props().label }')
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('props object: an inlined outer read inside a `props` row stays on `_p`', () => {
|
|
76
|
+
const body = initBody(clientJs(`
|
|
77
|
+
"use client";
|
|
78
|
+
import { createSignal } from '@barefootjs/client'
|
|
79
|
+
export function Rows(props: { title: string }) {
|
|
80
|
+
const t = props.title
|
|
81
|
+
const [items, setItems] = createSignal([{ id: 1 }])
|
|
82
|
+
return (
|
|
83
|
+
<div>
|
|
84
|
+
<ul>{items().map((props) => <li key={props.id}>{props.id}{t}</li>)}</ul>
|
|
85
|
+
<button onClick={() => setItems([...items(), { id: 2 }])}>add</button>
|
|
86
|
+
</div>
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
`))
|
|
90
|
+
expect(body).toContain('const props = () => __e.item')
|
|
91
|
+
expect(body).toContain('_p.title')
|
|
92
|
+
expect(body).not.toMatch(/const _p = /)
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('a handler arrow param and a filter callback param keep their names', () => {
|
|
96
|
+
const body = initBody(clientJs(`
|
|
97
|
+
"use client";
|
|
98
|
+
import { createSignal } from '@barefootjs/client'
|
|
99
|
+
export function Counter({ step }: { step: number }) {
|
|
100
|
+
const [n, setN] = createSignal(0)
|
|
101
|
+
const [items, setItems] = createSignal([{ id: 1 }, { id: 2 }])
|
|
102
|
+
const bump = (props: { by: number }) => setN(n() + props.by * step)
|
|
103
|
+
return (
|
|
104
|
+
<div>
|
|
105
|
+
<button onClick={() => bump({ by: 2 })}>{n()}</button>
|
|
106
|
+
<button onClick={() => setItems(items().filter((props) => props.id !== step))}>{items().length}</button>
|
|
107
|
+
</div>
|
|
108
|
+
)
|
|
109
|
+
}
|
|
110
|
+
`))
|
|
111
|
+
expect(body).toContain('const bump = (props) => setN(n() + props.by * _p.step)')
|
|
112
|
+
expect(body).toContain('.filter((props) => props.id !== _p.step)')
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
test('a nested function param keeps its name', () => {
|
|
116
|
+
const body = initBody(clientJs(`
|
|
117
|
+
"use client";
|
|
118
|
+
import { createSignal } from '@barefootjs/client'
|
|
119
|
+
export function Say({ suffix }: { suffix: string }) {
|
|
120
|
+
const [last, setLast] = createSignal('')
|
|
121
|
+
function describe(props: { label: string }) { return props.label + suffix }
|
|
122
|
+
return <button onClick={() => setLast(describe({ label: 'x' }))}>{last()}</button>
|
|
123
|
+
}
|
|
124
|
+
`))
|
|
125
|
+
expect(body).toContain('props.label + _p.suffix')
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('a free `props` read in a destructured component is still renamed', () => {
|
|
129
|
+
const body = initBody(clientJs(`
|
|
130
|
+
"use client";
|
|
131
|
+
import { createSignal } from '@barefootjs/client'
|
|
132
|
+
export function Free({ id }: { id: string }) {
|
|
133
|
+
const [n, setN] = createSignal(0)
|
|
134
|
+
return <button data-id={id} onClick={() => setN(n() + (props as any).delta)}>{n()}</button>
|
|
135
|
+
}
|
|
136
|
+
`))
|
|
137
|
+
expect(body).toContain('(_p).delta')
|
|
138
|
+
})
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
describe('a `var` binds in its whole function, not just its block', () => {
|
|
142
|
+
test('a `var props` in a nested block shadows the props rename for the whole handler', () => {
|
|
143
|
+
const body = initBody(clientJs(`
|
|
144
|
+
"use client";
|
|
145
|
+
import { createSignal } from '@barefootjs/client'
|
|
146
|
+
export function A({ step }: { step: number }) {
|
|
147
|
+
const [n, setN] = createSignal(0)
|
|
148
|
+
return <button onClick={() => { if (step > 0) { var props = { by: 2 } } setN(n() + props.by * step) }}>{n()}</button>
|
|
149
|
+
}
|
|
150
|
+
`))
|
|
151
|
+
expect(body).toContain('{ var props = { by: 2 } } setN(n() + props.by * _p.step)')
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
test('a `for (var props …)` head shadows the props rename after the loop', () => {
|
|
155
|
+
const body = initBody(clientJs(`
|
|
156
|
+
"use client";
|
|
157
|
+
import { createSignal } from '@barefootjs/client'
|
|
158
|
+
export function B({ step }: { step: number }) {
|
|
159
|
+
const [n, setN] = createSignal(0)
|
|
160
|
+
return <button onClick={() => { for (var props = 0; props < step; props++) {} setN(n() + props) }}>{n()}</button>
|
|
161
|
+
}
|
|
162
|
+
`))
|
|
163
|
+
expect(body).toContain('for (var props = 0; props < _p.step; props++) {} setN(n() + props)')
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
test('a block-nested `var` named like a destructured prop is not rewritten to the prop', () => {
|
|
167
|
+
const body = initBody(clientJs(`
|
|
168
|
+
"use client";
|
|
169
|
+
import { createSignal } from '@barefootjs/client'
|
|
170
|
+
export function C({ step }: { step: number }) {
|
|
171
|
+
const [n, setN] = createSignal(0)
|
|
172
|
+
return <button data-step={step} onClick={() => { if (n() >= 0) { var step = 5 } setN(n() + step) }}>{n()}</button>
|
|
173
|
+
}
|
|
174
|
+
`))
|
|
175
|
+
expect(body).toContain('{ var step = 5 } setN(n() + step)')
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
test('a `let` in a nested block still binds only inside that block', () => {
|
|
179
|
+
const body = initBody(clientJs(`
|
|
180
|
+
"use client";
|
|
181
|
+
import { createSignal } from '@barefootjs/client'
|
|
182
|
+
export function D({ step }: { step: number }) {
|
|
183
|
+
const [n, setN] = createSignal(0)
|
|
184
|
+
return <button onClick={() => { if (n() >= 0) { let step = 5; setN(step) } setN(n() + step) }}>{n()}</button>
|
|
185
|
+
}
|
|
186
|
+
`))
|
|
187
|
+
expect(body).toContain('{ let step = 5; setN(step) } setN(n() + _p.step)')
|
|
188
|
+
})
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
describe('the Phase-1 prop rewrite shares the same `var` scoping', () => {
|
|
192
|
+
const toProp = (name: string) => `_p.${name}`
|
|
193
|
+
test('a block-nested `var` binds for the whole arrow', () => {
|
|
194
|
+
const out = rewriteScopedValueRefs('() => { if (c) { var step = 5 } return step }', new Set(['step']), toProp)
|
|
195
|
+
expect(out).toBe('() => { if (c) { var step = 5 } return step }')
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
test('a `for (var …)` head binds after the loop', () => {
|
|
199
|
+
const out = rewriteScopedValueRefs('() => { for (var step = 0; step < 3; step++) {} return step }', new Set(['step']), toProp)
|
|
200
|
+
expect(out).toBe('() => { for (var step = 0; step < 3; step++) {} return step }')
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
test('a `let` stays block-scoped', () => {
|
|
204
|
+
const out = rewriteScopedValueRefs('() => { if (c) { let step = 5 } return step }', new Set(['step']), toProp)
|
|
205
|
+
expect(out).toBe('() => { if (c) { let step = 5 } return _p.step }')
|
|
206
|
+
})
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
describe('a parameter default does not see the body\'s declarations', () => {
|
|
210
|
+
const toProp = (name: string) => `_p.${name}`
|
|
211
|
+
test('Phase-1 rewrite: a default reads the prop despite a same-named body `var`', () => {
|
|
212
|
+
const out = rewriteScopedValueRefs('(value = step) => { var step = 0; return value }', new Set(['step']), toProp)
|
|
213
|
+
expect(out).toBe('(value = _p.step) => { var step = 0; return value }')
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
test('Phase-1 rewrite: a default reads the prop despite a same-named body `let`', () => {
|
|
217
|
+
const out = rewriteScopedValueRefs('(value = step) => { let step = 0; return value + step }', new Set(['step']), toProp)
|
|
218
|
+
expect(out).toBe('(value = _p.step) => { let step = 0; return value + step }')
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
test('a handler-local arrow whose default reads a destructured prop shadowed in its body', () => {
|
|
222
|
+
const body = initBody(clientJs(`
|
|
223
|
+
"use client";
|
|
224
|
+
import { createSignal } from '@barefootjs/client'
|
|
225
|
+
export function E({ step }: { step: number }) {
|
|
226
|
+
const [n, setN] = createSignal(0)
|
|
227
|
+
return <button onClick={() => { const f = (value = step) => { var step = 0; return value + step }; setN(f()) }}>{n()}</button>
|
|
228
|
+
}
|
|
229
|
+
`))
|
|
230
|
+
expect(body).toContain('const f = (value = _p.step) => { var step = 0; return value + step }')
|
|
231
|
+
})
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
describe('a class static block owns its `var`s', () => {
|
|
235
|
+
const toProp = (name: string) => `_p.${name}`
|
|
236
|
+
test('Phase-1 rewrite: a read inside the static block is local, a read after the class is the prop', () => {
|
|
237
|
+
const out = rewriteScopedValueRefs(
|
|
238
|
+
'() => { class C { static { var step = 0; C.s = step } }; return step }',
|
|
239
|
+
new Set(['step']),
|
|
240
|
+
toProp,
|
|
241
|
+
)
|
|
242
|
+
expect(out).toBe('() => { class C { static { var step = 0; C.s = step } }; return _p.step }')
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
test('a handler whose class static block declares a `var` named like a destructured prop', () => {
|
|
246
|
+
const body = initBody(clientJs(`
|
|
247
|
+
"use client";
|
|
248
|
+
import { createSignal } from '@barefootjs/client'
|
|
249
|
+
export function F({ step }: { step: number }) {
|
|
250
|
+
const [n, setN] = createSignal(0)
|
|
251
|
+
return <button onClick={() => { class C { static { var step = 0; C.s = step } }; setN(n() + step) }}>{n()}</button>
|
|
252
|
+
}
|
|
253
|
+
`))
|
|
254
|
+
expect(body).toContain('class C { static { var step = 0; C.s = step } }; setN(n() + _p.step)')
|
|
255
|
+
})
|
|
256
|
+
})
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The client template reads a renamed body destructure binding that holds
|
|
3
|
+
* its prop (`const { b: local = d } = props`) as that prop with its default,
|
|
4
|
+
* like the parameter form (#3579). A binding that no longer holds the prop
|
|
5
|
+
* (a `let` reassigned before the return, a folded reassignment, a value
|
|
6
|
+
* mutated in place) stays a local: the template leaves its slot empty and
|
|
7
|
+
* defers a child that receives it, instead of reading `_p.b ?? d`.
|
|
8
|
+
*
|
|
9
|
+
* The reassigned shapes have no conformance fixture: the reference adapter
|
|
10
|
+
* itself drops the reassignment of a destructured binding, so these pin the
|
|
11
|
+
* client template only.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { describe, test, expect } from 'bun:test'
|
|
15
|
+
import { compileJSX } from '../compiler'
|
|
16
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
17
|
+
|
|
18
|
+
const adapter = new TestAdapter()
|
|
19
|
+
const transpiler = new Bun.Transpiler({ loader: 'js' })
|
|
20
|
+
|
|
21
|
+
const CHILD = 'function Child(props: { xs: string[] }) { return <i>{props.xs.length}</i> }'
|
|
22
|
+
|
|
23
|
+
/** The `template: (_p) => …` lambda the client registers for `C`. */
|
|
24
|
+
function templateOf(body: string, jsx: string, propsType = '{ b?: string[] | null }'): string {
|
|
25
|
+
const source = `
|
|
26
|
+
'use client'
|
|
27
|
+
${CHILD}
|
|
28
|
+
export function C(props: ${propsType}) {
|
|
29
|
+
${body}
|
|
30
|
+
return ${jsx}
|
|
31
|
+
}
|
|
32
|
+
`
|
|
33
|
+
const result = compileJSX(source, 'C.tsx', { adapter })
|
|
34
|
+
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
35
|
+
const js = result.files.find(f => f.type === 'clientJs')!.content
|
|
36
|
+
expect(() => transpiler.transformSync(js)).not.toThrow()
|
|
37
|
+
const line = js.split('\n').find(l => l.includes("hydrate('C'") && l.includes('template: (_p) =>'))
|
|
38
|
+
expect(line).toBeDefined()
|
|
39
|
+
return line!
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
describe('renamed body destructure binding in the client template (#3579)', () => {
|
|
43
|
+
test('a default holding a callback reads the prop with its default', () => {
|
|
44
|
+
const t = templateOf("const { b: local = ['y', ''].filter((x) => x !== '') } = props", '<p>{local.length}</p>')
|
|
45
|
+
expect(t).toContain("_p.b ?? (['y', ''].filter((x) => x !== ''))")
|
|
46
|
+
expect(t).not.toContain("${''}")
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('a child prop holding it renders the child in place', () => {
|
|
50
|
+
const t = templateOf("const { b: local = ['y', ''].filter((x) => x !== '') } = props", '<p><Child xs={local} /></p>')
|
|
51
|
+
expect(t).toContain("renderChild('Child")
|
|
52
|
+
expect(t).not.toContain('data-bf-ph')
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('a let reassigned before the return keeps the child deferred', () => {
|
|
56
|
+
const t = templateOf("let { b: local = null } = props\n local = ['changed']", '<p><Child xs={local} /></p>')
|
|
57
|
+
expect(t).toContain('data-bf-ph')
|
|
58
|
+
expect(t).not.toContain('_p.b ?? null')
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('a let reassigned before the return is not read as the prop in text or an attribute', () => {
|
|
62
|
+
const t = templateOf("let { b: local = 'x' } = props\n local = local + '!'", '<p data-v={local}>{local}</p>', '{ b?: string }')
|
|
63
|
+
expect(t).not.toContain("_p.b ?? 'x'")
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('a folded conditional reassignment is not read as the prop', () => {
|
|
67
|
+
const t = templateOf("let { b: local = 'x' } = props\n if (!local) local = 'y'", '<p>{local}</p>', '{ b?: string }')
|
|
68
|
+
expect(t).not.toContain("_p.b ?? 'x'")
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('an un-renamed let reassigned before the return keeps the child deferred too', () => {
|
|
72
|
+
const t = templateOf("let { b = null } = props\n b = ['changed']", '<p><Child xs={b} /></p>')
|
|
73
|
+
expect(t).toContain('data-bf-ph')
|
|
74
|
+
expect(t).not.toContain('_p.b ?? null')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('an un-renamed binding with a callback default renders the child in place', () => {
|
|
78
|
+
const t = templateOf("const { b = ['y', ''].filter((x) => x !== '') } = props", '<p><Child xs={b} /></p>')
|
|
79
|
+
expect(t).toContain("renderChild('Child")
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test('a value mutated in place is not read as the prop', () => {
|
|
83
|
+
const t = templateOf("const { b: local = ['x'] } = props\n local.push('y')", '<p>{local.length}</p>', '{ b?: string[] }')
|
|
84
|
+
expect(t).not.toContain("_p.b ?? ['x']")
|
|
85
|
+
})
|
|
86
|
+
})
|
|
@@ -120,4 +120,94 @@ describe('rewritePropsObjectRef', () => {
|
|
|
120
120
|
const out = rewritePropsObjectRef('const x = propsX.name', 'props')
|
|
121
121
|
expect(out).toBe('const x = propsX.name')
|
|
122
122
|
})
|
|
123
|
+
|
|
124
|
+
describe('an inner binding of the same name shadows the props object', () => {
|
|
125
|
+
// The client JS reads the component's props through `_p`. A callback
|
|
126
|
+
// parameter or local spelled like the props object belongs to its own
|
|
127
|
+
// binding inside its scope: renaming it too would make every `_p.<prop>`
|
|
128
|
+
// read the compiler emits inside that scope (a destructured prop, a
|
|
129
|
+
// forwarded child prop) read the parameter instead.
|
|
130
|
+
test('a loop row callback param keeps its name; the outer `_p` read stays outer', () => {
|
|
131
|
+
const code = 'mapArray(() => items(), el, (props) => String(props.id), (props) => props().label + _p.suffix)'
|
|
132
|
+
expect(rewritePropsObjectRef(code, null)).toBe(code)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test('a row accessor `const props = () => __e.item` keeps its name', () => {
|
|
136
|
+
const code = 'const row = (__e) => {\n const props = () => __e.item\n return props().label + _p.suffix\n}'
|
|
137
|
+
expect(rewritePropsObjectRef(code, null)).toBe(code)
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
test('a handler arrow param and a nested function param keep their names', () => {
|
|
141
|
+
const code = 'const bump = (props) => set(props.by * _p.step)\nfunction describe(props) { return props.label + _p.suffix }'
|
|
142
|
+
expect(rewritePropsObjectRef(code, null)).toBe(code)
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test('references outside the shadowing scope are still rewritten', () => {
|
|
146
|
+
const out = rewritePropsObjectRef('const a = props.x\nconst f = (props) => props.y\nconst b = props.z', 'props')
|
|
147
|
+
expect(out).toBe('const a = _p.x\nconst f = (props) => props.y\nconst b = _p.z')
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('a rest binding name is shadowed the same way', () => {
|
|
151
|
+
const out = rewritePropsObjectRef('const a = rest\nconst f = (rest) => rest.y', null, 'rest')
|
|
152
|
+
expect(out).toBe('const a = _p\nconst f = (rest) => rest.y')
|
|
153
|
+
})
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
describe('`var` is function-scoped', () => {
|
|
157
|
+
test('a `var props` in a nested block binds for the whole enclosing function', () => {
|
|
158
|
+
const code = 'const f = () => { if (c) { var props = { by: 2 } } return props.by }\nconst g = props.x'
|
|
159
|
+
expect(rewritePropsObjectRef(code, null)).toBe('const f = () => { if (c) { var props = { by: 2 } } return props.by }\nconst g = _p.x')
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('a `for (var props …)` head binds after the loop, but not outside its function', () => {
|
|
163
|
+
const code = 'function f() { for (var props = 0; props < 3; props++) {} return props }\nconst g = props.x'
|
|
164
|
+
expect(rewritePropsObjectRef(code, null)).toBe('function f() { for (var props = 0; props < 3; props++) {} return props }\nconst g = _p.x')
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
test('a `var` inside a nested function does not leak into the outer one', () => {
|
|
168
|
+
const code = 'const f = () => { const h = () => { var props = 1; return props }; return props.x }'
|
|
169
|
+
expect(rewritePropsObjectRef(code, null)).toBe('const f = () => { const h = () => { var props = 1; return props }; return _p.x }')
|
|
170
|
+
})
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
describe('a parameter default does not see the body\'s declarations', () => {
|
|
174
|
+
test('a default reads the props object despite a same-named body `var`', () => {
|
|
175
|
+
const code = 'const f = (v = props.x) => { var props = 1; return v + props }'
|
|
176
|
+
expect(rewritePropsObjectRef(code, null)).toBe('const f = (v = _p.x) => { var props = 1; return v + props }')
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
test('a default reads the props object despite a same-named body `let`', () => {
|
|
180
|
+
const code = 'function f(v = props.x) { let props = 1; return v + props }'
|
|
181
|
+
expect(rewritePropsObjectRef(code, null)).toBe('function f(v = _p.x) { let props = 1; return v + props }')
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
test('a default sees an earlier parameter of the same name, not the props object', () => {
|
|
185
|
+
const code = 'const f = (props, v = props.x) => v'
|
|
186
|
+
expect(rewritePropsObjectRef(code, null)).toBe(code)
|
|
187
|
+
})
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
describe('a class static block owns its `var`s', () => {
|
|
191
|
+
test('a read inside the block is local; a read after the class is the props object', () => {
|
|
192
|
+
const code = 'const f = () => { class C { static { var props = 1; C.p = props } }; return props.x }'
|
|
193
|
+
expect(rewritePropsObjectRef(code, null)).toBe('const f = () => { class C { static { var props = 1; C.p = props } }; return _p.x }')
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
test('a `var` in a method or a field-initializer arrow does not leak into the enclosing function', () => {
|
|
197
|
+
const code = 'const f = () => { class C { m() { var props = 1; return props } g = () => { var props = 2; return props } }; return props.x }'
|
|
198
|
+
expect(rewritePropsObjectRef(code, null)).toBe('const f = () => { class C { m() { var props = 1; return props } g = () => { var props = 2; return props } }; return _p.x }')
|
|
199
|
+
})
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
describe('a computed method name is read in the enclosing scope', () => {
|
|
203
|
+
test('the key reads the props object; the body reads the parameter', () => {
|
|
204
|
+
const code = 'const o = { [props.k](props) { return props } }'
|
|
205
|
+
expect(rewritePropsObjectRef(code, null)).toBe('const o = { [_p.k](props) { return props } }')
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
test('the same holds for a class method and an accessor', () => {
|
|
209
|
+
const code = 'class C { [props.a](props) { return props } get [props.b]() { const props = 1; return props } }'
|
|
210
|
+
expect(rewritePropsObjectRef(code, null)).toBe('class C { [_p.a](props) { return props } get [_p.b]() { const props = 1; return props } }')
|
|
211
|
+
})
|
|
212
|
+
})
|
|
123
213
|
})
|