@barefootjs/jsx 0.41.1 → 0.41.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/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/const-identity.d.ts +38 -0
- package/dist/const-identity.d.ts.map +1 -0
- package/dist/index.d.ts +9 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2328 -1340
- 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/types.d.ts +45 -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__/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-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/compiler.ts +4 -4
- package/src/const-identity.ts +189 -0
- package/src/index.ts +9 -4
- package/src/ir-to-client-js/child-props-object.ts +94 -62
- package/src/ir-to-client-js/collect-elements.ts +28 -22
- 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 +23 -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/types.ts +45 -0
|
@@ -5,16 +5,20 @@
|
|
|
5
5
|
* Every call-site builder (top-level child, loop row, composite loop row,
|
|
6
6
|
* conditional branch, static-array row) decides each explicit prop's own
|
|
7
7
|
* value spelling, then hands the list here. This module is the one place
|
|
8
|
-
* that decides how a spread of the component's own
|
|
9
|
-
*
|
|
10
|
-
* in
|
|
8
|
+
* that decides how a spread of one of the component's own prop bags (the
|
|
9
|
+
* props object, `SpreadAttr.propsObject`, or its `...rest` binding,
|
|
10
|
+
* `SpreadAttr.callerBag === 'rest'`; both set in Phase 1) merges with the
|
|
11
|
+
* explicit props in source order (#3688, #3713, #3714):
|
|
11
12
|
*
|
|
12
13
|
* - a spread prop never becomes an entry of its own;
|
|
13
|
-
* - an explicit prop written before
|
|
14
|
-
* default: the caller's own value wins when the caller passed
|
|
14
|
+
* - an explicit prop written before a bag spread that carries its key is
|
|
15
|
+
* only a default: the caller's own value wins when the caller passed
|
|
16
|
+
* that key;
|
|
15
17
|
* - an explicit prop written after it wins as written;
|
|
16
|
-
* - every key the caller passed
|
|
17
|
-
* through `forwardProps`, as getters, so it stays
|
|
18
|
+
* - every key the caller passed, the call did not name and a bag spread
|
|
19
|
+
* carries is forwarded through `forwardProps`, as getters, so it stays
|
|
20
|
+
* reactive. A rest bag does not carry the keys the component
|
|
21
|
+
* destructured (`SpreadAttr.restOmittedKeys`).
|
|
18
22
|
*/
|
|
19
23
|
|
|
20
24
|
import type { AttrValue } from '../types.ts'
|
|
@@ -38,25 +42,64 @@ export function isSpreadPropName(name: string): boolean {
|
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* overridden explicit expression can't overwrite the spread-selected value
|
|
47
|
-
* on update.
|
|
45
|
+
* The caller keys a spread of one of the component's own prop bags leaves
|
|
46
|
+
* out, or null when `value` is not such a spread: none for the props object
|
|
47
|
+
* (`SpreadAttr.propsObject`), the destructured keys for a `{...rest}` bag
|
|
48
|
+
* (`SpreadAttr.restOmittedKeys`, #3714). The client holds either bag as the
|
|
49
|
+
* caller's whole props object (`_p`).
|
|
48
50
|
*/
|
|
49
|
-
export function
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
})
|
|
54
|
-
return at
|
|
51
|
+
export function callerBagSpreadOmit(value: AttrValue): readonly string[] | null {
|
|
52
|
+
if (value.kind !== 'spread') return null
|
|
53
|
+
if (value.propsObject === true) return []
|
|
54
|
+
return value.callerBag === 'rest' ? (value.restOmittedKeys ?? []) : null
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
58
|
+
* Whether a caller-bag spread written after `index` in a child component
|
|
59
|
+
* call carries the key `name` (`<Tag tone="first" {...props} />`,
|
|
60
|
+
* `<Tag tone="first" {...rest} />`). The explicit prop at `index` is then
|
|
61
|
+
* only a default: the spread overrides it when the caller passed that key,
|
|
62
|
+
* as in JS (#3688). A rest bag never carries a key the component
|
|
63
|
+
* destructured, so `<Tag title="x" {...rest} />` keeps `title` as written
|
|
64
|
+
* (#3714). Both the props object built here and the top-level reactive DOM
|
|
65
|
+
* mirrors read this one decision, so an overridden explicit expression
|
|
66
|
+
* can't overwrite the spread-selected value on update.
|
|
67
|
+
*/
|
|
68
|
+
function laterCallerBagCarries(props: readonly ChildPropLike[], index: number, name: string): boolean {
|
|
69
|
+
for (let at = index + 1; at < props.length; at++) {
|
|
70
|
+
const omit = callerBagSpreadOmit(props[at]!.value)
|
|
71
|
+
if (omit !== null && !omit.includes(name)) return true
|
|
72
|
+
}
|
|
73
|
+
return false
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The caller keys no caller-bag spread of the call carries, or null when
|
|
78
|
+
* the call spreads no caller bag: the keys every one of them leaves out.
|
|
79
|
+
*/
|
|
80
|
+
function callerBagsOmit(props: readonly ChildPropLike[]): string[] | null {
|
|
81
|
+
let omitted: string[] | null = null
|
|
82
|
+
for (const prop of props) {
|
|
83
|
+
const omit = callerBagSpreadOmit(prop.value)
|
|
84
|
+
if (omit === null) continue
|
|
85
|
+
omitted = omitted === null ? [...omit] : omitted.filter(key => omit.includes(key))
|
|
86
|
+
}
|
|
87
|
+
return omitted
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* A `{...rest}` bag as a props object: the caller's props without the keys
|
|
92
|
+
* the component destructured (`omit`), each read through a getter, so it
|
|
93
|
+
* stays live. The one spelling the child props object and the registration
|
|
94
|
+
* template share.
|
|
95
|
+
*/
|
|
96
|
+
export function restBagExpr(omit: readonly string[]): string {
|
|
97
|
+
return `forwardProps(${PROPS_PARAM}, {}, ${codeSafeStringArray(omit)})`
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The value of an explicit prop a later caller-bag spread overrides (see
|
|
102
|
+
* `laterCallerBagCarries`): the caller's own value when it passed the
|
|
60
103
|
* key, `expr` otherwise. `forwardProps` lets every override win, so the
|
|
61
104
|
* merge is spelled into the value itself.
|
|
62
105
|
*/
|
|
@@ -70,8 +113,8 @@ export function callerFirstPropExpr(name: string, expr: string): string {
|
|
|
70
113
|
* (`<Chip {...row} />`, a loop row `{...r}`): not one of the caller's own
|
|
71
114
|
* prop bags (`SpreadAttr.callerBag`, decided in Phase 1). Its keys are
|
|
72
115
|
* merged in source order through `spreadChildProps` (#3861). A caller-bag
|
|
73
|
-
* spread is the props object
|
|
74
|
-
* `
|
|
116
|
+
* spread is the props object or a `{...rest}` bag
|
|
117
|
+
* (`callerBagSpreadOmit`), merged by `forwardProps`.
|
|
75
118
|
*/
|
|
76
119
|
export function isObjectSpreadProp(prop: ChildPropLike): boolean {
|
|
77
120
|
return isSpreadPropName(prop.name) && prop.value.kind === 'spread' && prop.value.callerBag === undefined
|
|
@@ -80,18 +123,11 @@ export function isObjectSpreadProp(prop: ChildPropLike): boolean {
|
|
|
80
123
|
export interface ChildPropsObjectOptions {
|
|
81
124
|
/** Entries appended after the explicit props (e.g. a row's `children` getter). */
|
|
82
125
|
trailing?: readonly string[]
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* The caller's `{...rest}` bag as a props object (the caller's props
|
|
90
|
-
* without the keys the component destructured), so a call that also
|
|
91
|
-
* spreads an object merges the bag in source order as one more part.
|
|
92
|
-
* Without it a rest bag spread is left to `forwardCaller`.
|
|
93
|
-
*/
|
|
94
|
-
restBag?: string
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Whether a child component call spreads one of the component's own prop bags (`callerBagSpreadOmit`). */
|
|
129
|
+
export function spreadsCallerBag(props: readonly ChildPropLike[]): boolean {
|
|
130
|
+
return props.some(prop => callerBagSpreadOmit(prop.value) !== null)
|
|
95
131
|
}
|
|
96
132
|
|
|
97
133
|
/**
|
|
@@ -120,9 +156,9 @@ export function lastObjectSpreadIndex(props: readonly ChildPropLike[]): number {
|
|
|
120
156
|
* The value of the explicit prop at `index` for a reactive DOM mirror,
|
|
121
157
|
* merged with every spread written after it, as the props object merges
|
|
122
158
|
* them: a later object spread (`spreadOf` spells its value) wins when its
|
|
123
|
-
* object has the key, and a later
|
|
124
|
-
*
|
|
125
|
-
* follows.
|
|
159
|
+
* object has the key, and a later caller-bag spread that carries the key
|
|
160
|
+
* (`callerBagSpreadOmit`) wins when the caller passed it
|
|
161
|
+
* (`callerFirstPropExpr`). `expr` as written when no spread follows.
|
|
126
162
|
*/
|
|
127
163
|
export function mergedExplicitPropExpr<P extends ChildPropLike>(
|
|
128
164
|
props: readonly P[],
|
|
@@ -133,11 +169,13 @@ export function mergedExplicitPropExpr<P extends ChildPropLike>(
|
|
|
133
169
|
const name = props[index]!.name
|
|
134
170
|
const key = codeSafeStringLiteral(name)
|
|
135
171
|
let merged = expr
|
|
136
|
-
// The last spread kind applied: two
|
|
172
|
+
// The last spread kind applied: two caller-bag spreads in a row merge once.
|
|
137
173
|
let last: 'props' | 'object' | null = null
|
|
138
174
|
for (let at = index + 1; at < props.length; at++) {
|
|
139
175
|
const prop = props[at]!
|
|
140
|
-
|
|
176
|
+
const omit = callerBagSpreadOmit(prop.value)
|
|
177
|
+
if (omit !== null) {
|
|
178
|
+
if (omit.includes(name)) continue
|
|
141
179
|
if (last !== 'props') merged = callerFirstPropExpr(name, merged)
|
|
142
180
|
last = 'props'
|
|
143
181
|
} else if (isObjectSpreadProp(prop)) {
|
|
@@ -154,13 +192,12 @@ export function mergedExplicitPropExpr<P extends ChildPropLike>(
|
|
|
154
192
|
* also spells an object spread's value (`isObjectSpreadProp`).
|
|
155
193
|
*
|
|
156
194
|
* Without an object spread, the explicit props form one object literal,
|
|
157
|
-
* wrapped in `forwardProps` for a
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
* the
|
|
162
|
-
*
|
|
163
|
-
* forwarded off `_p` after the merge.
|
|
195
|
+
* wrapped in `forwardProps` for a caller-bag spread. With one, every part
|
|
196
|
+
* merges in source order through `spreadChildProps` (#3861): each run of
|
|
197
|
+
* explicit props is an object literal, an object spread a thunk of its
|
|
198
|
+
* value, a props-object spread the thunk `() => _p`, and a `{...rest}` bag
|
|
199
|
+
* the caller's props without the destructured keys (`restBagExpr`), so a
|
|
200
|
+
* later part wins as in JS.
|
|
164
201
|
*/
|
|
165
202
|
export function childPropsObjectExpr<P extends ChildPropLike>(
|
|
166
203
|
props: readonly P[],
|
|
@@ -168,7 +205,7 @@ export function childPropsObjectExpr<P extends ChildPropLike>(
|
|
|
168
205
|
options: ChildPropsObjectOptions = {},
|
|
169
206
|
): string {
|
|
170
207
|
if (lastObjectSpreadIndex(props) >= 0) return spreadPartsExpr(props, valueOf, options)
|
|
171
|
-
const
|
|
208
|
+
const bagsOmit = callerBagsOmit(props)
|
|
172
209
|
const entries: string[] = []
|
|
173
210
|
const explicitNames: string[] = []
|
|
174
211
|
props.forEach((prop, index) => {
|
|
@@ -177,7 +214,7 @@ export function childPropsObjectExpr<P extends ChildPropLike>(
|
|
|
177
214
|
if (!value) return
|
|
178
215
|
explicitNames.push(prop.name)
|
|
179
216
|
const key = quotePropName(prop.name)
|
|
180
|
-
if (index
|
|
217
|
+
if (laterCallerBagCarries(props, index, prop.name)) {
|
|
181
218
|
entries.push(`get ${key}() { return ${callerFirstPropExpr(prop.name, value.expr)} }`)
|
|
182
219
|
} else {
|
|
183
220
|
entries.push(explicitEntry(key, value))
|
|
@@ -185,8 +222,9 @@ export function childPropsObjectExpr<P extends ChildPropLike>(
|
|
|
185
222
|
})
|
|
186
223
|
entries.push(...(options.trailing ?? []))
|
|
187
224
|
const object = objectLiteral(entries)
|
|
188
|
-
if (
|
|
189
|
-
|
|
225
|
+
if (bagsOmit === null) return object
|
|
226
|
+
const excluded = [...explicitNames, ...bagsOmit.filter(key => !explicitNames.includes(key))]
|
|
227
|
+
return `forwardProps(${PROPS_PARAM}, ${object}, ${codeSafeStringArray(excluded)})`
|
|
190
228
|
}
|
|
191
229
|
|
|
192
230
|
/** One explicit prop's object-literal entry: a getter or a data property. */
|
|
@@ -202,8 +240,6 @@ function spreadPartsExpr<P extends ChildPropLike>(
|
|
|
202
240
|
): string {
|
|
203
241
|
const parts: string[] = []
|
|
204
242
|
let entries: string[] = []
|
|
205
|
-
const explicitNames: string[] = []
|
|
206
|
-
let mergesCaller = false
|
|
207
243
|
const flush = (): void => {
|
|
208
244
|
if (entries.length > 0) parts.push(objectLiteral(entries))
|
|
209
245
|
entries = []
|
|
@@ -212,13 +248,12 @@ function spreadPartsExpr<P extends ChildPropLike>(
|
|
|
212
248
|
if (prop.value.kind === 'spread' && prop.value.propsObject === true) {
|
|
213
249
|
flush()
|
|
214
250
|
parts.push(`() => ${PROPS_PARAM}`)
|
|
215
|
-
mergesCaller = true
|
|
216
251
|
continue
|
|
217
252
|
}
|
|
218
|
-
|
|
253
|
+
const restOmit = callerBagSpreadOmit(prop.value)
|
|
254
|
+
if (restOmit !== null) {
|
|
219
255
|
flush()
|
|
220
|
-
parts.push(
|
|
221
|
-
mergesCaller = true
|
|
256
|
+
parts.push(restBagExpr(restOmit))
|
|
222
257
|
continue
|
|
223
258
|
}
|
|
224
259
|
if (isSpreadPropName(prop.name) && !isObjectSpreadProp(prop)) continue
|
|
@@ -229,12 +264,9 @@ function spreadPartsExpr<P extends ChildPropLike>(
|
|
|
229
264
|
parts.push(`() => (${value.expr})`)
|
|
230
265
|
continue
|
|
231
266
|
}
|
|
232
|
-
explicitNames.push(prop.name)
|
|
233
267
|
entries.push(explicitEntry(quotePropName(prop.name), value))
|
|
234
268
|
}
|
|
235
269
|
entries.push(...(options.trailing ?? []))
|
|
236
270
|
flush()
|
|
237
|
-
|
|
238
|
-
if (!options.forwardCaller || mergesCaller) return merged
|
|
239
|
-
return `forwardProps(${PROPS_PARAM}, ${merged}, ${codeSafeStringArray(explicitNames)})`
|
|
271
|
+
return `spreadChildProps(${parts.join(', ')})`
|
|
240
272
|
}
|
|
@@ -11,7 +11,7 @@ import { detectRootNamespaceWrapTag } from './control-flow/stringify/template-pa
|
|
|
11
11
|
import { expandDynamicPropValue, expandConstantForReactivity, resolveRestSpreadOrigin, resolveRestSpreadNames } from './prop-handling.ts'
|
|
12
12
|
import { extractFreeIdentifiersFromText } from './csr-substitute.ts'
|
|
13
13
|
import { walkIR, stopAt } from './walker.ts'
|
|
14
|
-
import { childPropsObjectExpr, codeSafeStringArray, isSpreadPropName, mergedExplicitPropExpr } from './child-props-object.ts'
|
|
14
|
+
import { childPropsObjectExpr, codeSafeStringArray, isSpreadPropName, mergedExplicitPropExpr, spreadsCallerBag } from './child-props-object.ts'
|
|
15
15
|
import { buildLoopChainExpr } from '../loop-chain.ts'
|
|
16
16
|
import { classifyDOMProp } from '@barefootjs/shared'
|
|
17
17
|
import type { BindingScope } from '../scope/binding-scope.ts'
|
|
@@ -540,7 +540,7 @@ function jsxChildrenContainComponent(nodes: IRNode[]): boolean {
|
|
|
540
540
|
* and whether it is read through a getter (deferred, so reactive) or is a
|
|
541
541
|
* plain data property.
|
|
542
542
|
*/
|
|
543
|
-
function componentInitPropValue(prop: IRProp, ctx: ClientJsContext): { expr: string; getter: boolean } {
|
|
543
|
+
function componentInitPropValue(prop: Pick<IRProp, 'name' | 'value'>, ctx: ClientJsContext): { expr: string; getter: boolean } {
|
|
544
544
|
if (isEventHandlerName(prop.name)) {
|
|
545
545
|
// Event handlers reach here only as `expression` variants.
|
|
546
546
|
return { expr: attrValueToString(prop.value) ?? prop.name, getter: false }
|
|
@@ -594,24 +594,30 @@ function restKeyLiveRead(attr: IRElement['attrs'][number]): string {
|
|
|
594
594
|
}
|
|
595
595
|
|
|
596
596
|
/** Build propsExpr for a child component from its IR props. */
|
|
597
|
-
function buildComponentPropsExpr(
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
if (p.value.kind !== 'spread' && p.value.kind !== 'expression') return false
|
|
601
|
-
const expr = p.value.kind === 'spread' ? p.value.expr : p.value.expr
|
|
602
|
-
// #2723: resolve through any `const x__alias = x` hop onto the
|
|
603
|
-
// rest/props binding — see `resolveRestSpreadOrigin`'s docstring.
|
|
604
|
-
return resolveRestSpreadOrigin(ctx, expr) !== null
|
|
605
|
-
})
|
|
606
|
-
// The rest bag without the keys the component destructured, keyed the
|
|
607
|
-
// way they arrive on `_p` (the `applyRestAttrs` exclusion set).
|
|
608
|
-
const consumedKeys = ctx.propsParams.map(p => p.sourceName ?? p.name)
|
|
609
|
-
return childPropsObjectExpr(props, prop => componentInitPropValue(prop, ctx), {
|
|
610
|
-
forwardCaller: knownSpreadProp !== undefined,
|
|
611
|
-
restBag: `forwardProps(${PROPS_PARAM}, {}, ${codeSafeStringArray(consumedKeys)})`,
|
|
597
|
+
function buildComponentPropsExpr(comp: Pick<IRComponent, 'props' | 'children'>, ctx: ClientJsContext): string {
|
|
598
|
+
return childPropsObjectExpr(comp.props, prop => componentInitPropValue(prop, ctx), {
|
|
599
|
+
trailing: callerBagCallChildrenEntry(comp, ctx),
|
|
612
600
|
})
|
|
613
601
|
}
|
|
614
602
|
|
|
603
|
+
/**
|
|
604
|
+
* The `children` entry of a call that spreads one of the component's own
|
|
605
|
+
* prop bags (`<Child {...rest}>…</Child>`, `<Child {...props}>…</Child>`)
|
|
606
|
+
* and has JSX children: the children, spelled as a `children={…}` prop is
|
|
607
|
+
* (`componentInitPropValue`), written last. JSX children are the call's
|
|
608
|
+
* last `children`, so they win over a `children` key the bag carries, and a
|
|
609
|
+
* bag that leaves `children` out (the component destructured it) still
|
|
610
|
+
* hands the child what the call wrote: `{children}`, `{children ?? 'x'}`,
|
|
611
|
+
* text or an element (#3714). Empty for a call without such a spread,
|
|
612
|
+
* which has no caller `children` to tell apart from its own.
|
|
613
|
+
*/
|
|
614
|
+
function callerBagCallChildrenEntry(comp: Pick<IRComponent, 'props' | 'children'>, ctx: ClientJsContext): string[] {
|
|
615
|
+
if (comp.children.length === 0 || !spreadsCallerBag(comp.props)) return []
|
|
616
|
+
if (comp.props.some(prop => prop.name === 'children')) return []
|
|
617
|
+
const value = componentInitPropValue({ name: 'children', value: { kind: 'jsx-children', children: comp.children } }, ctx)
|
|
618
|
+
return value.expr === "''" ? [] : [`get children() { return ${value.expr} }`]
|
|
619
|
+
}
|
|
620
|
+
|
|
615
621
|
/**
|
|
616
622
|
* Push reactive child-prop entries for a component node into `ctx.reactiveChildProps`.
|
|
617
623
|
* Mirrors the wrap-decision pass done during propsExpr construction; kept as a dedicated
|
|
@@ -666,13 +672,13 @@ function collectReactiveChildProps(node: IRComponent, ctx: ClientJsContext): voi
|
|
|
666
672
|
|
|
667
673
|
/** Convert raw component info from IR traversal to ConditionalBranchChildComponent with built propsExpr. */
|
|
668
674
|
function buildBranchChildComponents(
|
|
669
|
-
rawComponents: Array<{ name: string; slotId: string | null; props: IRProp[] }>,
|
|
675
|
+
rawComponents: Array<{ name: string; slotId: string | null; props: IRProp[]; children: IRNode[] }>,
|
|
670
676
|
ctx: ClientJsContext,
|
|
671
677
|
): ConditionalBranchChildComponent[] {
|
|
672
678
|
return rawComponents.map(comp => ({
|
|
673
679
|
name: comp.name,
|
|
674
680
|
slotId: comp.slotId,
|
|
675
|
-
propsExpr: buildComponentPropsExpr(comp
|
|
681
|
+
propsExpr: buildComponentPropsExpr(comp, ctx),
|
|
676
682
|
}))
|
|
677
683
|
}
|
|
678
684
|
|
|
@@ -1063,7 +1069,7 @@ export function collectElements(
|
|
|
1063
1069
|
ctx.childInits.push({
|
|
1064
1070
|
name: c.name,
|
|
1065
1071
|
slotId: c.slotId,
|
|
1066
|
-
propsExpr: buildComponentPropsExpr(c
|
|
1072
|
+
propsExpr: buildComponentPropsExpr(c, ctx),
|
|
1067
1073
|
})
|
|
1068
1074
|
}
|
|
1069
1075
|
|
|
@@ -1147,8 +1153,8 @@ function collectFromElement(element: IRElement, ctx: ClientJsContext, insideCond
|
|
|
1147
1153
|
// `applyRestAttrs` filters `Object.keys(_p)` — which is always
|
|
1148
1154
|
// caller-keyed (#2524 CSR half) — so the exclude list must use
|
|
1149
1155
|
// the caller-facing key too, not the local binding name.
|
|
1150
|
-
|
|
1151
|
-
|
|
1156
|
+
// Phase 1 lists them on the spread (`SpreadAttr.restOmittedKeys`).
|
|
1157
|
+
const consumedKeys = (attr.value.kind === 'spread' && attr.value.restOmittedKeys) || []
|
|
1152
1158
|
const staticAttrKeys = element.attrs
|
|
1153
1159
|
.filter(a => a.name !== '...')
|
|
1154
1160
|
.map(a => a.name)
|
|
@@ -61,8 +61,19 @@ export function computeFileScope(entryPath: string): string {
|
|
|
61
61
|
*/
|
|
62
62
|
let _activeScope: { fileScope: string; nonExportedSiblings: Set<string> } | null = null
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Install `scope` and return the scope it replaces. A compile restores the
|
|
66
|
+
* returned scope when it finishes instead of clearing to `null`: an adapter
|
|
67
|
+
* may compile another file from inside `generate()` (go-template reads a
|
|
68
|
+
* cross-file child's shape that way, #3753), and the outer compile's client
|
|
69
|
+
* JS is emitted after that nested compile returns.
|
|
70
|
+
*/
|
|
71
|
+
export function setActiveComponentScope(
|
|
72
|
+
scope: { fileScope: string; nonExportedSiblings: Set<string> } | null,
|
|
73
|
+
): { fileScope: string; nonExportedSiblings: Set<string> } | null {
|
|
74
|
+
const previous = _activeScope
|
|
65
75
|
_activeScope = scope
|
|
76
|
+
return previous
|
|
66
77
|
}
|
|
67
78
|
|
|
68
79
|
/**
|
|
@@ -101,8 +112,11 @@ export function buildImportAliasMap(imports: readonly ImportInfo[]): Map<string,
|
|
|
101
112
|
return aliases
|
|
102
113
|
}
|
|
103
114
|
|
|
104
|
-
|
|
115
|
+
/** Install `aliases` and return the map it replaces (restored the same way as the component scope). */
|
|
116
|
+
export function setActiveImportAliases(aliases: ReadonlyMap<string, string> | null): ReadonlyMap<string, string> | null {
|
|
117
|
+
const previous = _activeImportAliases
|
|
105
118
|
_activeImportAliases = aliases
|
|
119
|
+
return previous
|
|
106
120
|
}
|
|
107
121
|
|
|
108
122
|
/**
|
|
@@ -76,6 +76,7 @@ function wrapAttrValueExpression(value: AttrValue, wrap: (s: string) => string):
|
|
|
76
76
|
...AttrValueOf.spread(wrap(value.expr), value.templateExpr ? wrap(value.templateExpr) : undefined),
|
|
77
77
|
...(value.propsObject && { propsObject: true as const }),
|
|
78
78
|
...(value.callerBag !== undefined && { callerBag: value.callerBag }),
|
|
79
|
+
...(value.restOmittedKeys !== undefined && { restOmittedKeys: value.restOmittedKeys }),
|
|
79
80
|
}
|
|
80
81
|
}
|
|
81
82
|
}
|
|
@@ -65,6 +65,7 @@ function wrapAttrValueExpression(value: AttrValue, wrap: (s: string) => string):
|
|
|
65
65
|
...AttrValueOf.spread(wrap(value.expr), value.templateExpr ? wrap(value.templateExpr) : undefined),
|
|
66
66
|
...(value.propsObject && { propsObject: true as const }),
|
|
67
67
|
...(value.callerBag !== undefined && { callerBag: value.callerBag }),
|
|
68
|
+
...(value.restOmittedKeys !== undefined && { restOmittedKeys: value.restOmittedKeys }),
|
|
68
69
|
}
|
|
69
70
|
}
|
|
70
71
|
}
|
|
@@ -19,7 +19,7 @@ import { interp, spliceChildValue, conditionalMarkup, renderChildCall, mappedRow
|
|
|
19
19
|
import { markupSlotIdsOf } from './markup-slots.ts'
|
|
20
20
|
import { isEventHandlerName } from '../event-handler-name.ts'
|
|
21
21
|
import { writesAnyBinding } from '../props-binding.ts'
|
|
22
|
-
import { isObjectSpreadProp } from './child-props-object.ts'
|
|
22
|
+
import { callerBagSpreadOmit, isObjectSpreadProp, restBagExpr } from './child-props-object.ts'
|
|
23
23
|
|
|
24
24
|
/** Static HTML is embedded in a JS template literal; dynamic expressions use interp(). */
|
|
25
25
|
function escapeStaticTemplateHtml(text: string): string {
|
|
@@ -422,13 +422,14 @@ function renderTemplateAttrPart(
|
|
|
422
422
|
* (#3688) — a later key overrides an earlier one, and the spread overrides
|
|
423
423
|
* only the keys the caller passed. A spread of any other object
|
|
424
424
|
* (`isObjectSpreadProp`: a loop row, an object prop, a local) is kept the
|
|
425
|
-
* same way, so its keys reach the child in source order (#3861).
|
|
426
|
-
* `{...rest}` bag
|
|
425
|
+
* same way, so its keys reach the child in source order (#3861). So is a
|
|
426
|
+
* `{...rest}` bag, spelled without the keys the component destructured
|
|
427
|
+
* (`restBagTemplateEntry`, #3714).
|
|
427
428
|
*/
|
|
428
429
|
function keepsTemplateComponentProp(p: IRProp): boolean {
|
|
429
430
|
if (p.name === 'key') return false
|
|
430
431
|
if (p.name === '...' || p.name.startsWith('...')) {
|
|
431
|
-
return p.value.kind === 'spread' && (p.value
|
|
432
|
+
return p.value.kind === 'spread' && (callerBagSpreadOmit(p.value) !== null || isObjectSpreadProp(p))
|
|
432
433
|
}
|
|
433
434
|
return true
|
|
434
435
|
}
|
|
@@ -438,6 +439,19 @@ function templatePropEntry(p: IRProp, valueExpr: string): string {
|
|
|
438
439
|
return p.name === '...' || p.name.startsWith('...') ? `...${valueExpr}` : `${quotePropName(p.name)}: ${valueExpr}`
|
|
439
440
|
}
|
|
440
441
|
|
|
442
|
+
/**
|
|
443
|
+
* The `renderChild(...)` props-object entry of a `{...rest}` bag spread
|
|
444
|
+
* (`SpreadAttr.callerBag === 'rest'`), or null when `p` is not one. The
|
|
445
|
+
* template holds the bag as the caller's whole props object (`_p`), so the
|
|
446
|
+
* entry spreads it without the keys the component destructured
|
|
447
|
+
* (`restBagExpr`, the spelling the init's props object uses), in the
|
|
448
|
+
* spread's source position (#3714).
|
|
449
|
+
*/
|
|
450
|
+
function restBagTemplateEntry(p: IRProp): string | null {
|
|
451
|
+
if (p.value.kind !== 'spread' || p.value.callerBag !== 'rest') return null
|
|
452
|
+
return `...${restBagExpr(p.value.restOmittedKeys ?? [])}`
|
|
453
|
+
}
|
|
454
|
+
|
|
441
455
|
/**
|
|
442
456
|
* Source-of-truth predicates for the collision-safe spread-attrs merge
|
|
443
457
|
* shared by all three CSR-side emit paths (`irToHtmlTemplate`,
|
|
@@ -927,6 +941,8 @@ export function irToHtmlTemplate(node: IRNode, restSpreadNames?: ReadonlySet<str
|
|
|
927
941
|
.map(p => {
|
|
928
942
|
// `/* @client */` defers the prop to hydrate via initChild.
|
|
929
943
|
if (p.clientOnly) return null
|
|
944
|
+
const restBagEntry = restBagTemplateEntry(p)
|
|
945
|
+
if (restBagEntry !== null) return restBagEntry
|
|
930
946
|
switch (p.value.kind) {
|
|
931
947
|
case 'jsx-children': {
|
|
932
948
|
const hoistedRecurse = (n: IRNode): string => irToHtmlTemplate(n, restSpreadNames, loopDepth, loopParams, branchSlotsVar, true)
|
|
@@ -2094,6 +2110,8 @@ function irToComponentTemplateWithOpts(node: IRNode, opts: TemplateOptions): str
|
|
|
2094
2110
|
.map(p => {
|
|
2095
2111
|
// `/* @client */` defers the prop to hydrate via initChild.
|
|
2096
2112
|
if (p.clientOnly) return null
|
|
2113
|
+
const restBagEntry = restBagTemplateEntry(p)
|
|
2114
|
+
if (restBagEntry !== null) return restBagEntry
|
|
2097
2115
|
switch (p.value.kind) {
|
|
2098
2116
|
case 'jsx-children': {
|
|
2099
2117
|
const hoistedRecurse = (n: IRNode): string => irToComponentTemplateWithOpts(n, { ...opts, inHoistedChildren: true })
|
|
@@ -2784,6 +2802,8 @@ function generateCsrTemplateWithOpts(node: IRNode, opts: TemplateOptions): strin
|
|
|
2784
2802
|
// (built in collect-elements) reads the value once init
|
|
2785
2803
|
// runs, mirroring the existing UNSAFE strip path below.
|
|
2786
2804
|
if (p.clientOnly) return null
|
|
2805
|
+
const restBagEntry = restBagTemplateEntry(p)
|
|
2806
|
+
if (restBagEntry !== null) return restBagEntry
|
|
2787
2807
|
switch (p.value.kind) {
|
|
2788
2808
|
case 'jsx-children': {
|
|
2789
2809
|
const hoistedRecurse = (n: IRNode): string => generateCsrTemplateWithOpts(n, { ...opts, inHoistedChildren: true })
|
|
@@ -96,7 +96,7 @@ export function generateClientJsWithSourceMap(
|
|
|
96
96
|
// harness (`csr-render.ts`) and `analyzeClientNeeds` both do. Cleared
|
|
97
97
|
// in `finally` so a thrown error never leaks the alias map into an
|
|
98
98
|
// unrelated later compile.
|
|
99
|
-
setActiveImportAliases(buildImportAliasMap(ir.metadata.imports))
|
|
99
|
+
const outerAliases = setActiveImportAliases(buildImportAliasMap(ir.metadata.imports))
|
|
100
100
|
try {
|
|
101
101
|
const ctx = createContext(ir, scope, adapterCapabilities, profile)
|
|
102
102
|
const siblingOffsets = computeLoopSiblingOffsets(ir.root)
|
|
@@ -125,7 +125,7 @@ export function generateClientJsWithSourceMap(
|
|
|
125
125
|
|
|
126
126
|
return { code }
|
|
127
127
|
} finally {
|
|
128
|
-
setActiveImportAliases(
|
|
128
|
+
setActiveImportAliases(outerAliases)
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
131
|
|
|
@@ -118,6 +118,10 @@ export function expandDynamicPropValue(value: string, ctx: ClientJsContext, scop
|
|
|
118
118
|
// Re-embedding it here would silently drop the mutation; keep the bare
|
|
119
119
|
// reference instead so the emitted getter reads the LIVE binding.
|
|
120
120
|
if (constant?.mutatedAfterDeclaration) return value
|
|
121
|
+
// An initializer that allocates and reads nothing reactive (#3868) would
|
|
122
|
+
// hand every prop read a new object; the getter returns the one value the
|
|
123
|
+
// declaration created instead (init declares the binding).
|
|
124
|
+
if (constant?.identityBound) return value
|
|
121
125
|
if (constant && constant.value && !initializerCapturedBy(constant, scope)) {
|
|
122
126
|
return constant.value
|
|
123
127
|
}
|
package/src/jsx-to-ir.ts
CHANGED
|
@@ -59,7 +59,7 @@ import {
|
|
|
59
59
|
collectAstPropRefs,
|
|
60
60
|
rewriteScopedValueRefs,
|
|
61
61
|
} from './prop-rewrite.ts'
|
|
62
|
-
import { boundPropLocalNames, buildPropAliasMap, templatePropReadParams, resolveAliasOrigin, resolveRestSpreadOriginCore, livePropReadExpr } from './props-binding.ts'
|
|
62
|
+
import { boundPropLocalNames, buildPropAliasMap, templatePropReadParams, resolveAliasOrigin, resolveRestSpreadOriginCore, restBagOmittedKeys, livePropReadExpr } from './props-binding.ts'
|
|
63
63
|
import { PROPS_PARAM } from './ir-to-client-js/utils.ts'
|
|
64
64
|
import { walkIR } from './ir-to-client-js/walker.ts'
|
|
65
65
|
import { resolveFreeRefs, rebindHelperParamRefs, isNameBound as isNameBoundInEnv, type BindingEnvironment } from './free-refs.ts'
|
|
@@ -7368,9 +7368,17 @@ function expandSpreadAttribute(
|
|
|
7368
7368
|
const slotId = generateSpreadSlotId(ctx)
|
|
7369
7369
|
const value = AttrValueOf.spread(spreadExpr, ctx.getTemplateJS(attr.expression), slotId)
|
|
7370
7370
|
const callerBag = callerBagOfSpread(value, ctx)
|
|
7371
|
+
const shadowsPropsObject = spreadShadowsPropsObject(attr, ctx)
|
|
7371
7372
|
return [{
|
|
7372
7373
|
name: '...',
|
|
7373
|
-
value: callerBag
|
|
7374
|
+
value: callerBag || shadowsPropsObject
|
|
7375
|
+
? {
|
|
7376
|
+
...value,
|
|
7377
|
+
...(callerBag && { callerBag }),
|
|
7378
|
+
...(callerBag === 'rest' && { restOmittedKeys: restBagOmittedKeys(ctx.analyzer) }),
|
|
7379
|
+
...(shadowsPropsObject && { shadowsPropsObject: true as const }),
|
|
7380
|
+
}
|
|
7381
|
+
: value,
|
|
7374
7382
|
loc,
|
|
7375
7383
|
freeIdentifiers: spreadFreeIdentifiers,
|
|
7376
7384
|
}]
|
|
@@ -7727,6 +7735,19 @@ function staticObjectSpreadEntries(init: ts.ObjectLiteralExpression): Array<{ ke
|
|
|
7727
7735
|
return [...entries].map(([key, text]) => ({ key, text }))
|
|
7728
7736
|
}
|
|
7729
7737
|
|
|
7738
|
+
/**
|
|
7739
|
+
* Whether `{...name}` spreads a binding spelled like the component's props
|
|
7740
|
+
* object (`SpreadAttr.shadowsPropsObject`): a loop row or callback parameter
|
|
7741
|
+
* of that name hides the props object at the spread (#3727).
|
|
7742
|
+
*/
|
|
7743
|
+
function spreadShadowsPropsObject(attr: ts.JsxSpreadAttribute, ctx: TransformContext): boolean {
|
|
7744
|
+
const propsName = ctx.analyzer.propsObjectName
|
|
7745
|
+
if (propsName === null) return false
|
|
7746
|
+
let expr: ts.Expression = attr.expression
|
|
7747
|
+
while (ts.isParenthesizedExpression(expr)) expr = expr.expression
|
|
7748
|
+
return ts.isIdentifier(expr) && expr.text === propsName && ctx.scope.isBound(propsName)
|
|
7749
|
+
}
|
|
7750
|
+
|
|
7730
7751
|
/**
|
|
7731
7752
|
* Which caller prop bag a spread forwards whole (`SpreadAttr.callerBag`):
|
|
7732
7753
|
* its `expr` / `templateExpr` resolved through any alias hop
|