@barefootjs/jsx 0.40.0 → 0.40.1

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 (140) hide show
  1. package/dist/adapters/ambiguous-nil-arithmetic.d.ts +41 -0
  2. package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -0
  3. package/dist/adapters/attr-plan.d.ts.map +1 -1
  4. package/dist/adapters/boolean-carrier.d.ts +31 -0
  5. package/dist/adapters/boolean-carrier.d.ts.map +1 -0
  6. package/dist/adapters/component-props.d.ts +22 -7
  7. package/dist/adapters/component-props.d.ts.map +1 -1
  8. package/dist/adapters/computed-const.d.ts +17 -0
  9. package/dist/adapters/computed-const.d.ts.map +1 -1
  10. package/dist/adapters/dsl-template-adapter.d.ts +18 -1
  11. package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
  12. package/dist/adapters/number-value.d.ts +43 -0
  13. package/dist/adapters/number-value.d.ts.map +1 -0
  14. package/dist/analyzer-context.d.ts +7 -0
  15. package/dist/analyzer-context.d.ts.map +1 -1
  16. package/dist/analyzer.d.ts +34 -0
  17. package/dist/analyzer.d.ts.map +1 -1
  18. package/dist/expression-parser.d.ts.map +1 -1
  19. package/dist/index.d.ts +6 -3
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +1006 -351
  22. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  23. package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
  24. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts +29 -0
  25. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  26. package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts +2 -0
  27. package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts.map +1 -1
  28. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  29. package/dist/ir-to-client-js/control-flow/plan/build-reactive-effects.d.ts.map +1 -1
  30. package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts +5 -4
  31. package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts.map +1 -1
  32. package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts +2 -0
  33. package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts.map +1 -1
  34. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +15 -0
  35. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
  36. package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts +2 -0
  37. package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts.map +1 -1
  38. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  39. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +20 -0
  40. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
  41. package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
  42. package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +1 -1
  43. package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
  44. package/dist/ir-to-client-js/control-flow/stringify/lazy-row.d.ts.map +1 -1
  45. package/dist/ir-to-client-js/html-template.d.ts +3 -2
  46. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  47. package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
  48. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +7 -1
  49. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
  50. package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
  51. package/dist/ir-to-client-js/safe-html.d.ts +9 -3
  52. package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
  53. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
  54. package/dist/ir-to-client-js/types.d.ts +30 -0
  55. package/dist/ir-to-client-js/types.d.ts.map +1 -1
  56. package/dist/ir-to-client-js/utils.d.ts +8 -0
  57. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  58. package/dist/jsx-to-ir.d.ts.map +1 -1
  59. package/dist/nullable-signal-attr.d.ts +29 -3
  60. package/dist/nullable-signal-attr.d.ts.map +1 -1
  61. package/dist/props-binding.d.ts +27 -3
  62. package/dist/props-binding.d.ts.map +1 -1
  63. package/dist/scope/binding-scope.d.ts +22 -1
  64. package/dist/scope/binding-scope.d.ts.map +1 -1
  65. package/dist/scope/root-prop-alias.d.ts +14 -1
  66. package/dist/scope/root-prop-alias.d.ts.map +1 -1
  67. package/dist/signal-prop-name-collision.d.ts.map +1 -1
  68. package/dist/ssr-defaults.d.ts +4 -2
  69. package/dist/ssr-defaults.d.ts.map +1 -1
  70. package/dist/ssr-seed-plan.d.ts +14 -0
  71. package/dist/ssr-seed-plan.d.ts.map +1 -1
  72. package/dist/template-parts.d.ts.map +1 -1
  73. package/dist/types.d.ts +30 -0
  74. package/dist/types.d.ts.map +1 -1
  75. package/package.json +2 -2
  76. package/src/__tests__/binding-scope-ratchet.test.ts +12 -2
  77. package/src/__tests__/boolean-carrier.test.ts +74 -0
  78. package/src/__tests__/component-props.test.ts +14 -0
  79. package/src/__tests__/computed-const.test.ts +85 -0
  80. package/src/__tests__/expression-parser.test.ts +24 -0
  81. package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +142 -0
  82. package/src/__tests__/loop-row-prop-read.test.ts +111 -0
  83. package/src/__tests__/memo-body-expr.test.ts +137 -0
  84. package/src/__tests__/nullable-signal-attr.test.ts +97 -2
  85. package/src/__tests__/profile-binding-ids.test.ts +14 -0
  86. package/src/__tests__/root-prop-alias.test.ts +34 -1
  87. package/src/__tests__/signal-prop-name-collision.test.ts +102 -2
  88. package/src/__tests__/source-file-cache.test.ts +105 -0
  89. package/src/__tests__/ssr-seed-plan.test.ts +43 -0
  90. package/src/__tests__/template-parts-literal-boundary.test.ts +19 -0
  91. package/src/adapters/ambiguous-nil-arithmetic.ts +94 -0
  92. package/src/adapters/attr-plan.ts +6 -2
  93. package/src/adapters/boolean-carrier.ts +76 -0
  94. package/src/adapters/component-props.ts +28 -8
  95. package/src/adapters/computed-const.ts +206 -7
  96. package/src/adapters/dsl-template-adapter.ts +39 -0
  97. package/src/adapters/number-value.ts +95 -0
  98. package/src/analyzer-context.ts +8 -0
  99. package/src/analyzer.ts +187 -66
  100. package/src/expression-parser.ts +4 -1
  101. package/src/index.ts +12 -2
  102. package/src/ir-to-client-js/collect-elements.ts +104 -46
  103. package/src/ir-to-client-js/compute-inlinability.ts +1 -1
  104. package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +15 -1
  105. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +79 -4
  106. package/src/ir-to-client-js/control-flow/plan/build-lazy-row.ts +3 -0
  107. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +1 -0
  108. package/src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts +1 -0
  109. package/src/ir-to-client-js/control-flow/plan/inner-loop.ts +5 -4
  110. package/src/ir-to-client-js/control-flow/plan/loop-child-arm.ts +2 -0
  111. package/src/ir-to-client-js/control-flow/plan/loop.ts +12 -0
  112. package/src/ir-to-client-js/control-flow/plan/reactive-effects.ts +2 -0
  113. package/src/ir-to-client-js/control-flow/shared.ts +3 -2
  114. package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +27 -0
  115. package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +8 -2
  116. package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +8 -1
  117. package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +8 -7
  118. package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +8 -7
  119. package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +4 -4
  120. package/src/ir-to-client-js/control-flow/stringify/loop.ts +3 -3
  121. package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +6 -6
  122. package/src/ir-to-client-js/emit-reactive.ts +1 -1
  123. package/src/ir-to-client-js/html-template.ts +70 -30
  124. package/src/ir-to-client-js/plan/build-static-array-child-init.ts +11 -1
  125. package/src/ir-to-client-js/plan/static-array-child-init.ts +7 -1
  126. package/src/ir-to-client-js/reactivity.ts +14 -0
  127. package/src/ir-to-client-js/safe-html.ts +11 -4
  128. package/src/ir-to-client-js/stringify/static-array-child-init.ts +6 -4
  129. package/src/ir-to-client-js/types.ts +23 -0
  130. package/src/ir-to-client-js/utils.ts +15 -1
  131. package/src/jsx-to-ir.ts +78 -35
  132. package/src/nullable-signal-attr.ts +134 -13
  133. package/src/props-binding.ts +54 -3
  134. package/src/scope/binding-scope.ts +30 -2
  135. package/src/scope/root-prop-alias.ts +17 -1
  136. package/src/signal-prop-name-collision.ts +113 -22
  137. package/src/ssr-defaults.ts +10 -4
  138. package/src/ssr-seed-plan.ts +82 -17
  139. package/src/template-parts.ts +21 -1
  140. package/src/types.ts +30 -0
@@ -71,36 +71,113 @@ interface SeparatedBinding {
71
71
  name: string
72
72
  loc: SourceLocation
73
73
  prefix: 'bfSignal' | 'bfMemo'
74
+ /** Its own initializer derives from the same-named prop. */
75
+ selfDerived: boolean
74
76
  }
75
77
 
76
78
  /**
77
79
  * Signal getters and memos that share a name with a prop of a bare-props
78
80
  * component (`props.label` beside `const [label] = createSignal(...)` or
79
- * `const label = createMemo(...)`). The comparison ignores case because a
80
- * backend may fold it (Go exports both `label` and `Label` as the field
81
- * `Label`).
81
+ * `const label = createMemo(...)`). `propNames` holds lower-cased names:
82
+ * the comparison ignores case because a backend may fold it (Go exports
83
+ * both `label` and `Label` as the field `Label`).
82
84
  *
83
85
  * A binding whose own initializer derives from that same prop
84
- * (`createSignal(props.label ?? 'x')`) is left alone: the template-stash
85
- * backends keep it on the prop's variable, seeded from the raw prop, and
86
- * `extractSsrDefaults` decides that case with the same `referencesOwnProp`
87
- * test. A raw `props.label` read beside such a binding then sees the
88
- * derived value (limitation `signal-from-same-named-prop-raw-read`).
86
+ * (`createSignal(props.label ?? 'x')`) is marked `selfDerived`: the
87
+ * template-stash backends keep it on the prop's variable, seeded from the
88
+ * raw prop (`extractSsrDefaults` decides that case with the same
89
+ * `referencesOwnProp` test), unless the component also reads the raw prop
90
+ * (`separateSignalPropBindings`).
89
91
  */
90
- function collidingBindings(ctx: AnalyzerContext, propsObjectName: string): SeparatedBinding[] {
91
- const propNames = new Set(ctx.propsParams.map(p => p.name.toLowerCase()))
92
+ function collidingBindings(ctx: AnalyzerContext, propsObjectName: string, propNames: ReadonlySet<string>): SeparatedBinding[] {
92
93
  const localConsts = localConstValuesByName(ctx)
93
- const collides = (name: string, derivation: string): boolean =>
94
- propNames.has(name.toLowerCase()) && !referencesOwnProp(derivation, propsObjectName, name, localConsts)
94
+ const collides = (name: string): boolean => propNames.has(name.toLowerCase())
95
+ const selfDerived = (name: string, derivation: string): boolean =>
96
+ referencesOwnProp(derivation, propsObjectName, name, localConsts)
95
97
  const signals: SeparatedBinding[] = ctx.signals
96
- .filter(s => !s.isModule && !s.getterElided && !s.envReader && collides(s.getter, s.initialValue))
97
- .map(s => ({ name: s.getter, loc: s.loc, prefix: 'bfSignal' }))
98
+ .filter(s => !s.isModule && !s.getterElided && !s.envReader && collides(s.getter))
99
+ .map(s => ({ name: s.getter, loc: s.loc, prefix: 'bfSignal', selfDerived: selfDerived(s.getter, s.initialValue) }))
98
100
  const memos: SeparatedBinding[] = ctx.memos
99
- .filter(m => !m.isModule && collides(m.name, m.computation))
100
- .map(m => ({ name: m.name, loc: m.loc, prefix: 'bfMemo' }))
101
+ .filter(m => !m.isModule && collides(m.name))
102
+ .map(m => ({ name: m.name, loc: m.loc, prefix: 'bfMemo', selfDerived: selfDerived(m.name, m.computation) }))
101
103
  return [...signals, ...memos]
102
104
  }
103
105
 
106
+ /**
107
+ * A property read off the props object (`props.label`): its lower-cased
108
+ * name and position. `name` is null for a computed key (`props[k]`), which
109
+ * may read any prop.
110
+ */
111
+ interface PropsRead {
112
+ name: string | null
113
+ pos: number
114
+ }
115
+
116
+ /**
117
+ * The key a props read names: an identifier member (`props.label`), or an
118
+ * element access with a literal key (`props['label']`, `` props[`label`] ``,
119
+ * `props[0]`), each also optional (`props?.['label']`). The key is the
120
+ * literal's own text, so a numeric key names its number and never matches
121
+ * an identifier. Null for a computed key. Undefined when `node` is no
122
+ * member access off `propsObjectName`.
123
+ */
124
+ function propsReadKey(node: ts.Node, propsObjectName: string): { receiver: ts.Identifier; key: string | null } | undefined {
125
+ if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.name)) {
126
+ return isNamedReceiver(node.expression, propsObjectName) ? { receiver: node.expression, key: node.name.text } : undefined
127
+ }
128
+ if (!ts.isElementAccessExpression(node) || !isNamedReceiver(node.expression, propsObjectName)) return undefined
129
+ const arg = node.argumentExpression
130
+ const literal = ts.isStringLiteral(arg) || ts.isNoSubstitutionTemplateLiteral(arg) || ts.isNumericLiteral(arg)
131
+ return { receiver: node.expression, key: literal ? arg.text : null }
132
+ }
133
+
134
+ function isNamedReceiver(expr: ts.Expression, propsObjectName: string): expr is ts.Identifier {
135
+ return ts.isIdentifier(expr) && expr.text === propsObjectName
136
+ }
137
+
138
+ /**
139
+ * Every property read off the props object (`props.label`,
140
+ * `props['label']`) inside the component. An untyped `props` parameter
141
+ * declares no `propsParams`, so these reads are its only prop names.
142
+ * `isPropsObject` decides whether an access's receiver is the props
143
+ * binding itself.
144
+ */
145
+ function propsReads(
146
+ component: ts.Node,
147
+ propsObjectName: string,
148
+ isPropsObject: (receiver: ts.Identifier) => boolean,
149
+ ): PropsRead[] {
150
+ const reads: PropsRead[] = []
151
+ function visit(node: ts.Node): void {
152
+ const read = propsReadKey(node, propsObjectName)
153
+ if (read && isPropsObject(read.receiver)) {
154
+ reads.push({ name: read.key === null ? null : read.key.toLowerCase(), pos: node.getStart() })
155
+ }
156
+ ts.forEachChild(node, visit)
157
+ }
158
+ visit(component)
159
+ return reads
160
+ }
161
+
162
+ /** The prop names the reads spell; a computed-key read names none. */
163
+ function readNames(reads: readonly PropsRead[]): string[] {
164
+ return reads.flatMap(r => (r.name === null ? [] : [r.name]))
165
+ }
166
+
167
+ /**
168
+ * Whether `binding`, declared by `declaration`, must leave the prop's
169
+ * name. A self-derived binding shares the prop's template variable only
170
+ * while nothing else reads the raw prop: a `props.<name>` read outside its
171
+ * own initializer would otherwise see the derived value. A computed-key
172
+ * read (`props[k]`) may read that prop, so it counts too.
173
+ */
174
+ function needsSeparation(binding: SeparatedBinding, declaration: ts.VariableDeclaration, reads: readonly PropsRead[]): boolean {
175
+ if (!binding.selfDerived) return true
176
+ const init = declaration.initializer
177
+ const name = binding.name.toLowerCase()
178
+ return reads.some(r => (r.name === null || r.name === name) && (!init || r.pos < init.getStart() || r.pos >= init.getEnd()))
179
+ }
180
+
104
181
  /**
105
182
  * A prop and a signal getter or memo of the same name are different JS
106
183
  * bindings (`props.label` and `label()`), but scalar template backends
@@ -119,15 +196,29 @@ export function separateSignalPropBindings(
119
196
  ctx: AnalyzerContext,
120
197
  isNonReference: (id: ts.Identifier) => boolean,
121
198
  ): string | null {
122
- if (!ctx.propsObjectName || !ctx.componentNode) return null
123
- const candidates = collidingBindings(ctx, ctx.propsObjectName)
124
- if (candidates.length === 0) return null
199
+ const propsObjectName = ctx.propsObjectName
200
+ const component = ctx.componentNode
201
+ if (!propsObjectName || !component) return null
202
+ const declared = ctx.propsParams.map(p => p.name.toLowerCase())
203
+ // Every same-spelled read is a candidate before binding resolution, so
204
+ // the checker is built only when some binding can collide.
205
+ const spelled = readNames(propsReads(component, propsObjectName, () => true))
206
+ if (collidingBindings(ctx, propsObjectName, new Set([...declared, ...spelled])).length === 0) return null
125
207
 
126
208
  const sf = ctx.sourceFile
127
209
  const checker = ctx.checker ?? ts.createProgram([sf.fileName], { noLib: true, noResolve: true }, {
128
210
  ...ts.createCompilerHost({ noLib: true, noResolve: true }),
129
211
  getSourceFile: () => sf,
130
212
  }).getTypeChecker()
213
+ // A read counts only when its receiver is the props parameter, not a
214
+ // callback parameter or local that shadows the name.
215
+ const param = component.parameters.find(p => ts.isIdentifier(p.name) && p.name.text === propsObjectName)
216
+ const paramSymbol = param && checker.getSymbolAtLocation(param.name)
217
+ const reads = paramSymbol
218
+ ? propsReads(component, propsObjectName, id => checker.getSymbolAtLocation(id) === paramSymbol)
219
+ : []
220
+ const candidates = collidingBindings(ctx, propsObjectName, new Set([...declared, ...readNames(reads)]))
221
+ if (candidates.length === 0) return null
131
222
  const occupied = new Set<string>()
132
223
  function collectNames(node: ts.Node): void {
133
224
  // Also avoid case-fold collisions introduced by backend field naming.
@@ -145,7 +236,7 @@ export function separateSignalPropBindings(
145
236
  function collectBindings(node: ts.Node): void {
146
237
  if (ts.isVariableDeclaration(node)) {
147
238
  const binding = declarations.get(node.getStart(sf))
148
- if (binding) {
239
+ if (binding && needsSeparation(binding, node, reads)) {
149
240
  const names = ts.isIdentifier(node.name) ? [node.name]
150
241
  : ts.isArrayBindingPattern(node.name)
151
242
  ? node.name.elements.flatMap(e => ts.isBindingElement(e) && ts.isIdentifier(e.name) ? [e.name] : [])
@@ -164,7 +255,7 @@ export function separateSignalPropBindings(
164
255
  }
165
256
  ts.forEachChild(node, collectBindings)
166
257
  }
167
- collectBindings(ctx.componentNode)
258
+ collectBindings(component)
168
259
 
169
260
  const edits: { start: number; end: number; text: string }[] = []
170
261
  function collectEdits(node: ts.Node): void {
@@ -180,7 +271,7 @@ export function separateSignalPropBindings(
180
271
  }
181
272
  ts.forEachChild(node, collectEdits)
182
273
  }
183
- collectEdits(ctx.componentNode)
274
+ collectEdits(component)
184
275
  if (edits.length === 0) return null
185
276
  let source = sf.text
186
277
  for (const edit of edits.sort((a, b) => b.start - a.start)) {
@@ -176,10 +176,14 @@ function addPropSsrDefaults(
176
176
  // A prop whose default reads an earlier prop (`{ off = false, on = !off }`,
177
177
  // #3531) and that the seed plan re-binds in-template: its value depends on
178
178
  // what the caller passed, so no static value is right, and a non-null seed
179
- // would read as caller-supplied to the template's own `on ?? !off`.
179
+ // would read as caller-supplied to the template's own `on ?? !off`. A
180
+ // computed default that reads no earlier prop (`{ b = 'abc'.length }`)
181
+ // keeps its static value when it has one: that value is the default
182
+ // itself, so the template's `b ?? <default>` yields the same.
180
183
  const plan = metadata.ssrSeedPlan ?? computeSsrSeedPlan(metadata)
184
+ const readsEarlier = new Set(metadata.propsParams.filter(p => p.defaultParts).map(p => p.name))
181
185
  const templateDefaulted = new Set(
182
- plan.steps.filter(s => s.kind === 'derived' && s.origin === 'prop').map(s => s.name),
186
+ plan.steps.filter(s => s.kind === 'derived' && s.origin === 'prop' && readsEarlier.has(s.name)).map(s => s.name),
183
187
  )
184
188
  for (const p of metadata.propsParams) {
185
189
  if (p.isRest) continue
@@ -487,8 +491,10 @@ export function localConstValuesByName(metadata: Pick<IRMetadata, 'localConstant
487
491
  *
488
492
  * Also the analyzer's test for which same-named signal/memo keeps sharing
489
493
  * the prop's template variable (`separateSignalPropBindings`): a binding
490
- * this answers `false` for is renamed before IR is built, so the two
491
- * callers cannot disagree about which collisions stay collapsed.
494
+ * this answers `false` for is renamed before IR is built, and so is one it
495
+ * answers `true` for when the component also reads the raw prop elsewhere.
496
+ * A renamed binding no longer matches here, so the two callers cannot
497
+ * disagree about which collisions stay collapsed.
492
498
  */
493
499
  export function referencesOwnProp(
494
500
  expr: string | undefined,
@@ -44,6 +44,7 @@ import {
44
44
  stringifyParsedExpr,
45
45
  type ParsedExpr,
46
46
  } from './expression-parser.ts'
47
+ import { templatePropReadParams } from './props-binding.ts'
47
48
  import type { IRMetadata, ParamInfo } from './types.ts'
48
49
  import { isElidedFactoryGetter } from './types.ts'
49
50
 
@@ -216,31 +217,95 @@ function memoSeedExpr(memo: IRMetadata['memos'][number]): string {
216
217
  }
217
218
 
218
219
  /**
219
- * A `derived` step per prop whose destructure default reads an earlier prop of
220
- * the same pattern (`ParamInfo.defaultParts`, #3531): `on ?? <default>`, where
221
- * the default's identifiers are the earlier props' template variables. Every
222
- * prop is already in `available` (baseScope), and the steps run in dependency
223
- * order ({@link inDefaultReadOrder}), so a chain (`{ a = 1, b = a, c = b }`)
224
- * reads `b` after its own step re-bound it. A default the plan cannot lower
225
- * (or a function default) adds no step and keeps the static ssr-defaults
226
- * seed. A callback inside the default (`a.filter((x) => …)`) does not make
227
- * it a function default.
220
+ * Whether `p`'s destructure default is computed when the prop is absent: a
221
+ * default that is neither a function nor literal data
222
+ * ({@link isLiteralData}). A default reading an earlier prop
223
+ * (`ParamInfo.defaultParts`, `{ a, b = a }`) is one, and so is one that
224
+ * calls or reads anything else (`{ b = ['y', ''].filter((x) => x !== '') }`,
225
+ * `{ n = 'abc'.length }`, `{ m = DEFAULT }`). A callback inside the default
226
+ * does not make it a function default.
227
+ *
228
+ * Every backend applies such a default when it renders (the SSR seed plan's
229
+ * prop steps, Go's props constructor) instead of a value fixed at compile
230
+ * time, which only literal data has.
228
231
  */
229
- function dependentPropDefaultSteps(
230
- metadata: IRMetadata,
231
- available: ReadonlySet<string>,
232
- inlined: ReadonlySet<string>,
233
- ): SsrSeedStep[] {
232
+ export function hasComputedDefault(p: ParamInfo): boolean {
233
+ if (p.isRest || p.defaultValue === undefined || !p.parsed || p.parsed.kind === 'arrow') return false
234
+ return p.defaultParts !== undefined || !isLiteralData(p.parsed)
235
+ }
236
+
237
+ /**
238
+ * A literal (`'x'`, `-1`, `null`, `undefined`), a template literal with no
239
+ * substitution, or an array / object literal of literal data: a value that
240
+ * reads and calls nothing.
241
+ */
242
+ function isLiteralData(e: ParsedExpr): boolean {
243
+ switch (e.kind) {
244
+ case 'literal':
245
+ return true
246
+ case 'identifier':
247
+ return e.name === 'undefined'
248
+ case 'unary':
249
+ return (e.op === '-' || e.op === '+') && e.argument.kind === 'literal' && e.argument.literalType === 'number'
250
+ case 'template-literal':
251
+ return e.parts.every(part => part.type === 'string')
252
+ case 'array-literal':
253
+ return e.elements.every(isLiteralData)
254
+ case 'object-literal':
255
+ return e.properties.every(prop => prop.kind !== 'spread' && isLiteralData(prop.value))
256
+ default:
257
+ return false
258
+ }
259
+ }
260
+
261
+ /**
262
+ * A `derived` step per prop with a computed destructure default
263
+ * ({@link hasComputedDefault}): `on ?? <default>`. A default reading an
264
+ * earlier prop of the same pattern (`ParamInfo.defaultParts`, #3531) reads
265
+ * the earlier props' template variables. Every prop is already in
266
+ * `available` (baseScope), and the steps run in dependency order
267
+ * ({@link inDefaultReadOrder}), so a chain (`{ a = 1, b = a, c = b }`) reads
268
+ * `b` after its own step re-bound it. A default the plan cannot lower adds
269
+ * no step and keeps the static ssr-defaults seed.
270
+ */
271
+ function dependentPropDefaultSteps(metadata: IRMetadata, inlined: ReadonlySet<string>): SsrSeedStep[] {
272
+ const { readable, available, unshadowed } = propDefaultScope(metadata, inlined)
234
273
  const steps: SsrSeedStep[] = []
235
274
  for (const p of inDefaultReadOrder(metadata.propsParams)) {
236
- if (p.isRest || !p.defaultParts || !p.parsed || p.parsed.kind === 'arrow') continue
275
+ if (!hasComputedDefault(p) || !p.parsed || !readable.has(p.name)) continue
237
276
  const parsed: ParsedExpr = { kind: 'logical', op: '??', left: { kind: 'identifier', name: p.name }, right: p.parsed }
238
- const step = classify(p.name, 'prop', stringifyParsedExpr(parsed), parsed, available, inlined)
277
+ const step = classify(p.name, 'prop', stringifyParsedExpr(parsed), parsed, available, unshadowed)
239
278
  if (step.kind === 'derived') steps.push(step)
240
279
  }
241
280
  return steps
242
281
  }
243
282
 
283
+ /**
284
+ * The names a prop default step may read: the props a template reads as
285
+ * their live prop (`templatePropReadParams`, so a binding that a local of
286
+ * the same name shadows gets no step), the props object, and the module
287
+ * string consts no component-scope binding shadows (`const DEF = 'z'` in
288
+ * the body hides a module `DEF`, whatever the declaration order).
289
+ */
290
+ function propDefaultScope(
291
+ metadata: IRMetadata,
292
+ inlined: ReadonlySet<string>,
293
+ ): { readable: ReadonlySet<string>; available: ReadonlySet<string>; unshadowed: ReadonlySet<string> } {
294
+ const readable = new Set(templatePropReadParams(metadata).map(p => p.name))
295
+ const locals = new Set<string>()
296
+ for (const c of metadata.localConstants ?? []) if (!c.isModule) locals.add(c.name)
297
+ for (const f of metadata.localFunctions ?? []) if (!f.isModule) locals.add(f.name)
298
+ for (const s of metadata.signals) {
299
+ locals.add(s.getter)
300
+ if (s.setter) locals.add(s.setter)
301
+ }
302
+ for (const m of metadata.memos) locals.add(m.name)
303
+ const unshadowed = new Set([...inlined].filter(name => !locals.has(name)))
304
+ const available = new Set([...readable, ...unshadowed])
305
+ if (metadata.propsObjectName) available.add(metadata.propsObjectName)
306
+ return { readable, available, unshadowed }
307
+ }
308
+
244
309
  /**
245
310
  * `params` with every prop placed after the props its destructure default
246
311
  * reads (`ParamInfo.defaultParts`). `propsParams` follows the props TYPE's
@@ -278,7 +343,7 @@ export function computeSsrSeedPlan(metadata: IRMetadata): SsrSeedPlan {
278
343
  const localConsts = localConstExprsByName(metadata)
279
344
  const steps: SsrSeedStep[] = []
280
345
 
281
- steps.push(...dependentPropDefaultSteps(metadata, available, inlined))
346
+ steps.push(...dependentPropDefaultSteps(metadata, inlined))
282
347
 
283
348
  for (const signal of metadata.signals) {
284
349
  if (signal.envReader) {
@@ -60,6 +60,26 @@ export function lookupPartToJsExpr(
60
60
  return `(${obj}${typed})[${key}]`
61
61
  }
62
62
 
63
+ /**
64
+ * Spell a `ternary` part's branch — the literal's COOKED value, as the IR
65
+ * carries it — as a single-quoted JS string literal. The cooked value of
66
+ * `'a\nb'` holds a real newline and that of `"it's"` a bare quote, so
67
+ * wrapping it in quotes unescaped yields an unterminated or unbalanced
68
+ * literal. Escapes only what a single-quoted literal cannot hold raw (the
69
+ * backslash, the quote and the line terminators), so a branch without
70
+ * them prints exactly as before.
71
+ */
72
+ function singleQuotedJsString(value: string): string {
73
+ const body = value
74
+ .replace(/\\/g, '\\\\')
75
+ .replace(/'/g, "\\'")
76
+ .replace(/\n/g, '\\n')
77
+ .replace(/\r/g, '\\r')
78
+ .replace(/\u2028/g, '\\u2028')
79
+ .replace(/\u2029/g, '\\u2029')
80
+ return `'${body}'`
81
+ }
82
+
63
83
  /** Convert a `template` variant's parts into a JS template-literal string. */
64
84
  export function templatePartsToJsExpr(
65
85
  parts: readonly IRTemplatePart[],
@@ -73,7 +93,7 @@ export function templatePartsToJsExpr(
73
93
  result += `\${${opts?.useTemplate ? part.templateExpr ?? part.expr : part.expr}}`
74
94
  } else if (part.type === 'ternary') {
75
95
  const cond = (opts?.useTemplate && part.templateCondition) ? part.templateCondition : part.condition
76
- result += `\${${cond} ? '${part.whenTrue}' : '${part.whenFalse}'}`
96
+ result += `\${${cond} ? ${singleQuotedJsString(part.whenTrue)} : ${singleQuotedJsString(part.whenFalse)}}`
77
97
  } else if (part.type === 'lookup') {
78
98
  result += `\${${lookupPartToJsExpr(part, opts)}}`
79
99
  }
package/src/types.ts CHANGED
@@ -102,6 +102,16 @@ export interface PropertyInfo {
102
102
  export interface ParamInfo {
103
103
  name: string
104
104
  type: TypeInfo
105
+ /**
106
+ * The binding's declared type when `type` is `unknown` only because a
107
+ * destructured binding's declared type does not resolve to a concrete
108
+ * representation (`isResolvableMemberType` declines a union such as
109
+ * `n?: number | null`). `type` stays `unknown` so typed backends keep
110
+ * their untyped field; a reader that classifies the value's nil from the
111
+ * declaration (`collectNullishAttrContext`) reads this, as it reads
112
+ * `type` for the `props.n` form.
113
+ */
114
+ declaredType?: TypeInfo
105
115
  optional: boolean
106
116
  defaultValue?: JsSourceText
107
117
  /**
@@ -522,6 +532,17 @@ export interface IRExpression {
522
532
  * escaping it, while a non-empty fallback stays escaped text.
523
533
  */
524
534
  childrenRead?: 'bare' | 'nullish-empty' | 'nullish-fallback' | 'or-fallback'
535
+ /**
536
+ * A child-position read, inside a loop row, of one of the component's own
537
+ * props other than `children` (`{props.footer}`, a destructured `{footer}`,
538
+ * or a const alias of either), not shadowed by a row binding. Decided once
539
+ * in Phase 1 from the binding. The caller may pass a JSX element at that
540
+ * prop, which reaches the client as a `bfMarkup()` value or a `__slot()`
541
+ * marker, so the client writes the row slot as markup, exactly like the
542
+ * same read outside a loop. A row's own data (the item, a preamble local)
543
+ * is never flagged and stays escaped text.
544
+ */
545
+ loopRowPropRead?: true
525
546
  /** When true, expression calls signal getters or memos (has reactive `foo()` pattern). */
526
547
  callsReactiveGetters?: boolean
527
548
  /** When true, expression contains function call(s) — any `identifier()` pattern (computed from AST). */
@@ -1481,6 +1502,15 @@ export interface SpreadAttr {
1481
1502
  * per-key expansion earlier and leave this unset (#1407).
1482
1503
  */
1483
1504
  slotId?: string
1505
+ /**
1506
+ * Set on a child-component call's spread (`<Child {...props} />`) whose
1507
+ * source is the component's own props object, through any alias hop
1508
+ * (`resolveRestSpreadOriginCore` → `'props'`). Such a spread carries
1509
+ * exactly the keys the caller passed, so the client JS merges it with the
1510
+ * call's explicit props in source order, as JS does (#3688). Unset for
1511
+ * any other spread, including a `{...rest}` bag.
1512
+ */
1513
+ propsObject?: true
1484
1514
  }
1485
1515
 
1486
1516
  export interface JsxChildrenAttr {