@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
@@ -5,15 +5,19 @@
5
5
  * Every DSL adapter assembles them the same way: callback props and `ref`
6
6
  * are client-only and dropped; a named JSX-valued prop is captured as
7
7
  * HTML; `{...props}` (the component's own props object, which no DSL scope
8
- * holds as one value) enumerates the props params; any other spread is
9
- * folded in at its source position, so a later entry wins on a key
10
- * conflict, as in `{...a, ...b}`; then the compiler-controlled `_bf_slot`
11
- * and `children` entries come last, so a spread can never override them.
8
+ * holds as one value) enumerates the props params, and a param that
9
+ * something earlier in the call may have set is carried only when the
10
+ * caller passed it ({@link propsSpreadParam}); any other spread is folded
11
+ * in at its source position, so a later entry wins on a key conflict, as in
12
+ * `{...a, ...b}`; then the compiler-controlled `_bf_slot` and `children`
13
+ * entries come last, so a spread can never override them.
12
14
  * {@link planComponentProps} makes those decisions once. An adapter spells
13
15
  * the entries and spreads through {@link spellComponentProps}, keeps its
14
16
  * own capture mechanism (`{% set %}…{% endset %}`, `_erbout.slice!`,
15
17
  * `begin … end`, a Kolon macro), and folds the spelled segments into its
16
- * own hash literal.
18
+ * own hash literal. An adapter that builds the child's input as a typed
19
+ * literal at compile time instead (Go's `<Child>Input{...}`) reads the same
20
+ * plan one key at a time through {@link resolveComponentPropKeys}.
17
21
  */
18
22
 
19
23
  import { resolveJsxChildrenProp } from '@barefootjs/shared'
@@ -33,16 +37,60 @@ export type ComponentPropEntry =
33
37
  /** The forwarded children (nested `<Box>…</Box>`, or `children={<jsx/>}`): captured as HTML. */
34
38
  | { kind: 'children'; children: IRNode[] }
35
39
 
36
- /** A run of entries, or a spread folded in at its source position. */
40
+ /**
41
+ * A run of entries, a spread folded in at its source position, or one param
42
+ * of `{...props}` that the spread carries only when the caller passed it
43
+ * ({@link propsSpreadParam}). An adapter folds a `present-param` in like a
44
+ * spread of a one-key (or empty) hash.
45
+ */
37
46
  export type ComponentPropSegment =
38
47
  | { kind: 'entries'; entries: ComponentPropEntry[] }
39
48
  | { kind: 'spread'; value: SpreadAttr }
49
+ | { kind: 'present-param'; name: string }
50
+
51
+ /** A props param as `{...props}` enumerates it. */
52
+ export interface ComponentPropsParam {
53
+ name: string
54
+ /** `false` for a required param, which the caller always passes. Unknown counts as optional. */
55
+ optional?: boolean
56
+ }
40
57
 
41
58
  export interface ComponentPropsOptions {
42
59
  /** The SolidJS-style props identifier (`function C(props: P)`), if any. */
43
60
  propsObjectName: string | null
44
61
  /** The analyzer-extracted props params, which `{...props}` enumerates. */
45
- propsParams: ReadonlyArray<{ name: string }>
62
+ propsParams: ReadonlyArray<ComponentPropsParam>
63
+ }
64
+
65
+ /** What a call has set before a `{...props}` spread, as far as the compiler can see. */
66
+ export interface EarlierComponentProps {
67
+ /** The keys an earlier entry sets. */
68
+ keys: ReadonlySet<string>
69
+ /** Whether an earlier spread whose keys are unknown may have set any key. */
70
+ spread: boolean
71
+ }
72
+
73
+ /**
74
+ * How `{...props}` carries one param. JS spreads only the keys the props
75
+ * object has, so a param the caller left out keeps whatever an earlier entry
76
+ * or spread set: `<Tag size="s" {...props} />` keeps `size="s"` when the
77
+ * caller passes no `size`.
78
+ *
79
+ * - `'entry'`: an ordinary entry. Either nothing earlier can set the key, so
80
+ * an absent param and a nil one read the same to the child, or the param is
81
+ * required, so the caller always passes it and it always wins.
82
+ * - `'present'`: carried only when the caller passed it, so an absent param
83
+ * leaves the earlier value alone.
84
+ *
85
+ * A template backend reads an absent prop and an explicit `undefined` or
86
+ * `null` one as the same nil, so "passed" is spelled "not nil" there: a
87
+ * caller that passes `undefined`/`null` keeps the earlier value, where JS
88
+ * would override it with the nil (limitation
89
+ * `props-spread-explicit-null-override`).
90
+ */
91
+ export function propsSpreadParam(param: ComponentPropsParam, earlier: EarlierComponentProps): 'entry' | 'present' {
92
+ if (param.optional === false) return 'entry'
93
+ return earlier.spread || earlier.keys.has(param.name) ? 'present' : 'entry'
46
94
  }
47
95
 
48
96
  /** The key an entry sets in the props hash. */
@@ -54,12 +102,17 @@ export function componentPropKey(entry: ComponentPropEntry): string {
54
102
 
55
103
  /**
56
104
  * The ordered props segments for `comp`. The first segment is always an
57
- * (possibly empty) entries segment, and a spread is followed by a new one
58
- * only when an entry comes after it.
105
+ * (possibly empty) entries segment, and a spread or a `present-param` is
106
+ * followed by a new one only when an entry comes after it.
59
107
  */
60
- export function planComponentProps(comp: IRComponent, options: ComponentPropsOptions): ComponentPropSegment[] {
108
+ export function planComponentProps(
109
+ comp: Pick<IRComponent, 'props' | 'children' | 'slotId' | 'loopItemRoot'>,
110
+ options: ComponentPropsOptions,
111
+ ): ComponentPropSegment[] {
61
112
  const segments: ComponentPropSegment[] = [{ kind: 'entries', entries: [] }]
113
+ const earlier = { keys: new Set<string>(), spread: false }
62
114
  const push = (entry: ComponentPropEntry) => {
115
+ earlier.keys.add(componentPropKey(entry))
63
116
  const last = segments[segments.length - 1]!
64
117
  if (last.kind !== 'entries') {
65
118
  segments.push({ kind: 'entries', entries: [entry] })
@@ -72,6 +125,19 @@ export function planComponentProps(comp: IRComponent, options: ComponentPropsOpt
72
125
  if (at === -1) last.entries.push(entry)
73
126
  else last.entries[at] = entry
74
127
  }
128
+ const pushPropsSpread = () => {
129
+ // Every param is decided against what came before the spread, not
130
+ // against the params this same spread has already pushed.
131
+ const before: EarlierComponentProps = { keys: new Set(earlier.keys), spread: earlier.spread }
132
+ for (const param of options.propsParams) {
133
+ if (propsSpreadParam(param, before) === 'entry') {
134
+ push({ kind: 'props-param', name: param.name })
135
+ } else {
136
+ earlier.keys.add(param.name)
137
+ segments.push({ kind: 'present-param', name: param.name })
138
+ }
139
+ }
140
+ }
75
141
  for (const p of comp.props) {
76
142
  // Callback props and `ref` are client-only for SSR: Hono renders
77
143
  // neither, and the client JS wires them at hydration.
@@ -81,8 +147,9 @@ export function planComponentProps(comp: IRComponent, options: ComponentPropsOpt
81
147
  if (p.name !== 'children') push({ kind: 'named-slot', name: p.name, children: p.value.children })
82
148
  } else if (p.value.kind === 'spread') {
83
149
  if (options.propsObjectName !== null && options.propsObjectName === p.value.expr.trim()) {
84
- for (const param of options.propsParams) push({ kind: 'props-param', name: param.name })
150
+ pushPropsSpread()
85
151
  } else {
152
+ earlier.spread = true
86
153
  segments.push({ kind: 'spread', value: p.value })
87
154
  }
88
155
  } else {
@@ -96,7 +163,7 @@ export function planComponentProps(comp: IRComponent, options: ComponentPropsOpt
96
163
  return segments
97
164
  }
98
165
 
99
- /** A spelled segment: the entries' hash-literal parts, or the spread's expression. */
166
+ /** A spelled segment: the entries' hash-literal parts, or the expression a spread folds in. */
100
167
  export type SpelledComponentPropSegment = { kind: 'entries'; parts: string[] } | { kind: 'spread'; expr: string }
101
168
 
102
169
  export interface ComponentPropSpelling {
@@ -104,6 +171,12 @@ export interface ComponentPropSpelling {
104
171
  entry(entry: ComponentPropEntry): string
105
172
  /** The expression a spread folds in. */
106
173
  spread(value: SpreadAttr): string
174
+ /**
175
+ * The expression a `present-param` segment folds in like a spread: a
176
+ * one-key hash `{ name: <param> }` when the param is not nil, an empty
177
+ * hash when it is.
178
+ */
179
+ presentParam(name: string): string
107
180
  }
108
181
 
109
182
  /**
@@ -116,6 +189,7 @@ export function spellComponentProps(
116
189
  ): SpelledComponentPropSegment[] {
117
190
  return segments.map((segment) => {
118
191
  if (segment.kind === 'spread') return { kind: 'spread', expr: spelling.spread(segment.value) }
192
+ if (segment.kind === 'present-param') return { kind: 'spread', expr: spelling.presentParam(segment.name) }
119
193
  const parts: string[] = []
120
194
  for (const entry of segment.entries) {
121
195
  const part = spelling.entry(entry)
@@ -125,6 +199,50 @@ export function spellComponentProps(
125
199
  })
126
200
  }
127
201
 
202
+ /**
203
+ * One key of the props hash, resolved at compile time: the latest of
204
+ * `presentParams` the caller passed, else `base`.
205
+ */
206
+ export interface ResolvedComponentProp {
207
+ key: string
208
+ /** The last entry that sets the key unconditionally, or null when none does. */
209
+ base: ComponentPropEntry | null
210
+ /** The `{...props}` params that override `base` when passed, earliest first. */
211
+ presentParams: string[]
212
+ }
213
+
214
+ /**
215
+ * A plan resolved one key at a time, in the order the keys are first set,
216
+ * for an adapter that builds the child's input as a typed literal at
217
+ * compile time rather than merging hashes at render time. Null when the
218
+ * plan folds in a spread whose keys are unknown: only a render-time merge
219
+ * can resolve that.
220
+ */
221
+ export function resolveComponentPropKeys(segments: readonly ComponentPropSegment[]): ResolvedComponentProp[] | null {
222
+ const byKey = new Map<string, ResolvedComponentProp>()
223
+ const at = (key: string): ResolvedComponentProp => {
224
+ let resolved = byKey.get(key)
225
+ if (!resolved) {
226
+ resolved = { key, base: null, presentParams: [] }
227
+ byKey.set(key, resolved)
228
+ }
229
+ return resolved
230
+ }
231
+ for (const segment of segments) {
232
+ if (segment.kind === 'spread') return null
233
+ if (segment.kind === 'present-param') {
234
+ at(segment.name).presentParams.push(segment.name)
235
+ continue
236
+ }
237
+ for (const entry of segment.entries) {
238
+ const resolved = at(componentPropKey(entry))
239
+ resolved.base = entry
240
+ resolved.presentParams = []
241
+ }
242
+ }
243
+ return [...byKey.values()]
244
+ }
245
+
128
246
  /** Whether a spelled plan passes no props at all. */
129
247
  export function isEmptyComponentProps(segments: readonly SpelledComponentPropSegment[]): boolean {
130
248
  return segments.every((segment) => segment.kind === 'entries' && segment.parts.length === 0)
@@ -0,0 +1,185 @@
1
+ /**
2
+ * A component- or module-scope `const` computed from props, signals or other
3
+ * consts (`const cls = 'btn ' + variant`, `const twice = props.n * 2`) has no
4
+ * binding in a template: the backend receives the props, not the
5
+ * component's locals. Every template adapter read such a const as a template
6
+ * variable nothing defines, so it rendered empty, a prop of the same name, or
7
+ * failed the render (#3494).
8
+ *
9
+ * `inlineComputedConsts` substitutes each such read with the const's
10
+ * initializer tree, exactly as if the source had written the expression in
11
+ * place; the adapter then lowers it like any other expression, refusing what
12
+ * it cannot lower (`Object.keys(labels)` has no template form, BF101) instead
13
+ * of reading an unbound name. The substitution is structural
14
+ * (`inlineBinding`), so precedence, string literals and callback params that
15
+ * shadow the name are respected.
16
+ */
17
+
18
+ import { collectContextConsumers, lookupLiteralConst } from '../augment-inherited-props.ts'
19
+ import { freeIdentifiers, inlineBinding, parseExpression, stringifyParsedExpr, type ParsedExpr } from '../expression-parser.ts'
20
+ import type { ConstantInfo, ExpressionAttr, IRMetadata } from '../types.ts'
21
+ import { componentBindings } from '../relocate.ts'
22
+
23
+ export interface ComputedConstContext {
24
+ constants: IRMetadata['localConstants'] | undefined
25
+ /** Signal getters and memo names: the zero-arg calls a template reads as values. */
26
+ getters: ReadonlySet<string>
27
+ /** Context consumers (`const theme = useContext(ThemeContext)`), which the adapter seeds itself. */
28
+ contextConsumers: ReadonlySet<string>
29
+ /** Consts a props destructure binds, which the adapter binds as props params. */
30
+ propsBindings: ReadonlySet<string>
31
+ /** Whether a loop row or callback binds `name` where the expression is read. */
32
+ isShadowed(name: string): boolean
33
+ /** Whether the adapter binds the const `name` itself, so a read of it stays as written. */
34
+ isAdapterBound(name: string): boolean
35
+ /**
36
+ * Whether `name` read where the expression is read is a JavaScript global:
37
+ * bound by neither the component nor its module (`componentBindings`), nor
38
+ * the rest props, nor an enclosing loop or callback.
39
+ */
40
+ isGlobal(name: string): boolean
41
+ }
42
+
43
+ /** The {@link ComputedConstContext} fields that come from the component's metadata. */
44
+ export function computedConstContext(
45
+ metadata: IRMetadata,
46
+ isShadowed: (name: string) => boolean,
47
+ isAdapterBound: (name: string) => boolean = () => false,
48
+ ): ComputedConstContext {
49
+ const bindings = componentBindings(metadata)
50
+ return {
51
+ constants: metadata.localConstants,
52
+ getters: new Set([...metadata.signals.map(s => s.getter), ...metadata.memos.map(m => m.name)]),
53
+ contextConsumers: new Set(collectContextConsumers(metadata).map(c => c.localName)),
54
+ propsBindings: propsBindingNames(metadata),
55
+ isShadowed,
56
+ isAdapterBound,
57
+ isGlobal: name => !bindings.has(name) && name !== metadata.restPropsName && !isShadowed(name),
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Consts a props destructure binds (`const { label: text = 'none' } = props`).
63
+ * The analyzer records each as a props param, renamed and defaulted, and the
64
+ * adapters bind it as one: its default applies to `undefined` alone, which
65
+ * the `??` of its recorded value would not keep on a backend whose `??`
66
+ * also tests `''`. A const merely named like a prop
67
+ * (`const label = props.label ?? 'Default'`, the prop itself carrying no
68
+ * default) is not one.
69
+ */
70
+ function propsBindingNames(metadata: IRMetadata): Set<string> {
71
+ const names = new Set<string>()
72
+ const props = metadata.propsObjectName
73
+ if (!props) return names
74
+ for (const constant of metadata.localConstants ?? []) {
75
+ const param = metadata.propsParams.find(p => p.name === constant.name)
76
+ const value = constant.value?.trim()
77
+ if (!param || value === undefined) continue
78
+ const read = `${props}.${param.sourceName ?? param.name}`
79
+ if (value === read || (param.defaultValue !== undefined && value === `${read} ?? ${param.defaultValue}`)) {
80
+ names.add(constant.name)
81
+ }
82
+ }
83
+ return names
84
+ }
85
+
86
+ /** Upper bound on const-of-const chains, a guard against a malformed cycle. */
87
+ const MAX_DEPTH = 16
88
+
89
+ /**
90
+ * `expr` with each read of a computed const replaced by its initializer,
91
+ * repeated so a const read inside an initializer is replaced too.
92
+ */
93
+ export function inlineComputedConsts(expr: ParsedExpr, ctx: ComputedConstContext | null): ParsedExpr {
94
+ if (!ctx?.constants?.length) return expr
95
+ let current = expr
96
+ for (let depth = 0; depth < MAX_DEPTH; depth++) {
97
+ const next = inlineOnce(current, ctx)
98
+ if (next === current) return current
99
+ current = next
100
+ }
101
+ return current
102
+ }
103
+
104
+ function inlineOnce(expr: ParsedExpr, ctx: ComputedConstContext): ParsedExpr {
105
+ const free = freeIdentifiers(expr)
106
+ if (!free) return expr
107
+ let result = expr
108
+ for (const name of free) {
109
+ const init = inlinableInitializer(name, ctx)
110
+ if (!init) continue
111
+ result = inlineBinding(result, name, init) ?? result
112
+ }
113
+ return result
114
+ }
115
+
116
+ /**
117
+ * An `{expr}` attribute value read through {@link inlineComputedConsts}, so
118
+ * the attribute's own plan (a nullable prop read drops the attribute) sees
119
+ * the const's initializer, not its name: `title={label}` with
120
+ * `const label = props.label ?? 'Default'` is not a bare read of `label`.
121
+ */
122
+ export function inlineComputedConstAttr(value: ExpressionAttr, ctx: ComputedConstContext | null): ExpressionAttr {
123
+ if (!ctx) return value
124
+ const parsed = value.parsed ?? parseExpression(value.expr.trim())
125
+ const inlined = inlineComputedConsts(parsed, ctx)
126
+ if (inlined === parsed) return value
127
+ // `templateExpr` and `parts` are derived from the original `expr`; dropped
128
+ // so no consumer reads the const's name through them.
129
+ return { ...value, expr: stringifyParsedExpr(inlined), parsed: inlined, templateExpr: undefined, parts: undefined }
130
+ }
131
+
132
+ /** The initializer of a const read `name` stands for, or `null` when the read stays as it is. */
133
+ function inlinableInitializer(name: string, ctx: ComputedConstContext): ParsedExpr | null {
134
+ const constant = ctx.constants?.find(c => c.name === name)
135
+ const init = constant?.parsed
136
+ if (!constant || !init || !isPlainComputedConst(constant, init)) return null
137
+ if (ctx.isShadowed(name) || ctx.getters.has(name) || ctx.contextConsumers.has(name) || ctx.propsBindings.has(name)) return null
138
+ if (ctx.isAdapterBound(name)) return null
139
+ // An adapter already inlines a literal const in its own syntax.
140
+ if (lookupLiteralConst(name, ctx.constants, ctx.isShadowed) !== null) return null
141
+ // A free name the read site rebinds would be captured by the substitution.
142
+ const free = freeIdentifiers(init)
143
+ if (!free || [...free].some(n => ctx.isShadowed(n))) return null
144
+ return callsOnlyGetters(init, ctx.getters) ? init : null
145
+ }
146
+
147
+ /**
148
+ * A `const` holding a value computed by an expression, as opposed to a
149
+ * function, JSX, a static object or array the adapters resolve through their
150
+ * own lookups, or a runtime construct. A `let` no statement reassigns after
151
+ * its declaration holds one value too: its render-time reassignments were
152
+ * folded into the initializer (`fold-render-reassignments.ts`, #3501).
153
+ */
154
+ function isPlainComputedConst(constant: ConstantInfo, init: ParsedExpr): boolean {
155
+ if (constant.mutatedAfterDeclaration) return false
156
+ if (constant.isJsx || constant.isJsxFunction || constant.containsArrow || constant.isFunctionValue) return false
157
+ if (constant.systemConstructKind) return false
158
+ if (init.kind === 'call' && init.callee.kind === 'identifier' && init.callee.name === 'useContext') return false
159
+ return init.kind !== 'object-literal' && init.kind !== 'array-literal' && init.kind !== 'unsupported'
160
+ }
161
+
162
+ /**
163
+ * Whether every zero-arg identifier call in `init` is a signal or memo read.
164
+ * Any other one (`const label = makeLabel()`) is an opaque accessor: inlined,
165
+ * it would read as a getter of the helper's name, so the const keeps its own
166
+ * refusal path instead.
167
+ */
168
+ function callsOnlyGetters(init: ParsedExpr, getters: ReadonlySet<string>): boolean {
169
+ let ok = true
170
+ const visit = (node: unknown): void => {
171
+ if (!ok || node === null || typeof node !== 'object') return
172
+ if (Array.isArray(node)) {
173
+ node.forEach(visit)
174
+ return
175
+ }
176
+ const e = node as ParsedExpr
177
+ if (e.kind === 'call' && e.callee.kind === 'identifier' && e.args.length === 0 && !getters.has(e.callee.name)) {
178
+ ok = false
179
+ return
180
+ }
181
+ for (const value of Object.values(e)) visit(value)
182
+ }
183
+ visit(init)
184
+ return ok
185
+ }
@@ -243,3 +243,36 @@ export function dangerousInnerHtmlDiagnostic(
243
243
  },
244
244
  }
245
245
  }
246
+
247
+ /**
248
+ * The children `element`'s `dangerouslySetInnerHTML` renders in place of
249
+ * its own, or `null` when it has none. A refusal is pushed onto `errors`
250
+ * and renders as `''`. `rawSink` writes a dynamic value through the
251
+ * adapter's raw-output sink; the runtime evaluates the expression, so no
252
+ * template-metachar guard applies, and the element already carries its
253
+ * hydration slot marker (#2319). A static literal is spliced as trusted
254
+ * template text after the `adapterId` metachar guard.
255
+ */
256
+ export function renderDangerousInnerHtml(
257
+ element: IRElement,
258
+ errors: CompilerError[],
259
+ adapterId: string,
260
+ rawSink: (valueExpr: string, valueParsed: ParsedExpr, loc: SourceLocation) => string,
261
+ ): string | null {
262
+ const resolution = resolveDangerousInnerHtml(element)
263
+ if (!resolution) return null
264
+ if (resolution.kind === 'unlowerable') {
265
+ errors.push(dangerousInnerHtmlDiagnostic(resolution.expr, resolution.loc))
266
+ return ''
267
+ }
268
+ if (resolution.kind === 'dynamic') {
269
+ return rawSink(resolution.valueExpr, resolution.valueParsed, resolution.loc)
270
+ }
271
+ const violation = dangerousInnerHtmlMetacharViolation(resolution.html, adapterId)
272
+ if (violation) {
273
+ const attr = element.attrs.find(isDangerousInnerHtmlAttr)!
274
+ errors.push(dangerousInnerHtmlDiagnostic(`{ __html: ${JSON.stringify(resolution.html)} }`, attr.loc, violation))
275
+ return ''
276
+ }
277
+ return resolution.html
278
+ }