@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
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Which inherited literal-const reads (`R.toString`) the server render of a
|
|
2
|
+
// component performs (`refuseInheritedConstServerReads`), against IR built by
|
|
3
|
+
// the real pipeline. The rendered contract is pinned by the conformance
|
|
4
|
+
// fixtures `module-const-inherited-key*`.
|
|
5
|
+
|
|
6
|
+
import { describe, test, expect } from 'bun:test'
|
|
7
|
+
import { analyzeComponent } from '../analyzer'
|
|
8
|
+
import { buildMetadata } from '../compiler'
|
|
9
|
+
import { jsxToIR } from '../jsx-to-ir'
|
|
10
|
+
import { refuseInheritedConstServerReads } from '../adapters/inherited-const-server-read'
|
|
11
|
+
import type { CompilerError, ComponentIR } from '../types'
|
|
12
|
+
|
|
13
|
+
const R = `const R = { k: 'v' }`
|
|
14
|
+
const READ = `(R.toString ? 'y' : 'n')`
|
|
15
|
+
|
|
16
|
+
function irFor(body: string, preamble = ''): ComponentIR {
|
|
17
|
+
const ctx = analyzeComponent(`
|
|
18
|
+
'use client'
|
|
19
|
+
import { createSignal, createMemo } from '@barefootjs/client'
|
|
20
|
+
function Child(props: { v?: string; f?: () => string }) { return <b>{props.v}</b> }
|
|
21
|
+
${preamble}
|
|
22
|
+
export function C() {
|
|
23
|
+
${body}
|
|
24
|
+
}
|
|
25
|
+
`, 'C.tsx', 'C')
|
|
26
|
+
return { version: '0.1', metadata: buildMetadata(ctx), root: jsxToIR(ctx)!, errors: [] }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function refused(body: string, preamble = ''): string[] {
|
|
30
|
+
const ir = irFor(body, preamble)
|
|
31
|
+
return errorsOf(ir).map(e => e.message.slice(0, e.message.indexOf(' reads ')))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function errorsOf(ir: ComponentIR): CompilerError[] {
|
|
35
|
+
const errors: CompilerError[] = []
|
|
36
|
+
refuseInheritedConstServerReads(ir, { errors, componentName: 'C' })
|
|
37
|
+
expect(errors.every(e => e.code === 'BF101')).toBe(true)
|
|
38
|
+
return errors
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe('refuseInheritedConstServerReads', () => {
|
|
42
|
+
test('a read in each kind of server-rendered position', () => {
|
|
43
|
+
expect(refused(`${R}; return <p>{${READ}}</p>`)).toEqual(['`R.toString`'])
|
|
44
|
+
expect(refused(`${R}; return <p data-a={R['valueOf'] ? 'y' : 'n'}>t</p>`)).toEqual(['`R.valueOf`'])
|
|
45
|
+
expect(refused(`${R}; return <p>{R.toString ? <b>y</b> : <i>n</i>}</p>`)).toEqual(['`R.toString`'])
|
|
46
|
+
expect(refused(`${R}; return <p><Child v={${READ}} /></p>`)).toEqual(['`R.toString`'])
|
|
47
|
+
expect(refused(`${R}; return <ul>{['a'].map(i => <li key={i}>{R.toString ? i : 'n'}</li>)}</ul>`)).toEqual(['`R.toString`'])
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('a read in a declaration the server render reads', () => {
|
|
51
|
+
expect(refused(`${R}; const [s] = createSignal(${READ}); return <p>{s()}</p>`)).toEqual(['`R.toString`'])
|
|
52
|
+
expect(refused(`${R}; const m = createMemo(() => ${READ}); return <p>{m()}</p>`)).toEqual(['`R.toString`'])
|
|
53
|
+
expect(refused(`${R}; const c = ${READ}; const m = createMemo(() => c); return <p>{m()}</p>`)).toEqual(['`R.toString`'])
|
|
54
|
+
expect(refused(`${R}; function value() { return ${READ} }; return <p>{value()}</p>`)).toEqual(['`R.toString`'])
|
|
55
|
+
expect(refused(`${R}; const m = createMemo(() => ${READ}); function value() { return m() }; return <p>{value()}</p>`)).toEqual(['`R.toString`'])
|
|
56
|
+
expect(refused(`return <p>{value()}</p>`, `${R}\nfunction value() { return ${READ} }`)).toEqual(['`R.toString`'])
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('a read only a /* @client */ position reaches is not refused', () => {
|
|
60
|
+
expect(refused(`${R}; return <p>{/* @client */ ${READ}}</p>`)).toEqual([])
|
|
61
|
+
expect(refused(`${R}; const m = createMemo(() => ${READ}); return <p>{/* @client */ m()}</p>`)).toEqual([])
|
|
62
|
+
expect(refused(`${R}; const [s] = createSignal(${READ}); return <p>{/* @client */ s()}</p>`)).toEqual([])
|
|
63
|
+
expect(refused(`${R}; function value() { return ${READ} }; return <p>{/* @client */ value()}</p>`)).toEqual([])
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('a loop row or callback param named like a declaration is not that declaration', () => {
|
|
67
|
+
expect(refused(`${R}; function value() { return ${READ} }
|
|
68
|
+
return <p>{['ok'].map(value => <span key={value}>{value}</span>)}{/* @client */ value()}</p>`)).toEqual([])
|
|
69
|
+
expect(refused(`${R}; const m = createMemo(() => ${READ})
|
|
70
|
+
return <p>{['ok'].map(m => <span key={m}>{m}</span>)}{/* @client */ m()}</p>`)).toEqual([])
|
|
71
|
+
expect(refused(`${R}; return <ul>{[{ toString: 'x' }].map(R => <li key={R.toString}>{R.toString}</li>)}</ul>`)).toEqual([])
|
|
72
|
+
expect(refused(`${R}; const m = createMemo(() => [{ toString: 1 }].filter(R => R.toString).length); return <p>{m()}</p>`)).toEqual([])
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('a function is entered where it is called, not where it is stored', () => {
|
|
76
|
+
const H = `const H = { safe: 'ok', get: () => ${READ} }`
|
|
77
|
+
expect(refused(`${R}; ${H}; return <p>{H.safe}|{/* @client */ H.get()}</p>`)).toEqual([])
|
|
78
|
+
expect(refused(`${R}; ${H}; const m = createMemo(() => H.safe); return <p>{m()}</p>`)).toEqual([])
|
|
79
|
+
expect(refused(`${R}; ${H}; return <p>{H.get()}</p>`)).toEqual(['`R.toString`'])
|
|
80
|
+
expect(refused(`${R}; ${H}; const m = createMemo(() => H.get()); return <p>{m()}</p>`)).toEqual(['`R.toString`'])
|
|
81
|
+
expect(refused(`${R}; return <p>{(() => ${READ})()}</p>`)).toEqual(['`R.toString`'])
|
|
82
|
+
expect(refused(`${R}; return <p><Child f={() => ${READ}} /></p>`)).toEqual([])
|
|
83
|
+
expect(refused(`${R}; const [s, setS] = createSignal('a'); return <button onClick={() => setS(${READ})}>{s()}</button>`)).toEqual([])
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('an arrow const called with arguments is entered', () => {
|
|
87
|
+
expect(refused(`${R}; const f = (x: string) => (R.toString ? x : 'n'); const m = createMemo(() => f('a')); return <p>{m()}</p>`)).toEqual(['`R.toString`'])
|
|
88
|
+
expect(refused(`${R}; const f = (R: { toString: string }) => R.toString; const m = createMemo(() => f({ toString: 'a' })); return <p>{m()}</p>`)).toEqual([])
|
|
89
|
+
expect(refused(`${R}; const f = (x: string) => (R.toString ? x : 'n'); const m = createMemo(() => ['a'].map(f => f).length); return <p>{m()}</p>`)).toEqual([])
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
test('a const that only names another is read as that one', () => {
|
|
93
|
+
expect(refused(`${R}; const A = R; const m = createMemo(() => (A.toString ? 'y' : 'n')); return <p>{m()}</p>`)).toEqual(['`R.toString`'])
|
|
94
|
+
expect(refused(`${R}; const A = R; const m = createMemo(() => A.k + (A.nope ?? '')); return <p>{m()}</p>`)).toEqual([])
|
|
95
|
+
expect(refused(`${R}; const A = R; const m = createMemo(() => ['a'].map(A => A.toString).length); return <p>{m()}</p>`)).toEqual([])
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('a block-bodied memo is read through the expression it folds to', () => {
|
|
99
|
+
expect(refused(`${R}; const m = createMemo(() => { const x = R.toString; if (x) { return 'y' } return 'n' }); return <p>{m()}</p>`)).toEqual(['`R.toString`'])
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
test('a method that calls itself ends', () => {
|
|
103
|
+
expect(refused(`${R}; const H = { get: () => (R.toString ? H.get() : 'n') }; return <p>{H.get()}</p>`)).toEqual(['`R.toString`'])
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('only a read /* @client */ can defer claims that escape', () => {
|
|
107
|
+
const claims = (body: string) => errorsOf(irFor(body)).map(e => e.suggestion?.escape?.map(x => x.kind) ?? [])
|
|
108
|
+
expect(claims(`${R}; return <p>{${READ}}</p>`)).toEqual([['client-directive']])
|
|
109
|
+
expect(claims(`${R}; const m = createMemo(() => ${READ}); return <p>{m()}</p>`)).toEqual([['client-directive']])
|
|
110
|
+
expect(claims(`${R}; return <p><Child v={${READ}} /></p>`)).toEqual([[]])
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('a listed or provably missing key is not refused', () => {
|
|
114
|
+
expect(refused(`${R}; const m = createMemo(() => R.k + (R.nope ?? '')); return <p data-a={R.nope}>{R.k}{m()}</p>`)).toEqual([])
|
|
115
|
+
})
|
|
116
|
+
})
|
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* props, and reads an explicit prop written before it from the caller's
|
|
8
8
|
* props first in `initChild`'s `forwardProps(...)` overrides, so both merge
|
|
9
9
|
* like the JSX call does. A `{...rest}` bag is not the caller's props (it
|
|
10
|
-
* leaves out the destructured keys) and is not marked
|
|
10
|
+
* leaves out the destructured keys) and is not marked; it merges without
|
|
11
|
+
* those keys (#3714, `issue-3714-open-rest-spread-child.test.ts`).
|
|
11
12
|
*
|
|
12
13
|
* The end-to-end runtime check lives in
|
|
13
14
|
* `packages/client/__tests__/runtime/issue-3688-props-spread-merge.test.ts`;
|
|
@@ -130,14 +131,18 @@ describe('#3688 — the client JS merges a props-object spread in source order',
|
|
|
130
131
|
expect(js).not.toContain(`"tone" in _p`)
|
|
131
132
|
})
|
|
132
133
|
|
|
133
|
-
test('an open rest bag
|
|
134
|
+
test('an open rest bag is not merged as the props object', () => {
|
|
134
135
|
const js = clientJs(`
|
|
135
136
|
import { Tag } from './tag'
|
|
136
137
|
export function Rest({ title, ...rest }: { title?: string; [key: string]: any }) {
|
|
137
138
|
return <p title={title}><Tag title="x" {...rest} /></p>
|
|
138
139
|
}
|
|
139
140
|
`)
|
|
140
|
-
|
|
141
|
+
// The bag leaves out the destructured `title` (#3714), so the template
|
|
142
|
+
// spreads the caller's props without it, never `..._p`, and the explicit
|
|
143
|
+
// `title` written before it stays as written.
|
|
144
|
+
expect(js).toContain(`renderChild('Tag', {title: "x", ...forwardProps(_p, {}, ["title"])}`)
|
|
145
|
+
expect(js).not.toContain(`..._p`)
|
|
141
146
|
expect(js).not.toContain(`"title" in _p`)
|
|
142
147
|
})
|
|
143
148
|
})
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #3714 — an open-type `{...rest}` bag spread onto a child component in the
|
|
3
|
+
* client JS.
|
|
4
|
+
*
|
|
5
|
+
* The client holds the rest bag as the caller's whole props object (`_p`),
|
|
6
|
+
* so Phase 1 lists the caller keys the bag leaves out on the spread
|
|
7
|
+
* (`SpreadAttr.restOmittedKeys`): the keys the component destructured, from
|
|
8
|
+
* the signature or from a body destructure of the props object. Every
|
|
9
|
+
* reader of the bag takes them out: `initChild`'s `forwardProps(...)`, the
|
|
10
|
+
* registration template's `renderChild(...)` props, and `applyRestAttrs`.
|
|
11
|
+
*
|
|
12
|
+
* The rendered output is pinned by the `open-rest-spread-child*` and
|
|
13
|
+
* `open-body-rest-spread-*` conformance fixtures, in a real browser by
|
|
14
|
+
* `e2e/fixture-hydrate.playwright.ts` and `e2e/oracle.playwright.ts`.
|
|
15
|
+
*/
|
|
16
|
+
import { describe, test, expect } from 'bun:test'
|
|
17
|
+
import { compileJSX } from '../compiler'
|
|
18
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
19
|
+
import { analyzeComponent } from '../analyzer'
|
|
20
|
+
import { jsxToIR } from '../jsx-to-ir'
|
|
21
|
+
import type { AttrValue, IRNode } from '../types'
|
|
22
|
+
|
|
23
|
+
const adapter = new TestAdapter()
|
|
24
|
+
|
|
25
|
+
function clientJs(source: string): string {
|
|
26
|
+
const result = compileJSX(source, 'Repro.tsx', { adapter })
|
|
27
|
+
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
28
|
+
return result.files.find(f => f.type === 'clientJs')!.content
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Every spread value in `node`'s subtree, on elements and on component calls, in source order. */
|
|
32
|
+
function spreads(node: IRNode, out: AttrValue[] = []): AttrValue[] {
|
|
33
|
+
const attrs = node.type === 'element' ? node.attrs : node.type === 'component' ? node.props : []
|
|
34
|
+
for (const attr of attrs) if (attr.value.kind === 'spread') out.push(attr.value)
|
|
35
|
+
if (node.type === 'conditional') {
|
|
36
|
+
spreads(node.whenTrue, out)
|
|
37
|
+
spreads(node.whenFalse, out)
|
|
38
|
+
}
|
|
39
|
+
const children = 'children' in node && Array.isArray(node.children) ? (node.children as IRNode[]) : []
|
|
40
|
+
for (const child of children) spreads(child, out)
|
|
41
|
+
return out
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function spreadsOf(source: string, componentName: string): AttrValue[] {
|
|
45
|
+
return spreads(jsxToIR(analyzeComponent(source, 'Repro.tsx', componentName))!)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const OPEN = '{ title?: string; label?: string; kind?: string; tone?: string; [key: string]: unknown }'
|
|
49
|
+
|
|
50
|
+
describe('#3714 — Phase 1 lists the keys a rest bag leaves out', () => {
|
|
51
|
+
test('a signature rest omits every destructured key, by its caller-facing name', () => {
|
|
52
|
+
const [onChild, onElement] = spreadsOf(`
|
|
53
|
+
import { Tag } from './tag'
|
|
54
|
+
export function A({ title, label: text, kind = 'k', ...rest }: ${OPEN}) {
|
|
55
|
+
return <p title={title} lang={text} dir={kind}><Tag {...rest} /><i {...rest} /></p>
|
|
56
|
+
}
|
|
57
|
+
`, 'A')
|
|
58
|
+
expect(onChild).toMatchObject({ callerBag: 'rest', restOmittedKeys: ['title', 'label', 'kind'] })
|
|
59
|
+
expect(onElement).toMatchObject({ callerBag: 'rest', restOmittedKeys: ['title', 'label', 'kind'] })
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('a body rest omits only the keys its destructure takes, through an alias too', () => {
|
|
63
|
+
const [direct, aliased] = spreadsOf(`
|
|
64
|
+
import { Tag } from './tag'
|
|
65
|
+
export function B(props: ${OPEN}) {
|
|
66
|
+
const { title, ...rest } = props
|
|
67
|
+
const bag = rest
|
|
68
|
+
return <p title={title}><Tag {...rest} /><Tag {...bag} /></p>
|
|
69
|
+
}
|
|
70
|
+
`, 'B')
|
|
71
|
+
expect(direct).toMatchObject({ callerBag: 'rest', restOmittedKeys: ['title'] })
|
|
72
|
+
expect(aliased).toMatchObject({ callerBag: 'rest', restOmittedKeys: ['title'] })
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('the props object and a loop row named like the rest carry no omit list', () => {
|
|
76
|
+
const [props] = spreadsOf(`
|
|
77
|
+
import { Tag } from './tag'
|
|
78
|
+
export function C(props: ${OPEN}) { return <p><Tag {...props} /></p> }
|
|
79
|
+
`, 'C')
|
|
80
|
+
expect(props).toMatchObject({ callerBag: 'props' })
|
|
81
|
+
expect(props).not.toHaveProperty('restOmittedKeys')
|
|
82
|
+
const [row] = spreadsOf(`
|
|
83
|
+
import { Tag } from './tag'
|
|
84
|
+
export function D({ rows, ...rest }: { rows: Array<{ tone: string }>; [key: string]: unknown }) {
|
|
85
|
+
return <ul>{rows.map(rest => <li><Tag {...rest} /></li>)}</ul>
|
|
86
|
+
}
|
|
87
|
+
`, 'D')
|
|
88
|
+
expect(row).not.toHaveProperty('callerBag')
|
|
89
|
+
expect(row).not.toHaveProperty('restOmittedKeys')
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
describe('#3714 — the client reads a rest bag without the destructured keys', () => {
|
|
94
|
+
const js = clientJs(`
|
|
95
|
+
'use client'
|
|
96
|
+
import { Tag } from './tag'
|
|
97
|
+
export function Card({ title, label: text, items, ...rest }: { title?: string; label?: string; items: string[]; tone?: string; [key: string]: unknown }) {
|
|
98
|
+
return (
|
|
99
|
+
<div title={title} lang={text}>
|
|
100
|
+
<Tag {...rest} />
|
|
101
|
+
<Tag title="x" tone="first" {...rest} />
|
|
102
|
+
<Tag {...rest} tone="last" />
|
|
103
|
+
{title ? <Tag {...rest} /> : null}
|
|
104
|
+
<ul>{items.map(it => <li key={it}><Tag label={it} {...rest} /></li>)}</ul>
|
|
105
|
+
<i {...rest} />
|
|
106
|
+
</div>
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
`)
|
|
110
|
+
|
|
111
|
+
test('initChild forwards the bag without them, and merges in source order', () => {
|
|
112
|
+
// No explicit prop: only the destructured keys are held back.
|
|
113
|
+
expect(js).toContain(`forwardProps(_p, {}, ["title","label","items"])`)
|
|
114
|
+
// `title` is not in the bag, so the explicit one stays as written; the
|
|
115
|
+
// caller's `tone` overrides the one written before the spread.
|
|
116
|
+
expect(js).toContain(`forwardProps(_p, { title: "x", get tone() { return ("tone" in _p ? _p["tone"] : "first") } }, ["title","tone","label","items"])`)
|
|
117
|
+
// An explicit prop after the spread wins as written.
|
|
118
|
+
expect(js).toContain(`forwardProps(_p, { tone: "last" }, ["tone","title","label","items"])`)
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
test('a loop row and a conditional branch forward the bag too', () => {
|
|
122
|
+
expect(js).toContain(`upsertChild(__el, 'Tag', 's5', forwardProps(_p, { get label() { return it() } }, ["label","title","items"])`)
|
|
123
|
+
expect(js).toContain(`if (__c0) initChild('Tag', __c0, forwardProps(_p, {}, ["title","label","items"]))`)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
test('the registration template spreads the bag at its source position', () => {
|
|
127
|
+
const bag = `...forwardProps(_p, {}, ["title","label","items"])`
|
|
128
|
+
expect(js).toContain(`renderChild('Tag', {${bag}}, undefined, 's0')`)
|
|
129
|
+
expect(js).toContain(`renderChild('Tag', {title: "x", tone: "first", ${bag}}, undefined, 's1')`)
|
|
130
|
+
expect(js).toContain(`renderChild('Tag', {${bag}, tone: "last"}, undefined, 's2')`)
|
|
131
|
+
expect(js).toContain(`renderChild('Tag', {label: it, ${bag}}, undefined, 's5')`)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
test('an element spread keeps its exclusion list', () => {
|
|
135
|
+
expect(js).toContain(`applyRestAttrs(_s7, _p, ["title","label","items"])`)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('a body rest holds back only the keys its destructure takes', () => {
|
|
139
|
+
const body = clientJs(`
|
|
140
|
+
'use client'
|
|
141
|
+
import { Tag } from './tag'
|
|
142
|
+
export function Card(props: { title?: string; tone?: string; [key: string]: unknown }) {
|
|
143
|
+
const { title, ...rest } = props
|
|
144
|
+
return <div title={title}><Tag tone="first" {...rest} /><i {...rest} /></div>
|
|
145
|
+
}
|
|
146
|
+
`)
|
|
147
|
+
expect(body).toContain(`forwardProps(_p, { get tone() { return ("tone" in _p ? _p["tone"] : "first") } }, ["tone","title"])`)
|
|
148
|
+
expect(body).toContain(`renderChild('Tag', {tone: "first", ...forwardProps(_p, {}, ["title"])}, undefined, 's0')`)
|
|
149
|
+
expect(body).toContain(`applyRestAttrs(_s1, _p, ["title"])`)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
test("a call that spreads a caller bag spells its JSX children as the last `children`", () => {
|
|
153
|
+
const kbd = (children: string, params = '{ className, children, ...rest }') => clientJs(`
|
|
154
|
+
'use client'
|
|
155
|
+
import { Slot } from './slot'
|
|
156
|
+
export function Kbd(${params}: { className?: string; children?: string; [key: string]: unknown }) {
|
|
157
|
+
return <div><Slot className={className} {...rest}>${children}</Slot></div>
|
|
158
|
+
}
|
|
159
|
+
`)
|
|
160
|
+
// The bag leaves the destructured `children` out; the call's JSX
|
|
161
|
+
// children are written as its own entry, whatever they are.
|
|
162
|
+
expect(kbd('{children}')).toContain(`get children() { return children } }, ["className","children"]))`)
|
|
163
|
+
expect(kbd(`{children ?? 'fallback'}`)).toContain(`get children() { return children ?? 'fallback' } }, ["className","children"]))`)
|
|
164
|
+
expect(kbd('text')).toContain(`get children() { return "text" } }, ["className","children"]))`)
|
|
165
|
+
expect(kbd('<b>x</b>')).toContain('get children() { return `<b>x</b>` } }, ["className","children"]))')
|
|
166
|
+
// The bag carries the caller's `children`: the call's own still win,
|
|
167
|
+
// as an override `forwardProps` never replaces.
|
|
168
|
+
expect(kbd('own', '{ className, ...rest }')).toContain(`get children() { return "own" } }, ["className"]))`)
|
|
169
|
+
// The props object is a caller bag too.
|
|
170
|
+
expect(clientJs(`
|
|
171
|
+
'use client'
|
|
172
|
+
import { Slot } from './slot'
|
|
173
|
+
export function Whole(props: { children?: string; [key: string]: unknown }) {
|
|
174
|
+
return <div><Slot {...props}>own</Slot></div>
|
|
175
|
+
}
|
|
176
|
+
`)).toContain(`initChild('Slot', _s0, forwardProps(_p, { get children() { return "own" } }, []))`)
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
test('a call without a caller bag keeps its props object as written', () => {
|
|
180
|
+
expect(clientJs(`
|
|
181
|
+
'use client'
|
|
182
|
+
import { Slot } from './slot'
|
|
183
|
+
export function Plain({ children }: { children?: string }) {
|
|
184
|
+
return <div><Slot tone="a">{children}</Slot></div>
|
|
185
|
+
}
|
|
186
|
+
`)).toContain(`initChild('Slot', _s0, { tone: "a" })`)
|
|
187
|
+
})
|
|
188
|
+
})
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Which zero-argument calls a template reads as the called function's
|
|
2
|
+
// returned expression (`localCallBodies`) and which it refuses
|
|
3
|
+
// (`uninlinableLocalCalls`), against metadata built by the real pipeline. The
|
|
4
|
+
// rendered values are asserted by the conformance fixtures
|
|
5
|
+
// `memo-local-function-named-like-prop`, `signal-seed-local-function-named-like-prop`,
|
|
6
|
+
// `markup-local-function-named-like-prop` and `local-bindings-named-like-props`.
|
|
7
|
+
|
|
8
|
+
import { describe, test, expect } from 'bun:test'
|
|
9
|
+
import { analyzeComponent } from '../analyzer'
|
|
10
|
+
import { buildMetadata } from '../compiler'
|
|
11
|
+
import { localCallBodies, uninlinableLocalCalls } from '../local-call-body'
|
|
12
|
+
import { stringifyParsedExpr } from '../expression-parser'
|
|
13
|
+
|
|
14
|
+
function metadataFor(body: string, signature = 'props: { value?: number }', preamble = '') {
|
|
15
|
+
return buildMetadata(analyzeComponent(`
|
|
16
|
+
'use client'
|
|
17
|
+
import { createSignal } from '@barefootjs/client'
|
|
18
|
+
${preamble}
|
|
19
|
+
export function C(${signature}) {
|
|
20
|
+
${body}
|
|
21
|
+
return <p>x</p>
|
|
22
|
+
}
|
|
23
|
+
`, 'C.tsx'))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function bodies(metadata: ReturnType<typeof metadataFor>): Record<string, string> {
|
|
27
|
+
return Object.fromEntries([...localCallBodies(metadata)].map(([name, c]) => [`${name}@${c.scope}`, stringifyParsedExpr(c.body)]))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe('localCallBodies', () => {
|
|
31
|
+
test('a component function named like a typed prop the body never binds', () => {
|
|
32
|
+
expect(bodies(metadataFor('function value() { return 1 }'))).toEqual({ 'value@component': '1' })
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test('a zero-param arrow const and a module function', () => {
|
|
36
|
+
const m = metadataFor('const value = () => 1', 'props: { value?: number }', "function tag() { return 'T' }")
|
|
37
|
+
expect(bodies(m)).toEqual({ 'value@component': '1', 'tag@module': '"T"' })
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test('a function with a parameter or a multi-statement body is not listed', () => {
|
|
41
|
+
expect(bodies(metadataFor('function a(n: number) { return n } function b() { const v = 1; return v }'))).toEqual({})
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('a module function a destructured parameter of the same name hides', () => {
|
|
45
|
+
expect(bodies(metadataFor('', '{ tag }: { tag?: string }', "function tag() { return 'T' }"))).toEqual({})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('a module function a body destructure of the prop hides', () => {
|
|
49
|
+
expect(bodies(metadataFor('const { tag } = props', 'props: { tag?: string }', "function tag() { return 'T' }"))).toEqual({})
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
describe('lexical resolution', () => {
|
|
54
|
+
// The rendered refusal is `signal-seed-uninlinable-local-function-hides-module-function`.
|
|
55
|
+
test('a component function a template cannot inline hides the module function of its name', () => {
|
|
56
|
+
const m = metadataFor('function value() { const x = 2; return x }', 'props: { value?: number }', 'function value() { return 1 }')
|
|
57
|
+
expect(bodies(m)).toEqual({})
|
|
58
|
+
expect([...uninlinableLocalCalls(m)]).toEqual(['value'])
|
|
59
|
+
// A module function's body still calls the module's.
|
|
60
|
+
expect(Object.fromEntries([...localCallBodies(m, 'module')].map(([n, c]) => [n, stringifyParsedExpr(c.body)]))).toEqual({ value: '1' })
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('a component arrow const with a parameter hides the module function of its name', () => {
|
|
64
|
+
const m = metadataFor('const value = (a?: number) => 2', 'props: { value?: number }', 'function value() { return 1 }')
|
|
65
|
+
expect(bodies(m)).toEqual({})
|
|
66
|
+
expect([...uninlinableLocalCalls(m)]).toEqual(['value'])
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('a function declared inside a nested function hides nothing', () => {
|
|
70
|
+
const m = metadataFor("function inner() { function tag() { return 'X' } return tag() }", 'props: {}', "function tag() { return 'T' }")
|
|
71
|
+
expect(bodies(m)['tag@module']).toBe('"T"')
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test("a module function's call of another function is the module's, inlined in module scope", () => {
|
|
75
|
+
const m = metadataFor("function inner() { return 'component' }", 'props: {}', "function inner() { return 'module' } function outer() { return inner() }")
|
|
76
|
+
expect(bodies(m)).toEqual({ 'inner@component': '"component"', 'outer@module': '"module"' })
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('a module function calling one a template cannot inline is not inlinable either', () => {
|
|
80
|
+
const m = metadataFor('', 'props: {}', 'function inner() { const x = 1; return x } function outer() { return inner() }')
|
|
81
|
+
expect(bodies(m)).toEqual({})
|
|
82
|
+
expect([...uninlinableLocalCalls(m)].sort()).toEqual(['inner', 'outer'])
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('a module function calling itself is not inlinable', () => {
|
|
86
|
+
const m = metadataFor('', 'props: {}', 'function loop(): number { return loop() }')
|
|
87
|
+
expect(bodies(m)).toEqual({})
|
|
88
|
+
expect([...uninlinableLocalCalls(m)]).toEqual(['loop'])
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
describe('uninlinableLocalCalls', () => {
|
|
93
|
+
test('lists the functions a template cannot inline, not the ones it can', () => {
|
|
94
|
+
const m = metadataFor('function a(n: number) { return n } function b() { const v = 1; return v } function c() { return 1 }')
|
|
95
|
+
expect([...uninlinableLocalCalls(m)].sort()).toEqual(['a', 'b'])
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('a signal getter is not a function call to refuse', () => {
|
|
99
|
+
expect([...uninlinableLocalCalls(metadataFor('const [n] = createSignal(0)'))]).toEqual([])
|
|
100
|
+
})
|
|
101
|
+
})
|