@barefootjs/jsx 0.39.7 → 0.39.9

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/callback-method.d.ts +36 -7
  2. package/dist/adapters/callback-method.d.ts.map +1 -1
  3. package/dist/adapters/component-props.d.ts +80 -13
  4. package/dist/adapters/component-props.d.ts.map +1 -1
  5. package/dist/adapters/dsl-template-adapter.d.ts +153 -0
  6. package/dist/adapters/dsl-template-adapter.d.ts.map +1 -0
  7. package/dist/adapters/jsx-adapter.d.ts +4 -0
  8. package/dist/adapters/jsx-adapter.d.ts.map +1 -1
  9. package/dist/adapters/literal-value.d.ts +2 -2
  10. package/dist/adapters/literal-value.d.ts.map +1 -1
  11. package/dist/adapters/loop-plan.d.ts +133 -9
  12. package/dist/adapters/loop-plan.d.ts.map +1 -1
  13. package/dist/adapters/parsed-expr-emitter.d.ts +44 -0
  14. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  15. package/dist/adapters/style-object.d.ts +33 -0
  16. package/dist/adapters/style-object.d.ts.map +1 -0
  17. package/dist/analyzer.d.ts.map +1 -1
  18. package/dist/assignment-target.d.ts +26 -0
  19. package/dist/assignment-target.d.ts.map +1 -0
  20. package/dist/children-nullish.d.ts +22 -0
  21. package/dist/children-nullish.d.ts.map +1 -0
  22. package/dist/errors.d.ts +2 -0
  23. package/dist/errors.d.ts.map +1 -1
  24. package/dist/expression-parser.d.ts +9 -19
  25. package/dist/expression-parser.d.ts.map +1 -1
  26. package/dist/free-refs.d.ts +6 -0
  27. package/dist/free-refs.d.ts.map +1 -1
  28. package/dist/index.d.ts +17 -14
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +15040 -13899
  31. package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
  32. package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
  33. package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts +2 -1
  34. package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts.map +1 -1
  35. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -2
  36. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts.map +1 -1
  37. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +24 -1
  38. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
  39. package/dist/ir-to-client-js/control-flow/shared.d.ts +16 -1
  40. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  41. package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts +6 -1
  42. package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
  43. package/dist/ir-to-client-js/control-flow/stringify/event-delegation.d.ts.map +1 -1
  44. package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +10 -9
  45. package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
  46. package/dist/ir-to-client-js/csr-substitute.d.ts +13 -2
  47. package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
  48. package/dist/ir-to-client-js/emit-reactive.d.ts +1 -1
  49. package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
  50. package/dist/ir-to-client-js/emit-registration.d.ts +10 -8
  51. package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
  52. package/dist/ir-to-client-js/generate-init.d.ts.map +1 -1
  53. package/dist/ir-to-client-js/html-template.d.ts +0 -9
  54. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  55. package/dist/ir-to-client-js/identifiers.d.ts +6 -8
  56. package/dist/ir-to-client-js/identifiers.d.ts.map +1 -1
  57. package/dist/ir-to-client-js/imports.d.ts +23 -2
  58. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  59. package/dist/ir-to-client-js/phases/props-extraction.d.ts +6 -3
  60. package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
  61. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +8 -0
  62. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
  63. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +1 -1
  64. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
  65. package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
  66. package/dist/ir-to-client-js/rewrite-destructured-props.d.ts +1 -1
  67. package/dist/ir-to-client-js/rewrite-destructured-props.d.ts.map +1 -1
  68. package/dist/ir-to-client-js/rewrite-props-object.d.ts.map +1 -1
  69. package/dist/ir-to-client-js/safe-html.d.ts +20 -2
  70. package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
  71. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +1 -0
  72. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
  73. package/dist/ir-to-client-js/types.d.ts +3 -3
  74. package/dist/ir-to-client-js/types.d.ts.map +1 -1
  75. package/dist/ir-to-client-js/utils.d.ts +2 -11
  76. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  77. package/dist/jsx-to-ir.d.ts.map +1 -1
  78. package/dist/nullable-signal-attr.d.ts +40 -10
  79. package/dist/nullable-signal-attr.d.ts.map +1 -1
  80. package/dist/prop-rewrite.d.ts +35 -8
  81. package/dist/prop-rewrite.d.ts.map +1 -1
  82. package/dist/props-binding.d.ts +66 -1
  83. package/dist/props-binding.d.ts.map +1 -1
  84. package/dist/scanner/js-scanner.d.ts.map +1 -1
  85. package/dist/scanner/js-scanner.js +30 -2
  86. package/dist/scope/binding-scope.d.ts +30 -2
  87. package/dist/scope/binding-scope.d.ts.map +1 -1
  88. package/dist/scope/lexical-scope-walk.d.ts +26 -0
  89. package/dist/scope/lexical-scope-walk.d.ts.map +1 -0
  90. package/dist/signal-prop-name-collision.d.ts +6 -5
  91. package/dist/signal-prop-name-collision.d.ts.map +1 -1
  92. package/dist/ssr-defaults.d.ts +28 -0
  93. package/dist/ssr-defaults.d.ts.map +1 -1
  94. package/dist/ssr-seed-plan.d.ts +39 -14
  95. package/dist/ssr-seed-plan.d.ts.map +1 -1
  96. package/dist/types.d.ts +133 -19
  97. package/dist/types.d.ts.map +1 -1
  98. package/dist/value-references.d.ts +21 -0
  99. package/dist/value-references.d.ts.map +1 -1
  100. package/package.json +2 -2
  101. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +18 -18
  102. package/src/__tests__/adapter-output.test.ts +5 -3
  103. package/src/__tests__/analyzer.test.ts +33 -0
  104. package/src/__tests__/arithmetic-lowering.test.ts +48 -0
  105. package/src/__tests__/binding-scope.test.ts +29 -0
  106. package/src/__tests__/children-read.test.ts +226 -0
  107. package/src/__tests__/client-template-escape-soundness.test.ts +31 -1
  108. package/src/__tests__/component-props.test.ts +68 -1
  109. package/src/__tests__/constant-inlining-tree.test.ts +136 -0
  110. package/src/__tests__/create-query.test.ts +1 -1
  111. package/src/__tests__/destructure-default-reads-earlier-param.test.ts +138 -0
  112. package/src/__tests__/dsl-template-adapter.test.ts +173 -0
  113. package/src/__tests__/emit-to-string-call.test.ts +58 -0
  114. package/src/__tests__/event-handler-verbatim.test.ts +73 -0
  115. package/src/__tests__/expression-parser.test.ts +1 -44
  116. package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +90 -31
  117. package/src/__tests__/loop-plan.test.ts +131 -1
  118. package/src/__tests__/loop-row-child-children-handler.test.ts +129 -0
  119. package/src/__tests__/loop-row-child-children-inner-loop-shadow.test.ts +74 -0
  120. package/src/__tests__/loop-row-child-children-ref.test.ts +117 -0
  121. package/src/__tests__/nullable-signal-attr.test.ts +67 -3
  122. package/src/__tests__/props-alias-const.test.ts +60 -1
  123. package/src/__tests__/props-named-callback-param.test.ts +256 -0
  124. package/src/__tests__/reference-graph-identifiers.test.ts +161 -0
  125. package/src/__tests__/renamed-body-destructure-template.test.ts +86 -0
  126. package/src/__tests__/rewrite-props-object.test.ts +90 -0
  127. package/src/__tests__/runtime-import-detection.test.ts +134 -0
  128. package/src/__tests__/signal-prop-name-collision.test.ts +30 -0
  129. package/src/__tests__/ssr-defaults.test.ts +19 -1
  130. package/src/__tests__/ssr-seed-plan.test.ts +119 -0
  131. package/src/__tests__/style-object-plan.test.ts +54 -0
  132. package/src/__tests__/written-binding-substitution.test.ts +430 -0
  133. package/src/__tests__/zero-param-helper-inline.test.ts +57 -0
  134. package/src/adapters/callback-method.ts +48 -10
  135. package/src/adapters/component-props.ts +130 -12
  136. package/src/adapters/dsl-template-adapter.ts +259 -0
  137. package/src/adapters/jsx-adapter.ts +12 -6
  138. package/src/adapters/literal-value.ts +2 -2
  139. package/src/adapters/loop-plan.ts +201 -13
  140. package/src/adapters/parsed-expr-emitter.ts +67 -0
  141. package/src/adapters/style-object.ts +61 -0
  142. package/src/analyzer.ts +396 -16
  143. package/src/assignment-target.ts +79 -0
  144. package/src/children-nullish.ts +33 -0
  145. package/src/debug.ts +2 -2
  146. package/src/errors.ts +23 -0
  147. package/src/expression-parser.ts +56 -53
  148. package/src/free-refs.ts +28 -17
  149. package/src/index.ts +31 -10
  150. package/src/ir-to-client-js/compute-inlinability.ts +35 -5
  151. package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +21 -5
  152. package/src/ir-to-client-js/control-flow/plan/event-collision.ts +2 -1
  153. package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -2
  154. package/src/ir-to-client-js/control-flow/plan/loop.ts +25 -1
  155. package/src/ir-to-client-js/control-flow/shared.ts +41 -3
  156. package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +23 -3
  157. package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +3 -2
  158. package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +13 -12
  159. package/src/ir-to-client-js/csr-substitute.ts +82 -24
  160. package/src/ir-to-client-js/emit-reactive.ts +3 -3
  161. package/src/ir-to-client-js/emit-registration.ts +34 -58
  162. package/src/ir-to-client-js/generate-init.ts +17 -1
  163. package/src/ir-to-client-js/html-template.ts +48 -57
  164. package/src/ir-to-client-js/identifiers.ts +75 -24
  165. package/src/ir-to-client-js/imports.ts +56 -19
  166. package/src/ir-to-client-js/index.ts +2 -1
  167. package/src/ir-to-client-js/phases/event-handlers.ts +2 -2
  168. package/src/ir-to-client-js/phases/props-extraction.ts +39 -17
  169. package/src/ir-to-client-js/plan/build-declaration-emit.ts +2 -2
  170. package/src/ir-to-client-js/plan/build-static-array-child-init.ts +7 -0
  171. package/src/ir-to-client-js/plan/static-array-child-init.ts +8 -0
  172. package/src/ir-to-client-js/prune-unused-prop-extractions.ts +7 -4
  173. package/src/ir-to-client-js/reactivity.ts +2 -2
  174. package/src/ir-to-client-js/rewrite-destructured-props.ts +22 -5
  175. package/src/ir-to-client-js/rewrite-props-object.ts +14 -8
  176. package/src/ir-to-client-js/safe-html.ts +29 -35
  177. package/src/ir-to-client-js/stringify/static-array-child-init.ts +4 -1
  178. package/src/ir-to-client-js/types.ts +3 -2
  179. package/src/ir-to-client-js/utils.ts +42 -122
  180. package/src/jsx-to-ir.ts +310 -90
  181. package/src/nullable-signal-attr.ts +149 -33
  182. package/src/prop-rewrite.ts +72 -72
  183. package/src/props-binding.ts +90 -3
  184. package/src/scanner/__tests__/js-scanner.test.ts +37 -0
  185. package/src/scanner/js-scanner.ts +64 -4
  186. package/src/scope/binding-scope.ts +34 -3
  187. package/src/scope/lexical-scope-walk.ts +167 -0
  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 +103 -23
  191. package/src/types.ts +140 -20
  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
@@ -40,6 +40,14 @@ export type ScopeBindingSource = 'item' | 'index' | 'destructure' | 'preamble' |
40
40
  export interface ScopeBinding {
41
41
  readonly source: ScopeBindingSource
42
42
  readonly stringValue?: boolean
43
+ /**
44
+ * The row a `'item'` / `'destructure'` binding reads: the loop's item type
45
+ * and the accessor path from the item to the binding (empty for the plain
46
+ * item). Absent when the loop has no item type or the binding is a rest
47
+ * element. Lets a typed backend resolve the type of a row read
48
+ * (`row.id`, a destructured `id`) at the read.
49
+ */
50
+ readonly row?: { readonly itemType: LoopValueType; readonly segments: readonly RowPathSegment[] }
43
51
  }
44
52
 
45
53
  export interface ScopeFrame {
@@ -56,16 +64,32 @@ export interface ScopeFrame {
56
64
  export interface LoopBindingSource {
57
65
  readonly param: string
58
66
  readonly itemType?: LoopValueType | null
67
+ /**
68
+ * The item type a binding's `row` carries, when the caller knows more than
69
+ * `itemType` (which is scalar evidence only): a backend that resolves an
70
+ * object row's type from the loop's array source. Defaults to `itemType`.
71
+ */
72
+ readonly rowType?: LoopValueType | null
59
73
  readonly index?: string | null
60
- readonly paramBindings?: readonly { readonly name: string }[]
74
+ readonly paramBindings?: readonly {
75
+ readonly name: string
76
+ readonly segments?: readonly RowPathSegment[]
77
+ readonly rest?: unknown
78
+ }[]
61
79
  readonly preamble?: { readonly declaredNames: readonly string[] } | null
62
80
  }
63
81
 
64
- interface LoopValueType {
82
+ /** Structural pick of `TypeInfo` (`packages/jsx/src/types.ts`). */
83
+ export interface LoopValueType {
65
84
  readonly primitive?: string
66
85
  readonly unionTypes?: readonly LoopValueType[]
67
86
  }
68
87
 
88
+ /** Structural copy of `LoopBindingPathSegment` (`packages/jsx/src/types.ts`). */
89
+ export type RowPathSegment =
90
+ | { readonly kind: 'field'; readonly key: string; readonly isIdent: boolean }
91
+ | { readonly kind: 'index'; readonly index: number }
92
+
69
93
  function isStringLoopValue(type: LoopValueType): boolean {
70
94
  return type.primitive === 'string' ||
71
95
  (type.unionTypes !== undefined && type.unionTypes.length > 0 && type.unionTypes.every(isStringLoopValue))
@@ -121,12 +145,19 @@ export class BindingScope {
121
145
  */
122
146
  enterLoopRow(loop: LoopBindingSource): BindingScope {
123
147
  const bindings = new Map<string, ScopeBinding>()
148
+ const rowType = loop.rowType ?? loop.itemType
124
149
  if (loop.paramBindings && loop.paramBindings.length > 0) {
125
- for (const b of loop.paramBindings) bindings.set(b.name, { source: 'destructure' })
150
+ for (const b of loop.paramBindings) {
151
+ bindings.set(b.name, {
152
+ source: 'destructure',
153
+ ...(rowType && b.segments && b.rest === undefined ? { row: { itemType: rowType, segments: b.segments } } : {}),
154
+ })
155
+ }
126
156
  } else {
127
157
  bindings.set(loop.param, {
128
158
  source: 'item',
129
159
  ...(loop.itemType ? { stringValue: isStringLoopValue(loop.itemType) } : {}),
160
+ ...(rowType ? { row: { itemType: rowType, segments: [] } } : {}),
130
161
  })
131
162
  }
132
163
  if (loop.index != null) bindings.set(loop.index, { source: 'index' })
@@ -0,0 +1,167 @@
1
+ /**
2
+ * Lexical binding frames for TS-AST walks over emitted or source JS text.
3
+ *
4
+ * Scope model: a name bound INSIDE the walked node — a nested arrow /
5
+ * function parameter, a block-scoped declaration, a `for` head's `let` /
6
+ * `const`, a catch variable — refers to that binding, not to an outer
7
+ * name of the same spelling, for the whole binding's scope. Names bound
8
+ * OUTSIDE the walked text (a loop callback that encloses a template
9
+ * fragment) are the caller's job — that is what `BindingScope`
10
+ * (`./binding-scope.ts`) answers; this module answers the complementary
11
+ * question for binders that live inside the text itself.
12
+ *
13
+ * Shared by the prop-reference rewrites in `prop-rewrite.ts`, the
14
+ * props-object rename in `ir-to-client-js/rewrite-props-object.ts` and the
15
+ * loop-param accessor rewrite in `ir-to-client-js/utils.ts`
16
+ * (`rewriteIdentifierReferences`, #3595), so all of them agree on which
17
+ * identifiers an inner binding shadows.
18
+ */
19
+
20
+ import ts from 'typescript'
21
+
22
+ /** Collect every name introduced by a binding name (identifier or pattern). */
23
+ function collectBindingNames(name: ts.BindingName, out: Set<string>): void {
24
+ if (ts.isIdentifier(name)) {
25
+ out.add(name.text)
26
+ return
27
+ }
28
+ for (const el of name.elements) {
29
+ if (ts.isBindingElement(el)) collectBindingNames(el.name, out)
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Names bound by `n` for its subtree, or null when `n` introduces no
35
+ * scope. The frames follow JS's two scoping rules:
36
+ *
37
+ * - Function scope: a function-like binds its parameters and a function
38
+ * expression's own name; this frame covers the parameter list, so a
39
+ * default initializer sees the parameters but none of the body's
40
+ * declarations. A method's or accessor's computed name (`[k](k) { … }`)
41
+ * sits outside this frame: it is evaluated in the enclosing scope. The
42
+ * function's body block binds, on top of its own
43
+ * `let` / `const`, every `var` declared anywhere in the body — inside
44
+ * nested blocks and loop heads too (`if (c) { var x }` and `for (var x …)`
45
+ * are visible to the whole body), but not inside a nested function, which
46
+ * owns its own `var`s. A concise arrow body declares nothing. A class
47
+ * `static { … }` block is a var scope of its own in the same way. A named
48
+ * class expression binds its own name inside the class, as a named
49
+ * function expression does.
50
+ * - Block scope: a block binds its statement-level `let` / `const`, class
51
+ * and function declarations (the emitted client JS is a module, so it is
52
+ * strict code: a function declared in a block is scoped to that block);
53
+ * a `for` head binds its `let` / `const`; a catch clause binds its
54
+ * variable.
55
+ *
56
+ * A `var` with no enclosing function in the walked text binds at its top
57
+ * level, where no frame is pushed — the same as any other top-level
58
+ * declaration.
59
+ */
60
+ function scopeFrameOf(n: ts.Node, parent: ts.Node | undefined): Set<string> | null {
61
+ const frame = new Set<string>()
62
+ if (ts.isFunctionLike(n)) collectFunctionBindings(n, frame)
63
+ else if (ts.isBlock(n)) {
64
+ if (parent && isFunctionBody(n, parent)) collectVarBindings(n, frame)
65
+ collectBlockBindings(n, frame)
66
+ }
67
+ else if (ts.isForStatement(n) || ts.isForOfStatement(n) || ts.isForInStatement(n)) collectForHeadBindings(n, frame)
68
+ else if (ts.isCatchClause(n) && n.variableDeclaration) collectBindingNames(n.variableDeclaration.name, frame)
69
+ else if (ts.isClassExpression(n) && n.name) frame.add(n.name.text)
70
+ return frame.size > 0 ? frame : null
71
+ }
72
+
73
+ function collectFunctionBindings(n: ts.SignatureDeclaration, frame: Set<string>): void {
74
+ for (const p of n.parameters) collectBindingNames(p.name, frame)
75
+ if ((ts.isFunctionExpression(n) || ts.isFunctionDeclaration(n)) && n.name) frame.add(n.name.text)
76
+ }
77
+
78
+ /**
79
+ * A var-scope boundary: a function-like (function declaration / expression,
80
+ * arrow, method, accessor, constructor) or a class `static { … }` block. Each
81
+ * owns the `var`s declared in it. A class field initializer is an
82
+ * expression, so a `var` can only sit inside one of these boundaries there.
83
+ */
84
+ function isVarScopeBoundary(n: ts.Node): n is ts.SignatureDeclaration | ts.ClassStaticBlockDeclaration {
85
+ return ts.isFunctionLike(n) || ts.isClassStaticBlockDeclaration(n)
86
+ }
87
+
88
+ /** True when `block` is the body of the var-scope boundary `parent`. */
89
+ function isFunctionBody(block: ts.Block, parent: ts.Node): boolean {
90
+ return isVarScopeBoundary(parent) && (parent as { body?: ts.Node }).body === block
91
+ }
92
+
93
+ /** True for a `let` / `const` (and `using`) declaration list; false for `var`. */
94
+ function isBlockScopedList(list: ts.VariableDeclarationList): boolean {
95
+ return (list.flags & ts.NodeFlags.BlockScoped) !== 0
96
+ }
97
+
98
+ /**
99
+ * Every `var`-declared name under `n`, through blocks and loop heads but not
100
+ * into a nested var-scope boundary (each binds its own `var`s).
101
+ */
102
+ function collectVarBindings(n: ts.Node, frame: Set<string>): void {
103
+ if (ts.isVariableDeclarationList(n) && !isBlockScopedList(n)) {
104
+ for (const d of n.declarations) collectBindingNames(d.name, frame)
105
+ }
106
+ ts.forEachChild(n, child => {
107
+ if (!isVarScopeBoundary(child)) collectVarBindings(child, frame)
108
+ })
109
+ }
110
+
111
+ function collectBlockBindings(n: ts.Block, frame: Set<string>): void {
112
+ for (const st of n.statements) {
113
+ if (ts.isVariableStatement(st)) {
114
+ if (!isBlockScopedList(st.declarationList)) continue
115
+ for (const d of st.declarationList.declarations) collectBindingNames(d.name, frame)
116
+ } else if ((ts.isFunctionDeclaration(st) || ts.isClassDeclaration(st)) && st.name) {
117
+ frame.add(st.name.text)
118
+ }
119
+ }
120
+ }
121
+
122
+ /**
123
+ * A `for` head's `let` / `const` binds for the loop (`for (let i = 0; …;
124
+ * i++)`, `for (const x of xs)`): its condition, incrementor and body read
125
+ * the loop variable, not an outer prop of the same name.
126
+ */
127
+ function collectForHeadBindings(n: ts.ForStatement | ts.ForOfStatement | ts.ForInStatement, frame: Set<string>): void {
128
+ if (!n.initializer || !ts.isVariableDeclarationList(n.initializer)) return
129
+ // A `for (var …)` binds in the enclosing function (`collectVarBindings`).
130
+ if (!isBlockScopedList(n.initializer)) return
131
+ for (const d of n.initializer.declarations) collectBindingNames(d.name, frame)
132
+ }
133
+
134
+ /** The computed property name of a function-like (`[k](k) { … }`), if any. */
135
+ function computedNameOf(n: ts.Node): ts.ComputedPropertyName | undefined {
136
+ if (!ts.isFunctionLike(n)) return undefined
137
+ const name = (n as { name?: ts.Node }).name
138
+ return name && ts.isComputedPropertyName(name) ? name : undefined
139
+ }
140
+
141
+ /**
142
+ * Depth-first walk that maintains the binding stack described above and
143
+ * reports every identifier along with whether a binding within `root`
144
+ * currently shadows it.
145
+ */
146
+ export function walkWithScope(
147
+ root: ts.Node,
148
+ visit: (ident: ts.Identifier, parent: ts.Node | undefined, shadowed: boolean) => void,
149
+ ): void {
150
+ const scopeStack: Set<string>[] = []
151
+ const isShadowed = (name: string) => scopeStack.some(frame => frame.has(name))
152
+ function rec(n: ts.Node, parent?: ts.Node) {
153
+ const frame = scopeFrameOf(n, parent)
154
+ // A method's / accessor's computed name (`{ [k](k) { … } }`) is evaluated
155
+ // in the enclosing scope, before the parameters exist: walk it outside
156
+ // the function's frame.
157
+ const outerName = frame ? computedNameOf(n) : undefined
158
+ if (outerName) rec(outerName, n)
159
+ if (frame) scopeStack.push(frame)
160
+ if (ts.isIdentifier(n)) visit(n, parent, isShadowed(n.text))
161
+ ts.forEachChild(n, child => {
162
+ if (child !== outerName) rec(child, n)
163
+ })
164
+ if (frame) scopeStack.pop()
165
+ }
166
+ rec(root)
167
+ }
@@ -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,