@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
package/src/types.ts CHANGED
@@ -7,6 +7,17 @@
7
7
  import type { ParsedExpr, ParsedStatement } from './expression-parser.ts'
8
8
  import type { SsrSeedPlan } from './ssr-seed-plan.ts'
9
9
 
10
+ /**
11
+ * JS/TS source text the IR carries verbatim (an expression, a handler, a
12
+ * statement body), as written in the component. Phase 2 passes it through to
13
+ * output or reads the parsed tree beside it; it does not re-derive meaning
14
+ * from the text with regex or string search (spec/adapter-architecture.md,
15
+ * rule 1). A flavor, not a strict brand: a plain `string` is assignable, so
16
+ * producers need no cast, while a read keeps the type and the emit-parse
17
+ * ratchet counts a string operation on it (#3543).
18
+ */
19
+ export type JsSourceText = string & { readonly __jsSourceText?: 'JsSourceText' }
20
+
10
21
  /**
11
22
  * Loop-hoisted sort comparator for the `.sort().map()` / `.toSorted().map()`
12
23
  * pattern (#2018 P5). Carries the generic comparator `arrow` (params + body)
@@ -92,7 +103,7 @@ export interface ParamInfo {
92
103
  name: string
93
104
  type: TypeInfo
94
105
  optional: boolean
95
- defaultValue?: string
106
+ defaultValue?: JsSourceText
96
107
  /**
97
108
  * `defaultValue` parsed into a structured tree, mirroring `SignalInfo.parsed`
98
109
  * (Roadmap A). Attached best-effort by the analyzer (`tsNodeToParsedExpr` on
@@ -105,6 +116,16 @@ export interface ParamInfo {
105
116
  parsed?: ParsedExpr
106
117
  /** When true, the default value contains an arrow function or function expression (computed from AST). */
107
118
  defaultContainsArrow?: boolean
119
+ /**
120
+ * `defaultValue` split at each read of an EARLIER binding of the same props
121
+ * destructure (`{ off = false, on = off }` → `on`'s parts are one `param`
122
+ * part naming `off`). The analyzer resolves the references against the
123
+ * pattern's own bindings (#3531): an earlier binding is in scope for a later
124
+ * default, so its name there is that prop, never an outer name. Set only
125
+ * when the default reads at least one; `parsed` keeps the identifiers, which
126
+ * a template backend reads as the earlier props' own template variables.
127
+ */
128
+ defaultParts?: PropDefaultPart[]
108
129
  /** When true, the parameter is a rest spread (`...args`) — emit must prepend `...`. */
109
130
  isRest?: boolean
110
131
  /**
@@ -118,6 +139,22 @@ export interface ParamInfo {
118
139
  sourceName?: string
119
140
  }
120
141
 
142
+ /**
143
+ * One piece of a destructure default that reads an earlier binding of its own
144
+ * pattern (`ParamInfo.defaultParts`): the default's text between the reads,
145
+ * and each read as the earlier prop it names. A client-JS emitter renders a
146
+ * `param` part as that prop's own live read (`_p.off ?? false`), so the
147
+ * default never names a binding the emitted scope lacks. A `param` part
148
+ * carries what that read needs, its own `defaultParts` included, so a chain
149
+ * (`{ a = 1, b = a, c = b }`) renders without a lookup.
150
+ */
151
+ export type PropDefaultPart =
152
+ | { kind: 'text'; text: JsSourceText }
153
+ | { kind: 'param'; param: PropDefaultRead }
154
+
155
+ /** The earlier binding a `param` part reads: the fields a live prop read needs. */
156
+ export type PropDefaultRead = Pick<ParamInfo, 'name' | 'sourceName' | 'defaultValue' | 'defaultContainsArrow' | 'defaultParts'>
157
+
121
158
  // =============================================================================
122
159
  // Staged-IR primitives: Phase / Scope / Effect (#1138)
123
160
  // =============================================================================
@@ -412,7 +449,7 @@ export interface IRText {
412
449
 
413
450
  export interface IRExpression {
414
451
  type: 'expression'
415
- expr: string
452
+ expr: JsSourceText
416
453
  /**
417
454
  * Pre-transformed expr with destructured prop refs rewritten to _p.xxx
418
455
  * (for client JS templates). Carries ONLY that rewrite — never an
@@ -459,6 +496,20 @@ export interface IRExpression {
459
496
  * unification A3) is its only wiring.
460
497
  */
461
498
  preambleRegion?: boolean
499
+ /**
500
+ * How the expression reads the component's own `children` prop (#3539):
501
+ * a bare read (`children` bound by the props destructure, renamed or not,
502
+ * `<propsObject>.children`, or a body alias such as `const { children: kids }
503
+ * = props`), that read on the left of `?? ''`, on the
504
+ * left of `?? <other fallback>`, or on the left of `|| <fallback>` (#3554).
505
+ * Decided once in Phase 1 from the binding,
506
+ * so a local `const children`, a loop row named `children`, or another
507
+ * object's `.children` is never mistaken for the prop. The children value
508
+ * is pre-rendered markup: adapters that cannot tell markup from a string
509
+ * by value (ERB) and the client template's child splice read this to skip
510
+ * escaping it, while a non-empty fallback stays escaped text.
511
+ */
512
+ childrenRead?: 'bare' | 'nullish-empty' | 'nullish-fallback' | 'or-fallback'
462
513
  /** When true, expression calls signal getters or memos (has reactive `foo()` pattern). */
463
514
  callsReactiveGetters?: boolean
464
515
  /** When true, expression contains function call(s) — any `identifier()` pattern (computed from AST). */
@@ -496,7 +547,7 @@ export interface IRExpression {
496
547
 
497
548
  export interface IRConditional {
498
549
  type: 'conditional'
499
- condition: string
550
+ condition: JsSourceText
500
551
  /** Pre-transformed condition with destructured prop refs rewritten to _p.xxx. */
501
552
  templateCondition?: string
502
553
  /**
@@ -562,7 +613,7 @@ export interface IRLoop {
562
613
  * inline via placeholder substitution — see `FlatMapCallback`.
563
614
  */
564
615
  method?: 'flatMap'
565
- array: string
616
+ array: JsSourceText
566
617
  /**
567
618
  * Structured parse of `array` (`parseExpression(array.trim())`), attached
568
619
  * during IR construction so adapters lower the loop's array from the tree
@@ -577,7 +628,7 @@ export interface IRLoop {
577
628
  itemType: TypeInfo | null
578
629
  param: string
579
630
  index: string | null
580
- key: string | null
631
+ key: JsSourceText | null
581
632
  children: IRNode[]
582
633
  slotId: string | null
583
634
  /**
@@ -1024,7 +1075,7 @@ export interface PreambleValueDeclaration {
1024
1075
  */
1025
1076
  export interface PreambleRegionSource {
1026
1077
  slotId: string
1027
- expr: string
1078
+ expr: JsSourceText
1028
1079
  joinArrayChild?: boolean
1029
1080
  }
1030
1081
 
@@ -1238,7 +1289,7 @@ export interface IRAsync {
1238
1289
  export interface IRIfStatement {
1239
1290
  type: 'if-statement'
1240
1291
  /** The condition expression (e.g., "name === 'github'") */
1241
- condition: string
1292
+ condition: JsSourceText
1242
1293
  /** Pre-transformed condition with destructured prop refs rewritten to _p.xxx. */
1243
1294
  templateCondition?: string
1244
1295
  /**
@@ -1337,10 +1388,10 @@ export interface LiteralAttr {
1337
1388
  export interface ExpressionAttr {
1338
1389
  kind: 'expression'
1339
1390
  /** Source-level JS expression text (e.g. `count()`, `props.checked`). */
1340
- expr: string
1391
+ expr: JsSourceText
1341
1392
  /** `expr` with destructured prop refs rewritten to `_p.xxx`, for SSR
1342
1393
  * template inlining. Absent when no rewrite was needed. */
1343
- templateExpr?: string
1394
+ templateExpr?: JsSourceText
1344
1395
  /**
1345
1396
  * Structured parse of `expr` (`parseExpression(expr.trim())`), attached
1346
1397
  * during IR construction so adapters lower the attribute value from the tree
@@ -1364,6 +1415,21 @@ export interface ExpressionAttr {
1364
1415
  * props.
1365
1416
  */
1366
1417
  parts?: IRTemplatePart[]
1418
+ /**
1419
+ * The author's source for `expr` with its TypeScript syntax intact
1420
+ * (`store as never`, `value!`, `(v: string) => …`), for adapters that emit
1421
+ * type-checked TSX (`preserveTypes`). Set only when `expr` came from a
1422
+ * type-stripped read of the source and the two texts differ. `forExpr` is
1423
+ * the `expr` it was captured beside: a later rewrite of `expr` makes them
1424
+ * differ, and the adapter then falls back to `expr`.
1425
+ */
1426
+ typed?: TypedExprSource
1427
+ }
1428
+
1429
+ /** See {@link ExpressionAttr.typed}. */
1430
+ export interface TypedExprSource {
1431
+ tsx: TsxSourceText
1432
+ forExpr: JsSourceText
1367
1433
  }
1368
1434
 
1369
1435
  export interface BooleanAttr {
@@ -1381,7 +1447,7 @@ export interface TemplateAttr {
1381
1447
 
1382
1448
  export interface SpreadAttr {
1383
1449
  kind: 'spread'
1384
- expr: string
1450
+ expr: JsSourceText
1385
1451
  templateExpr?: string
1386
1452
  /**
1387
1453
  * Structured parse of `expr` (`parseExpression(expr.trim())`), attached
@@ -1421,7 +1487,12 @@ export const AttrValueOf = {
1421
1487
  },
1422
1488
  expression(
1423
1489
  expr: string,
1424
- opts?: { templateExpr?: string; presenceOrUndefined?: boolean; parts?: IRTemplatePart[] },
1490
+ opts?: {
1491
+ templateExpr?: string
1492
+ presenceOrUndefined?: boolean
1493
+ parts?: IRTemplatePart[]
1494
+ typed?: TypedExprSource
1495
+ },
1425
1496
  ): ExpressionAttr {
1426
1497
  return {
1427
1498
  kind: 'expression',
@@ -1429,6 +1500,7 @@ export const AttrValueOf = {
1429
1500
  ...(opts?.templateExpr !== undefined && { templateExpr: opts.templateExpr }),
1430
1501
  ...(opts?.presenceOrUndefined !== undefined && { presenceOrUndefined: opts.presenceOrUndefined }),
1431
1502
  ...(opts?.parts !== undefined && { parts: opts.parts }),
1503
+ ...(opts?.typed !== undefined && { typed: opts.typed }),
1432
1504
  }
1433
1505
  },
1434
1506
  booleanAttr(): BooleanAttr {
@@ -1550,7 +1622,12 @@ export interface IRAttribute extends AttrMeta {
1550
1622
  export interface IREvent {
1551
1623
  name: string // 'click', 'input', 'keydown'
1552
1624
  originalAttr?: string // Original JSX attribute name: 'onClick', 'onKeyDown'
1553
- handler: string // JS expression: '() => setCount(n => n + 1)'
1625
+ /**
1626
+ * JS expression: '() => setCount(n => n + 1)'. Always a single expression
1627
+ * (a comma sequence arrives parenthesized), passed verbatim as the
1628
+ * listener argument.
1629
+ */
1630
+ handler: JsSourceText
1554
1631
  loc: SourceLocation
1555
1632
  }
1556
1633
 
@@ -1578,10 +1655,22 @@ export interface IRProp extends AttrMeta {
1578
1655
  // Metadata
1579
1656
  // =============================================================================
1580
1657
 
1658
+ /** How a signal's initial value reads a prop (`SignalInfo.propSeed`). */
1659
+ export interface SignalPropSeed {
1660
+ /** The prop read: the first property after the props object, or the destructured binding. */
1661
+ propName: string
1662
+ via: 'member' | 'binding'
1663
+ /** True when the read goes past the prop (`props.row.count`, `props.items[0]`). */
1664
+ nested: boolean
1665
+ /** Whether `propName` is one of the component's declared props (`propsParams`). */
1666
+ declared: boolean
1667
+ fallback: { op: '??' | '||'; expr: ParsedExpr } | null
1668
+ }
1669
+
1581
1670
  export interface SignalInfo {
1582
1671
  getter: string
1583
1672
  setter: string | null
1584
- initialValue: string
1673
+ initialValue: JsSourceText
1585
1674
  /**
1586
1675
  * `initialValue` parsed into a structured tree (Roadmap A). Attached
1587
1676
  * best-effort by the analyzer so adapters can lower a literal initial value
@@ -1590,6 +1679,16 @@ export interface SignalInfo {
1590
1679
  * consumers fall back to parsing `initialValue`.
1591
1680
  */
1592
1681
  parsed?: ParsedExpr
1682
+ /**
1683
+ * Set when `initialValue` is a read of one prop, bare or with a single
1684
+ * `??` / `||` fallback (`props.checked`, `checked ?? false`,
1685
+ * `props.value || ''`, `props.row.count`), recorded by the analyzer from
1686
+ * `parsed`. `via` is
1687
+ * how the prop is read: a member of the props object, or a binding a props
1688
+ * destructure introduced. The CSR init body asks this field, not the text,
1689
+ * whether the signal seeds from a prop and what default it keeps (#3508).
1690
+ */
1691
+ propSeed?: SignalPropSeed
1593
1692
  /** Initial value with TypeScript type annotations preserved, for .tsx output */
1594
1693
  typedInitialValue?: string
1595
1694
  /**
@@ -1703,6 +1802,18 @@ export function isElidedFactoryGetter(signal: Pick<SignalInfo, 'factory' | 'gett
1703
1802
  return Boolean(signal.factory && signal.getterElided)
1704
1803
  }
1705
1804
 
1805
+ /**
1806
+ * Whether a props param binds the component's own `children` prop. Keyed by
1807
+ * the caller-facing key (`sourceName ?? name`), so a binding renamed in the
1808
+ * destructure (`{ children: kids }`) is the children prop exactly as
1809
+ * `{ children }` is (#3572). The one answer for every site that sets the
1810
+ * children prop apart from the data props: it is content the parent
1811
+ * renders, not a reactive value, a client-side prop read or hydration data.
1812
+ */
1813
+ export function isChildrenPropParam(p: Pick<ParamInfo, 'name' | 'sourceName'>): boolean {
1814
+ return (p.sourceName ?? p.name) === 'children'
1815
+ }
1816
+
1706
1817
  /**
1707
1818
  * The async reactive factory call a signal's value comes from (#3165). Every
1708
1819
  * backend that re-emits the declaration reads this instead of assuming
@@ -1736,16 +1847,32 @@ export interface SignalFactoryCall {
1736
1847
 
1737
1848
  export interface MemoInfo {
1738
1849
  name: string
1739
- computation: string
1850
+ computation: JsSourceText
1740
1851
  /**
1741
- * `computation` with bare destructured-prop refs rewritten to `_p.X` —
1852
+ * The expression the memo evaluates to, read from the callback node at
1853
+ * analysis time (#3510): the body of a zero-parameter arrow or `function`
1854
+ * expression (`() => e`, `() => { return e }`, `function () { return e }`),
1855
+ * or a call of the whole callback (`(() => { … })()`) for any other shape.
1856
+ * Type-stripped. Consumers substitute it for `memo()` instead of matching
1857
+ * `computation` against `() =>`. Unset only when `createMemo` has no
1858
+ * argument.
1859
+ */
1860
+ bodyExpr?: string
1861
+ /**
1862
+ * Set when `bodyExpr` is a call of the whole callback rather than the
1863
+ * expression the callback returns — a multi-statement body, or a callback
1864
+ * that is not a plain zero-parameter function.
1865
+ */
1866
+ bodyExprIsCall?: boolean
1867
+ /**
1868
+ * `bodyExpr` with bare destructured-prop refs rewritten to `_p.X` —
1742
1869
  * the memo twin of `SignalInfo.templateInitialValue` (#2265). Consumed by
1743
1870
  * `buildSignalMemoEnv` when the memo body is inlined into the module-scope
1744
1871
  * CSR `template:` arrow, which is not a closure over init's destructured
1745
1872
  * extractions (#2468). Only set for destructured-arg components that
1746
1873
  * actually reference a prop.
1747
1874
  */
1748
- templateComputation?: string
1875
+ templateBodyExpr?: string
1749
1876
  /** Computation with TypeScript type annotations preserved, for .tsx output */
1750
1877
  typedComputation?: string
1751
1878
  /**
@@ -1810,7 +1937,7 @@ export interface MemoInfo {
1810
1937
  }
1811
1938
 
1812
1939
  export interface EffectInfo {
1813
- body: string
1940
+ body: JsSourceText
1814
1941
  deps: string[]
1815
1942
  /**
1816
1943
  * When set, the effect was captured as `const <captureName> = createEffect(...)`
@@ -1823,7 +1950,7 @@ export interface EffectInfo {
1823
1950
  }
1824
1951
 
1825
1952
  export interface OnMountInfo {
1826
- body: string
1953
+ body: JsSourceText
1827
1954
  loc: SourceLocation
1828
1955
  }
1829
1956
 
@@ -1840,7 +1967,7 @@ export interface OnMountInfo {
1840
1967
  */
1841
1968
  export interface InitStatementInfo {
1842
1969
  /** Raw JS source of the statement (TypeScript types already stripped). */
1843
- body: string
1970
+ body: JsSourceText
1844
1971
  loc: SourceLocation
1845
1972
  /**
1846
1973
  * Free identifier references used by this statement. Used by the emitter
@@ -2023,7 +2150,7 @@ export interface ImportSpecifier {
2023
2150
  export interface FunctionInfo {
2024
2151
  name: string
2025
2152
  params: ParamInfo[]
2026
- body: string
2153
+ body: JsSourceText
2027
2154
  /** Body with TypeScript type annotations preserved, for .tsx output */
2028
2155
  typedBody?: string
2029
2156
  /**
@@ -2082,7 +2209,7 @@ export interface FunctionInfo {
2082
2209
 
2083
2210
  export interface ConstantInfo {
2084
2211
  name: string
2085
- value?: string
2212
+ value?: JsSourceText
2086
2213
  /**
2087
2214
  * `value` parsed into a structured tree (Roadmap A). Attached best-effort by
2088
2215
  * the analyzer (parsed from the parenthesised value so a bare object literal
@@ -2119,6 +2246,16 @@ export interface ConstantInfo {
2119
2246
  isJsxFunction?: boolean
2120
2247
  /** When true, the initializer contains an arrow function or function expression (computed from AST). */
2121
2248
  containsArrow?: boolean
2249
+ /**
2250
+ * Set when this const is a binding of a body destructure of the props
2251
+ * object (`const { label = 'Default' } = props`), recorded by the analyzer
2252
+ * from the binding pattern itself. `sourceKey` is the prop it reads (the
2253
+ * property key, which differs from `name` for a renamed binding). Its
2254
+ * `value` text (`props.label ?? 'Default'`) is the same as a hand-written
2255
+ * `const label = props.label ?? 'Default'`, so consumers ask this field,
2256
+ * not the text, whether the const IS the prop (#3501).
2257
+ */
2258
+ propsDestructure?: { sourceKey: string }
2122
2259
  /**
2123
2260
  * When true, the initializer IS an arrow function or function expression
2124
2261
  * (`const status = () => …`), so reading the constant yields a function and
@@ -2168,6 +2305,13 @@ export interface TypeDefinition {
2168
2305
  * type aliases that aren't object types (e.g. string-literal unions).
2169
2306
  */
2170
2307
  properties?: PropertyInfo[]
2308
+ /**
2309
+ * The members of a type alias whose type is a string literal or a union of
2310
+ * string literals only (`type Mode = 'light' | 'dark'`, in any quoting or
2311
+ * layout). Absent for every other definition, a union mixing in a
2312
+ * non-string member included, so adapters need not re-read `definition`.
2313
+ */
2314
+ stringLiteralUnion?: string[]
2171
2315
  loc: SourceLocation
2172
2316
  }
2173
2317
 
@@ -128,3 +128,135 @@ export function collectValueReferencedNames(code: string): Set<string> | null {
128
128
  visit(sourceFile)
129
129
  return names
130
130
  }
131
+
132
+ /**
133
+ * The nodes that open a binding scope in emitted JS: the module, every
134
+ * function-like node and class static block (its parameters and hoisted
135
+ * `var` / function names),
136
+ * blocks, `for` heads, `case` blocks and `catch` clauses (`let` / `const` /
137
+ * class names), and class expressions (their own name).
138
+ */
139
+ function isScopeNode(node: ts.Node): boolean {
140
+ return (
141
+ isVarScopeNode(node) ||
142
+ ts.isBlock(node) ||
143
+ ts.isForStatement(node) ||
144
+ ts.isForInStatement(node) ||
145
+ ts.isForOfStatement(node) ||
146
+ ts.isCaseBlock(node) ||
147
+ ts.isCatchClause(node) ||
148
+ ts.isClassExpression(node)
149
+ )
150
+ }
151
+
152
+ /**
153
+ * The nodes a `var` (and a parameter) binds into: the module, every
154
+ * function-like node, and a class static block, whose `var`s stay inside
155
+ * it (#3537 review).
156
+ */
157
+ function isVarScopeNode(node: ts.Node): boolean {
158
+ return ts.isSourceFile(node) || ts.isFunctionLike(node) || ts.isClassStaticBlockDeclaration(node)
159
+ }
160
+
161
+ function nearestScope(from: ts.Node, functionScoped: boolean): ts.Node {
162
+ let node: ts.Node | undefined = from
163
+ while (node) {
164
+ if (functionScoped ? isVarScopeNode(node) : isScopeNode(node)) return node
165
+ node = node.parent
166
+ }
167
+ return from.getSourceFile()
168
+ }
169
+
170
+ function bindingNames(name: ts.BindingName, out: string[]): string[] {
171
+ if (ts.isIdentifier(name)) {
172
+ out.push(name.text)
173
+ } else {
174
+ for (const element of name.elements) {
175
+ if (ts.isBindingElement(element)) bindingNames(element.name, out)
176
+ }
177
+ }
178
+ return out
179
+ }
180
+
181
+ /** The scope each declaration in `sourceFile` binds its names into. */
182
+ function collectScopeBindings(sourceFile: ts.SourceFile): Map<ts.Node, Set<string>> {
183
+ const bindings = new Map<ts.Node, Set<string>>()
184
+ const bind = (scope: ts.Node, names: readonly string[]): void => {
185
+ let set = bindings.get(scope)
186
+ if (!set) bindings.set(scope, (set = new Set()))
187
+ for (const name of names) set.add(name)
188
+ }
189
+ function visit(node: ts.Node): void {
190
+ if (ts.isVariableDeclaration(node)) {
191
+ const list = node.parent
192
+ const isVar = ts.isVariableDeclarationList(list) && (list.flags & ts.NodeFlags.BlockScoped) === 0
193
+ bind(nearestScope(list, isVar), bindingNames(node.name, []))
194
+ } else if (ts.isParameter(node)) {
195
+ bind(node.parent, bindingNames(node.name, []))
196
+ } else if ((ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node)) && node.name) {
197
+ bind(nearestScope(node.parent, false), [node.name.text])
198
+ } else if ((ts.isFunctionExpression(node) || ts.isClassExpression(node)) && node.name) {
199
+ bind(node, [node.name.text])
200
+ } else if (ts.isCatchClause(node) && node.variableDeclaration) {
201
+ bind(node, bindingNames(node.variableDeclaration.name, []))
202
+ node.block.statements.forEach(visit)
203
+ return
204
+ } else if (ts.isImportClause(node) && node.name) {
205
+ bind(sourceFile, [node.name.text])
206
+ } else if (ts.isNamespaceImport(node) || ts.isImportSpecifier(node)) {
207
+ bind(sourceFile, [node.name.text])
208
+ }
209
+ ts.forEachChild(node, visit)
210
+ }
211
+ visit(sourceFile)
212
+ return bindings
213
+ }
214
+
215
+ /** What {@link analyzeEmittedReferences} reports about one emitted module. */
216
+ export interface EmittedReferences {
217
+ /** Value references that no enclosing scope binds: imports or globals. */
218
+ free: Set<string>
219
+ /** Names called directly (`name(…)`), bound or not. */
220
+ called: Set<string>
221
+ }
222
+
223
+ /**
224
+ * Parse emitted client JS and report its unbound value references and its
225
+ * directly called names.
226
+ *
227
+ * Unlike {@link collectValueReferencedNames}, `free` resolves scopes, so a
228
+ * name the module declares itself (a module-level helper emitted as
229
+ * `var insert = …`, a parameter, a block-scoped `const`) is not reported
230
+ * where that declaration is visible (#3537). Hoisting is modelled per
231
+ * scope, not per position: a binding counts for its whole scope.
232
+ *
233
+ * Returns `null` when the text did not parse cleanly, with the same
234
+ * "cannot answer" contract as {@link collectValueReferencedNames}.
235
+ */
236
+ export function analyzeEmittedReferences(code: string): EmittedReferences | null {
237
+ let sourceFile: ts.SourceFile
238
+ try {
239
+ sourceFile = ts.createSourceFile('generated.js', code, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS)
240
+ } catch {
241
+ return null
242
+ }
243
+ const diagnostics = (sourceFile as unknown as { parseDiagnostics?: readonly unknown[] }).parseDiagnostics
244
+ if (diagnostics && diagnostics.length > 0) return null
245
+
246
+ const bindings = collectScopeBindings(sourceFile)
247
+ const isBound = (id: ts.Identifier): boolean => {
248
+ for (let node: ts.Node | undefined = id.parent; node; node = node.parent) {
249
+ if (bindings.get(node)?.has(id.text)) return true
250
+ }
251
+ return false
252
+ }
253
+ const free = new Set<string>()
254
+ const called = new Set<string>()
255
+ function visit(node: ts.Node): void {
256
+ if (ts.isIdentifier(node) && isValueReferenceIdentifier(node) && !isBound(node)) free.add(node.text)
257
+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) called.add(node.expression.text)
258
+ ts.forEachChild(node, visit)
259
+ }
260
+ visit(sourceFile)
261
+ return { free, called }
262
+ }