@barefootjs/jsx 0.39.5 → 0.39.7

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 (129) hide show
  1. package/dist/adapters/array-method.d.ts +93 -0
  2. package/dist/adapters/array-method.d.ts.map +1 -0
  3. package/dist/adapters/attr-plan.d.ts +142 -0
  4. package/dist/adapters/attr-plan.d.ts.map +1 -0
  5. package/dist/adapters/boolean-result.d.ts +84 -9
  6. package/dist/adapters/boolean-result.d.ts.map +1 -1
  7. package/dist/adapters/callback-method.d.ts +153 -0
  8. package/dist/adapters/callback-method.d.ts.map +1 -0
  9. package/dist/adapters/component-props.d.ts +99 -0
  10. package/dist/adapters/component-props.d.ts.map +1 -0
  11. package/dist/adapters/component-tree.d.ts +25 -0
  12. package/dist/adapters/component-tree.d.ts.map +1 -0
  13. package/dist/adapters/computed-const.d.ts +52 -0
  14. package/dist/adapters/computed-const.d.ts.map +1 -0
  15. package/dist/adapters/dangerous-inner-html.d.ts +10 -0
  16. package/dist/adapters/dangerous-inner-html.d.ts.map +1 -1
  17. package/dist/adapters/element-shell.d.ts +44 -0
  18. package/dist/adapters/element-shell.d.ts.map +1 -0
  19. package/dist/adapters/interface.d.ts +2 -0
  20. package/dist/adapters/interface.d.ts.map +1 -1
  21. package/dist/adapters/literal-value.d.ts +22 -0
  22. package/dist/adapters/literal-value.d.ts.map +1 -0
  23. package/dist/adapters/loop-plan.d.ts +53 -0
  24. package/dist/adapters/loop-plan.d.ts.map +1 -0
  25. package/dist/adapters/prop-classes.d.ts +37 -0
  26. package/dist/adapters/prop-classes.d.ts.map +1 -0
  27. package/dist/analyzer.d.ts.map +1 -1
  28. package/dist/augment-inherited-props.d.ts +7 -0
  29. package/dist/augment-inherited-props.d.ts.map +1 -1
  30. package/dist/expression-parser.d.ts +38 -0
  31. package/dist/expression-parser.d.ts.map +1 -1
  32. package/dist/fold-render-reassignments.d.ts +43 -0
  33. package/dist/fold-render-reassignments.d.ts.map +1 -0
  34. package/dist/free-refs.d.ts +9 -0
  35. package/dist/free-refs.d.ts.map +1 -1
  36. package/dist/index.d.ts +23 -5
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js +3954 -2401
  39. package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
  40. package/dist/ir-to-client-js/csr-substitute.d.ts +0 -13
  41. package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
  42. package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
  43. package/dist/ir-to-client-js/html-template.d.ts +9 -10
  44. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  45. package/dist/ir-to-client-js/index.d.ts.map +1 -1
  46. package/dist/ir-to-client-js/plan/declaration-emit.d.ts +5 -0
  47. package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
  48. package/dist/ir-to-client-js/prop-handling.d.ts +11 -12
  49. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  50. package/dist/ir-to-client-js/utils.d.ts +13 -0
  51. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  52. package/dist/jsx-to-ir.d.ts +465 -1
  53. package/dist/jsx-to-ir.d.ts.map +1 -1
  54. package/dist/nullable-signal-attr.d.ts +22 -2
  55. package/dist/nullable-signal-attr.d.ts.map +1 -1
  56. package/dist/props-binding.d.ts +34 -5
  57. package/dist/props-binding.d.ts.map +1 -1
  58. package/dist/relocate.d.ts +16 -0
  59. package/dist/relocate.d.ts.map +1 -1
  60. package/dist/ssr-seed-plan.d.ts +0 -6
  61. package/dist/ssr-seed-plan.d.ts.map +1 -1
  62. package/dist/types.d.ts +54 -2
  63. package/dist/types.d.ts.map +1 -1
  64. package/package.json +2 -2
  65. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +1 -1
  66. package/src/__tests__/action-accessor.test.ts +6 -24
  67. package/src/__tests__/array-method-catalogue.test.ts +99 -0
  68. package/src/__tests__/attr-plan.test.ts +121 -0
  69. package/src/__tests__/binding-scope-ratchet.test.ts +12 -12
  70. package/src/__tests__/block-memo-type.test.ts +51 -0
  71. package/src/__tests__/boolean-result-expr.test.ts +106 -0
  72. package/src/__tests__/boolean-result.test.ts +34 -1
  73. package/src/__tests__/callback-method.test.ts +264 -0
  74. package/src/__tests__/client-only-date-lowering.test.ts +2 -2
  75. package/src/__tests__/component-props.test.ts +148 -0
  76. package/src/__tests__/computed-const.test.ts +101 -0
  77. package/src/__tests__/delegated-handler-preamble.test.ts +4 -4
  78. package/src/__tests__/dsl-shared-helpers.test.ts +166 -0
  79. package/src/__tests__/element-shell.test.ts +78 -0
  80. package/src/__tests__/fold-render-reassignments.test.ts +96 -0
  81. package/src/__tests__/inlinable-local-method-call.test.ts +81 -0
  82. package/src/__tests__/issue-3240-nested-static-map-event-delegation.test.ts +1 -1
  83. package/src/__tests__/loop-plan.test.ts +104 -0
  84. package/src/__tests__/map-call-decisions.test.ts +521 -0
  85. package/src/__tests__/memo-body-expr.test.ts +120 -0
  86. package/src/__tests__/nullable-signal-attr.test.ts +45 -0
  87. package/src/__tests__/prop-handling.test.ts +35 -16
  88. package/src/__tests__/props-alias-const.test.ts +65 -0
  89. package/src/__tests__/signal-prop-seed.test.ts +90 -0
  90. package/src/__tests__/ssr-seed-plan.test.ts +54 -1
  91. package/src/__tests__/static-template-reachability.test.ts +137 -0
  92. package/src/adapters/array-method.ts +193 -0
  93. package/src/adapters/attr-plan.ts +228 -0
  94. package/src/adapters/boolean-result.ts +208 -12
  95. package/src/adapters/callback-method.ts +363 -0
  96. package/src/adapters/component-props.ts +144 -0
  97. package/src/adapters/component-tree.ts +118 -0
  98. package/src/adapters/computed-const.ts +185 -0
  99. package/src/adapters/dangerous-inner-html.ts +33 -0
  100. package/src/adapters/element-shell.ts +160 -0
  101. package/src/adapters/interface.ts +8 -0
  102. package/src/adapters/literal-value.ts +36 -0
  103. package/src/adapters/loop-plan.ts +88 -0
  104. package/src/adapters/prop-classes.ts +73 -0
  105. package/src/analyzer.ts +271 -82
  106. package/src/augment-inherited-props.ts +12 -0
  107. package/src/expression-parser.ts +537 -374
  108. package/src/fold-render-reassignments.ts +314 -0
  109. package/src/free-refs.ts +49 -0
  110. package/src/index.ts +61 -3
  111. package/src/ir-to-client-js/compute-inlinability.ts +93 -17
  112. package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +9 -7
  113. package/src/ir-to-client-js/csr-substitute.ts +13 -35
  114. package/src/ir-to-client-js/emit-registration.ts +3 -2
  115. package/src/ir-to-client-js/html-template.ts +26 -70
  116. package/src/ir-to-client-js/index.ts +3 -2
  117. package/src/ir-to-client-js/init-declarations.ts +1 -1
  118. package/src/ir-to-client-js/plan/build-declaration-emit.ts +25 -51
  119. package/src/ir-to-client-js/plan/declaration-emit.ts +5 -0
  120. package/src/ir-to-client-js/prop-handling.ts +13 -54
  121. package/src/ir-to-client-js/rewrite-destructured-props.ts +6 -5
  122. package/src/ir-to-client-js/stringify/declaration-emit.ts +1 -1
  123. package/src/ir-to-client-js/utils.ts +11 -0
  124. package/src/jsx-to-ir.ts +1346 -1022
  125. package/src/nullable-signal-attr.ts +85 -4
  126. package/src/props-binding.ts +100 -5
  127. package/src/relocate.ts +40 -8
  128. package/src/ssr-seed-plan.ts +18 -6
  129. package/src/types.ts +50 -2
@@ -89,6 +89,10 @@ export interface NullishAttrContext {
89
89
  * (`arithmeticOperandIsUndefined`). */
90
90
  readonly undefinedSeededSignals?: ReadonlySet<string>
91
91
  readonly memos: ReadonlyMap<string, MemoInfo>
92
+ /** Props declared as a number that may be `null` or absent (`n: number |
93
+ * null`, `n?: number`), by the name a read uses: the binding name, and
94
+ * `<propsObjectName>.<key>` for a member read (#3474). */
95
+ readonly nullableNumberProps?: ReadonlySet<string>
92
96
  }
93
97
 
94
98
  export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
@@ -102,9 +106,31 @@ export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
102
106
  signals.filter(s => s.parsed?.kind === 'identifier' && s.parsed.name === 'undefined').map(s => s.getter),
103
107
  ),
104
108
  memos: new Map((ir.metadata.memos ?? []).map(m => [m.name, m])),
109
+ nullableNumberProps: collectNullableNumberProps(ir),
105
110
  }
106
111
  }
107
112
 
113
+ /** Whether `type` is a number that admits `null` / `undefined` and no other family. */
114
+ function isNullableNumberType(type: TypeInfo | undefined, optional: boolean): boolean {
115
+ if (!type) return false
116
+ const members = type.kind === 'union' ? (type.unionTypes ?? []) : [type]
117
+ const families = new Set(members.map(m => (m.kind === 'primitive' ? m.primitive : m.kind)))
118
+ const nullable = optional || families.has('null') || families.has('undefined')
119
+ families.delete('null')
120
+ families.delete('undefined')
121
+ return nullable && families.size === 1 && families.has('number')
122
+ }
123
+
124
+ function collectNullableNumberProps(ir: ComponentIR): ReadonlySet<string> {
125
+ const names = new Set<string>()
126
+ const objectName = ir.metadata.propsObjectName
127
+ for (const p of ir.metadata.propsParams) {
128
+ if (p.isRest || p.defaultValue !== undefined || !isNullableNumberType(p.type, p.optional)) continue
129
+ names.add(objectName ? `${objectName}.${p.sourceName ?? p.name}` : p.name)
130
+ }
131
+ return names
132
+ }
133
+
108
134
  /**
109
135
  * Whether an attribute value's SSR value can be `undefined` / `null`, so a
110
136
  * DSL adapter must guard the attribute on a nil check to omit it the way
@@ -114,8 +140,9 @@ export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
114
140
  *
115
141
  * - a read of a nullable signal or of a memo whose type or body can be
116
142
  * nullish (`label()` with `label = createMemo(() => s())`);
117
- * - an optional member read (`user()?.name`), or a member read of a value
118
- * that can itself be nullish;
143
+ * - an optional member read (`user()?.name`), an index or computed-key read
144
+ * (`pair[1]`, `row[key]`), or a member read of a value that can itself be
145
+ * nullish;
119
146
  * - a ternary branch, or an `??` / `||` / `&&` operand the result can be;
120
147
  * - a literal `undefined` / `null`.
121
148
  *
@@ -152,10 +179,12 @@ export function attrValueMayBeNullish(
152
179
  visiting.delete(getter)
153
180
  return result
154
181
  }
182
+ // A computed key or an index (`pair[1]`, `row[key]`) reads `undefined`
183
+ // when it is out of range or absent (#3497).
155
184
  case 'member':
156
- return expr.optional || walk(expr.object, shadowed)
185
+ return expr.optional || expr.computed || walk(expr.object, shadowed)
157
186
  case 'index-access':
158
- return walk(expr.object, shadowed)
187
+ return true
159
188
  case 'conditional':
160
189
  return walk(expr.consequent, shadowed) || walk(expr.alternate, shadowed)
161
190
  case 'logical':
@@ -210,6 +239,8 @@ function arithmeticOperandSeed(
210
239
  const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): 'null' | 'undefined' | null => {
211
240
  if (expr.kind === 'literal') return expr.literalType === 'null' ? 'null' : null
212
241
  if (expr.kind === 'identifier' && expr.name === 'undefined' && !shadowed('undefined')) return 'undefined'
242
+ const prop = nullableNumberPropRead(expr, ctx, shadowed)
243
+ if (prop) return 'null'
213
244
  const getter = expr.kind === 'identifier' ? expr.name : bareGetterCallName(expr)
214
245
  if (getter === null || shadowed(getter)) return null
215
246
  if (ctx.nullSeededSignals?.has(getter)) return 'null'
@@ -224,6 +255,25 @@ function arithmeticOperandSeed(
224
255
  return parsed === undefined ? null : walk(parsed, isShadowed)
225
256
  }
226
257
 
258
+ /**
259
+ * A read of a prop declared as a nullable number. Its SSR value may be
260
+ * `null` or absent; it is coerced like a `null` operand. JS reads an absent
261
+ * prop as `undefined` (`NaN` in arithmetic), which a template engine cannot
262
+ * tell from `null` (the `omitted-optional-number-arithmetic` limitation).
263
+ */
264
+ function nullableNumberPropRead(
265
+ expr: ParsedExpr,
266
+ ctx: NullishAttrContext,
267
+ shadowed: (name: string) => boolean,
268
+ ): boolean {
269
+ if (!ctx.nullableNumberProps?.size) return false
270
+ if (expr.kind === 'identifier') return !shadowed(expr.name) && ctx.nullableNumberProps.has(expr.name)
271
+ if (expr.kind === 'member' && !expr.computed && expr.object.kind === 'identifier' && !shadowed(expr.object.name)) {
272
+ return ctx.nullableNumberProps.has(`${expr.object.name}.${expr.property}`)
273
+ }
274
+ return false
275
+ }
276
+
227
277
  /**
228
278
  * Arithmetic operators JS applies `ToNumber` to, so a `null` operand reads
229
279
  * as `0` (`null * 2` is `0`). A template engine's native operator rejects a
@@ -232,3 +282,34 @@ function arithmeticOperandSeed(
232
282
  * concatenates when either side is a string.
233
283
  */
234
284
  export const NULL_COERCING_ARITHMETIC_OPS: ReadonlySet<string> = new Set(['-', '*', '/', '%', '**'])
285
+
286
+ /** How a DSL adapter coerces an arithmetic operand (#3474): `'null'` reads a
287
+ * nil as `0` (JS `ToNumber(null)`), `'undefined'` reads it as `NaN`
288
+ * (`ToNumber(undefined)`), `null` leaves the operand as is. */
289
+ export type ArithmeticCoercion = 'null' | 'undefined' | null
290
+
291
+ /**
292
+ * The one decision behind every DSL adapter's arithmetic over a nullish
293
+ * operand (#3474). An operand known to be `null` / `undefined`
294
+ * (`arithmeticOperandIsNull` / `arithmeticOperandIsUndefined`, or a nullable
295
+ * number prop) is coerced under the operators JS applies `ToNumber` to
296
+ * ({@link NULL_COERCING_ARITHMETIC_OPS}), and under `+` when the other
297
+ * operand is a number literal, where `+` cannot concatenate. Each adapter
298
+ * only spells the coercion; one whose engine already reads nil as `0`
299
+ * spells `'null'` as the operand itself.
300
+ */
301
+ export function arithmeticOperandCoercion(
302
+ op: string,
303
+ operand: ParsedExpr | undefined,
304
+ other: ParsedExpr | undefined,
305
+ ctx: NullishAttrContext,
306
+ isShadowed: (name: string) => boolean = () => false,
307
+ ): ArithmeticCoercion {
308
+ const numeric = NULL_COERCING_ARITHMETIC_OPS.has(op) || (op === '+' && isNumberLiteral(other))
309
+ return numeric ? arithmeticOperandSeed(operand, ctx, isShadowed) : null
310
+ }
311
+
312
+ function isNumberLiteral(expr: ParsedExpr | undefined): boolean {
313
+ if (expr?.kind === 'unary' && expr.op === '-') return isNumberLiteral(expr.argument)
314
+ return expr?.kind === 'literal' && expr.literalType === 'number'
315
+ }
@@ -1,5 +1,6 @@
1
1
  import ts from 'typescript'
2
- import type { ConstantInfo, ParamInfo, PropUsage } from './types.ts'
2
+ import type { ConstantInfo, ParamInfo, PropUsage, SignalPropSeed } from './types.ts'
3
+ import type { ParsedExpr } from './expression-parser.ts'
3
4
  import { propHasPropertyAccess } from './ir-to-client-js/compute-prop-usage.ts'
4
5
  import { parsePropReadInitializer } from './ir-to-client-js/prune-unused-prop-extractions.ts'
5
6
  import { PROPS_PARAM } from './ir-to-client-js/utils.ts'
@@ -52,6 +53,99 @@ export function propsDestructureBinding(p: ParamInfo): string {
52
53
  return p.defaultValue ? `${binding} = ${p.defaultValue}` : binding
53
54
  }
54
55
 
56
+ /**
57
+ * The prop a parsed initializer reads off the props object: a bare
58
+ * `props.<key>` member read, or one with a `?? <default>` fallback.
59
+ * `null` for any other shape.
60
+ */
61
+ function parsedPropRead(
62
+ read: ParsedExpr | undefined,
63
+ propsObjectName: string,
64
+ ): { key: string; hasDefault: boolean } | null {
65
+ const hasDefault = read?.kind === 'logical' && read.op === '??'
66
+ const member = hasDefault ? read.left : read
67
+ if (member?.kind !== 'member' || member.computed || member.optional) return null
68
+ if (member.object.kind !== 'identifier' || member.object.name !== propsObjectName) return null
69
+ return { key: member.property, hasDefault }
70
+ }
71
+
72
+ /**
73
+ * Whether a component-scope const is the props-object read of its own prop,
74
+ * so a template may read the prop in its place: a bare alias
75
+ * (`const label = props.label`, read from its parsed initializer), or an
76
+ * un-renamed binding of a body destructure (`const { label = 'Default' } =
77
+ * props`, whose default the analyzer also records on the prop's `ParamInfo`).
78
+ *
79
+ * A hand-written `const label = props.label ?? 'Default'` has the same
80
+ * `value` text as that destructure but is NOT the prop: its default lives
81
+ * only in the const, and reading the prop instead drops it (#3501). The
82
+ * analyzer marks the destructure on `ConstantInfo.propsDestructure`, so the
83
+ * two are told apart by that field, not by the text. A reassignment that
84
+ * directly follows the declaration folds into the initializer
85
+ * (`fold-render-reassignments.ts`), so the binding keeps one value; one that
86
+ * cannot fold (`mutatedAfterDeclaration`) leaves no single value a template
87
+ * could inline, so the binding reads the prop it holds until reassigned.
88
+ */
89
+ export function isPropsAliasConst(
90
+ c: Pick<ConstantInfo, 'name' | 'parsed' | 'propsDestructure' | 'mutatedAfterDeclaration'>,
91
+ propsObjectName: string | null,
92
+ ): boolean {
93
+ if (propsObjectName === null) return false
94
+ if (c.propsDestructure) return c.propsDestructure.sourceKey === c.name
95
+ const read = parsedPropRead(c.parsed, propsObjectName)
96
+ return read !== null && read.key === c.name && (!read.hasDefault || c.mutatedAfterDeclaration === true)
97
+ }
98
+
99
+ /**
100
+ * How a parsed signal initializer reads a prop: a member chain rooted at the
101
+ * props object (`props.<name>`, `props.row.count`, `props.items[0]`) or a
102
+ * destructured binding `<name>`, bare or under a `??` / `||` fallback chain
103
+ * of one operator (`props.a ?? props.b ?? 0`). `null` for any other shape
104
+ * (`props.a && x`, `props.a + 1`, a call). A binding counts only when it is
105
+ * one of `propsParams`.
106
+ */
107
+ export function signalPropSeed(
108
+ parsed: ParsedExpr | undefined,
109
+ propsParams: readonly ParamInfo[],
110
+ propsObjectName: string | null,
111
+ ): SignalPropSeed | null {
112
+ if (!parsed) return null
113
+ const { read, fallback } = splitFallbackChain(parsed)
114
+ const declared = (name: string) => propsParams.some(p => p.name === name)
115
+ if (read.kind === 'identifier') {
116
+ return declared(read.name) ? { propName: read.name, via: 'binding', nested: false, declared: true, fallback } : null
117
+ }
118
+ const hop = propsMemberRoot(read, propsObjectName)
119
+ if (!hop) return null
120
+ return { propName: hop.property, via: 'member', nested: hop !== read, declared: declared(hop.property), fallback }
121
+ }
122
+
123
+ /**
124
+ * Split a left-associated `??` / `||` chain of ONE operator into its leftmost
125
+ * operand and the fallback after it: `(a ?? b) ?? 0` reads `a` with fallback
126
+ * `b ?? 0`. Anything else is a read with no fallback.
127
+ */
128
+ function splitFallbackChain(parsed: ParsedExpr): { read: ParsedExpr; fallback: SignalPropSeed['fallback'] } {
129
+ if (parsed.kind !== 'logical' || (parsed.op !== '??' && parsed.op !== '||')) return { read: parsed, fallback: null }
130
+ const op = parsed.op
131
+ const rights: ParsedExpr[] = []
132
+ let node: ParsedExpr = parsed
133
+ while (node.kind === 'logical' && node.op === op) {
134
+ rights.unshift(node.right)
135
+ node = node.left
136
+ }
137
+ const expr = rights.slice(1).reduce<ParsedExpr>((left, right) => ({ ...parsed, left, right }), rights[0])
138
+ return { read: node, fallback: { op, expr } }
139
+ }
140
+
141
+ /** The `props.<name>` hop a member / index chain is rooted at, or `null`. */
142
+ function propsMemberRoot(read: ParsedExpr, propsObjectName: string | null): Extract<ParsedExpr, { kind: 'member' }> | null {
143
+ let node = read
144
+ while ((node.kind === 'member' || node.kind === 'index-access') && !(node.object.kind === 'identifier')) node = node.object
145
+ if (node.kind !== 'member' || node.computed || node.optional) return null
146
+ return node.object.kind === 'identifier' && node.object.name === propsObjectName ? node : null
147
+ }
148
+
55
149
  /**
56
150
  * Local-name → caller-facing-key map for prop-reference rewrites —
57
151
  * entries only for `ParamInfo`s that actually rename (`sourceName` set,
@@ -120,10 +214,11 @@ export function resolveBodyDestructuredPropAliases(
120
214
  * Foo(props: Props)`) whose value is a PURE alias of a single caller-facing
121
215
  * prop — either a body destructure (`const { value } = props`, `const {
122
216
  * label = 'none' } = props`) or an equivalent bare member read (`const label
123
- * = props.label ?? 'fallback'`); the analyzer's IR can't distinguish the two
124
- * shapes (both parse to the same `ConstantInfo`), and neither can this
125
- * resolver — see `resolveBodyAliasReads`'s (`rewrite-destructured-props.ts`)
126
- * docstring for why that's the right call.
217
+ * = props.label ?? 'fallback'`). Both shapes share the same `value` text;
218
+ * the IR tells them apart via `ConstantInfo.propsDestructure` (#3501), but
219
+ * this resolver deliberately treats them alike — see
220
+ * `rewriteBodyAliasReads`'s (`rewrite-destructured-props.ts`) docstring for
221
+ * why that's the right call.
127
222
  */
128
223
  export interface BodyPropAlias {
129
224
  /** Caller-facing prop key (`props.<key>`). */
package/src/relocate.ts CHANGED
@@ -15,7 +15,7 @@ import type { Scope, BindingKind, IRMetadata } from './types.ts'
15
15
  import { isVisibleIn } from './types.ts'
16
16
  import type { AnalyzerContext } from './analyzer-context.ts'
17
17
  import { PROPS_PARAM } from './ir-to-client-js/utils.ts'
18
- import { buildPropAliasMap } from './props-binding.ts'
18
+ import { buildPropAliasMap, isPropsAliasConst } from './props-binding.ts'
19
19
  import type {
20
20
  TemplatePrimitiveRegistry,
21
21
  TemplateCallAcceptor,
@@ -78,6 +78,15 @@ export interface RelocateEnv {
78
78
  * in `templatePrimitives`. Returning true marks the call as accepted.
79
79
  */
80
80
  acceptsTemplateCall?: TemplateCallAcceptor
81
+ /**
82
+ * Component-scope locals already classified as inlinable into template
83
+ * scope (#3513), filled in declaration order by `computeInlinability`. A
84
+ * method call on one (`text.toUpperCase()` with `const text = props.label
85
+ * ?? 'd'`) runs on the local's inlined value, so a broad
86
+ * `acceptsTemplateCall` can vouch for it even though the receiver is an
87
+ * init-local.
88
+ */
89
+ inlinableLocals?: ReadonlySet<string>
81
90
  /**
82
91
  * Lowering-registry matchers (#2057) bound to this component's metadata
83
92
  * (`prepareLoweringMatchers`), consulted by `isCallAcceptedByAdapter` as a
@@ -476,6 +485,24 @@ const REGISTRY_SAFE_BINDING_KINDS: ReadonlySet<BindingKind> = new Set([
476
485
  'module-local',
477
486
  ])
478
487
 
488
+ /**
489
+ * A method call on an inlinable local (`text.toUpperCase()`) runs on that
490
+ * local's value in template scope (#3513). Only the broad
491
+ * `acceptsTemplateCall` can vouch for it: the path-keyed registries name
492
+ * globals, which this receiver isn't. A bare call of the local itself is
493
+ * never accepted.
494
+ */
495
+ function isAcceptedMethodCallOnInlinableLocal(
496
+ originalPath: string,
497
+ leftmost: string | null,
498
+ resolvedLeftmost: string,
499
+ resolvedPath: string,
500
+ env: RelocateEnv,
501
+ ): boolean {
502
+ if (originalPath === leftmost || env.inlinableLocals?.has(resolvedLeftmost) !== true) return false
503
+ return env.acceptsTemplateCall?.(resolvedPath) === true
504
+ }
505
+
479
506
  /**
480
507
  * Whether `env`'s adapter promises it can render this call in template
481
508
  * scope. Three independent acceptance paths, any of which is sufficient:
@@ -540,7 +567,7 @@ function isCallAcceptedByAdapter(
540
567
  if (resolvedLeftmost !== null) {
541
568
  const kind = env.bindings.get(resolvedLeftmost)
542
569
  if (kind !== undefined && !REGISTRY_SAFE_BINDING_KINDS.has(kind)) {
543
- return false
570
+ return isAcceptedMethodCallOnInlinableLocal(originalPath, leftmost, resolvedLeftmost, resolvedPath, env)
544
571
  }
545
572
  }
546
573
 
@@ -791,6 +818,16 @@ export function buildRelocateEnvFromIR(
791
818
  return env
792
819
  }
793
820
 
821
+ /**
822
+ * Every name the component or its module binds, by kind: imports, module
823
+ * consts and functions, props, init locals, signals and memos (`classify`'s
824
+ * table). A free name absent from it, and bound by no enclosing loop or
825
+ * callback, is a JavaScript global.
826
+ */
827
+ export function componentBindings(metadata: IRMetadata): ReadonlyMap<string, BindingKind> {
828
+ return buildRelocateEnvFromFields(metadata).bindings
829
+ }
830
+
794
831
  interface EnvFields {
795
832
  imports: AnalyzerContext['imports']
796
833
  localFunctions: AnalyzerContext['localFunctions']
@@ -841,12 +878,7 @@ function buildRelocateEnvFromFields(src: EnvFields): RelocateEnv {
841
878
  const propsObjectName = src.propsObjectName
842
879
  for (const c of src.localConstants) {
843
880
  if (c.isModule) continue
844
- const isPureAlias =
845
- typeof c.value === 'string' &&
846
- propsObjectName !== null &&
847
- (c.value === `${propsObjectName}.${c.name}` ||
848
- c.value.startsWith(`${propsObjectName}.${c.name} ??`))
849
- bindings.set(c.name, isPureAlias ? 'prop' : 'init-local')
881
+ bindings.set(c.name, isPropsAliasConst(c, propsObjectName) ? 'prop' : 'init-local')
850
882
  }
851
883
 
852
884
  // Init-body local functions.
@@ -35,11 +35,11 @@ import { collectModuleStringConsts } from './augment-inherited-props.ts'
35
35
  import { collectAliasableGetterNames, resolveGetterAliases } from './ir-to-client-js/csr-substitute.ts'
36
36
  import { envSignalReaderFor, type EnvSignalReader } from './adapters/env-signal.ts'
37
37
  import {
38
- extractArrowBodyExpression,
39
38
  freeIdentifiers,
40
39
  inlineBinding,
41
40
  isSupportedValue,
42
41
  parseExpression,
42
+ stringifyParsedExpr,
43
43
  type ParsedExpr,
44
44
  } from './expression-parser.ts'
45
45
  import type { IRMetadata } from './types.ts'
@@ -174,10 +174,23 @@ function resolveThroughLocalConsts(
174
174
 
175
175
  /**
176
176
  * Compute the component's SSR seed plan from its metadata. See the module
177
- * doc for the contract. Memo steps are gated to EXPRESSION-BODIED memos
178
- * (`extractArrowBodyExpression` returns the body): a block-bodied memo is
179
- * `opaque` even when the analyzer folded it to a `parsed` expression.
177
+ * doc for the contract. A memo step reads the expression its callback
178
+ * returns (`MemoInfo.bodyExpr`): an arrow expression body, or a block or
179
+ * `function` body that is a single `return`. A multi-statement body seeds
180
+ * from the analyzer's fold of its statements (`MemoInfo.parsed`, #2040,
181
+ * #3523), and is `opaque` when the fold failed.
180
182
  */
183
+ /**
184
+ * The expression a memo seeds from: the returned expression, as the analyzer
185
+ * read it from the callback node (#3510). A body that must be called as a
186
+ * whole seeds from the analyzer's fold of its statements into one expression
187
+ * when there is one (#3523); `''` (opaque) otherwise.
188
+ */
189
+ function memoSeedExpr(memo: IRMetadata['memos'][number]): string {
190
+ if (!memo.bodyExprIsCall) return memo.bodyExpr?.trim() ?? ''
191
+ return memo.parsed ? stringifyParsedExpr(memo.parsed) : ''
192
+ }
193
+
181
194
  export function computeSsrSeedPlan(metadata: IRMetadata): SsrSeedPlan {
182
195
  const baseScope: string[] = metadata.propsParams.map(p => p.name)
183
196
  if (metadata.propsObjectName) baseScope.push(metadata.propsObjectName)
@@ -236,8 +249,7 @@ export function computeSsrSeedPlan(metadata: IRMetadata): SsrSeedPlan {
236
249
  }
237
250
 
238
251
  for (const memo of metadata.memos) {
239
- const body = extractArrowBodyExpression(memo.computation)
240
- const expr = body?.trim() ?? ''
252
+ const expr = memoSeedExpr(memo)
241
253
  steps.push(
242
254
  expr === ''
243
255
  ? { kind: 'opaque', name: memo.name, origin: 'memo' }
package/src/types.ts CHANGED
@@ -1578,6 +1578,18 @@ export interface IRProp extends AttrMeta {
1578
1578
  // Metadata
1579
1579
  // =============================================================================
1580
1580
 
1581
+ /** How a signal's initial value reads a prop (`SignalInfo.propSeed`). */
1582
+ export interface SignalPropSeed {
1583
+ /** The prop read: the first property after the props object, or the destructured binding. */
1584
+ propName: string
1585
+ via: 'member' | 'binding'
1586
+ /** True when the read goes past the prop (`props.row.count`, `props.items[0]`). */
1587
+ nested: boolean
1588
+ /** Whether `propName` is one of the component's declared props (`propsParams`). */
1589
+ declared: boolean
1590
+ fallback: { op: '??' | '||'; expr: ParsedExpr } | null
1591
+ }
1592
+
1581
1593
  export interface SignalInfo {
1582
1594
  getter: string
1583
1595
  setter: string | null
@@ -1590,6 +1602,16 @@ export interface SignalInfo {
1590
1602
  * consumers fall back to parsing `initialValue`.
1591
1603
  */
1592
1604
  parsed?: ParsedExpr
1605
+ /**
1606
+ * Set when `initialValue` is a read of one prop, bare or with a single
1607
+ * `??` / `||` fallback (`props.checked`, `checked ?? false`,
1608
+ * `props.value || ''`, `props.row.count`), recorded by the analyzer from
1609
+ * `parsed`. `via` is
1610
+ * how the prop is read: a member of the props object, or a binding a props
1611
+ * destructure introduced. The CSR init body asks this field, not the text,
1612
+ * whether the signal seeds from a prop and what default it keeps (#3508).
1613
+ */
1614
+ propSeed?: SignalPropSeed
1593
1615
  /** Initial value with TypeScript type annotations preserved, for .tsx output */
1594
1616
  typedInitialValue?: string
1595
1617
  /**
@@ -1738,14 +1760,30 @@ export interface MemoInfo {
1738
1760
  name: string
1739
1761
  computation: string
1740
1762
  /**
1741
- * `computation` with bare destructured-prop refs rewritten to `_p.X` —
1763
+ * The expression the memo evaluates to, read from the callback node at
1764
+ * analysis time (#3510): the body of a zero-parameter arrow or `function`
1765
+ * expression (`() => e`, `() => { return e }`, `function () { return e }`),
1766
+ * or a call of the whole callback (`(() => { … })()`) for any other shape.
1767
+ * Type-stripped. Consumers substitute it for `memo()` instead of matching
1768
+ * `computation` against `() =>`. Unset only when `createMemo` has no
1769
+ * argument.
1770
+ */
1771
+ bodyExpr?: string
1772
+ /**
1773
+ * Set when `bodyExpr` is a call of the whole callback rather than the
1774
+ * expression the callback returns — a multi-statement body, or a callback
1775
+ * that is not a plain zero-parameter function.
1776
+ */
1777
+ bodyExprIsCall?: boolean
1778
+ /**
1779
+ * `bodyExpr` with bare destructured-prop refs rewritten to `_p.X` —
1742
1780
  * the memo twin of `SignalInfo.templateInitialValue` (#2265). Consumed by
1743
1781
  * `buildSignalMemoEnv` when the memo body is inlined into the module-scope
1744
1782
  * CSR `template:` arrow, which is not a closure over init's destructured
1745
1783
  * extractions (#2468). Only set for destructured-arg components that
1746
1784
  * actually reference a prop.
1747
1785
  */
1748
- templateComputation?: string
1786
+ templateBodyExpr?: string
1749
1787
  /** Computation with TypeScript type annotations preserved, for .tsx output */
1750
1788
  typedComputation?: string
1751
1789
  /**
@@ -2119,6 +2157,16 @@ export interface ConstantInfo {
2119
2157
  isJsxFunction?: boolean
2120
2158
  /** When true, the initializer contains an arrow function or function expression (computed from AST). */
2121
2159
  containsArrow?: boolean
2160
+ /**
2161
+ * Set when this const is a binding of a body destructure of the props
2162
+ * object (`const { label = 'Default' } = props`), recorded by the analyzer
2163
+ * from the binding pattern itself. `sourceKey` is the prop it reads (the
2164
+ * property key, which differs from `name` for a renamed binding). Its
2165
+ * `value` text (`props.label ?? 'Default'`) is the same as a hand-written
2166
+ * `const label = props.label ?? 'Default'`, so consumers ask this field,
2167
+ * not the text, whether the const IS the prop (#3501).
2168
+ */
2169
+ propsDestructure?: { sourceKey: string }
2122
2170
  /**
2123
2171
  * When true, the initializer IS an arrow function or function expression
2124
2172
  * (`const status = () => …`), so reading the constant yields a function and