@barefootjs/jsx 0.40.1 → 0.40.2
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/ambiguous-nil-arithmetic.d.ts +4 -3
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -1
- package/dist/adapters/boolean-result.d.ts +32 -10
- package/dist/adapters/boolean-result.d.ts.map +1 -1
- package/dist/adapters/brace-delimiter-text.d.ts +18 -0
- package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
- package/dist/adapters/callback-method.d.ts +38 -5
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +11 -1
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +21 -0
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-bound-names.d.ts +9 -1
- package/dist/adapters/loop-bound-names.d.ts.map +1 -1
- package/dist/adapters/loop-row-type.d.ts +52 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -0
- package/dist/adapters/nil-reading.d.ts +68 -0
- package/dist/adapters/nil-reading.d.ts.map +1 -0
- package/dist/adapters/non-primitive-equality.d.ts +33 -0
- package/dist/adapters/non-primitive-equality.d.ts.map +1 -0
- package/dist/adapters/number-value.d.ts +5 -3
- package/dist/adapters/number-value.d.ts.map +1 -1
- package/dist/adapters/operand-typing.d.ts +126 -0
- package/dist/adapters/operand-typing.d.ts.map +1 -0
- package/dist/adapters/parsed-expr-emitter.d.ts +70 -12
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/primitive-plan.d.ts +291 -0
- package/dist/adapters/primitive-plan.d.ts.map +1 -0
- package/dist/analyzer-context.d.ts +4 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +63 -8
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/compiler.d.ts.map +1 -1
- package/dist/event-handler-name.d.ts +12 -0
- package/dist/event-handler-name.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +8 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +19 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4628 -2198
- package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
- package/dist/ir-to-client-js/child-props-object.d.ts +64 -0
- package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
- package/dist/ir-to-client-js/collect-elements.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/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +4 -2
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +2 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts +2 -6
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +30 -18
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts +1 -0
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +68 -9
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +7 -0
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-object-whole-read.d.ts +15 -0
- package/dist/props-object-whole-read.d.ts.map +1 -0
- package/dist/rich-type-refusal.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +85 -10
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/lexical-scope-walk.d.ts +3 -2
- package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
- package/dist/scope/root-prop-alias.d.ts +10 -7
- package/dist/scope/root-prop-alias.d.ts.map +1 -1
- package/dist/strip-types.d.ts +13 -0
- package/dist/strip-types.d.ts.map +1 -1
- package/dist/template-position-walk.d.ts +21 -3
- package/dist/template-position-walk.d.ts.map +1 -1
- package/dist/types.d.ts +90 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +3 -3
- package/src/__tests__/binding-scope.test.ts +89 -1
- package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
- package/src/__tests__/boolean-result.test.ts +22 -5
- package/src/__tests__/brace-delimiter-text.test.ts +26 -0
- package/src/__tests__/callback-method.test.ts +62 -2
- package/src/__tests__/child-components-in-map.test.ts +1 -1
- package/src/__tests__/computed-const.test.ts +103 -0
- package/src/__tests__/emit-to-string-call.test.ts +2 -1
- package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
- package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
- package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
- package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
- package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
- package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +2 -1
- package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
- package/src/__tests__/lookup-literal-const.test.ts +9 -3
- package/src/__tests__/loop-index-reactivity.test.ts +2 -2
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
- package/src/__tests__/loop-plan.test.ts +1 -1
- package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
- package/src/__tests__/loop-row-type.test.ts +64 -0
- package/src/__tests__/lowering-value-position.test.ts +4 -0
- package/src/__tests__/module-const-destructure-default.test.ts +156 -0
- package/src/__tests__/nested-loop-index-param.test.ts +1 -1
- package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
- package/src/__tests__/nested-row-shadowing.test.ts +2 -2
- package/src/__tests__/nil-reading.test.ts +77 -0
- package/src/__tests__/nullable-signal-attr.test.ts +134 -0
- package/src/__tests__/primitive-plan.test.ts +416 -0
- package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
- package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
- package/src/__tests__/props-object-whole-read.test.ts +40 -0
- package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
- package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
- package/src/__tests__/rest-binding-gaps.test.ts +192 -0
- package/src/__tests__/root-prop-alias.test.ts +53 -8
- package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
- package/src/__tests__/static-record-literal-array.test.ts +171 -0
- package/src/__tests__/string-concat-identifier.test.ts +40 -1
- package/src/adapters/ambiguous-nil-arithmetic.ts +22 -13
- package/src/adapters/boolean-result.ts +72 -66
- package/src/adapters/brace-delimiter-text.ts +25 -0
- package/src/adapters/callback-method.ts +151 -27
- package/src/adapters/computed-const.ts +238 -4
- package/src/adapters/dsl-template-adapter.ts +40 -2
- package/src/adapters/loop-bound-names.ts +9 -1
- package/src/adapters/loop-plan.ts +1 -1
- package/src/adapters/loop-row-type.ts +139 -0
- package/src/adapters/nil-reading.ts +297 -0
- package/src/adapters/non-primitive-equality.ts +92 -0
- package/src/adapters/number-value.ts +17 -4
- package/src/adapters/operand-typing.ts +522 -0
- package/src/adapters/parsed-expr-emitter.ts +154 -19
- package/src/adapters/primitive-plan.ts +696 -0
- package/src/analyzer-context.ts +6 -0
- package/src/analyzer.ts +505 -19
- package/src/augment-inherited-props.ts +108 -10
- package/src/compiler.ts +17 -0
- package/src/event-handler-name.ts +15 -0
- package/src/expression-parser.ts +70 -1
- package/src/index.ts +33 -7
- package/src/ir-to-client-js/build-references.ts +69 -0
- package/src/ir-to-client-js/child-props-object.ts +106 -0
- package/src/ir-to-client-js/collect-elements.ts +30 -55
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +6 -1
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +29 -28
- package/src/ir-to-client-js/control-flow/shared.ts +20 -22
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +5 -3
- package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +1 -1
- package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/loop.ts +1 -1
- package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +3 -3
- package/src/ir-to-client-js/html-template.ts +25 -8
- package/src/ir-to-client-js/imports.ts +1 -1
- package/src/ir-to-client-js/index.ts +53 -11
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +31 -14
- package/src/ir-to-client-js/plan/static-array-child-init.ts +27 -18
- package/src/ir-to-client-js/prop-handling.ts +22 -3
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
- package/src/jsx-to-ir.ts +446 -37
- package/src/nullable-signal-attr.ts +246 -29
- package/src/prop-rewrite.ts +30 -6
- package/src/props-object-whole-read.ts +87 -0
- package/src/rich-type-refusal.ts +2 -1
- package/src/scope/binding-scope.ts +182 -18
- package/src/scope/lexical-scope-walk.ts +4 -3
- package/src/scope/root-prop-alias.ts +15 -8
- package/src/strip-types.ts +31 -3
- package/src/template-position-walk.ts +57 -28
- package/src/types.ts +87 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the client reads beyond its named props, decided before the adapter
|
|
3
|
+
* runs (#3845): a destructured prop read only in a `.filter()` predicate
|
|
4
|
+
* reaches the module-scope client template as `_p.<key>`, a closed-type rest
|
|
5
|
+
* key the client reads is in `clientAnalysis.usedRestKeys`, and a spread of
|
|
6
|
+
* a whole caller bag sets `clientAnalysis.readsCallerBag`.
|
|
7
|
+
*
|
|
8
|
+
* The rendered shapes are conformance fixtures (`filter-predicate-on-prefixed-prop`,
|
|
9
|
+
* `rest-spread-root-hydration-props`, …). These pin the name-binding edges
|
|
10
|
+
* the fixtures do not reach: a filter parameter or a loop row named like the
|
|
11
|
+
* prop or the bag.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { describe, test, expect } from 'bun:test'
|
|
15
|
+
import { compileJSX } from '../compiler'
|
|
16
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
17
|
+
import { isHydrationPayloadPropName } from '../event-handler-name'
|
|
18
|
+
import type { ComponentIR } from '../types'
|
|
19
|
+
|
|
20
|
+
const adapter = new TestAdapter()
|
|
21
|
+
|
|
22
|
+
function compile(source: string): { js: string; ir: ComponentIR } {
|
|
23
|
+
const result = compileJSX(source, 'C.tsx', { adapter, outputIR: true })
|
|
24
|
+
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
25
|
+
const js = result.files.find(f => f.type === 'clientJs')!.content
|
|
26
|
+
const irs = result.files.filter(f => f.type === 'ir').map(f => JSON.parse(f.content) as ComponentIR)
|
|
27
|
+
const ir = irs.find(c => c.metadata.componentName === 'C')!
|
|
28
|
+
return { js, ir }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The `template: (_p) => …` lambda the client registers for `C`. */
|
|
32
|
+
function templateLine(js: string): string {
|
|
33
|
+
const line = js.split('\n').find(l => l.includes("hydrate('C'") && l.includes('template: (_p) =>'))
|
|
34
|
+
expect(line).toBeDefined()
|
|
35
|
+
return line!
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Everything the client registers for `C`, including a multi-line template. */
|
|
39
|
+
function templateSource(js: string): string {
|
|
40
|
+
const start = js.indexOf("hydrate('C'")
|
|
41
|
+
expect(start).toBeGreaterThanOrEqual(0)
|
|
42
|
+
return js.slice(start)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('filter predicate in the client template (#3845)', () => {
|
|
46
|
+
test('a destructured prop read in the predicate becomes `_p.<key>`', () => {
|
|
47
|
+
const { js } = compile(`
|
|
48
|
+
'use client'
|
|
49
|
+
export function C({ keys, one }: { keys: string[]; one: string }) {
|
|
50
|
+
return <ul>{keys.filter(k => k === one).map(k => <li key={k}>{k}</li>)}</ul>
|
|
51
|
+
}
|
|
52
|
+
`)
|
|
53
|
+
expect(templateLine(js)).toContain('.filter(k => k === _p.one)')
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('the predicate parameter keeps its name when a prop shares it', () => {
|
|
57
|
+
const { js } = compile(`
|
|
58
|
+
'use client'
|
|
59
|
+
export function C({ keys, k }: { keys: string[]; k: string }) {
|
|
60
|
+
return <div data-k={k}><ul>{keys.filter(k => k !== '').map(v => <li key={v}>{v}</li>)}</ul></div>
|
|
61
|
+
}
|
|
62
|
+
`)
|
|
63
|
+
expect(templateLine(js)).toContain(".filter(k => k !== '')")
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('an enclosing row binding named like the prop keeps its name', () => {
|
|
67
|
+
const { js } = compile(`
|
|
68
|
+
'use client'
|
|
69
|
+
export function C({ groups, one }: { groups: { one: string; keys: string[] }[]; one: string }) {
|
|
70
|
+
return (
|
|
71
|
+
<div data-one={one}>
|
|
72
|
+
{groups.map(({ one, keys }) => (
|
|
73
|
+
<ul key={one}>{keys.filter(k => k === one).map(k => <li key={k}>{k}</li>)}</ul>
|
|
74
|
+
))}
|
|
75
|
+
</div>
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
`)
|
|
79
|
+
const line = templateLine(js)
|
|
80
|
+
expect(line).toContain('.filter(k => k === one)')
|
|
81
|
+
expect(line).not.toContain('k === _p.one')
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('a block body rewrites the outer read and keeps an inner `const` of the same name', () => {
|
|
85
|
+
const { js } = compile(`
|
|
86
|
+
'use client'
|
|
87
|
+
export function C({ keys, one }: { keys: string[]; one: string }) {
|
|
88
|
+
return <ul>{keys.filter(k => {
|
|
89
|
+
const v = one
|
|
90
|
+
if (k === 'a') {
|
|
91
|
+
const one = 'a'
|
|
92
|
+
return k === one
|
|
93
|
+
}
|
|
94
|
+
return k === v
|
|
95
|
+
}).map(k => <li key={k}>{k}</li>)}</ul>
|
|
96
|
+
}
|
|
97
|
+
`)
|
|
98
|
+
const tpl = templateSource(js)
|
|
99
|
+
expect(tpl).toContain('const v = _p.one')
|
|
100
|
+
expect(tpl).toContain("const one = 'a'")
|
|
101
|
+
expect(tpl).toContain('return k === one')
|
|
102
|
+
expect(tpl).not.toContain('const _p.one')
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test('a block body keeps a nested-block `var` hoisted over the whole body', () => {
|
|
106
|
+
const { js } = compile(`
|
|
107
|
+
'use client'
|
|
108
|
+
export function C({ keys, one }: { keys: string[]; one: string }) {
|
|
109
|
+
return <ul>{keys.filter(k => {
|
|
110
|
+
if (k === 'a') {
|
|
111
|
+
var one = 'a'
|
|
112
|
+
}
|
|
113
|
+
return k === one
|
|
114
|
+
}).map(k => <li key={k}>{k}</li>)}</ul>
|
|
115
|
+
}
|
|
116
|
+
`)
|
|
117
|
+
const tpl = templateSource(js)
|
|
118
|
+
expect(tpl).toContain('return k === one')
|
|
119
|
+
expect(tpl).not.toContain('_p.one')
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
describe('client analysis of the hydration payload (#3845)', () => {
|
|
124
|
+
test('closed-type rest keys the client reads are listed', () => {
|
|
125
|
+
const { ir } = compile(`
|
|
126
|
+
'use client'
|
|
127
|
+
export function C({ label, ...rest }: { label: string; tone?: string; size?: string }) {
|
|
128
|
+
return <button {...rest} onClick={() => {}}>{label}</button>
|
|
129
|
+
}
|
|
130
|
+
`)
|
|
131
|
+
expect(ir.metadata.clientAnalysis?.usedRestKeys).toEqual(['tone', 'size'])
|
|
132
|
+
expect(ir.metadata.clientAnalysis?.readsCallerBag).toBe(false)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test('a whole props-object spread onto a child reads the caller bag', () => {
|
|
136
|
+
const { ir } = compile(`
|
|
137
|
+
'use client'
|
|
138
|
+
function Chip(props: { label: string }) { return <button onClick={() => {}}>{props.label}</button> }
|
|
139
|
+
export function C(props: { label: string }) {
|
|
140
|
+
return <div><Chip {...props} /></div>
|
|
141
|
+
}
|
|
142
|
+
`)
|
|
143
|
+
expect(ir.metadata.clientAnalysis?.readsCallerBag).toBe(true)
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
test('a loop row named like the props object is not the caller bag', () => {
|
|
147
|
+
const { ir } = compile(`
|
|
148
|
+
'use client'
|
|
149
|
+
function Chip(p: { label: string }) { return <button onClick={() => {}}>{p.label}</button> }
|
|
150
|
+
export function C(props: { rows: { label: string }[] }) {
|
|
151
|
+
return <div>{props.rows.map((props) => <Chip key={props.label} {...props} />)}</div>
|
|
152
|
+
}
|
|
153
|
+
`)
|
|
154
|
+
expect(ir.metadata.clientAnalysis?.readsCallerBag).toBe(false)
|
|
155
|
+
})
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
describe('isHydrationPayloadPropName', () => {
|
|
159
|
+
test('`on[A-Z]` handlers and `__` plumbing stay out; `on`-prefixed data props travel', () => {
|
|
160
|
+
expect(isHydrationPayloadPropName('onClick')).toBe(false)
|
|
161
|
+
expect(isHydrationPayloadPropName('__instanceId')).toBe(false)
|
|
162
|
+
for (const name of ['on', 'one', 'only', 'once', 'online']) {
|
|
163
|
+
expect(isHydrationPayloadPropName(name)).toBe(true)
|
|
164
|
+
}
|
|
165
|
+
})
|
|
166
|
+
})
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
* `.map()` of child components made the loop's array look, to
|
|
4
4
|
* `isArrayExprDirectPropRef` (jsx-to-ir.ts), like a local constant with no
|
|
5
5
|
* prop/signal origin. The loop was then classified `isStaticArray: true`
|
|
6
|
-
* and compiled to the static `
|
|
6
|
+
* and compiled to the static `childScopesBySlot` + `forEach` init path instead
|
|
7
7
|
* of `mapArray`. That static path's `renderChild()` calls (client runtime)
|
|
8
8
|
* carry no `bf-h`/`bf-m` scope-relationship attributes on a CSR-only mount
|
|
9
|
-
* (no existing SSR markup to hydrate against), so the `
|
|
9
|
+
* (no existing SSR markup to hydrate against), so the `childScopesBySlot`
|
|
10
10
|
* selector in the static init never matches — the row's child component
|
|
11
11
|
* never gets `initChild`ed, so its signals and event listeners never wire
|
|
12
12
|
* up. Found by the #2481 mutation sweep's `alias-props` mutation against
|
|
@@ -100,7 +100,7 @@ describe('#2724 — prop-alias keyed loop stays on mapArray, not the static path
|
|
|
100
100
|
}
|
|
101
101
|
`, 'AliasHop.tsx')
|
|
102
102
|
expect(clientJs).toMatch(/\bmapArray(Lazy)?\s*\(/)
|
|
103
|
-
expect(clientJs).not.toMatch(/
|
|
103
|
+
expect(clientJs).not.toMatch(/childScopesBySlot\(/)
|
|
104
104
|
})
|
|
105
105
|
|
|
106
106
|
test('local const derived from a whole-props member access uses mapArray', () => {
|
|
@@ -115,7 +115,7 @@ describe('#2724 — prop-alias keyed loop stays on mapArray, not the static path
|
|
|
115
115
|
}
|
|
116
116
|
`, 'MemberAlias.tsx')
|
|
117
117
|
expect(clientJs).toMatch(/\bmapArray(Lazy)?\s*\(/)
|
|
118
|
-
expect(clientJs).not.toMatch(/
|
|
118
|
+
expect(clientJs).not.toMatch(/childScopesBySlot\(/)
|
|
119
119
|
})
|
|
120
120
|
|
|
121
121
|
test('two-hop alias chain uses mapArray', () => {
|
|
@@ -131,7 +131,7 @@ describe('#2724 — prop-alias keyed loop stays on mapArray, not the static path
|
|
|
131
131
|
}
|
|
132
132
|
`, 'TwoHop.tsx')
|
|
133
133
|
expect(clientJs).toMatch(/\bmapArray(Lazy)?\s*\(/)
|
|
134
|
-
expect(clientJs).not.toMatch(/
|
|
134
|
+
expect(clientJs).not.toMatch(/childScopesBySlot\(/)
|
|
135
135
|
})
|
|
136
136
|
|
|
137
137
|
test('an alias of the WHOLE props object, accessed via property access, uses mapArray', () => {
|
|
@@ -202,7 +202,7 @@ describe('#2724 — prop-alias keyed loop stays on mapArray, not the static path
|
|
|
202
202
|
return <div>{ITEMS.map((item) => <Item key={item.label} label={item.label} />)}</div>
|
|
203
203
|
}
|
|
204
204
|
`, 'ModuleConst.tsx')
|
|
205
|
-
expect(clientJs).toMatch(/
|
|
205
|
+
expect(clientJs).toMatch(/childScopesBySlot\(/)
|
|
206
206
|
expect(clientJs).not.toMatch(/\bmapArray(Lazy)?\s*\(/)
|
|
207
207
|
})
|
|
208
208
|
|
|
@@ -221,7 +221,7 @@ describe('#2724 — prop-alias keyed loop stays on mapArray, not the static path
|
|
|
221
221
|
return <div>{state.items.map((item) => <Item key={item.label} label={item.label} />)}</div>
|
|
222
222
|
}
|
|
223
223
|
`, 'NameCollision.tsx')
|
|
224
|
-
expect(clientJs).toMatch(/
|
|
224
|
+
expect(clientJs).toMatch(/childScopesBySlot\(/)
|
|
225
225
|
expect(clientJs).not.toMatch(/\bmapArray(Lazy)?\s*\(/)
|
|
226
226
|
})
|
|
227
227
|
|
|
@@ -70,10 +70,13 @@ describe('#2754 — a caller-props forward earns a slot and an init', () => {
|
|
|
70
70
|
test('a spread of an ordinary object still inlines into the template and earns no slot', () => {
|
|
71
71
|
// Reverse direction: only the caller-props forward is unknowable at
|
|
72
72
|
// compile time. An ordinary object spread is fully emitted by both
|
|
73
|
-
// templates and must not start allocating slot ids.
|
|
73
|
+
// templates and must not start allocating slot ids. (Its value reads a
|
|
74
|
+
// name, so it stays a spread: a static literal one is expanded per key
|
|
75
|
+
// in Phase 1, #3844.)
|
|
74
76
|
const { clientJs, template } = compile(`
|
|
75
77
|
"use client";
|
|
76
|
-
const
|
|
78
|
+
const TITLE = 'x'
|
|
79
|
+
const extra = { title: TITLE }
|
|
77
80
|
export function Ordinary() {
|
|
78
81
|
return <span className="plain" {...extra} />
|
|
79
82
|
}
|
|
@@ -97,7 +97,7 @@ export function StaticNestedSignalCase() {
|
|
|
97
97
|
const js = compileClientJs(source)
|
|
98
98
|
// Pre-fix, this text was frozen at its SSR-baked value — no
|
|
99
99
|
// `createEffect` at all inside the inner forEach body.
|
|
100
|
-
expect(js).toMatch(/createEffect\(\(\) => \{ __bfw_\w+\('s\d+',
|
|
100
|
+
expect(js).toMatch(/createEffect\(\(\) => \{ __bfw_\w+\('s\d+', textOrNode\(count\(\)\)\) \}\)/)
|
|
101
101
|
})
|
|
102
102
|
|
|
103
103
|
test('a static outer loop with a nested .map() wires a reactive attr on the inner element', () => {
|
|
@@ -169,7 +169,7 @@ describe('#2835 — name collision with a whole-props type member is not prop-de
|
|
|
169
169
|
}
|
|
170
170
|
`, 'DestructuredRegression.tsx')
|
|
171
171
|
expect(clientJs).toMatch(/\bmapArray(Lazy)?\s*\(/)
|
|
172
|
-
expect(clientJs).not.toMatch(/
|
|
172
|
+
expect(clientJs).not.toMatch(/childScopesBySlot\(/)
|
|
173
173
|
})
|
|
174
174
|
|
|
175
175
|
test('regression guard: a renamed destructure ({ base: rows }) still resolves to mapArray', () => {
|
|
@@ -183,7 +183,7 @@ describe('#2835 — name collision with a whole-props type member is not prop-de
|
|
|
183
183
|
}
|
|
184
184
|
`, 'RenamedDestructureRegression.tsx')
|
|
185
185
|
expect(clientJs).toMatch(/\bmapArray(Lazy)?\s*\(/)
|
|
186
|
-
expect(clientJs).not.toMatch(/
|
|
186
|
+
expect(clientJs).not.toMatch(/childScopesBySlot\(/)
|
|
187
187
|
})
|
|
188
188
|
|
|
189
189
|
test('regression guard: a renamed destructure keeps a same-named module const static (no name collision through the rename)', () => {
|
|
@@ -212,7 +212,7 @@ describe('#2835 — name collision with a whole-props type member is not prop-de
|
|
|
212
212
|
}
|
|
213
213
|
`, 'WholePropsBodyDestructure.tsx')
|
|
214
214
|
expect(clientJs).toMatch(/\bmapArray(Lazy)?\s*\(/)
|
|
215
|
-
expect(clientJs).not.toMatch(/
|
|
215
|
+
expect(clientJs).not.toMatch(/childScopesBySlot\(/)
|
|
216
216
|
})
|
|
217
217
|
|
|
218
218
|
test('regression guard: direct `props.base` access still resolves to mapArray', () => {
|
|
@@ -226,7 +226,7 @@ describe('#2835 — name collision with a whole-props type member is not prop-de
|
|
|
226
226
|
}
|
|
227
227
|
`, 'DirectMemberAccessRegression.tsx')
|
|
228
228
|
expect(clientJs).toMatch(/\bmapArray(Lazy)?\s*\(/)
|
|
229
|
-
expect(clientJs).not.toMatch(/
|
|
229
|
+
expect(clientJs).not.toMatch(/childScopesBySlot\(/)
|
|
230
230
|
})
|
|
231
231
|
|
|
232
232
|
test('regression guard: a rest-spread sibling name is unaffected (no collision to guard against)', () => {
|
|
@@ -84,7 +84,7 @@ describe('#3044 — nested array off a destructured object prop uses mapArray, n
|
|
|
84
84
|
}
|
|
85
85
|
`, 'ReproChild.tsx')
|
|
86
86
|
expect(clientJs).toMatch(/\bmapArray(Lazy)?\s*\(/)
|
|
87
|
-
expect(clientJs).not.toMatch(/
|
|
87
|
+
expect(clientJs).not.toMatch(/childScopesBySlot\(/)
|
|
88
88
|
})
|
|
89
89
|
|
|
90
90
|
test('flags: `data.items.map(...)` is classified isPropDerivedArray / non-static', () => {
|
|
@@ -69,8 +69,9 @@ describe('#3688 — Phase 1 marks a props-object spread onto a child', () => {
|
|
|
69
69
|
expect(rest).not.toHaveProperty('propsObject')
|
|
70
70
|
const local = tagSpread(`
|
|
71
71
|
import { Tag } from './tag'
|
|
72
|
-
export function D(props: { tone?: string }) { const extra = { tone:
|
|
72
|
+
export function D(props: { tone?: string }) { const extra = { tone: props.tone }; return <p><Tag {...extra} /></p> }
|
|
73
73
|
`, 'D')
|
|
74
|
+
expect(local.kind).toBe('spread')
|
|
74
75
|
expect(local).not.toHaveProperty('propsObject')
|
|
75
76
|
})
|
|
76
77
|
})
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #3713 — Phase 1's props-object spread marker (`SpreadAttr.propsObject`)
|
|
3
|
+
* on a child component inside a loop row. The client's loop-row props
|
|
4
|
+
* builders merge a marked spread in source order through the shared
|
|
5
|
+
* `childPropsObjectExpr`, so the marker must be set on a row's
|
|
6
|
+
* `{...props}` and must not be set when a row binding named `props`
|
|
7
|
+
* shadows the props object.
|
|
8
|
+
*
|
|
9
|
+
* The end-to-end runtime check (mount, row update, hydration) lives in
|
|
10
|
+
* `packages/client/__tests__/runtime/issue-3713-props-spread-loop-row.test.ts`;
|
|
11
|
+
* the rendered output is pinned by the `component-*-in-loop-row` and
|
|
12
|
+
* `component-spread-props-in-static-loop-row` conformance fixtures.
|
|
13
|
+
*/
|
|
14
|
+
import { describe, test, expect } from 'bun:test'
|
|
15
|
+
import { analyzeComponent } from '../analyzer'
|
|
16
|
+
import { jsxToIR } from '../jsx-to-ir'
|
|
17
|
+
import type { IRNode, IRComponent, AttrValue } from '../types'
|
|
18
|
+
|
|
19
|
+
function findComponents(node: IRNode, name: string, out: IRComponent[] = []): IRComponent[] {
|
|
20
|
+
if (node.type === 'component' && node.name === name) out.push(node)
|
|
21
|
+
const children: IRNode[] = []
|
|
22
|
+
if ('children' in node && Array.isArray(node.children)) children.push(...(node.children as IRNode[]))
|
|
23
|
+
if (node.type === 'conditional') children.push(node.whenTrue, node.whenFalse)
|
|
24
|
+
for (const child of children) findComponents(child, name, out)
|
|
25
|
+
return out
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function tagSpreads(source: string, componentName: string): AttrValue[] {
|
|
29
|
+
const ctx = analyzeComponent(source, 'Repro.tsx', componentName)
|
|
30
|
+
const ir = jsxToIR(ctx)!
|
|
31
|
+
return findComponents(ir, 'Tag').map(tag => tag.props.find(p => p.name.startsWith('...'))!.value)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
describe('#3713 — the props-object spread marker in loop rows', () => {
|
|
35
|
+
test('a row spread of the props object is marked in every row shape', () => {
|
|
36
|
+
const spreads = tagSpreads(`
|
|
37
|
+
import { Tag } from './tag'
|
|
38
|
+
export function List(props: { items: string[]; groups: { id: string; cells: string[] }[]; tone?: string }) {
|
|
39
|
+
const fixed = ['x']
|
|
40
|
+
return (
|
|
41
|
+
<div>
|
|
42
|
+
<ul>{props.items.map((it) => <Tag key={it} {...props} label={it} />)}</ul>
|
|
43
|
+
<ol>{props.items.map((it) => <li key={it}>{props.tone ? <Tag {...props} /> : null}</li>)}</ol>
|
|
44
|
+
<nav>{fixed.map((it) => <Tag key={it} {...props} />)}</nav>
|
|
45
|
+
<dl>{props.groups.map((g) => <dd key={g.id}>{g.cells.map((c) => <Tag key={c} {...props} />)}</dd>)}</dl>
|
|
46
|
+
</div>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
`, 'List')
|
|
50
|
+
expect(spreads).toHaveLength(4)
|
|
51
|
+
for (const spread of spreads) expect(spread).toMatchObject({ kind: 'spread', propsObject: true })
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('a loop row named `props` shadows the props object and is not marked', () => {
|
|
55
|
+
const [spread] = tagSpreads(`
|
|
56
|
+
import { Tag } from './tag'
|
|
57
|
+
export function Shadow(props: { rows: { tone: string }[] }) {
|
|
58
|
+
const rows = props.rows
|
|
59
|
+
return <ul>{rows.map((props) => <Tag key={props.tone} {...props} />)}</ul>
|
|
60
|
+
}
|
|
61
|
+
`, 'Shadow')
|
|
62
|
+
expect(spread.kind).toBe('spread')
|
|
63
|
+
expect(spread).not.toHaveProperty('propsObject')
|
|
64
|
+
})
|
|
65
|
+
})
|
|
@@ -13,6 +13,8 @@ describe('lookupLiteralConst (#3312)', () => {
|
|
|
13
13
|
const consts = constantsOf(`
|
|
14
14
|
const enabled = false
|
|
15
15
|
const limit = -2
|
|
16
|
+
const nz = -0
|
|
17
|
+
const big = 1e21
|
|
16
18
|
const missing = null
|
|
17
19
|
export function C() {
|
|
18
20
|
const on = true
|
|
@@ -20,7 +22,7 @@ export function C() {
|
|
|
20
22
|
const ratio = 1.5
|
|
21
23
|
const multiline = 'a\\nb'
|
|
22
24
|
const derived = mode + '!'
|
|
23
|
-
return <div data-x={on && enabled ? mode : String(limit + ratio) + derived + multiline} data-m={missing ?? 'm'}>x</div>
|
|
25
|
+
return <div data-x={on && enabled ? mode : String(limit + ratio + nz + big) + derived + multiline} data-m={missing ?? 'm'}>x</div>
|
|
24
26
|
}
|
|
25
27
|
`)
|
|
26
28
|
const never = () => false
|
|
@@ -28,8 +30,12 @@ export function C() {
|
|
|
28
30
|
test('resolves boolean, number, string and null literals at module and function scope', () => {
|
|
29
31
|
expect(lookupLiteralConst('on', consts, never)).toEqual({ kind: 'boolean', text: 'true' })
|
|
30
32
|
expect(lookupLiteralConst('enabled', consts, never)).toEqual({ kind: 'boolean', text: 'false' })
|
|
31
|
-
expect(lookupLiteralConst('limit', consts, never)).toEqual({ kind: 'number', text: '-2' })
|
|
32
|
-
expect(lookupLiteralConst('ratio', consts, never)).toEqual({ kind: 'number', text: '1.5' })
|
|
33
|
+
expect(lookupLiteralConst('limit', consts, never)).toEqual({ kind: 'number', text: '-2', value: -2 })
|
|
34
|
+
expect(lookupLiteralConst('ratio', consts, never)).toEqual({ kind: 'number', text: '1.5', value: 1.5 })
|
|
35
|
+
// A number carries its value: `-0` keeps its sign, which `text` cannot (#3824 step 6).
|
|
36
|
+
const nz = lookupLiteralConst('nz', consts, never)
|
|
37
|
+
expect(nz?.kind === 'number' && Object.is(nz.value, -0)).toBe(true)
|
|
38
|
+
expect(lookupLiteralConst('big', consts, never)).toEqual({ kind: 'number', text: '1e+21', value: 1e21 })
|
|
33
39
|
expect(lookupLiteralConst('mode', consts, never)).toEqual({ kind: 'string', text: 'on' })
|
|
34
40
|
expect(lookupLiteralConst('multiline', consts, never)).toEqual({ kind: 'string', text: 'a\nb' })
|
|
35
41
|
expect(lookupLiteralConst('missing', consts, never)).toEqual({ kind: 'null', text: 'null' })
|
|
@@ -137,7 +137,7 @@ describe('nested loop: referencing an OUTER loop\'s own index (#2861)', () => {
|
|
|
137
137
|
// outer accessor must be read directly by name, exactly as `item()`
|
|
138
138
|
// reads the outer ITEM in the sibling case #2865 already covers.
|
|
139
139
|
expect(content).toMatch(
|
|
140
|
-
/mapArray\(\(\) => g\(\)\.items \|\| \[\], .*?, \(it, __innerIdx\d+_\d+, __existing\) => \{[\s\S]*?createEffect\(\(\) => \{ __bfw_s0\('s0',
|
|
140
|
+
/mapArray\(\(\) => g\(\)\.items \|\| \[\], .*?, \(it, __innerIdx\d+_\d+, __existing\) => \{[\s\S]*?createEffect\(\(\) => \{ __bfw_s0\('s0', textOrNode\(gi\(\)\)\) \}\)/
|
|
141
141
|
)
|
|
142
142
|
})
|
|
143
143
|
|
|
@@ -202,7 +202,7 @@ describe('nested loop: referencing an OUTER loop\'s own index (#2861)', () => {
|
|
|
202
202
|
// ONE LEVEL UP (the one whose conditional this branch belongs to) was
|
|
203
203
|
// classified 'none' and baked once into the branch's initial template.
|
|
204
204
|
expect(content).toMatch(
|
|
205
|
-
/mapArray\(\(\) => o\(\)\.items \|\| \[\], .*?, \(item, __bidxbr_\d+, __existing\) => \{[\s\S]*?createEffect\(\(\) => \{ __bfw_s1\('s1',
|
|
205
|
+
/mapArray\(\(\) => o\(\)\.items \|\| \[\], .*?, \(item, __bidxbr_\d+, __existing\) => \{[\s\S]*?createEffect\(\(\) => \{ __bfw_s1\('s1', textOrNode\(oi\(\)\)\) \}\)/
|
|
206
206
|
)
|
|
207
207
|
})
|
|
208
208
|
})
|
|
@@ -235,7 +235,7 @@ describe('.map() row: object-literal key / shorthand / member positions vs. the
|
|
|
235
235
|
// The genuine index references (passed to the child, and read directly
|
|
236
236
|
// in the row's own text) are still wrapped as accessor calls.
|
|
237
237
|
expect(js).toContain('return i()')
|
|
238
|
-
expect(js).toContain('
|
|
238
|
+
expect(js).toContain('textOrNode(i())')
|
|
239
239
|
})
|
|
240
240
|
})
|
|
241
241
|
|
|
@@ -117,7 +117,7 @@ describe('planLoop', () => {
|
|
|
117
117
|
refuse: (message) => refusals.push(message),
|
|
118
118
|
}
|
|
119
119
|
expect(spellSortedSource(null, 'xs', spelling)).toBe('xs')
|
|
120
|
-
const evaluated = spellSortedSource({ kind: 'eval', helper: 'sort_eval', operands: [{ kind: 'string', value: 'body' }], captured: ['k'] }, 'xs', spelling)
|
|
120
|
+
const evaluated = spellSortedSource({ kind: 'eval', helper: 'sort_eval', operands: [{ kind: 'string', value: 'body' }], captured: [{ name: 'k', value: { kind: 'identifier', name: 'k' } }] }, 'xs', spelling)
|
|
121
121
|
expect(evaluated).toBe(`sort_eval(xs, 'body', {k: v.k})`)
|
|
122
122
|
const structured = plan(`export function Test(props: { items: { name: string }[] }) { return <ul>{props.items.toSorted((a, b) => a.name.localeCompare(b.name)).map(x => <li key={x.name}>{x.name}</li>)}</ul> }`)
|
|
123
123
|
expect(spellSortedSource(structured.sort, 'xs', spelling)).toBe('sort(xs, name)')
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #3736: a component-scope const read in a loop row whose bindings shadow a
|
|
3
|
+
* name the const's initializer reads (`const b = a` read as `{b}` in
|
|
4
|
+
* `rows.map(a => …)`). `expandConstantForReactivity` /
|
|
5
|
+
* `expandDynamicPropValue` (`prop-handling.ts`) used to splice the
|
|
6
|
+
* initializer text (`a`) into the row, where it re-resolved to the row's own
|
|
7
|
+
* binding, so a client-built or updated row read the row value (`a()`).
|
|
8
|
+
* The initializer resolves in the component scope; when a row binding would
|
|
9
|
+
* capture one of its free names, the row reads the const itself, which init
|
|
10
|
+
* declares.
|
|
11
|
+
*
|
|
12
|
+
* The emitted client JS is read with the TypeScript AST: every per-row slot
|
|
13
|
+
* value is a `const __x = <expr>` declaration inside a row function.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { describe, test, expect } from 'bun:test'
|
|
17
|
+
import ts from 'typescript'
|
|
18
|
+
import { compileJSX } from '../compiler'
|
|
19
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
20
|
+
|
|
21
|
+
const adapter = new TestAdapter()
|
|
22
|
+
|
|
23
|
+
function clientJsFor(source: string): string {
|
|
24
|
+
const result = compileJSX(source, 'Repro.tsx', { adapter })
|
|
25
|
+
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
26
|
+
const clientJs = result.files.find(f => f.type === 'clientJs')
|
|
27
|
+
expect(clientJs).toBeDefined()
|
|
28
|
+
return clientJs!.content
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The initializer text of every `const __x = …` slot-value declaration. */
|
|
32
|
+
function slotValues(js: string): string[] {
|
|
33
|
+
const sf = ts.createSourceFile('out.js', js, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS)
|
|
34
|
+
const values: string[] = []
|
|
35
|
+
const visit = (node: ts.Node): void => {
|
|
36
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.name.text === '__x' && node.initializer) {
|
|
37
|
+
values.push(node.initializer.getText(sf))
|
|
38
|
+
}
|
|
39
|
+
ts.forEachChild(node, visit)
|
|
40
|
+
}
|
|
41
|
+
visit(sf)
|
|
42
|
+
return values
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('a const read in a loop row that shadows a name its initializer reads (#3736)', () => {
|
|
46
|
+
test('destructured prop hop: the row reads the const, not the row value', () => {
|
|
47
|
+
const values = slotValues(clientJsFor(`
|
|
48
|
+
'use client'
|
|
49
|
+
export function C({ a, rows }: { a: string; rows: string[] }) {
|
|
50
|
+
const b = a
|
|
51
|
+
return <ul>{rows.map(a => <li key={a} title={b}>{b}/{a}</li>)}</ul>
|
|
52
|
+
}
|
|
53
|
+
`))
|
|
54
|
+
// `title={b}` and `{b}` read the const; only `{a}` reads the row.
|
|
55
|
+
expect(values).toContain('b')
|
|
56
|
+
expect(values).toContain('a()')
|
|
57
|
+
expect(values.every(v => v === 'b' || v === 'a()')).toBe(true)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('a two-hop alias whose middle name the row binds', () => {
|
|
61
|
+
const values = slotValues(clientJsFor(`
|
|
62
|
+
'use client'
|
|
63
|
+
export function C({ label, rows }: { label: string; rows: string[] }) {
|
|
64
|
+
const a = label
|
|
65
|
+
const b = a
|
|
66
|
+
return <ul>{rows.map(a => <li key={a}>{b}</li>)}</ul>
|
|
67
|
+
}
|
|
68
|
+
`))
|
|
69
|
+
expect(values.length).toBeGreaterThan(0)
|
|
70
|
+
expect(values.every(v => v === 'b')).toBe(true)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('a row that binds the prop itself', () => {
|
|
74
|
+
const values = slotValues(clientJsFor(`
|
|
75
|
+
'use client'
|
|
76
|
+
export function C({ label, rows }: { label: string; rows: string[] }) {
|
|
77
|
+
const c = label
|
|
78
|
+
return <ul>{rows.map(label => <li key={label} title={c}>{c}</li>)}</ul>
|
|
79
|
+
}
|
|
80
|
+
`))
|
|
81
|
+
expect(values.length).toBeGreaterThan(0)
|
|
82
|
+
expect(values.every(v => v === 'c')).toBe(true)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('an index param and a callback preamble local shadow the name too', () => {
|
|
86
|
+
const indexValues = slotValues(clientJsFor(`
|
|
87
|
+
'use client'
|
|
88
|
+
export function C({ a, rows }: { a: string; rows: string[] }) {
|
|
89
|
+
const b = a
|
|
90
|
+
return <ul>{rows.map((r, a) => <li key={r}>{b}</li>)}</ul>
|
|
91
|
+
}
|
|
92
|
+
`))
|
|
93
|
+
expect(indexValues.length).toBeGreaterThan(0)
|
|
94
|
+
expect(indexValues.every(v => v === 'b')).toBe(true)
|
|
95
|
+
|
|
96
|
+
const preambleValues = slotValues(clientJsFor(`
|
|
97
|
+
'use client'
|
|
98
|
+
export function C({ a, rows }: { a: string; rows: string[] }) {
|
|
99
|
+
const b = a
|
|
100
|
+
return <ul>{rows.map(r => { const a = r + '!'; return <li key={r} title={a}>{b}</li> })}</ul>
|
|
101
|
+
}
|
|
102
|
+
`))
|
|
103
|
+
expect(preambleValues).toContain('b')
|
|
104
|
+
expect(preambleValues).not.toContain('_p.a')
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
test('function C(props) with a renamed body destructure', () => {
|
|
108
|
+
const values = slotValues(clientJsFor(`
|
|
109
|
+
'use client'
|
|
110
|
+
export function C(props: { label: string; rows: string[] }) {
|
|
111
|
+
const { label: text } = props
|
|
112
|
+
const c = text
|
|
113
|
+
return <ul>{props.rows.map(text => <li key={text} title={c}>{c}/{text}</li>)}</ul>
|
|
114
|
+
}
|
|
115
|
+
`))
|
|
116
|
+
expect(values).toContain('c')
|
|
117
|
+
expect(values.filter(v => v !== 'c').every(v => v === 'text()')).toBe(true)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('a const reading a signal getter the row shadows', () => {
|
|
121
|
+
const values = slotValues(clientJsFor(`
|
|
122
|
+
'use client'
|
|
123
|
+
import { createSignal } from '@barefootjs/client'
|
|
124
|
+
export function C({ rows }: { rows: string[] }) {
|
|
125
|
+
const [count] = createSignal(7)
|
|
126
|
+
const b = count()
|
|
127
|
+
return <ul>{rows.map(count => <li key={count}>{b}/{count}</li>)}</ul>
|
|
128
|
+
}
|
|
129
|
+
`))
|
|
130
|
+
expect(values).toContain('b')
|
|
131
|
+
expect(values).toContain('count()')
|
|
132
|
+
expect(values.every(v => v === 'b' || v === 'count()')).toBe(true)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test('a row that shadows nothing the initializer reads still expands it', () => {
|
|
136
|
+
const values = slotValues(clientJsFor(`
|
|
137
|
+
'use client'
|
|
138
|
+
export function C({ a, rows }: { a: string; rows: string[] }) {
|
|
139
|
+
const b = a
|
|
140
|
+
return <ul>{rows.map(r => <li key={r}>{b}</li>)}</ul>
|
|
141
|
+
}
|
|
142
|
+
`))
|
|
143
|
+
expect(values.length).toBeGreaterThan(0)
|
|
144
|
+
expect(values.every(v => v === '_p.a')).toBe(true)
|
|
145
|
+
})
|
|
146
|
+
})
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, test, expect } from 'bun:test'
|
|
2
|
+
import { concatType, loopRowType, type DeclaredValueTypes } from '../adapters/loop-row-type'
|
|
3
|
+
import { parseExpression } from '../expression-parser'
|
|
4
|
+
import { BindingScope } from '../scope/binding-scope'
|
|
5
|
+
import type { PropertyInfo, TypeInfo } from '../types'
|
|
6
|
+
|
|
7
|
+
// #3762 review: a `concat` row's field is a definite string only when it is
|
|
8
|
+
// one in the receiver's element type and in every argument's, whatever the
|
|
9
|
+
// types are named.
|
|
10
|
+
describe('concat row type', () => {
|
|
11
|
+
const str: TypeInfo = { kind: 'primitive', raw: 'string', primitive: 'string' }
|
|
12
|
+
const num: TypeInfo = { kind: 'primitive', raw: 'number', primitive: 'number' }
|
|
13
|
+
const field = (name: string, type: TypeInfo, optional = false): PropertyInfo => ({ name, type, optional, readonly: false })
|
|
14
|
+
const named: Record<string, PropertyInfo[]> = {
|
|
15
|
+
Row: [field('id', str)],
|
|
16
|
+
Other: [field('id', str)],
|
|
17
|
+
Numeric: [field('id', num)],
|
|
18
|
+
Optional: [field('id', str, true)],
|
|
19
|
+
Bare: [field('n', num)],
|
|
20
|
+
}
|
|
21
|
+
const arrayOf = (raw: string): TypeInfo => ({ kind: 'array', raw: `${raw}[]`, elementType: { kind: 'interface', raw } })
|
|
22
|
+
const types: DeclaredValueTypes = {
|
|
23
|
+
propsParams: [
|
|
24
|
+
...Object.keys(named).map(name => ({ name: name.toLowerCase(), type: arrayOf(name) })),
|
|
25
|
+
{ name: 'extras', type: { kind: 'array', raw: 'any[]', elementType: { kind: 'unknown', raw: 'any' } } },
|
|
26
|
+
{ name: 'one', type: { kind: 'interface', raw: 'Other' } },
|
|
27
|
+
],
|
|
28
|
+
signals: [],
|
|
29
|
+
memos: [],
|
|
30
|
+
propsObjectName: 'props',
|
|
31
|
+
namedFields: name => named[name],
|
|
32
|
+
}
|
|
33
|
+
const idIsString = (array: string): boolean => {
|
|
34
|
+
const rowType = loopRowType(types, { arrayParsed: parseExpression(array) }, BindingScope.EMPTY)
|
|
35
|
+
if (!rowType) return false
|
|
36
|
+
const scope = BindingScope.EMPTY.enterLoopRow({ param: 'r', rowType })
|
|
37
|
+
return scope.isStringValueName('r', () => false, false, ['id'], types.namedFields)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
test('a field that is a string in every member stays one, across differently named types', () => {
|
|
41
|
+
expect(idIsString('props.row.concat(props.row)')).toBe(true)
|
|
42
|
+
expect(idIsString('props.row.concat(props.other)')).toBe(true)
|
|
43
|
+
expect(idIsString('props.row.concat(props.one)')).toBe(true)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('an any, a number, an optional or a missing field in one member types nothing', () => {
|
|
47
|
+
expect(idIsString('props.row.concat(props.extras)')).toBe(false)
|
|
48
|
+
expect(idIsString('props.row.concat(props.numeric)')).toBe(false)
|
|
49
|
+
expect(idIsString('props.row.concat(props.optional)')).toBe(false)
|
|
50
|
+
expect(idIsString('props.row.concat(props.bare)')).toBe(false)
|
|
51
|
+
expect(idIsString('props.row.concat(props.other).concat(props.extras)')).toBe(false)
|
|
52
|
+
expect(idIsString('props.row.concat(props.missing)')).toBe(false)
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
describe('concat of the same row type keeps the plain array shape (#3762 review)', () => {
|
|
57
|
+
test('Row[] concat Row[] is Row[], not a union', () => {
|
|
58
|
+
const rows: TypeInfo = { kind: 'array', raw: 'Row[]', elementType: { kind: 'interface', raw: 'Row' } }
|
|
59
|
+
const copy: TypeInfo = { kind: 'array', raw: 'Row[]', elementType: { kind: 'interface', raw: 'Row' } }
|
|
60
|
+
const result = concatType(rows, [copy])
|
|
61
|
+
expect(result?.elementType).toBe(rows.elementType)
|
|
62
|
+
expect(result?.unionTypes).toBeUndefined()
|
|
63
|
+
})
|
|
64
|
+
})
|