@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.
Files changed (176) hide show
  1. package/dist/adapters/ambiguous-nil-arithmetic.d.ts +4 -3
  2. package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -1
  3. package/dist/adapters/boolean-result.d.ts +32 -10
  4. package/dist/adapters/boolean-result.d.ts.map +1 -1
  5. package/dist/adapters/brace-delimiter-text.d.ts +18 -0
  6. package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
  7. package/dist/adapters/callback-method.d.ts +38 -5
  8. package/dist/adapters/callback-method.d.ts.map +1 -1
  9. package/dist/adapters/computed-const.d.ts +11 -1
  10. package/dist/adapters/computed-const.d.ts.map +1 -1
  11. package/dist/adapters/dsl-template-adapter.d.ts +21 -0
  12. package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
  13. package/dist/adapters/loop-bound-names.d.ts +9 -1
  14. package/dist/adapters/loop-bound-names.d.ts.map +1 -1
  15. package/dist/adapters/loop-row-type.d.ts +52 -0
  16. package/dist/adapters/loop-row-type.d.ts.map +1 -0
  17. package/dist/adapters/nil-reading.d.ts +68 -0
  18. package/dist/adapters/nil-reading.d.ts.map +1 -0
  19. package/dist/adapters/non-primitive-equality.d.ts +33 -0
  20. package/dist/adapters/non-primitive-equality.d.ts.map +1 -0
  21. package/dist/adapters/number-value.d.ts +5 -3
  22. package/dist/adapters/number-value.d.ts.map +1 -1
  23. package/dist/adapters/operand-typing.d.ts +126 -0
  24. package/dist/adapters/operand-typing.d.ts.map +1 -0
  25. package/dist/adapters/parsed-expr-emitter.d.ts +70 -12
  26. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  27. package/dist/adapters/primitive-plan.d.ts +291 -0
  28. package/dist/adapters/primitive-plan.d.ts.map +1 -0
  29. package/dist/analyzer-context.d.ts +4 -0
  30. package/dist/analyzer-context.d.ts.map +1 -1
  31. package/dist/analyzer.d.ts.map +1 -1
  32. package/dist/augment-inherited-props.d.ts +63 -8
  33. package/dist/augment-inherited-props.d.ts.map +1 -1
  34. package/dist/compiler.d.ts.map +1 -1
  35. package/dist/event-handler-name.d.ts +12 -0
  36. package/dist/event-handler-name.d.ts.map +1 -1
  37. package/dist/expression-parser.d.ts +8 -0
  38. package/dist/expression-parser.d.ts.map +1 -1
  39. package/dist/index.d.ts +19 -9
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +4628 -2198
  42. package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
  43. package/dist/ir-to-client-js/child-props-object.d.ts +64 -0
  44. package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
  45. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  46. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  47. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  48. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  49. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +4 -2
  50. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
  51. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  52. package/dist/ir-to-client-js/imports.d.ts +2 -2
  53. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  54. package/dist/ir-to-client-js/index.d.ts +2 -6
  55. package/dist/ir-to-client-js/index.d.ts.map +1 -1
  56. package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
  57. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +30 -18
  58. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
  59. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  60. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
  61. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
  62. package/dist/jsx-to-ir.d.ts +1 -0
  63. package/dist/jsx-to-ir.d.ts.map +1 -1
  64. package/dist/nullable-signal-attr.d.ts +68 -9
  65. package/dist/nullable-signal-attr.d.ts.map +1 -1
  66. package/dist/prop-rewrite.d.ts +7 -0
  67. package/dist/prop-rewrite.d.ts.map +1 -1
  68. package/dist/props-object-whole-read.d.ts +15 -0
  69. package/dist/props-object-whole-read.d.ts.map +1 -0
  70. package/dist/rich-type-refusal.d.ts.map +1 -1
  71. package/dist/scope/binding-scope.d.ts +85 -10
  72. package/dist/scope/binding-scope.d.ts.map +1 -1
  73. package/dist/scope/lexical-scope-walk.d.ts +3 -2
  74. package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
  75. package/dist/scope/root-prop-alias.d.ts +10 -7
  76. package/dist/scope/root-prop-alias.d.ts.map +1 -1
  77. package/dist/strip-types.d.ts +13 -0
  78. package/dist/strip-types.d.ts.map +1 -1
  79. package/dist/template-position-walk.d.ts +21 -3
  80. package/dist/template-position-walk.d.ts.map +1 -1
  81. package/dist/types.d.ts +90 -0
  82. package/dist/types.d.ts.map +1 -1
  83. package/package.json +2 -2
  84. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +3 -3
  85. package/src/__tests__/binding-scope.test.ts +89 -1
  86. package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
  87. package/src/__tests__/boolean-result.test.ts +22 -5
  88. package/src/__tests__/brace-delimiter-text.test.ts +26 -0
  89. package/src/__tests__/callback-method.test.ts +62 -2
  90. package/src/__tests__/child-components-in-map.test.ts +1 -1
  91. package/src/__tests__/computed-const.test.ts +103 -0
  92. package/src/__tests__/emit-to-string-call.test.ts +2 -1
  93. package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
  94. package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
  95. package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
  96. package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
  97. package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
  98. package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
  99. package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +2 -1
  100. package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
  101. package/src/__tests__/lookup-literal-const.test.ts +9 -3
  102. package/src/__tests__/loop-index-reactivity.test.ts +2 -2
  103. package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
  104. package/src/__tests__/loop-plan.test.ts +1 -1
  105. package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
  106. package/src/__tests__/loop-row-type.test.ts +64 -0
  107. package/src/__tests__/lowering-value-position.test.ts +4 -0
  108. package/src/__tests__/module-const-destructure-default.test.ts +156 -0
  109. package/src/__tests__/nested-loop-index-param.test.ts +1 -1
  110. package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
  111. package/src/__tests__/nested-row-shadowing.test.ts +2 -2
  112. package/src/__tests__/nil-reading.test.ts +77 -0
  113. package/src/__tests__/nullable-signal-attr.test.ts +134 -0
  114. package/src/__tests__/primitive-plan.test.ts +416 -0
  115. package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
  116. package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
  117. package/src/__tests__/props-object-whole-read.test.ts +40 -0
  118. package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
  119. package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
  120. package/src/__tests__/rest-binding-gaps.test.ts +192 -0
  121. package/src/__tests__/root-prop-alias.test.ts +53 -8
  122. package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
  123. package/src/__tests__/static-record-literal-array.test.ts +171 -0
  124. package/src/__tests__/string-concat-identifier.test.ts +40 -1
  125. package/src/adapters/ambiguous-nil-arithmetic.ts +22 -13
  126. package/src/adapters/boolean-result.ts +72 -66
  127. package/src/adapters/brace-delimiter-text.ts +25 -0
  128. package/src/adapters/callback-method.ts +151 -27
  129. package/src/adapters/computed-const.ts +238 -4
  130. package/src/adapters/dsl-template-adapter.ts +40 -2
  131. package/src/adapters/loop-bound-names.ts +9 -1
  132. package/src/adapters/loop-plan.ts +1 -1
  133. package/src/adapters/loop-row-type.ts +139 -0
  134. package/src/adapters/nil-reading.ts +297 -0
  135. package/src/adapters/non-primitive-equality.ts +92 -0
  136. package/src/adapters/number-value.ts +17 -4
  137. package/src/adapters/operand-typing.ts +522 -0
  138. package/src/adapters/parsed-expr-emitter.ts +154 -19
  139. package/src/adapters/primitive-plan.ts +696 -0
  140. package/src/analyzer-context.ts +6 -0
  141. package/src/analyzer.ts +505 -19
  142. package/src/augment-inherited-props.ts +108 -10
  143. package/src/compiler.ts +17 -0
  144. package/src/event-handler-name.ts +15 -0
  145. package/src/expression-parser.ts +70 -1
  146. package/src/index.ts +33 -7
  147. package/src/ir-to-client-js/build-references.ts +69 -0
  148. package/src/ir-to-client-js/child-props-object.ts +106 -0
  149. package/src/ir-to-client-js/collect-elements.ts +30 -55
  150. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +6 -1
  151. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +29 -28
  152. package/src/ir-to-client-js/control-flow/shared.ts +20 -22
  153. package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +5 -3
  154. package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +2 -2
  155. package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +1 -1
  156. package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +2 -2
  157. package/src/ir-to-client-js/control-flow/stringify/loop.ts +1 -1
  158. package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +3 -3
  159. package/src/ir-to-client-js/html-template.ts +25 -8
  160. package/src/ir-to-client-js/imports.ts +1 -1
  161. package/src/ir-to-client-js/index.ts +53 -11
  162. package/src/ir-to-client-js/plan/build-static-array-child-init.ts +31 -14
  163. package/src/ir-to-client-js/plan/static-array-child-init.ts +27 -18
  164. package/src/ir-to-client-js/prop-handling.ts +22 -3
  165. package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
  166. package/src/jsx-to-ir.ts +446 -37
  167. package/src/nullable-signal-attr.ts +246 -29
  168. package/src/prop-rewrite.ts +30 -6
  169. package/src/props-object-whole-read.ts +87 -0
  170. package/src/rich-type-refusal.ts +2 -1
  171. package/src/scope/binding-scope.ts +182 -18
  172. package/src/scope/lexical-scope-walk.ts +4 -3
  173. package/src/scope/root-prop-alias.ts +15 -8
  174. package/src/strip-types.ts +31 -3
  175. package/src/template-position-walk.ts +57 -28
  176. package/src/types.ts +87 -0
@@ -30,12 +30,26 @@
30
30
  */
31
31
 
32
32
  /**
33
- * The identifier a template reads as the component's props object:
34
- * `props.X` is the component's prop `X` while no binding shadows `props`
35
- * (#3693).
33
+ * The identifier a template reads as the component's props object when the
34
+ * component does not name one itself: `props.X` is the component's prop `X`
35
+ * while no binding shadows `props` (#3693). A component that takes its props
36
+ * as one object names it with its parameter (`function C(p)`), and
37
+ * {@link BindingScope.forComponent} makes that name the props object instead.
36
38
  */
37
39
  export const PROPS_OBJECT_IDENTIFIER = 'props'
38
40
 
41
+ /**
42
+ * The identifier that names the props object of a component whose props
43
+ * parameter is `propsObjectName` (`IRMetadata.propsObjectName`): that name
44
+ * (`function C(p)` → `p`), or {@link PROPS_OBJECT_IDENTIFIER} when the
45
+ * component destructures its props (`null`). The single answer to "which
46
+ * identifier is the component's props object"; {@link BindingScope.propsObject}
47
+ * carries it.
48
+ */
49
+ export function propsObjectIdentifier(propsObjectName: string | null | undefined): string {
50
+ return propsObjectName ?? PROPS_OBJECT_IDENTIFIER
51
+ }
52
+
39
53
  /**
40
54
  * How a name inside a `ScopeFrame` came to be bound — the row shapes
41
55
  * (`'item'`/`'index'`/`'destructure'`/`'preamble'`) for `'loop-row'` frames,
@@ -88,10 +102,33 @@ export interface LoopBindingSource {
88
102
 
89
103
  /** Structural pick of `TypeInfo` (`packages/jsx/src/types.ts`). */
90
104
  export interface LoopValueType {
105
+ /** `TypeInfo.kind`; `'unknown'` for `any`, `unknown` and unresolved types. */
106
+ readonly kind?: string
91
107
  readonly primitive?: string
92
108
  readonly unionTypes?: readonly LoopValueType[]
109
+ /** A named type's name (`Row`), resolved through {@link NamedTypeFields}. */
110
+ readonly raw?: string
111
+ readonly properties?: readonly LoopValueField[]
112
+ readonly elementType?: LoopValueType
113
+ }
114
+
115
+ /** Structural pick of `PropertyInfo` (`packages/jsx/src/types.ts`). */
116
+ export interface LoopValueField {
117
+ readonly name: string
118
+ readonly type: LoopValueType
119
+ /** `name?:` — the field may be absent, so its value may be `undefined`. */
120
+ readonly optional?: boolean
93
121
  }
94
122
 
123
+ /** The value of a field that may be absent: its declared type or `undefined`. */
124
+ const ABSENT: LoopValueType = { kind: 'primitive', raw: 'undefined', primitive: 'undefined' }
125
+
126
+ /**
127
+ * The fields of a named type (`type Row = { … }`, an interface), by name;
128
+ * undefined when the component's file does not define it as an object type.
129
+ */
130
+ export type NamedTypeFields = (name: string) => readonly LoopValueField[] | undefined
131
+
95
132
  /** Structural copy of `LoopBindingPathSegment` (`packages/jsx/src/types.ts`). */
96
133
  export type RowPathSegment =
97
134
  | { readonly kind: 'field'; readonly key: string; readonly isIdent: boolean }
@@ -102,6 +139,85 @@ function isStringLoopValue(type: LoopValueType): boolean {
102
139
  (type.unionTypes !== undefined && type.unionTypes.length > 0 && type.unionTypes.every(isStringLoopValue))
103
140
  }
104
141
 
142
+ /**
143
+ * The type at `segments` below `type`, through object types, named types
144
+ * (`namedFields`) and array elements; null when a step is not known.
145
+ *
146
+ * A union resolves the path in each member and is the union of the results,
147
+ * null when any member has no such path. A field is therefore definitely a
148
+ * string only when it is one in every member: the element type of a
149
+ * `concat` (`loopRowType`) is the union of its receiver's and arguments'
150
+ * element types, so `Row` and a same-shaped `Other` both read `id: string`,
151
+ * while an `any` member or an omitted field reads nothing (#3762 review).
152
+ */
153
+ export function loopValueTypeAt(
154
+ type: LoopValueType,
155
+ segments: readonly RowPathSegment[],
156
+ namedFields: NamedTypeFields = () => undefined,
157
+ ): LoopValueType | null {
158
+ if (segments.length === 0) return type
159
+ if (type.unionTypes && type.unionTypes.length > 0) {
160
+ const members = type.unionTypes.map(member => loopValueTypeAt(member, segments, namedFields))
161
+ return members.every(m => m !== null) ? unionOfLoopValueTypes(members as LoopValueType[]) : null
162
+ }
163
+ const [segment, ...rest] = segments
164
+ const next = segment.kind === 'index' ? arrayElementType(type) : fieldType(type, segment.key, namedFields)
165
+ return next ? loopValueTypeAt(next, rest, namedFields) : null
166
+ }
167
+
168
+ /**
169
+ * The element type of an array type, or of a union whose every member is an
170
+ * array (a `concat` of arrays, a field read across `concat` rows): the union
171
+ * of their element types. Undefined for anything else. Every reader of an
172
+ * element type goes through this, so a union of arrays reads like an array
173
+ * (#3762 review).
174
+ */
175
+ export function arrayElementType<T extends LoopValueType>(type: T | null | undefined): T | undefined {
176
+ if (!type) return undefined
177
+ if (type.elementType) return type.elementType as T
178
+ const members = type.unionTypes
179
+ if (!members || members.length === 0) return undefined
180
+ const elements = members.map(member => arrayElementType(member))
181
+ return elements.every(e => e !== undefined) ? unionOfLoopValueTypes(elements as T[]) : undefined
182
+ }
183
+
184
+ /**
185
+ * The union of `members`, normalised: one member, or members that are all
186
+ * structurally the same type, is that member itself, so `Row[] ∪ Row[]`
187
+ * keeps the plain `Row[]` shape.
188
+ */
189
+ export function unionOfLoopValueTypes<T extends LoopValueType>(members: readonly T[]): T {
190
+ const [first] = members
191
+ if (members.every(member => isSameLoopValueShape(member, first))) return first
192
+ return { kind: 'union', raw: members.map(m => m.raw ?? 'unknown').join(' | '), unionTypes: members } as unknown as T
193
+ }
194
+
195
+ /** Whether two types have the same structure: kind, name, primitive, fields, elements and members. */
196
+ export function isSameLoopValueShape(a: LoopValueType | undefined, b: LoopValueType | undefined): boolean {
197
+ if (a === b) return true
198
+ if (!a || !b) return false
199
+ return a.kind === b.kind && a.raw === b.raw && a.primitive === b.primitive &&
200
+ isSameLoopValueShape(a.elementType, b.elementType) &&
201
+ sameMembers(a.unionTypes, b.unionTypes, isSameLoopValueShape) &&
202
+ sameMembers(a.properties, b.properties, (x, y) =>
203
+ x.name === y.name && Boolean(x.optional) === Boolean(y.optional) && isSameLoopValueShape(x.type, y.type))
204
+ }
205
+
206
+ function sameMembers<T>(a: readonly T[] | undefined, b: readonly T[] | undefined, same: (x: T, y: T) => boolean): boolean {
207
+ if (!a || !b) return a === b
208
+ return a.length === b.length && a.every((x, i) => same(x, b[i]))
209
+ }
210
+
211
+ /** A field's declared type; an optional one may also be `undefined`. */
212
+ function fieldType(type: LoopValueType, key: string, namedFields: NamedTypeFields): LoopValueType | undefined {
213
+ const fields: readonly LoopValueField[] | undefined =
214
+ type.properties ?? (type.raw !== undefined ? namedFields(type.raw) : undefined)
215
+ const field = fields?.find(f => f.name === key)
216
+ // An optional field reads `undefined` when the row omits it, so it is
217
+ // never definitely its declared type (#3762 review).
218
+ return field && (field.optional ? { kind: 'union', unionTypes: [field.type, ABSENT] } : field.type)
219
+ }
220
+
105
221
  /**
106
222
  * A stack of `ScopeFrame`s, innermost frame at index 0 of the internal
107
223
  * array (i.e. `frames[0]` is what `enterLoopRow`/`enterCallback` most
@@ -110,9 +226,30 @@ function isStringLoopValue(type: LoopValueType): boolean {
110
226
  * frames exist.
111
227
  */
112
228
  export class BindingScope {
113
- static readonly EMPTY: BindingScope = new BindingScope([])
229
+ static readonly EMPTY: BindingScope = new BindingScope([], PROPS_OBJECT_IDENTIFIER)
114
230
 
115
- private constructor(private readonly frames: readonly ScopeFrame[]) {}
231
+ /**
232
+ * `propsObject`: the identifier that names the component's props object
233
+ * in this scope's component ({@link forComponent}). Every child scope
234
+ * keeps it.
235
+ */
236
+ private constructor(
237
+ private readonly frames: readonly ScopeFrame[],
238
+ readonly propsObject: string,
239
+ ) {}
240
+
241
+ /**
242
+ * The empty scope of a component whose props object parameter is
243
+ * `propsObjectName` (`IRMetadata.propsObjectName`, decided in Phase 1):
244
+ * `function C(p)` reads its props as `p.X`, so `p` is the props object.
245
+ * A component that destructures its props (`null`) keeps
246
+ * {@link PROPS_OBJECT_IDENTIFIER}.
247
+ */
248
+ static forComponent(propsObjectName: string | null): BindingScope {
249
+ const propsObject = propsObjectIdentifier(propsObjectName)
250
+ if (propsObject === PROPS_OBJECT_IDENTIFIER) return BindingScope.EMPTY
251
+ return new BindingScope([], propsObject)
252
+ }
116
253
 
117
254
  /**
118
255
  * Child scope with a new `'loop-row'` frame for one loop's per-item
@@ -144,8 +281,8 @@ export class BindingScope {
144
281
  * `adapters/loop-bound-names.ts`'s `collectLoopBoundNames` adds BOTH
145
282
  * `node.param` AND every `paramBindings[i].name` unconditionally
146
283
  * (never skipping `param` in the destructured case) — a deliberately
147
- * coarser, over-inclusive collection used only to subtract names from
148
- * a flat string-typing Set (safe to over-exclude there). This method
284
+ * coarser, over-inclusive collection for whole-component "may be
285
+ * shadowed somewhere" checks (safe to over-exclude there). This method
149
286
  * follows the precise `jsx-to-ir.ts` `ctx.loopParams` semantics, since
150
287
  * that is the mechanism actually doing scope-shadowed name RESOLUTION
151
288
  * (the behavior `BindingScope` replaces), not coarse exclusion.
@@ -171,7 +308,7 @@ export class BindingScope {
171
308
  for (const name of loop.preamble?.declaredNames ?? []) bindings.set(name, { source: 'preamble' })
172
309
 
173
310
  const frame: ScopeFrame = { kind: 'loop-row', bindings }
174
- return new BindingScope([frame, ...this.frames])
311
+ return new BindingScope([frame, ...this.frames], this.propsObject)
175
312
  }
176
313
 
177
314
  /**
@@ -183,7 +320,7 @@ export class BindingScope {
183
320
  const bindings = new Map<string, ScopeBinding>()
184
321
  for (const name of params) bindings.set(name, { source: 'param' })
185
322
  const frame: ScopeFrame = { kind: 'callback', bindings }
186
- return new BindingScope([frame, ...this.frames])
323
+ return new BindingScope([frame, ...this.frames], this.propsObject)
187
324
  }
188
325
 
189
326
  /**
@@ -202,7 +339,7 @@ export class BindingScope {
202
339
  if (hit) bindings.set(alias, hit.binding)
203
340
  }
204
341
  if (bindings.size === 0) return this
205
- return new BindingScope([{ kind: 'callback', bindings }, ...this.frames])
342
+ return new BindingScope([{ kind: 'callback', bindings }, ...this.frames], this.propsObject)
206
343
  }
207
344
 
208
345
  /** Innermost-first membership check across every frame in the stack. */
@@ -215,25 +352,52 @@ export class BindingScope {
215
352
 
216
353
  /**
217
354
  * Whether a binding here shadows the component's props object
218
- * ({@link PROPS_OBJECT_IDENTIFIER}): a `.map((props) => …)` row, a
355
+ * ({@link propsObject}): a `.map((props) => …)` row, a
219
356
  * destructured `({ props })` field, a preamble local or a callback param.
220
357
  * `props.X` then reads that binding's member, not the component's prop
221
358
  * `X` (#3693).
222
359
  */
223
360
  shadowsPropsObject(): boolean {
224
- return this.isBound(PROPS_OBJECT_IDENTIFIER)
361
+ return this.isBound(this.propsObject)
225
362
  }
226
363
 
227
364
  /**
228
365
  * Resolve string typing at this position, respecting the nearest binding.
229
- * `componentProperty`: `name` is read as `props.<name>`, which is the
230
- * component's prop only while no binding shadows `props`; a shadowing
231
- * binding's member has no typing here.
366
+ * `componentProperty`: `name` is read as the literal `props.<name>`. That is
367
+ * the component's prop only when `props` is this component's props object
368
+ * and no binding shadows it; while a binding named `props` is in scope, it
369
+ * is that binding's member. `path`: the read is the member chain
370
+ * `name.<path…>` (`row.id`). A loop row binding resolves the chain against
371
+ * its row type (`LoopBindingSource.rowType`), through `namedFields` for a
372
+ * named type; an outer name has typing for itself only.
232
373
  */
233
- isStringValueName(name: string, outer: (name: string) => boolean, componentProperty = false): boolean {
234
- if (componentProperty) return !this.shadowsPropsObject() && outer(name)
374
+ isStringValueName(
375
+ name: string,
376
+ outer: (name: string) => boolean,
377
+ componentProperty = false,
378
+ path: readonly string[] = [],
379
+ namedFields?: NamedTypeFields,
380
+ ): boolean {
381
+ if (componentProperty) {
382
+ // The operand classifiers flag only the literal receiver `props`
383
+ // (`props.<name>`). A binding named `props` reads its own member.
384
+ if (this.isBound(PROPS_OBJECT_IDENTIFIER)) {
385
+ return this.isStringValueName(PROPS_OBJECT_IDENTIFIER, outer, false, [name, ...path], namedFields)
386
+ }
387
+ // In a component whose props object has another name (`function C(p)`),
388
+ // an unbound `props` is not that object, so it carries no prop typing.
389
+ if (this.propsObject !== PROPS_OBJECT_IDENTIFIER) return false
390
+ return path.length === 0 && outer(name)
391
+ }
235
392
  const hit = this.lookup(name)
236
- return hit ? hit.binding.stringValue === true : outer(name)
393
+ // An outer name's typing covers the name itself, not a member of it.
394
+ if (!hit) return path.length === 0 && outer(name)
395
+ const { stringValue, row } = hit.binding
396
+ if (path.length === 0 && stringValue !== undefined) return stringValue
397
+ if (!row) return false
398
+ const segments = [...row.segments, ...path.map(key => ({ kind: 'field' as const, key, isIdent: true }))]
399
+ const type = loopValueTypeAt(row.itemType, segments, namedFields)
400
+ return type !== null && isStringLoopValue(type)
237
401
  }
238
402
 
239
403
  /** Whether any frame is a loop row: the position sits inside a `.map()` row body. */
@@ -141,11 +141,12 @@ function computedNameOf(n: ts.Node): ts.ComputedPropertyName | undefined {
141
141
  /**
142
142
  * Depth-first walk that maintains the binding stack described above and
143
143
  * reports every identifier along with whether a binding within `root`
144
- * currently shadows it.
144
+ * currently shadows it. `isShadowed` answers the same question for any
145
+ * other name at that identifier's position.
145
146
  */
146
147
  export function walkWithScope(
147
148
  root: ts.Node,
148
- visit: (ident: ts.Identifier, parent: ts.Node | undefined, shadowed: boolean) => void,
149
+ visit: (ident: ts.Identifier, parent: ts.Node | undefined, shadowed: boolean, isShadowed: (name: string) => boolean) => void,
149
150
  ): void {
150
151
  const scopeStack: Set<string>[] = []
151
152
  const isShadowed = (name: string) => scopeStack.some(frame => frame.has(name))
@@ -157,7 +158,7 @@ export function walkWithScope(
157
158
  const outerName = frame ? computedNameOf(n) : undefined
158
159
  if (outerName) rec(outerName, n)
159
160
  if (frame) scopeStack.push(frame)
160
- if (ts.isIdentifier(n)) visit(n, parent, isShadowed(n.text))
161
+ if (ts.isIdentifier(n)) visit(n, parent, isShadowed(n.text), isShadowed)
161
162
  ts.forEachChild(n, child => {
162
163
  if (child !== outerName) rec(child, n)
163
164
  })
@@ -1,7 +1,7 @@
1
1
  import { collectLoopBoundNames } from '../adapters/loop-bound-names.ts'
2
2
  import type { ParsedExpr } from '../expression-parser.ts'
3
3
  import type { ComponentIR } from '../types.ts'
4
- import { PROPS_OBJECT_IDENTIFIER, type BindingScope } from './binding-scope.ts'
4
+ import type { BindingScope } from './binding-scope.ts'
5
5
 
6
6
  /**
7
7
  * Root-prop aliases for template engines that flatten `props.X` to the bare
@@ -84,14 +84,21 @@ export function rootPropReadName(
84
84
  /**
85
85
  * Whether `expr` reads the component's props object at a position where
86
86
  * `isBound` answers which names a binding shadows: the bare identifier
87
- * `props` that no binding shadows (#3693). A `.map((props) => …)` row, a
88
- * destructured `({ props })` field, a preamble local or a callback param
89
- * named `props` is that binding instead, so `props.X` reads its member
90
- * rather than flattening to the component's prop `X`. Template emitters
91
- * flatten a `props.X` read only when this holds; `isBound` is the
87
+ * `propsObject` that no binding shadows (#3693). `propsObject` is the
88
+ * component's props object identifier, `BindingScope.propsObject`: the
89
+ * props parameter's own name (`function C(p)` reads `p.X`), or `props` for
90
+ * a component that destructures its props. A `.map((props) => …)` row, a
91
+ * destructured `({ props })` field, a preamble local or a callback param of
92
+ * that name is that binding instead, so `props.X` reads its member rather
93
+ * than flattening to the component's prop `X`. Template emitters flatten a
94
+ * props-object member read only when this holds; `isBound` is the
92
95
  * adapter's current `BindingScope` (`scope.isBound`), or, in a filter
93
96
  * predicate, the predicate's own params plus the enclosing scope.
94
97
  */
95
- export function isRootPropsObject(expr: ParsedExpr, isBound: (name: string) => boolean): boolean {
96
- return expr.kind === 'identifier' && expr.name === PROPS_OBJECT_IDENTIFIER && !isBound(expr.name)
98
+ export function isRootPropsObject(
99
+ expr: ParsedExpr,
100
+ isBound: (name: string) => boolean,
101
+ propsObject: string,
102
+ ): boolean {
103
+ return expr.kind === 'identifier' && expr.name === propsObject && !isBound(expr.name)
97
104
  }
@@ -15,6 +15,31 @@ import ts from 'typescript'
15
15
  export interface ExcludeRange {
16
16
  start: number
17
17
  end: number
18
+ /**
19
+ * Text that stands in for the range instead of dropping it: a read the
20
+ * analyzer respells (`addSourceReplacement`). Applied only when the whole
21
+ * range lies inside the node being reconstructed; a node inside the range
22
+ * (`rest` of `rest.id`) keeps its own text.
23
+ */
24
+ replacement?: string
25
+ }
26
+
27
+ /**
28
+ * Insert `range` (with its `replacement`) into the sorted, merged `ranges`.
29
+ * The span must not overlap a range already there; one that does is not
30
+ * added, and false is returned.
31
+ */
32
+ export function addSourceReplacement(ranges: ExcludeRange[], range: Required<ExcludeRange>): boolean {
33
+ let i = 0
34
+ while (i < ranges.length && ranges[i].end <= range.start) i++
35
+ if (i < ranges.length && ranges[i].start < range.end) return false
36
+ ranges.splice(i, 0, range)
37
+ return true
38
+ }
39
+
40
+ /** Whether `range` applies to the node spanning `[nodeStart, nodeEnd)`. */
41
+ function appliesWithin(range: ExcludeRange, nodeStart: number, nodeEnd: number): boolean {
42
+ return range.replacement === undefined || (range.start >= nodeStart && range.end <= nodeEnd)
18
43
  }
19
44
 
20
45
  /**
@@ -59,9 +84,11 @@ export function reconstructWithoutTypes(
59
84
  for (let i = lo; i < ranges.length; i++) {
60
85
  const range = ranges[i]
61
86
  if (range.start >= nodeEnd) break
87
+ if (!appliesWithin(range, nodeStart, nodeEnd)) continue
62
88
  if (range.start > pos) {
63
89
  result += fullText.slice(pos, range.start)
64
90
  }
91
+ if (range.replacement !== undefined) result += range.replacement
65
92
  pos = Math.max(pos, range.end)
66
93
  }
67
94
  if (pos < nodeEnd) {
@@ -91,11 +118,11 @@ export function reconstructAsSegments(
91
118
  const nodeEnd = node.getEnd()
92
119
  const fullText = sourceFile.text
93
120
 
94
- type Edit = { start: number; end: number; marker: number | null }
121
+ type Edit = { start: number; end: number; marker: number | null; replacement?: string }
95
122
  const edits: Edit[] = []
96
123
  for (const r of ranges) {
97
- if (r.end <= nodeStart || r.start >= nodeEnd) continue
98
- edits.push({ start: r.start, end: r.end, marker: null })
124
+ if (r.end <= nodeStart || r.start >= nodeEnd || !appliesWithin(r, nodeStart, nodeEnd)) continue
125
+ edits.push({ start: r.start, end: r.end, marker: null, ...(r.replacement !== undefined && { replacement: r.replacement }) })
99
126
  }
100
127
  markers.forEach((m, i) => edits.push({ start: m.start, end: m.end, marker: i }))
101
128
  edits.sort((a, b) => a.start - b.start || b.end - a.end)
@@ -106,6 +133,7 @@ export function reconstructAsSegments(
106
133
  for (const edit of edits) {
107
134
  if (edit.start < pos) continue // subsumed by an already-emitted span
108
135
  jsBuf += fullText.slice(pos, edit.start)
136
+ if (edit.replacement !== undefined) jsBuf += edit.replacement
109
137
  if (edit.marker !== null) {
110
138
  if (jsBuf) segments.push({ js: jsBuf })
111
139
  jsBuf = ''
@@ -9,7 +9,9 @@
9
9
  * (expression, spread, and structured template parts), loop arrays, and the
10
10
  * children of elements, components, fragments, providers, async boundaries,
11
11
  * loops (including child components, nested components, and `flatMap` /
12
- * preamble JSX segments) and conditional branches.
12
+ * preamble JSX segments) and conditional branches, and a loop preamble's
13
+ * value-declaration initializers (`const same = a === b` in a `.map()` body),
14
+ * which a template adapter evaluates at SSR as a per-row local.
13
15
  *
14
16
  * A `/* @client *\/`-marked position (`clientOnly`) is skipped: the whole read
15
17
  * is deferred to the client, where real JS evaluates it.
@@ -29,12 +31,14 @@ import type {
29
31
  ExpressionAttr,
30
32
  IRConditional,
31
33
  IRIfStatement,
34
+ IRLoop,
32
35
  IRNode,
33
36
  IRTemplatePart,
34
37
  SourceLocation,
35
38
  } from './types.ts'
36
39
  import type { ParsedExpr } from './expression-parser.ts'
37
40
  import { parseExpression } from './expression-parser.ts'
41
+ import type { BindingScope } from './scope/binding-scope.ts'
38
42
 
39
43
  /**
40
44
  * How the template uses the value at a position:
@@ -51,6 +55,10 @@ import { parseExpression } from './expression-parser.ts'
51
55
  * text parsed on demand here; every adapter lowers them from that raw text,
52
56
  * so nothing attached to the IR can stand in for them.
53
57
  * - `loop-array` — a loop's array expression.
58
+ * - `preamble-init` — the initializer of a loop preamble's value declaration
59
+ * (`MapCallbackPreamble.declarations`), bound as a per-row local. The read
60
+ * of that local is a bare name elsewhere in the row, so the initializer is
61
+ * the only position that shows what the template computes.
54
62
  */
55
63
  export type TemplatePositionRole =
56
64
  | 'text'
@@ -60,6 +68,7 @@ export type TemplatePositionRole =
60
68
  | 'spread'
61
69
  | 'template-part'
62
70
  | 'loop-array'
71
+ | 'preamble-init'
63
72
 
64
73
  /**
65
74
  * The IR slot a position's `expr` lives in, for a pass that rewrites the
@@ -79,10 +88,21 @@ export interface TemplatePosition {
79
88
  /** The attribute or prop name, when the position is an attribute / prop value. */
80
89
  attrName?: string
81
90
  host?: TemplatePositionHost
91
+ /**
92
+ * The scope the position is read in, when the walk was given the
93
+ * component's scope: each loop's children are read in its row
94
+ * (`BindingScope.enterLoopRow`), its array outside it. The innermost
95
+ * scope wins: an outer wrapper never replaces a row's.
96
+ */
97
+ scope?: BindingScope
82
98
  }
83
99
 
84
- export function walkTemplatePositions(root: IRNode, visit: (position: TemplatePosition) => void): void {
85
- walkNode(root, visit)
100
+ /**
101
+ * Visit every template-lowered position under `root`. With `scope` (the
102
+ * component's), each position also carries the scope it is read in.
103
+ */
104
+ export function walkTemplatePositions(root: IRNode, visit: (position: TemplatePosition) => void, scope?: BindingScope): void {
105
+ walkNode(root, scope ? (position) => visit({ scope, ...position }) : visit, scope)
86
106
  }
87
107
 
88
108
  /**
@@ -142,7 +162,7 @@ function walkAttrValue(
142
162
  }
143
163
  }
144
164
 
145
- function walkNode(node: IRNode, visit: (position: TemplatePosition) => void): void {
165
+ function walkNode(node: IRNode, visit: (position: TemplatePosition) => void, scope?: BindingScope): void {
146
166
  if (node.type === 'expression') {
147
167
  if (!node.clientOnly && node.parsed) visit({ expr: node.parsed, loc: node.loc, role: 'text' })
148
168
  } else if (node.type === 'conditional') {
@@ -168,38 +188,47 @@ function walkNode(node: IRNode, visit: (position: TemplatePosition) => void): vo
168
188
  case 'component':
169
189
  case 'fragment':
170
190
  case 'provider':
171
- for (const child of node.children) walkNode(child, visit)
191
+ for (const child of node.children) walkNode(child, visit, scope)
172
192
  break
173
193
  case 'async':
174
- walkNode(node.fallback, visit)
175
- for (const child of node.children) walkNode(child, visit)
194
+ walkNode(node.fallback, visit, scope)
195
+ for (const child of node.children) walkNode(child, visit, scope)
176
196
  break
177
- case 'loop': {
178
- if (node.clientOnly) break
179
- if (node.arrayParsed) visit({ expr: node.arrayParsed, loc: node.loc, role: 'loop-array' })
180
- for (const child of node.children) walkNode(child, visit)
181
- if (node.childComponent) {
182
- for (const child of node.childComponent.children) walkNode(child, visit)
183
- }
184
- for (const nested of node.nestedComponents ?? []) {
185
- for (const child of nested.children) walkNode(child, visit)
186
- }
187
- for (const seg of node.flatMapCallback?.segments ?? []) {
188
- if (seg.kind === 'jsx') walkNode(seg.ir, visit)
189
- }
190
- for (const seg of node.preamble?.segments ?? []) {
191
- if (seg.kind === 'jsx') walkNode(seg.ir, visit)
192
- }
197
+ case 'loop':
198
+ walkLoop(node, visit, scope)
193
199
  break
194
- }
195
200
  case 'conditional':
196
201
  if (node.clientOnly) break
197
- walkNode(node.whenTrue, visit)
198
- walkNode(node.whenFalse, visit)
202
+ walkNode(node.whenTrue, visit, scope)
203
+ walkNode(node.whenFalse, visit, scope)
199
204
  break
200
205
  case 'if-statement':
201
- walkNode(node.consequent, visit)
202
- if (node.alternate) walkNode(node.alternate, visit)
206
+ walkNode(node.consequent, visit, scope)
207
+ if (node.alternate) walkNode(node.alternate, visit, scope)
203
208
  break
204
209
  }
205
210
  }
211
+
212
+ /**
213
+ * A loop's array, then its row's positions, each read in the row's scope:
214
+ * the preamble's declaration initializers first (the row scope binds every
215
+ * preamble name, and an initializer can only read an earlier one), then the
216
+ * row's children.
217
+ */
218
+ function walkLoop(node: IRLoop, visit: (position: TemplatePosition) => void, scope?: BindingScope): void {
219
+ if (node.clientOnly) return
220
+ if (node.arrayParsed) visit({ expr: node.arrayParsed, loc: node.loc, role: 'loop-array' })
221
+ const row = scope?.enterLoopRow(node)
222
+ const rowVisit = row ? (position: TemplatePosition) => visit({ scope: row, ...position }) : visit
223
+ for (const declaration of node.preamble?.declarations ?? []) {
224
+ rowVisit({ expr: declaration.valueParsed, loc: node.loc, role: 'preamble-init' })
225
+ }
226
+ const children = [
227
+ ...node.children,
228
+ ...(node.childComponent?.children ?? []),
229
+ ...(node.nestedComponents ?? []).flatMap(nested => nested.children),
230
+ ...(node.flatMapCallback?.segments ?? []).flatMap(seg => (seg.kind === 'jsx' ? [seg.ir] : [])),
231
+ ...(node.preamble?.segments ?? []).flatMap(seg => (seg.kind === 'jsx' ? [seg.ir] : [])),
232
+ ]
233
+ for (const child of children) walkNode(child, rowVisit, row)
234
+ }