@barefootjs/jsx 0.39.6 → 0.39.8

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 (192) hide show
  1. package/dist/adapters/attr-plan.d.ts +8 -1
  2. package/dist/adapters/attr-plan.d.ts.map +1 -1
  3. package/dist/adapters/callback-method.d.ts +44 -0
  4. package/dist/adapters/callback-method.d.ts.map +1 -1
  5. package/dist/adapters/component-props.d.ts +80 -13
  6. package/dist/adapters/component-props.d.ts.map +1 -1
  7. package/dist/adapters/computed-const.d.ts +52 -0
  8. package/dist/adapters/computed-const.d.ts.map +1 -0
  9. package/dist/adapters/dangerous-inner-html.d.ts +10 -0
  10. package/dist/adapters/dangerous-inner-html.d.ts.map +1 -1
  11. package/dist/adapters/dsl-template-adapter.d.ts +151 -0
  12. package/dist/adapters/dsl-template-adapter.d.ts.map +1 -0
  13. package/dist/adapters/element-shell.d.ts +44 -0
  14. package/dist/adapters/element-shell.d.ts.map +1 -0
  15. package/dist/adapters/interface.d.ts +2 -0
  16. package/dist/adapters/interface.d.ts.map +1 -1
  17. package/dist/adapters/jsx-adapter.d.ts +4 -0
  18. package/dist/adapters/jsx-adapter.d.ts.map +1 -1
  19. package/dist/adapters/literal-value.d.ts +2 -2
  20. package/dist/adapters/literal-value.d.ts.map +1 -1
  21. package/dist/adapters/parsed-expr-emitter.d.ts +27 -0
  22. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  23. package/dist/adapters/style-object.d.ts +33 -0
  24. package/dist/adapters/style-object.d.ts.map +1 -0
  25. package/dist/analyzer.d.ts.map +1 -1
  26. package/dist/augment-inherited-props.d.ts +7 -0
  27. package/dist/augment-inherited-props.d.ts.map +1 -1
  28. package/dist/children-nullish.d.ts +22 -0
  29. package/dist/children-nullish.d.ts.map +1 -0
  30. package/dist/errors.d.ts +1 -0
  31. package/dist/errors.d.ts.map +1 -1
  32. package/dist/expression-parser.d.ts +9 -19
  33. package/dist/expression-parser.d.ts.map +1 -1
  34. package/dist/fold-render-reassignments.d.ts +43 -0
  35. package/dist/fold-render-reassignments.d.ts.map +1 -0
  36. package/dist/free-refs.d.ts +15 -0
  37. package/dist/free-refs.d.ts.map +1 -1
  38. package/dist/index.d.ts +21 -14
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +24620 -23233
  41. package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
  42. package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts +2 -1
  43. package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts.map +1 -1
  44. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -2
  45. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts.map +1 -1
  46. package/dist/ir-to-client-js/control-flow/stringify/event-delegation.d.ts.map +1 -1
  47. package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +10 -9
  48. package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
  49. package/dist/ir-to-client-js/csr-substitute.d.ts +0 -13
  50. package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
  51. package/dist/ir-to-client-js/emit-reactive.d.ts +1 -1
  52. package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
  53. package/dist/ir-to-client-js/emit-registration.d.ts +10 -8
  54. package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
  55. package/dist/ir-to-client-js/generate-init.d.ts.map +1 -1
  56. package/dist/ir-to-client-js/html-template.d.ts +9 -19
  57. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  58. package/dist/ir-to-client-js/identifiers.d.ts +6 -8
  59. package/dist/ir-to-client-js/identifiers.d.ts.map +1 -1
  60. package/dist/ir-to-client-js/imports.d.ts +23 -2
  61. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  62. package/dist/ir-to-client-js/index.d.ts.map +1 -1
  63. package/dist/ir-to-client-js/phases/props-extraction.d.ts +3 -2
  64. package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
  65. package/dist/ir-to-client-js/plan/declaration-emit.d.ts +5 -0
  66. package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
  67. package/dist/ir-to-client-js/prop-handling.d.ts +11 -12
  68. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  69. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +1 -1
  70. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
  71. package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
  72. package/dist/ir-to-client-js/rewrite-destructured-props.d.ts +1 -1
  73. package/dist/ir-to-client-js/rewrite-destructured-props.d.ts.map +1 -1
  74. package/dist/ir-to-client-js/safe-html.d.ts +20 -2
  75. package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
  76. package/dist/ir-to-client-js/types.d.ts +3 -3
  77. package/dist/ir-to-client-js/types.d.ts.map +1 -1
  78. package/dist/ir-to-client-js/utils.d.ts +15 -11
  79. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  80. package/dist/jsx-to-ir.d.ts.map +1 -1
  81. package/dist/nullable-signal-attr.d.ts +43 -12
  82. package/dist/nullable-signal-attr.d.ts.map +1 -1
  83. package/dist/prop-rewrite.d.ts +18 -0
  84. package/dist/prop-rewrite.d.ts.map +1 -1
  85. package/dist/props-binding.d.ts +43 -6
  86. package/dist/props-binding.d.ts.map +1 -1
  87. package/dist/relocate.d.ts +16 -0
  88. package/dist/relocate.d.ts.map +1 -1
  89. package/dist/scanner/js-scanner.d.ts.map +1 -1
  90. package/dist/scanner/js-scanner.js +30 -2
  91. package/dist/signal-prop-name-collision.d.ts +6 -5
  92. package/dist/signal-prop-name-collision.d.ts.map +1 -1
  93. package/dist/ssr-defaults.d.ts +28 -0
  94. package/dist/ssr-defaults.d.ts.map +1 -1
  95. package/dist/ssr-seed-plan.d.ts +37 -18
  96. package/dist/ssr-seed-plan.d.ts.map +1 -1
  97. package/dist/types.d.ts +162 -20
  98. package/dist/types.d.ts.map +1 -1
  99. package/dist/value-references.d.ts +21 -0
  100. package/dist/value-references.d.ts.map +1 -1
  101. package/package.json +2 -2
  102. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +18 -18
  103. package/src/__tests__/adapter-output.test.ts +5 -3
  104. package/src/__tests__/analyzer.test.ts +33 -0
  105. package/src/__tests__/arithmetic-lowering.test.ts +44 -0
  106. package/src/__tests__/block-memo-type.test.ts +51 -0
  107. package/src/__tests__/callback-method.test.ts +75 -0
  108. package/src/__tests__/children-read.test.ts +226 -0
  109. package/src/__tests__/client-only-date-lowering.test.ts +2 -2
  110. package/src/__tests__/client-template-escape-soundness.test.ts +31 -1
  111. package/src/__tests__/component-props.test.ts +68 -1
  112. package/src/__tests__/computed-const.test.ts +101 -0
  113. package/src/__tests__/constant-inlining-tree.test.ts +136 -0
  114. package/src/__tests__/destructure-default-reads-earlier-param.test.ts +138 -0
  115. package/src/__tests__/dsl-template-adapter.test.ts +173 -0
  116. package/src/__tests__/element-shell.test.ts +78 -0
  117. package/src/__tests__/event-handler-verbatim.test.ts +73 -0
  118. package/src/__tests__/expression-parser.test.ts +1 -44
  119. package/src/__tests__/fold-render-reassignments.test.ts +96 -0
  120. package/src/__tests__/inlinable-local-method-call.test.ts +81 -0
  121. package/src/__tests__/memo-body-expr.test.ts +120 -0
  122. package/src/__tests__/nullable-signal-attr.test.ts +67 -3
  123. package/src/__tests__/prop-handling.test.ts +35 -16
  124. package/src/__tests__/props-alias-const.test.ts +65 -0
  125. package/src/__tests__/reference-graph-identifiers.test.ts +161 -0
  126. package/src/__tests__/runtime-import-detection.test.ts +134 -0
  127. package/src/__tests__/signal-prop-name-collision.test.ts +30 -0
  128. package/src/__tests__/signal-prop-seed.test.ts +90 -0
  129. package/src/__tests__/ssr-defaults.test.ts +19 -1
  130. package/src/__tests__/ssr-seed-plan.test.ts +173 -1
  131. package/src/__tests__/static-template-reachability.test.ts +137 -0
  132. package/src/__tests__/style-object-plan.test.ts +54 -0
  133. package/src/__tests__/zero-param-helper-inline.test.ts +57 -0
  134. package/src/adapters/attr-plan.ts +29 -1
  135. package/src/adapters/callback-method.ts +85 -0
  136. package/src/adapters/component-props.ts +130 -12
  137. package/src/adapters/computed-const.ts +185 -0
  138. package/src/adapters/dangerous-inner-html.ts +33 -0
  139. package/src/adapters/dsl-template-adapter.ts +248 -0
  140. package/src/adapters/element-shell.ts +160 -0
  141. package/src/adapters/interface.ts +8 -0
  142. package/src/adapters/jsx-adapter.ts +12 -6
  143. package/src/adapters/literal-value.ts +2 -2
  144. package/src/adapters/parsed-expr-emitter.ts +34 -0
  145. package/src/adapters/style-object.ts +61 -0
  146. package/src/analyzer.ts +382 -94
  147. package/src/augment-inherited-props.ts +12 -0
  148. package/src/children-nullish.ts +33 -0
  149. package/src/debug.ts +2 -2
  150. package/src/errors.ts +12 -0
  151. package/src/expression-parser.ts +56 -53
  152. package/src/fold-render-reassignments.ts +314 -0
  153. package/src/free-refs.ts +77 -17
  154. package/src/index.ts +27 -10
  155. package/src/ir-to-client-js/compute-inlinability.ts +122 -19
  156. package/src/ir-to-client-js/control-flow/plan/event-collision.ts +2 -1
  157. package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -2
  158. package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +3 -2
  159. package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +13 -12
  160. package/src/ir-to-client-js/csr-substitute.ts +25 -37
  161. package/src/ir-to-client-js/emit-reactive.ts +2 -2
  162. package/src/ir-to-client-js/emit-registration.ts +31 -59
  163. package/src/ir-to-client-js/generate-init.ts +17 -1
  164. package/src/ir-to-client-js/html-template.ts +56 -120
  165. package/src/ir-to-client-js/identifiers.ts +75 -24
  166. package/src/ir-to-client-js/imports.ts +56 -19
  167. package/src/ir-to-client-js/index.ts +5 -3
  168. package/src/ir-to-client-js/init-declarations.ts +1 -1
  169. package/src/ir-to-client-js/phases/event-handlers.ts +2 -2
  170. package/src/ir-to-client-js/phases/props-extraction.ts +16 -15
  171. package/src/ir-to-client-js/plan/build-declaration-emit.ts +26 -52
  172. package/src/ir-to-client-js/plan/declaration-emit.ts +5 -0
  173. package/src/ir-to-client-js/prop-handling.ts +13 -54
  174. package/src/ir-to-client-js/prune-unused-prop-extractions.ts +7 -4
  175. package/src/ir-to-client-js/reactivity.ts +2 -2
  176. package/src/ir-to-client-js/rewrite-destructured-props.ts +27 -9
  177. package/src/ir-to-client-js/safe-html.ts +29 -35
  178. package/src/ir-to-client-js/stringify/declaration-emit.ts +1 -1
  179. package/src/ir-to-client-js/types.ts +3 -2
  180. package/src/ir-to-client-js/utils.ts +13 -26
  181. package/src/jsx-to-ir.ts +318 -130
  182. package/src/nullable-signal-attr.ts +156 -37
  183. package/src/prop-rewrite.ts +41 -0
  184. package/src/props-binding.ts +117 -7
  185. package/src/relocate.ts +40 -8
  186. package/src/scanner/__tests__/js-scanner.test.ts +37 -0
  187. package/src/scanner/js-scanner.ts +64 -4
  188. package/src/signal-prop-name-collision.ts +50 -24
  189. package/src/ssr-defaults.ts +47 -20
  190. package/src/ssr-seed-plan.ts +121 -29
  191. package/src/types.ts +165 -21
  192. package/src/value-references.ts +132 -0
@@ -62,9 +62,14 @@ export function* iterateJsTokens(
62
62
  // closes an object literal nested inside.
63
63
  const templateStack: { braceDepth: number }[] = []
64
64
 
65
- // Previous *significant* (non-trivia) token kind. Drives the `/`
66
- // regex-vs-division decision.
65
+ // Whether a `/` after the previous *significant* (non-trivia) token
66
+ // starts a regex literal (`regexAfter`), and that token's kind.
67
67
  let prevSignificant: ts.SyntaxKind | undefined
68
+ let regexMayStart = true
69
+ // One entry per open `(`: whether it opens a `for (…)` / `for await (…)`
70
+ // header, where `of` can be the for-of separator.
71
+ const parens: boolean[] = []
72
+ let beforePrev: ts.SyntaxKind | undefined
68
73
 
69
74
  while (true) {
70
75
  let kind = scanner.scan()
@@ -74,7 +79,7 @@ export function* iterateJsTokens(
74
79
  // significant token cannot end an expression.
75
80
  if (
76
81
  (kind === ts.SyntaxKind.SlashToken || kind === ts.SyntaxKind.SlashEqualsToken)
77
- && canRegexStartHere(prevSignificant)
82
+ && regexMayStart
78
83
  ) {
79
84
  kind = scanner.reScanSlashToken()
80
85
  }
@@ -106,10 +111,65 @@ export function* iterateJsTokens(
106
111
 
107
112
  yield { kind, pos: scanner.getTokenStart(), end: scanner.getTokenEnd() }
108
113
 
109
- if (!isTriviaKind(kind)) prevSignificant = kind
114
+ if (!isTriviaKind(kind)) {
115
+ trackForHeaderParens(parens, kind, prevSignificant, beforePrev)
116
+ regexMayStart = regexAfter(kind, prevSignificant, parens[parens.length - 1] === true)
117
+ beforePrev = prevSignificant
118
+ prevSignificant = kind
119
+ }
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Push / pop `parens` for an open / close paren: each entry says whether that
125
+ * `(` opens a `for (…)` or `for await (…)` header.
126
+ */
127
+ function trackForHeaderParens(
128
+ parens: boolean[],
129
+ kind: ts.SyntaxKind,
130
+ prev: ts.SyntaxKind | undefined,
131
+ beforePrev: ts.SyntaxKind | undefined,
132
+ ): void {
133
+ if (kind === ts.SyntaxKind.OpenParenToken) {
134
+ parens.push(prev === ts.SyntaxKind.ForKeyword
135
+ || (prev === ts.SyntaxKind.AwaitKeyword && beforePrev === ts.SyntaxKind.ForKeyword))
136
+ } else if (kind === ts.SyntaxKind.CloseParenToken) {
137
+ parens.pop()
110
138
  }
111
139
  }
112
140
 
141
+ /**
142
+ * Whether a `/` right after a `kind` token (itself preceded by `before`)
143
+ * starts a regex literal. A keyword used as a name ends an expression like
144
+ * an identifier does, so `/` after it is division: a member name
145
+ * (`obj.default / 2`) and a contextual keyword, which is a legal binding
146
+ * name (`type / 2`). `await` keeps its operator reading (`await /re/`), and
147
+ * so does `of` as the separator of a `for (…)` header (`inForHeader`).
148
+ */
149
+ function regexAfter(kind: ts.SyntaxKind, before: ts.SyntaxKind | undefined, inForHeader: boolean): boolean {
150
+ if (kind >= ts.SyntaxKind.FirstKeyword && kind <= ts.SyntaxKind.LastKeyword) {
151
+ if (before === ts.SyntaxKind.DotToken || before === ts.SyntaxKind.QuestionDotToken) return false
152
+ // `of` right after a binding, directly inside a `for (…)` header, is the
153
+ // for-of separator, which an expression follows:
154
+ // `for (const hit of /re/.exec(s))`. Elsewhere (`let of = x⏎of /= 2`)
155
+ // it is a name.
156
+ if (kind === ts.SyntaxKind.OfKeyword && inForHeader && endsBinding(before)) return true
157
+ if (kind > ts.SyntaxKind.LastFutureReservedWord && kind !== ts.SyntaxKind.AwaitKeyword) return false
158
+ }
159
+ return canRegexStartHere(kind)
160
+ }
161
+
162
+ /**
163
+ * Whether a `before` token can end a `for…of` binding: a name (`x`, or a
164
+ * contextual keyword used as one, `type`), `[a, b]` or `{ a }`.
165
+ */
166
+ function endsBinding(before: ts.SyntaxKind | undefined): boolean {
167
+ return before === ts.SyntaxKind.Identifier
168
+ || before === ts.SyntaxKind.CloseBracketToken
169
+ || before === ts.SyntaxKind.CloseBraceToken
170
+ || (before !== undefined && before > ts.SyntaxKind.LastFutureReservedWord && before <= ts.SyntaxKind.LastKeyword)
171
+ }
172
+
113
173
  export function isTriviaKind(kind: ts.SyntaxKind): boolean {
114
174
  return (
115
175
  kind === ts.SyntaxKind.WhitespaceTrivia
@@ -1,7 +1,8 @@
1
1
  import ts from 'typescript'
2
2
  import path from 'node:path'
3
3
  import type { AnalyzerContext } from './analyzer-context.ts'
4
- import type { ParsedExpr } from './expression-parser.ts'
4
+ import type { SourceLocation } from './types.ts'
5
+ import { localConstValuesByName, referencesOwnProp } from './ssr-defaults.ts'
5
6
 
6
7
  // TypeScript exposes these resolution-cache readers on Program at runtime
7
8
  // (its own incremental rebuild uses them), but omits them from the public
@@ -65,21 +66,47 @@ export function programWithSeparatedSource(program: ts.Program, filePath: string
65
66
  } })
66
67
  }
67
68
 
68
- function isLiteralTree(expr: ParsedExpr): boolean {
69
- switch (expr.kind) {
70
- case 'literal': return true
71
- case 'array-literal': return expr.elements.every(isLiteralTree)
72
- case 'object-literal': return expr.properties.every(p => p.kind === 'prop' && isLiteralTree(p.value))
73
- case 'unary': return ['-', '+'].includes(expr.op) && expr.argument.kind === 'literal' && expr.argument.literalType === 'number'
74
- default: return false
75
- }
69
+ /** A component-scope signal getter or memo whose binding is renamed. */
70
+ interface SeparatedBinding {
71
+ name: string
72
+ loc: SourceLocation
73
+ prefix: 'bfSignal' | 'bfMemo'
74
+ }
75
+
76
+ /**
77
+ * Signal getters and memos that share a name with a prop of a bare-props
78
+ * 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`).
82
+ *
83
+ * 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`).
89
+ */
90
+ function collidingBindings(ctx: AnalyzerContext, propsObjectName: string): SeparatedBinding[] {
91
+ const propNames = new Set(ctx.propsParams.map(p => p.name.toLowerCase()))
92
+ const localConsts = localConstValuesByName(ctx)
93
+ const collides = (name: string, derivation: string): boolean =>
94
+ propNames.has(name.toLowerCase()) && !referencesOwnProp(derivation, propsObjectName, name, localConsts)
95
+ 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
+ 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
+ return [...signals, ...memos]
76
102
  }
77
103
 
78
104
  /**
79
- * A bare prop and a literal-seeded getter are different JS bindings, but
80
- * scalar template backends flatten both to one variable. Give the local
81
- * getter a hygienic name before building IR, so every backend (including
82
- * the client emitter) receives one consistent binding identity.
105
+ * A prop and a signal getter or memo of the same name are different JS
106
+ * bindings (`props.label` and `label()`), but scalar template backends
107
+ * flatten both to one variable. Give the local binding a hygienic name
108
+ * before building IR, so every backend (including the client emitter)
109
+ * receives one consistent binding identity.
83
110
  *
84
111
  * Source edits are AST/symbol-based and preserve untouched source bytes.
85
112
  * Property keys, strings, and shadowing callback bindings never rename;
@@ -88,15 +115,12 @@ function isLiteralTree(expr: ParsedExpr): boolean {
88
115
  * fallback binds the already-parsed SourceFile without parsing or loading
89
116
  * dependencies: only lexical symbol identity is needed, not types.
90
117
  */
91
- export function separateLiteralSignalPropBindings(
118
+ export function separateSignalPropBindings(
92
119
  ctx: AnalyzerContext,
93
120
  isNonReference: (id: ts.Identifier) => boolean,
94
121
  ): string | null {
95
122
  if (!ctx.propsObjectName || !ctx.componentNode) return null
96
- const propNames = new Set(ctx.propsParams.map(p => p.name))
97
- const candidates = ctx.signals.filter(s =>
98
- !s.isModule && !s.getterElided && propNames.has(s.getter) && s.parsed && isLiteralTree(s.parsed),
99
- )
123
+ const candidates = collidingBindings(ctx, ctx.propsObjectName)
100
124
  if (candidates.length === 0) return null
101
125
 
102
126
  const sf = ctx.sourceFile
@@ -111,23 +135,25 @@ export function separateLiteralSignalPropBindings(
111
135
  ts.forEachChild(node, collectNames)
112
136
  }
113
137
  collectNames(sf)
138
+ // A prop typed by an imported interface has no identifier in this file.
139
+ for (const p of ctx.propsParams) occupied.add(p.name.toLowerCase())
114
140
 
115
141
  const renames = new Map<ts.Symbol, string>()
116
- const declarations = new Map(candidates.map(s => [
117
- sf.getPositionOfLineAndCharacter(s.loc.start.line - 1, s.loc.start.column), s,
142
+ const declarations = new Map(candidates.map(b => [
143
+ sf.getPositionOfLineAndCharacter(b.loc.start.line - 1, b.loc.start.column), b,
118
144
  ]))
119
145
  function collectBindings(node: ts.Node): void {
120
146
  if (ts.isVariableDeclaration(node)) {
121
- const signal = declarations.get(node.getStart(sf))
122
- if (signal) {
147
+ const binding = declarations.get(node.getStart(sf))
148
+ if (binding) {
123
149
  const names = ts.isIdentifier(node.name) ? [node.name]
124
150
  : ts.isArrayBindingPattern(node.name)
125
151
  ? node.name.elements.flatMap(e => ts.isBindingElement(e) && ts.isIdentifier(e.name) ? [e.name] : [])
126
152
  : []
127
- const id = names.find(n => n.text === signal.getter)
153
+ const id = names.find(n => n.text === binding.name)
128
154
  const symbol = id && checker.getSymbolAtLocation(id)
129
155
  if (symbol) {
130
- const base = `bfSignal_${signal.getter}`
156
+ const base = `${binding.prefix}_${binding.name}`
131
157
  let name = base
132
158
  let suffix = 0
133
159
  while (occupied.has(name.toLowerCase())) name = `${base}_${++suffix}`
@@ -33,6 +33,7 @@ import ts from 'typescript'
33
33
  import { extractFreeIdentifiersFromNode } from './analyzer.ts'
34
34
  import { resolveGetterAliases, collectAliasableGetterNames } from './ir-to-client-js/csr-substitute.ts'
35
35
  import { resolveBodyDestructuredPropAliases } from './props-binding.ts'
36
+ import { computeSsrSeedPlan } from './ssr-seed-plan.ts'
36
37
  import type { IRMetadata } from './types.ts'
37
38
  import { isElidedFactoryGetter } from './types.ts'
38
39
 
@@ -162,6 +163,44 @@ interface EvalContext {
162
163
  propsLike: ReadonlySet<string>
163
164
  }
164
165
 
166
+ /**
167
+ * One entry per prop for {@link extractSsrDefaults} (see its comment on the
168
+ * two parameter forms): a static destructure default seeds its evaluated
169
+ * value, any other prop seeds `null`.
170
+ */
171
+ function addPropSsrDefaults(
172
+ metadata: IRMetadata,
173
+ propsLike: ReadonlySet<string>,
174
+ out: Record<string, SsrDefault>,
175
+ ): void {
176
+ // A prop whose default reads an earlier prop (`{ off = false, on = !off }`,
177
+ // #3531) and that the seed plan re-binds in-template: its value depends on
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`.
180
+ const plan = metadata.ssrSeedPlan ?? computeSsrSeedPlan(metadata)
181
+ const templateDefaulted = new Set(
182
+ plan.steps.filter(s => s.kind === 'derived' && s.origin === 'prop').map(s => s.name),
183
+ )
184
+ for (const p of metadata.propsParams) {
185
+ if (p.isRest) continue
186
+ // `propName` is the CALLER-facing key (`$props->{propName}` in the Perl
187
+ // consumer above) — for a renaming destructure (`{ n: count }`) that's
188
+ // `n`, not the local binding `count` the template variable is keyed by.
189
+ // `sourceName ?? name` is an identity for every un-aliased prop.
190
+ const callerPropName = p.sourceName ?? p.name
191
+ if (p.defaultValue !== undefined && !templateDefaulted.has(p.name)) {
192
+ const value = tryStaticEval(p.defaultValue, { bindings: {}, propsLike })
193
+ out[p.name] = { propName: callerPropName, value: resultToJsonable(value) }
194
+ } else {
195
+ // No destructure default — the template reads the prop as-is;
196
+ // Perl's `//` operator decides what `undef` becomes. The entry
197
+ // still matters so the template variable exists and consumers
198
+ // can supply the propName even with no static fallback.
199
+ out[p.name] = { propName: callerPropName, value: null }
200
+ }
201
+ }
202
+ }
203
+
165
204
  /**
166
205
  * Extract a JSON-encodable defaults map for the SSR stash.
167
206
  *
@@ -194,24 +233,7 @@ export function extractSsrDefaults(metadata: IRMetadata): Record<string, SsrDefa
194
233
  // evaluated value; one with none seeds `null` (→ undef; the
195
234
  // template-side `// …` recompute supplies the real default, and a
196
235
  // caller-passed prop wins via `propName`).
197
- for (const p of metadata.propsParams) {
198
- if (p.isRest) continue
199
- // `propName` is the CALLER-facing key (`$props->{propName}` in the Perl
200
- // consumer above) — for a renaming destructure (`{ n: count }`) that's
201
- // `n`, not the local binding `count` the template variable is keyed by.
202
- // `sourceName ?? name` is an identity for every un-aliased prop.
203
- const callerPropName = p.sourceName ?? p.name
204
- if (p.defaultValue !== undefined) {
205
- const value = tryStaticEval(p.defaultValue, { bindings: {}, propsLike })
206
- out[p.name] = { propName: callerPropName, value: resultToJsonable(value) }
207
- } else {
208
- // No destructure default — the template reads the prop as-is;
209
- // Perl's `//` operator decides what `undef` becomes. The entry
210
- // still matters so the template variable exists and consumers
211
- // can supply the propName even with no static fallback.
212
- out[p.name] = { propName: callerPropName, value: null }
213
- }
214
- }
236
+ addPropSsrDefaults(metadata, propsLike, out)
215
237
  // Rest-props bag (`...props`) — tracked separately from `propsParams`
216
238
  // by the analyzer. Default to an empty plain hash so adapters can
217
239
  // forward it through `spreadAttrs` without a nullability check.
@@ -444,7 +466,7 @@ export function extractSsrDefaults(metadata: IRMetadata): Record<string, SsrDefa
444
466
  * (a module const referencing `props` isn't legal JS — `props` is a
445
467
  * function parameter), so it can never contribute a prop reference here.
446
468
  */
447
- function localConstValuesByName(metadata: IRMetadata): ReadonlyMap<string, string> {
469
+ export function localConstValuesByName(metadata: Pick<IRMetadata, 'localConstants'>): ReadonlyMap<string, string> {
448
470
  const out = new Map<string, string>()
449
471
  for (const c of metadata.localConstants ?? []) {
450
472
  if (c.isModule || c.value === undefined) continue
@@ -462,8 +484,13 @@ function localConstValuesByName(metadata: IRMetadata): ReadonlyMap<string, strin
462
484
  * (never a bespoke walker — see the CLAUDE.md rule this file already
463
485
  * follows for `props.X` collection) and just checks membership of the one
464
486
  * name we care about.
487
+ *
488
+ * Also the analyzer's test for which same-named signal/memo keeps sharing
489
+ * 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.
465
492
  */
466
- function referencesOwnProp(
493
+ export function referencesOwnProp(
467
494
  expr: string | undefined,
468
495
  propsObjectName: string,
469
496
  name: string,
@@ -8,9 +8,11 @@
8
8
  * template adapters' seed paths; this module computes it once, on the IR, so
9
9
  * every adapter consumes the same plan and only supplies its own syntax.
10
10
  *
11
- * Ordering / acyclicity guarantee: `steps` lists the component's signals
12
- * first, then its memos, each group in declaration order (matching
13
- * `IRMetadata.signals` / `IRMetadata.memos`), then canonical getter aliases.
11
+ * Ordering / acyclicity guarantee: `steps` lists the props whose destructure
12
+ * default reads an earlier prop first, in parameter order, then the
13
+ * component's signals, then its memos, each group in declaration order
14
+ * (matching `IRMetadata.signals` / `IRMetadata.memos`), then canonical getter
15
+ * aliases.
14
16
  * A binding's name only enters
15
17
  * the scope set AFTER its own step is decided, so a `derived` step's `frees`
16
18
  * can only name `baseScope` entries or EARLIER steps — self- and
@@ -22,11 +24,11 @@
22
24
  * literal value — `collectModuleStringConsts` is the shared source of that
23
25
  * set — so a reference to one is never a template-variable read.
24
26
  *
25
- * A `derived` step with EMPTY `frees` is a constant expression (e.g.
26
- * `createSignal('b')`): the plan still classifies it as derived because the
27
- * expression is fully analyzable; emit-side constant-skipping (adapters keep
28
- * their existing ssr-defaults seeding for such inits) is an adapter concern,
29
- * not a plan concern. Likewise the plan makes no backend-specific choices —
27
+ * A `derived` step whose `frees` name only module pure-string consts (or
28
+ * nothing, e.g. `createSignal('b')`) is a constant expression, marked
29
+ * `constant`: the plan still classifies it as derived because the expression
30
+ * is fully analyzable, and adapters keep their static ssr-defaults seed for
31
+ * it instead of computing it in-template. Likewise the plan makes no backend-specific choices —
30
32
  * no target-variable checks, no self-shadowing rules, no per-backend shape
31
33
  * catalogs — those stay in the adapters.
32
34
  */
@@ -35,19 +37,20 @@ import { collectModuleStringConsts } from './augment-inherited-props.ts'
35
37
  import { collectAliasableGetterNames, resolveGetterAliases } from './ir-to-client-js/csr-substitute.ts'
36
38
  import { envSignalReaderFor, type EnvSignalReader } from './adapters/env-signal.ts'
37
39
  import {
38
- extractArrowBodyExpression,
39
40
  freeIdentifiers,
40
41
  inlineBinding,
41
42
  isSupportedValue,
42
43
  parseExpression,
44
+ stringifyParsedExpr,
43
45
  type ParsedExpr,
44
46
  } from './expression-parser.ts'
45
- import type { IRMetadata } from './types.ts'
47
+ import type { IRMetadata, ParamInfo } from './types.ts'
46
48
  import { isElidedFactoryGetter } from './types.ts'
47
49
 
48
50
  /**
49
- * One binding in dependency order (signals, memos, then getter aliases —
50
- * matching `IRMetadata` order and the adapters' iteration).
51
+ * One binding in dependency order (dependent prop defaults, signals, memos,
52
+ * then getter aliases — matching `IRMetadata` order and the adapters'
53
+ * iteration).
51
54
  *
52
55
  * - `env-reader`: an env signal whose `envReader` key resolves in the shared
53
56
  * registry (`envSignalReaderFor`). The runtime provides the per-request
@@ -56,7 +59,12 @@ import { isElidedFactoryGetter } from './types.ts'
56
59
  * registry falls through to the normal derived/opaque rules instead.
57
60
  * - `derived`: the binding's value expression is a supported shape whose free
58
61
  * identifiers are all in scope at this point (baseScope + earlier steps) —
59
- * an adapter may seed it in-template by lowering `parsed`/`expr`.
62
+ * an adapter may seed it in-template by lowering `parsed`/`expr`. An
63
+ * `origin: 'prop'` step re-binds a prop whose destructure default reads an
64
+ * earlier prop (`{ off = false, on = off }` → `on ?? off`, #3531): the
65
+ * static ssr-defaults seed cannot hold a value that depends on what the
66
+ * caller passed, so the template applies the default itself, reading the
67
+ * earlier prop's template variable.
60
68
  * - `opaque`: not seedable this way (empty init, unsupported shape,
61
69
  * unanalyzable free set, out-of-scope reference, or a block-bodied memo).
62
70
  * The name still enters scope for later steps; adapters keep their static
@@ -64,8 +72,21 @@ import { isElidedFactoryGetter } from './types.ts'
64
72
  */
65
73
  export type SsrSeedStep =
66
74
  | { kind: 'env-reader'; name: string; reader: EnvSignalReader }
67
- | { kind: 'derived'; name: string; origin: 'signal' | 'memo'; expr: string; parsed: ParsedExpr; frees: string[] }
68
- | { kind: 'opaque'; name: string; origin: 'signal' | 'memo' }
75
+ | {
76
+ kind: 'derived'
77
+ name: string
78
+ origin: 'signal' | 'memo' | 'prop'
79
+ expr: string
80
+ parsed: ParsedExpr
81
+ frees: string[]
82
+ /**
83
+ * Every free identifier is a module pure-string const, which every
84
+ * adapter inlines, so the lowered expression reads no template
85
+ * variable (#3542).
86
+ */
87
+ constant: boolean
88
+ }
89
+ | { kind: 'opaque'; name: string; origin: 'signal' | 'memo' | 'prop' }
69
90
 
70
91
  export interface SsrSeedPlan {
71
92
  /**
@@ -92,18 +113,21 @@ export interface SsrSeedPlan {
92
113
  */
93
114
  function classify(
94
115
  name: string,
95
- origin: 'signal' | 'memo',
116
+ origin: 'signal' | 'memo' | 'prop',
96
117
  expr: string,
97
118
  parsed: ParsedExpr,
98
119
  available: ReadonlySet<string>,
120
+ inlined: ReadonlySet<string>,
99
121
  ): SsrSeedStep {
100
- if (!isSupportedValue(parsed).supported) return { kind: 'opaque', name, origin }
122
+ const opaque: SsrSeedStep = { kind: 'opaque', name, origin }
123
+ if (!isSupportedValue(parsed).supported) return opaque
101
124
  const frees = freeIdentifiers(parsed)
102
- if (frees === null) return { kind: 'opaque', name, origin }
125
+ if (frees === null) return opaque
103
126
  for (const free of frees) {
104
- if (!available.has(free)) return { kind: 'opaque', name, origin }
127
+ if (!available.has(free)) return opaque
105
128
  }
106
- return { kind: 'derived', name, origin, expr, parsed, frees: [...frees] }
129
+ const constant = [...frees].every(free => inlined.has(free))
130
+ return { kind: 'derived', name, origin, expr, parsed, frees: [...frees], constant }
107
131
  }
108
132
 
109
133
  /**
@@ -174,21 +198,88 @@ function resolveThroughLocalConsts(
174
198
 
175
199
  /**
176
200
  * 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.
201
+ * doc for the contract. A memo step reads the expression its callback
202
+ * returns (`MemoInfo.bodyExpr`): an arrow expression body, or a block or
203
+ * `function` body that is a single `return`. A multi-statement body seeds
204
+ * from the analyzer's fold of its statements (`MemoInfo.parsed`, #2040,
205
+ * #3523), and is `opaque` when the fold failed.
206
+ */
207
+ /**
208
+ * The expression a memo seeds from: the returned expression, as the analyzer
209
+ * read it from the callback node (#3510). A body that must be called as a
210
+ * whole seeds from the analyzer's fold of its statements into one expression
211
+ * when there is one (#3523); `''` (opaque) otherwise.
212
+ */
213
+ function memoSeedExpr(memo: IRMetadata['memos'][number]): string {
214
+ if (!memo.bodyExprIsCall) return memo.bodyExpr?.trim() ?? ''
215
+ return memo.parsed ? stringifyParsedExpr(memo.parsed) : ''
216
+ }
217
+
218
+ /**
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.
228
+ */
229
+ function dependentPropDefaultSteps(
230
+ metadata: IRMetadata,
231
+ available: ReadonlySet<string>,
232
+ inlined: ReadonlySet<string>,
233
+ ): SsrSeedStep[] {
234
+ const steps: SsrSeedStep[] = []
235
+ for (const p of inDefaultReadOrder(metadata.propsParams)) {
236
+ if (p.isRest || !p.defaultParts || !p.parsed || p.parsed.kind === 'arrow') continue
237
+ 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)
239
+ if (step.kind === 'derived') steps.push(step)
240
+ }
241
+ return steps
242
+ }
243
+
244
+ /**
245
+ * `params` with every prop placed after the props its destructure default
246
+ * reads (`ParamInfo.defaultParts`). `propsParams` follows the props TYPE's
247
+ * member order for a body destructure (`const { a = 'x', b = a } = props`
248
+ * over `{ b?: string; a?: string }`), which need not be the pattern's order,
249
+ * so the order comes from the reads themselves. The reads only ever name an
250
+ * EARLIER binding of the same pattern, so they form no cycle; otherwise the
251
+ * input order is kept. Every backend applying these defaults one prop at a
252
+ * time (the in-template re-binds here, Go's constructor) uses this order.
180
253
  */
254
+ export function inDefaultReadOrder(params: readonly ParamInfo[]): ParamInfo[] {
255
+ const byName = new Map(params.filter(p => !p.isRest).map(p => [p.name, p]))
256
+ const ordered: ParamInfo[] = []
257
+ const placed = new Set<ParamInfo>()
258
+ const place = (p: ParamInfo): void => {
259
+ if (placed.has(p)) return
260
+ placed.add(p)
261
+ for (const part of p.defaultParts ?? []) {
262
+ const read = part.kind === 'param' ? byName.get(part.param.name) : undefined
263
+ if (read) place(read)
264
+ }
265
+ ordered.push(p)
266
+ }
267
+ for (const p of params) place(p)
268
+ return ordered
269
+ }
270
+
181
271
  export function computeSsrSeedPlan(metadata: IRMetadata): SsrSeedPlan {
182
272
  const baseScope: string[] = metadata.propsParams.map(p => p.name)
183
273
  if (metadata.propsObjectName) baseScope.push(metadata.propsObjectName)
184
- for (const name of collectModuleStringConsts(metadata.localConstants).keys()) {
185
- baseScope.push(name)
186
- }
274
+ const inlined = new Set(collectModuleStringConsts(metadata.localConstants).keys())
275
+ baseScope.push(...inlined)
187
276
 
188
277
  const available = new Set<string>(baseScope)
189
278
  const localConsts = localConstExprsByName(metadata)
190
279
  const steps: SsrSeedStep[] = []
191
280
 
281
+ steps.push(...dependentPropDefaultSteps(metadata, available, inlined))
282
+
192
283
  for (const signal of metadata.signals) {
193
284
  if (signal.envReader) {
194
285
  const reader = envSignalReaderFor(signal.envReader)
@@ -230,14 +321,14 @@ export function computeSsrSeedPlan(metadata: IRMetadata): SsrSeedPlan {
230
321
  // undefined) equally safe.
231
322
  resolveThroughLocalConsts(signal.parsed ?? parseExpression(`(${expr})`), localConsts),
232
323
  available,
324
+ inlined,
233
325
  ),
234
326
  )
235
327
  available.add(signal.getter)
236
328
  }
237
329
 
238
330
  for (const memo of metadata.memos) {
239
- const body = extractArrowBodyExpression(memo.computation)
240
- const expr = body?.trim() ?? ''
331
+ const expr = memoSeedExpr(memo)
241
332
  steps.push(
242
333
  expr === ''
243
334
  ? { kind: 'opaque', name: memo.name, origin: 'memo' }
@@ -247,6 +338,7 @@ export function computeSsrSeedPlan(metadata: IRMetadata): SsrSeedPlan {
247
338
  expr,
248
339
  resolveThroughLocalConsts(memo.parsed ?? parseExpression(expr), localConsts),
249
340
  available,
341
+ inlined,
250
342
  ),
251
343
  )
252
344
  available.add(memo.name)
@@ -259,7 +351,7 @@ export function computeSsrSeedPlan(metadata: IRMetadata): SsrSeedPlan {
259
351
  for (const [alias, origin] of resolveGetterAliases(metadata.localConstants ?? [], name => getters.has(name))) {
260
352
  if (metadata.propsParams.some(p => p.name === alias)) continue
261
353
  const parsed: ParsedExpr = { kind: 'call', callee: { kind: 'identifier', name: origin }, args: [] }
262
- steps.push(classify(alias, 'memo', `${origin}()`, parsed, available))
354
+ steps.push(classify(alias, 'memo', `${origin}()`, parsed, available, inlined))
263
355
  available.add(alias)
264
356
  }
265
357
  return { baseScope, steps }