@openelement/compiler 1.0.0-alpha.15 → 1.0.0-alpha.16

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 (51) hide show
  1. package/package.json +3 -2
  2. package/src/compiler.d.ts +8 -6
  3. package/src/compiler.js +7 -6
  4. package/src/internal/compiler/compiled-artifact-class-candidates.d.ts +5 -30
  5. package/src/internal/compiler/compiled-artifact-class-candidates.js +18 -32
  6. package/src/internal/compiler/compiled-artifact-style-requests.d.ts +9 -7
  7. package/src/internal/compiler/compiled-artifact-style-requests.js +9 -7
  8. package/src/internal/compiler/plugin.d.ts +31 -50
  9. package/src/internal/compiler/plugin.js +41 -108
  10. package/src/internal/compiler/semantic-core/analyze-module.d.ts +1 -45
  11. package/src/internal/compiler/semantic-core/analyze-module.js +10 -405
  12. package/src/internal/compiler/semantic-core/attribute-classification.js +92 -0
  13. package/src/internal/compiler/semantic-core/compiler-diagnostics.js +2 -16
  14. package/src/internal/compiler/semantic-core/expression-lowering.js +469 -0
  15. package/src/internal/compiler/semantic-core/expression-registry.js +165 -0
  16. package/src/internal/compiler/semantic-core/function-frontend.js +1229 -0
  17. package/src/internal/compiler/semantic-core/island-policy-statement.js +52 -0
  18. package/src/internal/compiler/semantic-core/jsx-module-id.d.ts +4 -0
  19. package/src/internal/compiler/semantic-core/jsx-module-id.js +7 -0
  20. package/src/internal/compiler/semantic-core/lower-program.d.ts +51 -0
  21. package/src/internal/compiler/semantic-core/lower-program.js +186 -90
  22. package/src/internal/compiler/semantic-core/module-analysis.d.ts +28 -5
  23. package/src/internal/compiler/semantic-core/module-analysis.js +16 -17
  24. package/src/internal/compiler/semantic-core/parse-module.js +16 -16
  25. package/src/internal/compiler/semantic-core/scope-resolution.d.ts +208 -0
  26. package/src/internal/compiler/semantic-core/scope-resolution.js +1121 -0
  27. package/src/internal/compiler/semantic-core/server-component.d.ts +22 -7
  28. package/src/internal/compiler/semantic-core/server-component.js +163 -57
  29. package/src/internal/compiler/semantic-core/type-erasure.js +156 -0
  30. package/src/internal/compiler/semantic-ir/generated-element/compile.d.ts +61 -0
  31. package/src/internal/compiler/semantic-ir/generated-element/compile.js +137 -0
  32. package/src/internal/compiler/semantic-ir/generated-element/contract.d.ts +201 -0
  33. package/src/internal/compiler/semantic-ir/generated-element/contract.js +147 -0
  34. package/src/internal/compiler/semantic-ir/generated-element/emit.js +452 -0
  35. package/src/internal/compiler/semantic-ir/generated-element/site.js +1195 -0
  36. package/src/internal/compiler/semantic-ir/generated-leaf/claim-emission.js +1372 -0
  37. package/src/internal/compiler/semantic-ir/generated-leaf/codegen-literals.js +31 -0
  38. package/src/internal/compiler/semantic-ir/generated-leaf/component-emission.js +647 -0
  39. package/src/internal/compiler/semantic-ir/generated-leaf/emission.js +388 -0
  40. package/src/internal/compiler/semantic-ir/generated-leaf/generate.js +395 -0
  41. package/src/internal/compiler/semantic-ir/generated-leaf/instance-emission.js +865 -0
  42. package/src/internal/compiler/semantic-ir/generated-leaf/mount-emission.js +960 -0
  43. package/src/internal/compiler/semantic-ir/generated-leaf/server-emission.js +1011 -0
  44. package/src/internal/compiler/semantic-ir/generated-leaf/structure.js +714 -0
  45. package/src/internal/compiler/style-requests.d.ts +0 -8
  46. package/src/internal/compiler/style-requests.js +1 -14
  47. package/src/internal/compiler/semantic-core/compile.d.ts +0 -47
  48. package/src/internal/compiler/semantic-core/compile.js +0 -100
  49. package/src/internal/compiler/semantic-core/emit-module.js +0 -317
  50. package/src/internal/compiler/semantic-core/emit-program.js +0 -141
  51. package/src/internal/compiler/semantic-core/style-imports.js +0 -87
@@ -0,0 +1,960 @@
1
+ import { BOOLEAN_ATTRIBUTES } from "../../semantic-core/attribute-classification.js";
2
+ import { ADMITTED_ATTRIBUTE_CHANNELS, ATTRIBUTE_PATTERN, CLIENT_ONLY_ATTRIBUTE_CHANNELS, CONSUMED_ATTRIBUTE_CHANNELS, ERROR_REPORT_PROPERTY, IDENTIFIER_PATTERN, INTERNAL_PREFIX, MOUNT_SCOPE, MOUNT_SUFFIX, SVG_NAMESPACE, TAG_PATTERN, componentPropAdmitted, declareReads, dependencyNames, diag, emitDependencySubscriptions, emitLocalRefresh, moduleUsesErrorBoundary, nextName, originArgs, push, pushParts, readsEntryLocal, recordFor, registerRefresh } from "./emission.js";
3
+ import { assertTextBoundaryContext, componentProjectsChildren, directoryParentAllowed, emissionNodes, normalizeTextRuns, referencedRecipeIds, staticElementDirectory, textOrdinal, textSlotOrdinals } from "./structure.js";
4
+ import { LEAF_SETUP_SUFFIX, childContextField, childErrorBoundaryField, childErrorReportField, childHostElementField, childPropertyChangeField, retainedTransactionField, siteKeyLiteral } from "./instance-emission.js";
5
+ import { quoteGeneratedJavaScriptValue } from "./codegen-literals.js";
6
+ import { unsafeStreamFrameAttribute } from "@openelement/protocol/stream-frame-policy";
7
+ //#region src/internal/compiler/semantic-ir/generated-leaf/mount-emission.ts
8
+ const EVENT_HANDLER_INVALID = "OEIR_LEAF_EVENT_HANDLER_INVALID";
9
+ const REF_HANDLER_INVALID = "OEIR_LEAF_REF_HANDLER_INVALID";
10
+ /** Append the sink entries to an element variable (authored child order). */
11
+ function appendSinkToElement(state, sink, targetVar) {
12
+ for (const entry of sink) if (entry.kind === "node") push(state, `${targetVar}.appendChild(${entry.name});`);
13
+ else if (entry.kind === "projection") push(state, `for (const ${INTERNAL_PREFIX}child of ${entry.name}.nodes) ${targetVar}.appendChild(${INTERNAL_PREFIX}child);`);
14
+ else push(state, `for (const ${INTERNAL_PREFIX}child of ${entry.name}) ${targetVar}.appendChild(${INTERNAL_PREFIX}child);`);
15
+ }
16
+ /** Append the sink entries to a generated node-array variable. */
17
+ function appendSinkToArray(state, sink, targetVar) {
18
+ for (const entry of sink) if (entry.kind === "node") push(state, `${targetVar}.push(${entry.name});`);
19
+ else if (entry.kind === "projection") push(state, `for (const ${INTERNAL_PREFIX}child of ${entry.name}.nodes) ${targetVar}.push(${INTERNAL_PREFIX}child);`);
20
+ else push(state, `for (const ${INTERNAL_PREFIX}child of ${entry.name}) ${targetVar}.push(${INTERNAL_PREFIX}child);`);
21
+ }
22
+ /**
23
+ * Emit one nested generated builder function that returns a node array. The
24
+ * builder gets its own scope variable so its resources register on the branch /
25
+ * entry owner, and lowered expressions inside keep the M03 placement ledger.
26
+ */
27
+ function emitNodesBuilder(state, functionName, scopeParam, nodes, records, anchor) {
28
+ push(state, `function ${functionName}(${scopeParam}) {`, anchor);
29
+ state.indent += 1;
30
+ const outerScope = state.scopeVar;
31
+ const outerOrdinals = state.textOrdinals;
32
+ const prepared = emissionNodes(state, nodes);
33
+ state.scopeVar = scopeParam;
34
+ state.textOrdinals = textSlotOrdinals(prepared);
35
+ push(state, `const ${INTERNAL_PREFIX}nodes = [];`);
36
+ const outerCollector = state.refreshStack.length;
37
+ state.refreshStack.push([]);
38
+ try {
39
+ for (const node of prepared) {
40
+ const sink = [];
41
+ emitNode(state, node, sink, records);
42
+ appendSinkToArray(state, sink, `${INTERNAL_PREFIX}nodes`);
43
+ }
44
+ } finally {
45
+ state.refreshStack.length = outerCollector;
46
+ }
47
+ push(state, `return ${INTERNAL_PREFIX}nodes;`);
48
+ state.textOrdinals = outerOrdinals;
49
+ state.scopeVar = outerScope;
50
+ state.indent -= 1;
51
+ push(state, "}");
52
+ }
53
+ /**
54
+ * Emit the per-entry list builder: binds the authored mapper parameter, builds
55
+ * the entry's detached nodes on the entry owner scope and returns an `update`
56
+ * hook so a keyed reuse re-evaluates the entry's refresh closures in place
57
+ * (DOM identity is preserved; per-read props replacement stays M06b/M07).
58
+ */
59
+ function emitEntryBuilder(state, functionName, scopeParam, itemParam, itemName, nodes, records, anchor) {
60
+ push(state, `function ${functionName}(${scopeParam}, ${itemParam}) {`, anchor);
61
+ state.indent += 1;
62
+ const outerScope = state.scopeVar;
63
+ const outerOrdinals = state.textOrdinals;
64
+ state.scopeVar = scopeParam;
65
+ const outerEntryNames = state.entryNames.has(itemName);
66
+ state.entryNames.add(itemName);
67
+ const prepared = emissionNodes(state, nodes);
68
+ state.textOrdinals = textSlotOrdinals(prepared);
69
+ push(state, `const ${INTERNAL_PREFIX}nodes = [];`);
70
+ push(state, `let ${itemName} = ${itemParam};`);
71
+ const collector = [];
72
+ state.refreshStack.push(collector);
73
+ for (const node of prepared) {
74
+ const sink = [];
75
+ emitNode(state, node, sink, records);
76
+ appendSinkToArray(state, sink, `${INTERNAL_PREFIX}nodes`);
77
+ }
78
+ state.refreshStack.pop();
79
+ if (!outerEntryNames) state.entryNames.delete(itemName);
80
+ const updateBody = [`${itemName} = next;`, ...collector.map((name) => `${name}(true);`)].join(" ");
81
+ push(state, `return { nodes: ${INTERNAL_PREFIX}nodes, update(next) { ${updateBody} } };`);
82
+ state.textOrdinals = outerOrdinals;
83
+ state.scopeVar = outerScope;
84
+ state.indent -= 1;
85
+ push(state, "}");
86
+ }
87
+ /**
88
+ * 16b-LC retained-child R1/R4: remove one `when` branch owner and release every retained
89
+ * child indexed under it (the branch is the structural owner of retained refs
90
+ * emitted inside it).
91
+ *
92
+ * Cleanup FIRST, release SECOND, and both always run (B2): a throwing retained
93
+ * setup cleanup must never skip the branch's own `dispose(true)` range removal.
94
+ * The pre-existing dispose error wins when both throw; release errors surface
95
+ * only when the dispose was clean.
96
+ */
97
+ function emitBranchRelease(state, ownerVar) {
98
+ const failureVar = nextName(state, "releaseFailure");
99
+ push(state, `if (${ownerVar} !== null) {`);
100
+ state.indent += 1;
101
+ push(state, `let ${failureVar} = null;`);
102
+ push(state, `try { ${ownerVar}.dispose(true); } catch (failure) { ${failureVar} = failure; }`);
103
+ push(state, `try { releaseUnderStructuralScope(${ownerVar}); } catch (failure) { ${failureVar} ??= failure; }`);
104
+ push(state, `if (${failureVar} !== null) throw ${failureVar};`);
105
+ state.indent -= 1;
106
+ push(state, `}`);
107
+ }
108
+ /**
109
+ * 16b-LC retained-child R2/R5: a `when` refresh failed while constructing the NEW branch.
110
+ * Attempt the branch owner's `dispose(true)` and the structural release, drop
111
+ * both cleanup errors and rethrow the ORIGINAL construction error (B2).
112
+ */
113
+ function emitBranchRollback(state, branchOwnerVar, activeVar) {
114
+ push(state, `} catch (error) {`);
115
+ state.indent += 1;
116
+ if (activeVar !== void 0) push(state, `${activeVar} = -1;`);
117
+ push(state, `try { ${branchOwnerVar}.dispose(true); } catch { /* construction error wins */ }`);
118
+ push(state, `try { releaseUnderStructuralScope(${branchOwnerVar}); } catch { /* construction error wins */ }`);
119
+ push(state, `throw error;`);
120
+ state.indent -= 1;
121
+ push(state, `}`);
122
+ }
123
+ /** True when this emission site sits in the ROOT retaining body itself. */
124
+ function emitsRetentionReleaseText(state) {
125
+ return state.retainBody && state.recipeDepth === 0 && state.claimTimeRecipeDepth === 0 && state.listDepth === 0;
126
+ }
127
+ /**
128
+ * Emit `when`/branch replacement through the M04 disposal mapping.
129
+ */
130
+ function emitCondition(state, node, sink, records) {
131
+ const record = recordFor(state, node.test, records);
132
+ if (record === null || !declareReads(state, record)) return;
133
+ const dependencies = dependencyNames(state, record);
134
+ if (dependencies === null) return;
135
+ const id = state.counter.value++;
136
+ const anchorVar = `${INTERNAL_PREFIX}whenAnchor${id}`;
137
+ const endVar = `${INTERNAL_PREFIX}whenEnd${id}`;
138
+ const activeVar = `${INTERNAL_PREFIX}whenActive${id}`;
139
+ const ownerVar = `${INTERNAL_PREFIX}whenOwner${id}`;
140
+ const refreshVar = `${INTERNAL_PREFIX}whenRefresh${id}`;
141
+ const consequentFn = node.consequent.length > 0 ? `${INTERNAL_PREFIX}whenThen${id}` : null;
142
+ const alternateFn = node.alternate !== null && node.alternate.length > 0 ? `${INTERNAL_PREFIX}whenElse${id}` : null;
143
+ push(state, `const ${anchorVar} = emitComment(MARKERS.when);`, node.span);
144
+ sink.push({
145
+ kind: "node",
146
+ name: anchorVar
147
+ });
148
+ if (consequentFn !== null) emitNodesBuilder(state, consequentFn, `${INTERNAL_PREFIX}whenThenScope${id}`, node.consequent, records, node.span);
149
+ if (alternateFn !== null) emitNodesBuilder(state, alternateFn, `${INTERNAL_PREFIX}whenElseScope${id}`, node.alternate, records, node.span);
150
+ push(state, `let ${activeVar} = -1;`);
151
+ push(state, `let ${ownerVar} = null;`);
152
+ push(state, `function ${refreshVar}(force) {`, node.span);
153
+ state.indent += 1;
154
+ const select = alternateFn === null ? `? 1 : 0` : `? 1 : 2`;
155
+ pushParts(state, [
156
+ `const ${INTERNAL_PREFIX}next = (`,
157
+ { record },
158
+ `) ${select};`
159
+ ]);
160
+ push(state, `if (!force && ${INTERNAL_PREFIX}next === ${activeVar}) return;`);
161
+ push(state, `${activeVar} = -1;`);
162
+ if (emitsRetentionReleaseText(state)) emitBranchRelease(state, ownerVar);
163
+ else push(state, `if (${ownerVar} !== null) ${ownerVar}.dispose(true);`);
164
+ push(state, `${ownerVar} = null;`);
165
+ push(state, `const ${INTERNAL_PREFIX}branchOwner = ${state.scopeVar}.child();`);
166
+ push(state, `let ${INTERNAL_PREFIX}branchNodes;`);
167
+ push(state, `try {`);
168
+ state.indent += 1;
169
+ const thenCall = consequentFn === null ? "[]" : `${consequentFn}(${INTERNAL_PREFIX}branchOwner)`;
170
+ const elseCall = alternateFn === null ? "[]" : `${alternateFn}(${INTERNAL_PREFIX}branchOwner)`;
171
+ push(state, `${INTERNAL_PREFIX}branchNodes = runUntracked(() => ${INTERNAL_PREFIX}next === 1 ? ${thenCall} : ${elseCall});`);
172
+ state.indent -= 1;
173
+ if (emitsRetentionReleaseText(state)) emitBranchRollback(state, `${INTERNAL_PREFIX}branchOwner`, activeVar);
174
+ else push(state, `} catch (error) { ${activeVar} = -1; ${INTERNAL_PREFIX}branchOwner.dispose(true); throw error; }`);
175
+ push(state, `${INTERNAL_PREFIX}branchOwner.addRangeCleanup(() => removeNodes(${INTERNAL_PREFIX}branchNodes));`);
176
+ push(state, `const ${INTERNAL_PREFIX}parent = ${anchorVar}.parentNode;`);
177
+ push(state, `if (${INTERNAL_PREFIX}parent !== null) { for (const ${INTERNAL_PREFIX}child of ${INTERNAL_PREFIX}branchNodes) ${INTERNAL_PREFIX}parent.insertBefore(${INTERNAL_PREFIX}child, ${endVar}); }`);
178
+ push(state, `${activeVar} = ${INTERNAL_PREFIX}next; ${ownerVar} = ${INTERNAL_PREFIX}branchOwner;`);
179
+ state.indent -= 1;
180
+ push(state, "}");
181
+ const initialOwnerVar = `${INTERNAL_PREFIX}initialOwner${id}`;
182
+ const initialTestVar = `${INTERNAL_PREFIX}initialTest${id}`;
183
+ const initialNodesVar = `${INTERNAL_PREFIX}initialNodes${id}`;
184
+ push(state, `const ${initialOwnerVar} = ${state.scopeVar}.child();`);
185
+ pushParts(state, [
186
+ `const ${initialTestVar} = (`,
187
+ { record },
188
+ ");"
189
+ ]);
190
+ push(state, `let ${initialNodesVar};`);
191
+ push(state, `try {`);
192
+ state.indent += 1;
193
+ const initialThen = consequentFn === null ? "[]" : `${consequentFn}(${initialOwnerVar})`;
194
+ const initialElse = alternateFn === null ? "[]" : `${alternateFn}(${initialOwnerVar})`;
195
+ push(state, `${initialNodesVar} = ${initialTestVar} ? ${initialThen} : ${initialElse};`);
196
+ state.indent -= 1;
197
+ push(state, `} catch (error) { ${initialOwnerVar}.dispose(true); throw error; }`);
198
+ push(state, `${initialOwnerVar}.addRangeCleanup(() => removeNodes(${initialNodesVar}));`);
199
+ push(state, `${activeVar} = ${initialTestVar} ? 1 : ${alternateFn === null ? 0 : 2}; ${ownerVar} = ${initialOwnerVar};`);
200
+ push(state, `const ${endVar} = emitComment(MARKERS.whenEnd);`, node.span);
201
+ sink.push({
202
+ kind: "spread",
203
+ name: initialNodesVar
204
+ });
205
+ sink.push({
206
+ kind: "node",
207
+ name: endVar
208
+ });
209
+ emitDependencySubscriptions(state, dependencies, refreshVar, node.span, { ignoreSignalValue: true });
210
+ registerRefresh(state, refreshVar);
211
+ }
212
+ /**
213
+ * Emit one list key thunk. Unkeyed lists keep the mount contract: the key is
214
+ * the item index, a finite number, so claim/SSR encode the same positional
215
+ * identity rather than inventing a second unkeyed rule. The mapper parameter is
216
+ * entry-local while the key record is validated; outer reads remain tracked.
217
+ */
218
+ function emitListKeyFunction(state, keyFn, node, records) {
219
+ if (node.key !== null) {
220
+ const keyRecord = recordFor(state, node.key, records);
221
+ if (keyRecord === null) return null;
222
+ const outerKeyEntryName = state.entryNames.has(node.parameter);
223
+ state.entryNames.add(node.parameter);
224
+ const declared = declareReads(state, keyRecord);
225
+ if (!outerKeyEntryName) state.entryNames.delete(node.parameter);
226
+ if (!declared) return null;
227
+ const keyDeps = dependencyNames(state, keyRecord);
228
+ if (keyDeps === null) return null;
229
+ pushParts(state, [
230
+ `function ${keyFn}(${INTERNAL_PREFIX}eachItem, ${INTERNAL_PREFIX}eachIndex) { const ${node.parameter} = ${INTERNAL_PREFIX}eachItem; return (`,
231
+ { record: keyRecord },
232
+ "); }"
233
+ ], node.key.span);
234
+ return { dependencies: keyDeps };
235
+ }
236
+ push(state, `function ${keyFn}(${INTERNAL_PREFIX}eachItem, ${INTERNAL_PREFIX}eachIndex) { return ${INTERNAL_PREFIX}eachIndex; }`, node.span);
237
+ return { dependencies: [] };
238
+ }
239
+ /** Emit `each`/map reconciliation with per-entry owners, keys and in-place updates. */
240
+ function emitList(state, node, sink, records) {
241
+ const itemsRecord = recordFor(state, node.items, records);
242
+ if (itemsRecord === null || !declareReads(state, itemsRecord)) return;
243
+ const dependencies = dependencyNames(state, itemsRecord);
244
+ if (dependencies === null) return;
245
+ if (!IDENTIFIER_PATTERN.test(node.parameter) || node.parameter.startsWith("__oe_")) {
246
+ diag(state, "OEIR_LEAF_INVALID_BINDING", `list mapper parameter '${node.parameter}' is not usable`, node.span);
247
+ return;
248
+ }
249
+ const id = state.counter.value++;
250
+ const anchorVar = `${INTERNAL_PREFIX}eachAnchor${id}`;
251
+ const entriesVar = `${INTERNAL_PREFIX}eachEntries${id}`;
252
+ const entryFn = `${INTERNAL_PREFIX}eachEntry${id}`;
253
+ const keyFn = `${INTERNAL_PREFIX}eachKey${id}`;
254
+ const refreshVar = `${INTERNAL_PREFIX}eachRefresh${id}`;
255
+ push(state, `const ${anchorVar} = emitComment(MARKERS.each);`, node.span);
256
+ sink.push({
257
+ kind: "node",
258
+ name: anchorVar
259
+ });
260
+ const wire = state.emitTextBoundaries;
261
+ const endVar = `${INTERNAL_PREFIX}eachEnd${id}`;
262
+ if (wire) push(state, `const ${endVar} = emitComment(MARKERS.eachEnd);`, node.span);
263
+ push(state, `let ${entriesVar} = [];`);
264
+ state.listDepth += 1;
265
+ try {
266
+ emitEntryBuilder(state, entryFn, `${INTERNAL_PREFIX}eachScope${id}`, `${INTERNAL_PREFIX}eachItem${id}`, node.parameter, node.template, records, node.span);
267
+ } finally {
268
+ state.listDepth -= 1;
269
+ }
270
+ const keyInfo = emitListKeyFunction(state, keyFn, node, records);
271
+ if (keyInfo === null) return;
272
+ const keyDependencies = keyInfo.dependencies;
273
+ push(state, `function ${refreshVar}() {`, node.span);
274
+ state.indent += 1;
275
+ pushParts(state, [
276
+ `const ${INTERNAL_PREFIX}items = (`,
277
+ { record: itemsRecord },
278
+ ");"
279
+ ]);
280
+ push(state, `const ${INTERNAL_PREFIX}previous = ${entriesVar};`);
281
+ push(state, `const ${INTERNAL_PREFIX}next = [];`);
282
+ push(state, `const ${INTERNAL_PREFIX}used = new Set();`);
283
+ push(state, `for (let ${INTERNAL_PREFIX}index = 0; ${INTERNAL_PREFIX}index < ${INTERNAL_PREFIX}items.length; ${INTERNAL_PREFIX}index++) {`);
284
+ state.indent += 1;
285
+ push(state, `const ${INTERNAL_PREFIX}item = ${INTERNAL_PREFIX}items[${INTERNAL_PREFIX}index];`);
286
+ push(state, `const ${INTERNAL_PREFIX}key = ${keyFn}(${INTERNAL_PREFIX}item, ${INTERNAL_PREFIX}index);`);
287
+ push(state, `let ${INTERNAL_PREFIX}found = null;`);
288
+ push(state, `for (const ${INTERNAL_PREFIX}entry of ${INTERNAL_PREFIX}previous) { if (${INTERNAL_PREFIX}entry.key === ${INTERNAL_PREFIX}key && !${INTERNAL_PREFIX}used.has(${INTERNAL_PREFIX}entry)) { ${INTERNAL_PREFIX}found = ${INTERNAL_PREFIX}entry; break; } }`);
289
+ push(state, `if (${INTERNAL_PREFIX}found !== null) {`);
290
+ state.indent += 1;
291
+ push(state, `${INTERNAL_PREFIX}used.add(${INTERNAL_PREFIX}found); ${INTERNAL_PREFIX}found.item = ${INTERNAL_PREFIX}item; ${INTERNAL_PREFIX}found.update(${INTERNAL_PREFIX}item); ${INTERNAL_PREFIX}next.push(${INTERNAL_PREFIX}found);`);
292
+ state.indent -= 1;
293
+ push(state, `} else {`);
294
+ state.indent += 1;
295
+ push(state, `const ${INTERNAL_PREFIX}owner = ${state.scopeVar}.child();`);
296
+ push(state, `let ${INTERNAL_PREFIX}built;`);
297
+ push(state, `try {`);
298
+ state.indent += 1;
299
+ push(state, `${INTERNAL_PREFIX}built = ${entryFn}(${INTERNAL_PREFIX}owner, ${INTERNAL_PREFIX}item);`);
300
+ state.indent -= 1;
301
+ push(state, `} catch (error) { ${INTERNAL_PREFIX}owner.dispose(true); throw error; }`);
302
+ if (wire) {
303
+ const tokenVar = nextName(state, "token");
304
+ push(state, `const ${tokenVar} = tryEncodeListKey(${INTERNAL_PREFIX}key);`);
305
+ push(state, `${INTERNAL_PREFIX}built.nodes.unshift(emitComment(entryKeyMarker(${tokenVar})));`);
306
+ }
307
+ push(state, `${INTERNAL_PREFIX}owner.addRangeCleanup(() => removeNodes(${INTERNAL_PREFIX}built.nodes));`);
308
+ push(state, `const ${INTERNAL_PREFIX}entry = { key: ${INTERNAL_PREFIX}key, item: ${INTERNAL_PREFIX}item, owner: ${INTERNAL_PREFIX}owner, nodes: ${INTERNAL_PREFIX}built.nodes, update: ${INTERNAL_PREFIX}built.update };`);
309
+ push(state, `${INTERNAL_PREFIX}used.add(${INTERNAL_PREFIX}entry); ${INTERNAL_PREFIX}next.push(${INTERNAL_PREFIX}entry);`);
310
+ state.indent -= 1;
311
+ push(state, `}`);
312
+ state.indent -= 1;
313
+ push(state, `}`);
314
+ push(state, `for (const ${INTERNAL_PREFIX}entry of ${INTERNAL_PREFIX}previous) if (!${INTERNAL_PREFIX}used.has(${INTERNAL_PREFIX}entry)) ${INTERNAL_PREFIX}entry.owner.dispose(true);`);
315
+ push(state, `const ${INTERNAL_PREFIX}parent = ${anchorVar}.parentNode;`);
316
+ push(state, `if (${INTERNAL_PREFIX}parent !== null) {`);
317
+ state.indent += 1;
318
+ push(state, `const ${INTERNAL_PREFIX}previousNodes = new Set();`);
319
+ push(state, `for (const ${INTERNAL_PREFIX}entry of ${INTERNAL_PREFIX}previous) for (const ${INTERNAL_PREFIX}node of ${INTERNAL_PREFIX}entry.nodes) ${INTERNAL_PREFIX}previousNodes.add(${INTERNAL_PREFIX}node);`);
320
+ if (wire) push(state, `const ${INTERNAL_PREFIX}tail = ${endVar};`);
321
+ else {
322
+ push(state, `let ${INTERNAL_PREFIX}tail = ${anchorVar}.nextSibling;`);
323
+ push(state, `while (${INTERNAL_PREFIX}tail !== null && ${INTERNAL_PREFIX}previousNodes.has(${INTERNAL_PREFIX}tail)) ${INTERNAL_PREFIX}tail = ${INTERNAL_PREFIX}tail.nextSibling;`);
324
+ }
325
+ push(state, `reconcileEachEntries(${INTERNAL_PREFIX}next, ${INTERNAL_PREFIX}parent, ${INTERNAL_PREFIX}tail, ${INTERNAL_PREFIX}previous);`);
326
+ state.indent -= 1;
327
+ push(state, `}`);
328
+ push(state, `${entriesVar} = ${INTERNAL_PREFIX}next;`);
329
+ state.indent -= 1;
330
+ push(state, "}");
331
+ push(state, `${refreshVar}();`);
332
+ const initialNodesVar = `${INTERNAL_PREFIX}eachInitialNodes${id}`;
333
+ push(state, `const ${initialNodesVar} = [];`);
334
+ push(state, `for (const ${INTERNAL_PREFIX}entry of ${entriesVar}) for (const ${INTERNAL_PREFIX}node of ${INTERNAL_PREFIX}entry.nodes) ${initialNodesVar}.push(${INTERNAL_PREFIX}node);`);
335
+ sink.push({
336
+ kind: "spread",
337
+ name: initialNodesVar
338
+ });
339
+ if (wire) sink.push({
340
+ kind: "node",
341
+ name: endVar
342
+ });
343
+ const subscribed = /* @__PURE__ */ new Set();
344
+ const uniqueDependencies = [];
345
+ for (const dependency of [...dependencies, ...keyDependencies]) {
346
+ if (subscribed.has(dependency.name)) continue;
347
+ subscribed.add(dependency.name);
348
+ uniqueDependencies.push(dependency);
349
+ }
350
+ emitDependencySubscriptions(state, uniqueDependencies, refreshVar, node.span);
351
+ registerRefresh(state, refreshVar);
352
+ }
353
+ function emitTextWrite(state, targetVar, record) {
354
+ pushParts(state, [
355
+ `${targetVar}.nodeValue = displayValue((`,
356
+ { record },
357
+ "));"
358
+ ]);
359
+ }
360
+ /** The DOM event name for one authored `onX` attribute (`onClick` -> `click`). */
361
+ function clientOnlyEventName(attributeName) {
362
+ const name = attributeName.slice(2).toLowerCase();
363
+ return name.length > 0 && /^[a-z][a-z0-9-]*$/.test(name) ? name : null;
364
+ }
365
+ /**
366
+ * Placement gate for the two client-only channels. Refused:
367
+ *
368
+ * - custom-element hosts (`<x-y>`): this slice admits the INTRINSIC DOM
369
+ * handler/ref channel; a host's event/ref contract belongs to its owner.
370
+ * - list entry templates: a keyed entry update re-binds the mapper parameter
371
+ * without re-running the mount, so an installed listener/ref would keep the
372
+ * first entry's values over reused DOM. The M06b2c per-entry update hook owns
373
+ * that shape.
374
+ */
375
+ function clientOnlyAttributeAdmitted(state, tag, attributeName, span) {
376
+ const label = attributeName.startsWith("on") ? "event" : "ref";
377
+ if (tag.includes("-")) {
378
+ diag(state, "OEIR_LEAF_UNSUPPORTED_ATTRIBUTE", `the ${label} channel on custom-element host <${tag}> is outside the generated-leaf slice; bind it on an intrinsic element or in the host component`, span);
379
+ return false;
380
+ }
381
+ if (state.listDepth > 0) {
382
+ diag(state, "OEIR_LEAF_UNSUPPORTED_ATTRIBUTE", `the ${label} channel inside a list entry template needs the per-entry update hook (M06b2c); keep the handler outside the list entry`, span);
383
+ return false;
384
+ }
385
+ return true;
386
+ }
387
+ /**
388
+ * Install one client-only attachment on an already-created element.
389
+ *
390
+ * - event: evaluate the handler record once (attach-time, matching the class
391
+ * path's handler Part), validate it is callable, then add one listener that
392
+ * calls it with the native event. No wrapper/try-catch: a throwing handler is
393
+ * platform dispatch, not a suppression point. The mount/claim scope owns the
394
+ * removal, so a throwing later installation step (or a throwing claim) runs
395
+ * this cleanup through the standard owner rollback while borrowed DOM stays.
396
+ * - ref: evaluate the handler record once, validate callable, call it with the
397
+ * element and queue the class-path detach: call the returned unsubscribe (if
398
+ * it is a function) and then call the handler with `null`, even when the
399
+ * cleanup throws (`finally`).
400
+ */
401
+ function emitClientOnlyInstall(state, elementVar, attribute, record) {
402
+ const span = attribute.span;
403
+ const handlerVar = nextName(state, attribute.channel === "event" ? "eventHandler" : "refHandler");
404
+ pushParts(state, [
405
+ `const ${handlerVar} = (`,
406
+ { record },
407
+ ");"
408
+ ], span);
409
+ if (attribute.channel === "event") {
410
+ push(state, `if (typeof ${handlerVar} !== "function") throw leafRuntimeError(${quoteGeneratedJavaScriptValue(EVENT_HANDLER_INVALID)}, ${quoteGeneratedJavaScriptValue(`${attribute.name} handler is not callable`)}${originArgs(span)});`, span);
411
+ const eventName = clientOnlyEventName(attribute.name);
412
+ if (eventName === null) {
413
+ diag(state, "OEIR_LEAF_UNSUPPORTED_ATTRIBUTE", `event attribute '${attribute.name}' does not name a DOM event`, span);
414
+ return;
415
+ }
416
+ const listenerVar = nextName(state, "eventListener");
417
+ push(state, moduleUsesErrorBoundary(state) ? `const ${listenerVar} = guardUpdate((event) => ${handlerVar}(event), ${state.hostVar}.${ERROR_REPORT_PROPERTY});` : `const ${listenerVar} = (event) => ${handlerVar}(event);`, span);
418
+ push(state, `${elementVar}.addEventListener(${quoteGeneratedJavaScriptValue(eventName)}, ${listenerVar});`, span);
419
+ push(state, `${state.scopeVar}.add(() => ${elementVar}.removeEventListener(${quoteGeneratedJavaScriptValue(eventName)}, ${listenerVar}));`, span);
420
+ return;
421
+ }
422
+ push(state, `if (typeof ${handlerVar} !== "function") throw leafRuntimeError(${quoteGeneratedJavaScriptValue(REF_HANDLER_INVALID)}, ${quoteGeneratedJavaScriptValue(`${attribute.name} handler is not callable`)}${originArgs(span)});`, span);
423
+ const cleanupVar = nextName(state, "refCleanup");
424
+ push(state, `const ${cleanupVar} = ${handlerVar}(${elementVar});`, span);
425
+ push(state, `${state.scopeVar}.add(() => { try { if (typeof ${cleanupVar} === "function") ${cleanupVar}(); } finally { ${handlerVar}(null); } });`, span);
426
+ }
427
+ function emitAttributeWrite(state, elementVar, name, channel, record, anchor) {
428
+ const literal = JSON.stringify(name);
429
+ if (channel === "boolean") {
430
+ pushParts(state, [
431
+ `${elementVar}.toggleAttribute(${literal}, Boolean((`,
432
+ { record },
433
+ ")));"
434
+ ], anchor);
435
+ return;
436
+ }
437
+ if (channel === "property") {
438
+ if (!IDENTIFIER_PATTERN.test(name)) {
439
+ diag(state, "OEIR_LEAF_UNSUPPORTED_ATTRIBUTE", `property '${name}' is not a valid identifier`, {
440
+ file: "",
441
+ start: 0,
442
+ end: 0,
443
+ line: 1,
444
+ column: 1
445
+ });
446
+ return;
447
+ }
448
+ pushParts(state, [
449
+ `${elementVar}[${literal}] = (`,
450
+ { record },
451
+ ");"
452
+ ], anchor);
453
+ return;
454
+ }
455
+ const valueVar = nextName(state, "value");
456
+ pushParts(state, [
457
+ `const ${valueVar} = attributeValueOf((`,
458
+ { record },
459
+ "));"
460
+ ], anchor);
461
+ push(state, `if (${valueVar} === null) ${elementVar}.removeAttribute(${literal});`, anchor);
462
+ push(state, `else {`, anchor);
463
+ push(state, ` assertSafeUrlAttribute(${literal}, ${valueVar});`, anchor);
464
+ push(state, ` ${elementVar}.setAttribute(${literal}, ${valueVar});`, anchor);
465
+ push(state, `}`, anchor);
466
+ }
467
+ function attributeAdmitted(state, attribute, span) {
468
+ if (attribute.kind === "static") {
469
+ if (!ATTRIBUTE_PATTERN.test(attribute.name)) {
470
+ diag(state, "OEIR_LEAF_UNSUPPORTED_ATTRIBUTE", `attribute name '${attribute.name}' is invalid`, span);
471
+ return false;
472
+ }
473
+ return true;
474
+ }
475
+ if (!ADMITTED_ATTRIBUTE_CHANNELS.has(attribute.channel)) {
476
+ diag(state, "OEIR_LEAF_UNSUPPORTED_ATTRIBUTE", `attribute channel '${attribute.channel}' is outside the leaf slice (M05b/M06 owns it)`, span);
477
+ return false;
478
+ }
479
+ return true;
480
+ }
481
+ /**
482
+ * Emit one module-local component-reference instance (M06b1a-1).
483
+ *
484
+ * The child body is the module-level `<Child>__mount`, so one component body is
485
+ * emitted once per module however often it is referenced. Each reference owns a
486
+ * fresh child scope (M04 ownership: disposal, rollback and range cleanup are the
487
+ * parent's), the parent appends the returned nodes to the enclosing sink in
488
+ * authored order, and props are a per-read getter record over the DEFINING
489
+ * (parent) scope — `{ get text() { return <parent read>; } }` — never cached.
490
+ * The child refreshes a prop-dependent slot through the defining-scope signals
491
+ * in `host.propDeps`: the parent bindings the passed prop expressions depend on,
492
+ * plus the parent's own props channel when a getter passes a parent prop.
493
+ */
494
+ function emitComponentRef(state, node, sink, records) {
495
+ const span = node.span;
496
+ if (state.listDepth > 0) {
497
+ diag(state, "OEIR_LEAF_UNSUPPORTED_COMPONENT", `component '<${node.name}>' inside a list entry template needs the child update hook (M06b1b); a keyed reuse would keep the first mount's props`, span);
498
+ return;
499
+ }
500
+ const target = state.componentByName.get(node.name);
501
+ if (target === void 0) {
502
+ diag(state, "OEIR_LEAF_UNKNOWN_COMPONENT", `component '<${node.name}>' is not declared in this module (external components are M06b2)`, span);
503
+ return;
504
+ }
505
+ if (target.propsParam === null) {
506
+ diag(state, "OEIR_LEAF_UNSUPPORTED_COMPONENT", `component '<${node.name}>' has no named props parameter (destructured or absent); component references need a named props parameter (M06b2 owns destructuring)`, span);
507
+ return;
508
+ }
509
+ if (target.resources.filter((resource) => resource.kind === "signal" || resource.kind === "computed").length > 0) {
510
+ diag(state, "OEIR_LEAF_UNSUPPORTED_COMPONENT", `component '<${node.name}>' owns signal/computed resources; a child instance cannot resolve host bindings without sharing the parent instance (M06b2/M07 own it)`, span);
511
+ return;
512
+ }
513
+ if (node.childrenRecipeId !== null && state.recipeDepth > 0) {
514
+ diag(state, "OEIR_LEAF_UNSUPPORTED_CHILDREN", `children on '<${node.name}>' inside a reusable JSX value body are outside the M06b2a slice: the children recipe would be created once per component mount, but its definition owner is the recipe instance scope (created per insertion)`, span);
515
+ return;
516
+ }
517
+ if (node.childrenRecipeId === null && componentProjectsChildren(target)) {
518
+ diag(state, "OEIR_LEAF_MISSING_CHILDREN", `component '<${node.name}>' projects props.children but this reference passes no JSX children; every projection needs a children recipe`, span);
519
+ return;
520
+ }
521
+ const childrenRecipeVar = node.childrenRecipeId === null ? null : state.recipeVars.get(node.childrenRecipeId) ?? null;
522
+ if (node.childrenRecipeId !== null && childrenRecipeVar === null) {
523
+ diag(state, "OEIR_LEAF_MISSING_RECIPE", `children recipe '${node.childrenRecipeId}' of '<${node.name}>' has no emitted recipe (it is not reachable from any admitted root)`, span);
524
+ return;
525
+ }
526
+ let refused = false;
527
+ const admitted = [];
528
+ for (const attribute of node.props) {
529
+ if (attribute.name === "key") continue;
530
+ if (attribute.name === "children") {
531
+ diag(state, "OEIR_LEAF_UNSUPPORTED_COMPONENT", `'children' on component '<${node.name}>' must be authored as JSX children, not a prop value: it is the recipe projection channel (JSX value model §4.4)`, span);
532
+ refused = true;
533
+ continue;
534
+ }
535
+ if (!componentPropAdmitted(state, attribute, node.name, span)) {
536
+ refused = true;
537
+ continue;
538
+ }
539
+ admitted.push(attribute);
540
+ }
541
+ if (refused) return;
542
+ const id = state.counter.value++;
543
+ const propsVar = `${INTERNAL_PREFIX}childProps${id}`;
544
+ const nodesVar = `${INTERNAL_PREFIX}childNodes${id}`;
545
+ const retained = state.retention !== null && state.retention.retainedRefs.has(node);
546
+ const hostVar = `${INTERNAL_PREFIX}childHost${id}`;
547
+ const scopeVar = `${INTERNAL_PREFIX}childScope${id}`;
548
+ const setupHostVar = `${INTERNAL_PREFIX}childSetupHost${id}`;
549
+ const instanceVar = `${INTERNAL_PREFIX}childInstance${id}`;
550
+ const failureVar = `${INTERNAL_PREFIX}childFailure${id}`;
551
+ const propDeps = /* @__PURE__ */ new Set();
552
+ let needsParentProps = false;
553
+ const resolved = [];
554
+ let failed = false;
555
+ for (const prop of admitted) {
556
+ if (prop.kind === "static") {
557
+ resolved.push(prop);
558
+ continue;
559
+ }
560
+ const record = recordFor(state, prop.expression, records);
561
+ if (record === null || !declareReads(state, record)) {
562
+ failed = true;
563
+ continue;
564
+ }
565
+ const dependencies = dependencyNames(state, record);
566
+ if (dependencies === null) {
567
+ failed = true;
568
+ continue;
569
+ }
570
+ for (const dependency of dependencies) if (dependency.fromProps) needsParentProps = true;
571
+ else propDeps.add(dependency.name);
572
+ resolved.push({
573
+ kind: "dynamic",
574
+ name: prop.name,
575
+ record,
576
+ span: prop.span
577
+ });
578
+ }
579
+ if (failed) return;
580
+ if (resolved.length > 0 || childrenRecipeVar !== null) {
581
+ push(state, `const ${propsVar} = {`, span);
582
+ state.indent += 1;
583
+ if (childrenRecipeVar !== null) push(state, `children: ${childrenRecipeVar},`, span);
584
+ for (const prop of resolved) {
585
+ if (prop.kind === "static") {
586
+ const key = IDENTIFIER_PATTERN.test(prop.name) ? prop.name : JSON.stringify(prop.name);
587
+ push(state, `${key}: ${JSON.stringify(prop.value)},`);
588
+ continue;
589
+ }
590
+ const key = IDENTIFIER_PATTERN.test(prop.name) ? prop.name : JSON.stringify(prop.name);
591
+ pushParts(state, [
592
+ `get ${key}() { return (`,
593
+ { record: prop.record },
594
+ "); },"
595
+ ], prop.span);
596
+ }
597
+ state.indent -= 1;
598
+ push(state, "};");
599
+ }
600
+ const propsExpr = resolved.length > 0 || childrenRecipeVar !== null ? propsVar : "{}";
601
+ const depParts = [...propDeps];
602
+ if (needsParentProps) depParts.push(`...${state.hostVar}.propDeps`);
603
+ const depList = `[${depParts.join(", ")}]`;
604
+ if (!retained) {
605
+ push(state, `const ${hostVar} = { bindings: ${state.hostVar}.bindings, propDeps: ${depList} };`, span);
606
+ push(state, `const ${scopeVar} = ${state.scopeVar}.child();`, span);
607
+ push(state, `let ${nodesVar};`);
608
+ push(state, `try {`);
609
+ state.indent += 1;
610
+ push(state, `${nodesVar} = ${node.name}${MOUNT_SUFFIX}(${scopeVar}, ${propsExpr}, ${hostVar});`, span);
611
+ state.indent -= 1;
612
+ push(state, `} catch (error) { ${scopeVar}.dispose(true); throw error; }`);
613
+ push(state, `${scopeVar}.addRangeCleanup(() => removeNodes(${nodesVar}));`);
614
+ sink.push({
615
+ kind: "spread",
616
+ name: nodesVar
617
+ });
618
+ return;
619
+ }
620
+ const siteKey = siteKeyLiteral(span);
621
+ const setupScopeVar = `${INTERNAL_PREFIX}setupScope${id}`;
622
+ const setupCall = state.retention.setupBodies.has(node.name) ? `(${setupScopeVar}) => ${node.name}${LEAF_SETUP_SUFFIX}(${setupScopeVar}, ${propsExpr}, ${setupHostVar})` : `() => ({})`;
623
+ push(state, `const ${setupHostVar} = { bindings: ${state.hostVar}.bindings, propDeps: ${depList}${childHostElementField(state, node)}${childPropertyChangeField(state, node)}${childErrorBoundaryField(state, node)}${childContextField(state, node)} };`, span);
624
+ push(state, `const ${instanceVar} = retainChildInstance(${state.hostVar}.instance, ${siteKey}, ${state.scopeVar}, ${setupCall});`, span);
625
+ push(state, `const ${scopeVar} = ${state.scopeVar}.child();`, span);
626
+ push(state, `const ${hostVar} = { bindings: ${setupHostVar}.bindings, propDeps: ${setupHostVar}.propDeps, instance: ${instanceVar}.scope, setup: ${instanceVar}.record${childHostElementField(state, node)}${childErrorReportField(state, node)}${childContextField(state, node)}${retainedTransactionField(state, node.name)} };`, span);
627
+ push(state, `let ${nodesVar};`);
628
+ push(state, `try {`);
629
+ state.indent += 1;
630
+ push(state, `${nodesVar} = ${node.name}${MOUNT_SUFFIX}(${scopeVar}, ${propsExpr}, ${hostVar});`, span);
631
+ state.indent -= 1;
632
+ push(state, `} catch (error) {`);
633
+ state.indent += 1;
634
+ push(state, `let ${failureVar} = null;`);
635
+ push(state, `try { ${scopeVar}.dispose(true); } catch (failure) { ${failureVar} = failure; }`);
636
+ push(state, `try { releaseChildInstance(${state.hostVar}.instance, ${siteKey}); } catch (failure) { ${failureVar} ??= failure; }`);
637
+ push(state, `throw ${failureVar} ?? error;`);
638
+ state.indent -= 1;
639
+ push(state, `}`);
640
+ push(state, `${scopeVar}.addRangeCleanup(() => removeNodes(${nodesVar}));`);
641
+ sink.push({
642
+ kind: "spread",
643
+ name: nodesVar
644
+ });
645
+ }
646
+ /**
647
+ * Emit one `createRecipe` per referenced reusable JSX value (M06b1a-2).
648
+ *
649
+ * The mount body stays inline in the component chunk with its own recipe scope:
650
+ * lowered expressions keep their A2 segments/placements (the per-chunk line
651
+ * rebase is unchanged) and refresh subscriptions bind on the recipe instance.
652
+ * Recipe variables are published before any body is emitted so an insertion
653
+ * site inside another recipe body can reference any referenced recipe; the
654
+ * recipe graph is acyclic (`findMountCycle` refuses cycles before emission).
655
+ */
656
+ function emitRecipes(state, file, records) {
657
+ const referenced = referencedRecipeIds(state.component);
658
+ if (referenced.size === 0) return;
659
+ const plans = [];
660
+ for (const recipe of state.component.recipes) {
661
+ if (!referenced.has(recipe.id)) continue;
662
+ const plan = {
663
+ recipe,
664
+ recipeVar: nextName(state, "recipe"),
665
+ scopeVar: nextName(state, "recipeScope"),
666
+ nodesVar: nextName(state, "recipeNodes")
667
+ };
668
+ state.recipeVars.set(recipe.id, plan.recipeVar);
669
+ plans.push(plan);
670
+ }
671
+ for (const plan of plans) {
672
+ const { recipe } = plan;
673
+ const outerOrdinals = state.textOrdinals;
674
+ const preparedTarget = normalizeTextRuns([recipe.target]);
675
+ state.textOrdinals = textSlotOrdinals(preparedTarget);
676
+ if (state.emitTextBoundaries && !assertTextBoundaryContext(state, preparedTarget)) {
677
+ state.textOrdinals = outerOrdinals;
678
+ return;
679
+ }
680
+ push(state, `const ${plan.recipeVar} = createRecipe({`, recipe.span);
681
+ state.indent += 1;
682
+ push(state, `definitionOwner: ${MOUNT_SCOPE},`);
683
+ push(state, `source: ${JSON.stringify(`${file}#${state.component.name}:${recipe.id.replace(/^#/, "")}`)},`);
684
+ push(state, `mount(${plan.scopeVar}) {`, recipe.span);
685
+ state.indent += 1;
686
+ push(state, `const ${plan.nodesVar} = [];`);
687
+ const outerScope = state.scopeVar;
688
+ state.scopeVar = plan.scopeVar;
689
+ state.recipeDepth += 1;
690
+ const sink = [];
691
+ emitNode(state, recipe.target, sink, records);
692
+ appendSinkToArray(state, sink, plan.nodesVar);
693
+ state.recipeDepth -= 1;
694
+ state.scopeVar = outerScope;
695
+ push(state, `return ${plan.nodesVar};`);
696
+ state.indent -= 1;
697
+ push(state, "},");
698
+ state.indent -= 1;
699
+ push(state, "});");
700
+ state.textOrdinals = outerOrdinals;
701
+ }
702
+ }
703
+ /**
704
+ * Emit one insertion site (M06b1a-2): a temporary `DocumentFragment` target,
705
+ * one M04 `insertRecipe` projection on the current builder scope, and a sink
706
+ * entry spreading the projection's `nodes` into the enclosing sink in authored
707
+ * order. The fragment is only the insertion target: M04 registers a
708
+ * parentNode-based `removeNodes(nodes)` range cleanup, so moving the nodes into
709
+ * their final parent keeps the default-dispose-keeps-DOM / `dispose(true)`
710
+ * region contract.
711
+ */
712
+ function emitInsertionSite(state, node, sink) {
713
+ if (!state.declaredRecipeIds.has(node.recipeId)) {
714
+ diag(state, "OEIR_LEAF_UNSUPPORTED_RECIPE", `insertion site '${node.recipeId}' does not target a direct component-local JSX initializer; only those values are reusable recipes in this slice`, node.span);
715
+ return;
716
+ }
717
+ const recipeVar = state.recipeVars.get(node.recipeId);
718
+ if (recipeVar === void 0) {
719
+ diag(state, "OEIR_LEAF_MISSING_RECIPE", `insertion site '${node.recipeId}' has no emitted recipe (the value is not reachable from any admitted root)`, node.span);
720
+ return;
721
+ }
722
+ const fragmentVar = nextName(state, "insertTarget");
723
+ const projectionVar = nextName(state, "projection");
724
+ push(state, `const ${fragmentVar} = document.createDocumentFragment();`, node.span);
725
+ push(state, `const ${projectionVar} = insertRecipe(${state.scopeVar}, ${recipeVar}, ${fragmentVar});`, node.span);
726
+ sink.push({
727
+ kind: "projection",
728
+ name: projectionVar
729
+ });
730
+ }
731
+ /**
732
+ * Emit one `{props.children}` projection (M06b2a, JSX value model §4.4): the
733
+ * mount's single `__oe_props.children` recipe inserted through the SAME
734
+ * `DocumentFragment` + `insertRecipe` mechanism as an insertion site, on the
735
+ * current builder scope (a live descendant of the defining owner, so the M04
736
+ * projection lineage admits). Every site is an independent projection — two
737
+ * sites against one recipe variable are two owners, two instances, two
738
+ * cleanups. Refresh of dynamic children content belongs to the children
739
+ * recipe body's own subscriptions; nothing here re-reads the child props.
740
+ */
741
+ function emitChildrenProjection(state, node, sink) {
742
+ if (state.recipeDepth > 0) {
743
+ diag(state, "OEIR_LEAF_UNSUPPORTED_NODE", `children projection inside a reusable JSX value body is outside the M06b2a slice: the projection would need the recipe instance scope, which exists only at insertion time`, node.span);
744
+ return;
745
+ }
746
+ const recipeVar = state.childrenRecipeVar;
747
+ if (recipeVar === null) {
748
+ diag(state, "OEIR_LEAF_MISSING_CHILDREN", "children projection has no mount-level `props.children` recipe read (internal model mismatch)", node.span);
749
+ return;
750
+ }
751
+ const fragmentVar = nextName(state, "childrenTarget");
752
+ const projectionVar = nextName(state, "childrenProjection");
753
+ push(state, `const ${fragmentVar} = document.createDocumentFragment();`, node.span);
754
+ push(state, `const ${projectionVar} = insertRecipe(${state.scopeVar}, ${recipeVar}, ${fragmentVar});`, node.span);
755
+ sink.push({
756
+ kind: "projection",
757
+ name: projectionVar
758
+ });
759
+ }
760
+ function appendGeneratedNode(state, target, nodeName) {
761
+ if (target.kind === "element") push(state, `${target.name}.appendChild(${nodeName});`);
762
+ else push(state, `${target.name}.push(${nodeName});`);
763
+ }
764
+ /**
765
+ * Mount one pure static directory. Interiors are emitted with directories
766
+ * suppressed, so a range does not contain another range. Called only when
767
+ * this component emits a claim function (ADR-0175 D-2).
768
+ */
769
+ function emitMountedDirectory(state, ranges, records, target) {
770
+ ranges.forEach((range, ordinal) => {
771
+ const span = range.span;
772
+ const start = nextName(state, "mark");
773
+ push(state, `const ${start} = emitComment(rangeMarker(${ordinal}));`, span);
774
+ appendGeneratedNode(state, target, start);
775
+ const sink = [];
776
+ emitNode(state, range, sink, records, false);
777
+ for (const entry of sink) if (entry.kind === "node") appendGeneratedNode(state, target, entry.name);
778
+ const end = nextName(state, "mark");
779
+ push(state, `const ${end} = emitComment(rangeMarker(${ordinal}, true));`, span);
780
+ appendGeneratedNode(state, target, end);
781
+ });
782
+ }
783
+ function emitNode(state, node, sink, records, allowDirectory = true, namespace = "html") {
784
+ const span = node.span;
785
+ if (node.kind === "text") {
786
+ const varName = nextName(state, "text");
787
+ push(state, `const ${varName} = document.createTextNode(${JSON.stringify(node.value)});`, node.span);
788
+ sink.push({
789
+ kind: "node",
790
+ name: varName
791
+ });
792
+ return;
793
+ }
794
+ if (node.kind === "fragment") {
795
+ for (const child of emissionNodes(state, node.children)) emitNode(state, child, sink, records, allowDirectory, namespace);
796
+ return;
797
+ }
798
+ if (node.kind === "condition") {
799
+ emitCondition(state, node, sink, records);
800
+ return;
801
+ }
802
+ if (node.kind === "list") {
803
+ emitList(state, node, sink, records);
804
+ return;
805
+ }
806
+ if (node.kind === "element") {
807
+ if (!TAG_PATTERN.test(node.tag)) {
808
+ diag(state, "OEIR_LEAF_UNSUPPORTED_TAG", `tag '${node.tag}' is not a plain intrinsic element name`, span);
809
+ return;
810
+ }
811
+ const varName = nextName(state, "el");
812
+ const elementNamespace = node.tag === "svg" ? "svg" : namespace;
813
+ push(state, elementNamespace === "svg" ? `const ${varName} = document.createElementNS(${JSON.stringify(SVG_NAMESPACE)}, ${JSON.stringify(node.tag)});` : `const ${varName} = document.createElement(${JSON.stringify(node.tag)});`, node.span);
814
+ for (const attribute of node.attributes) {
815
+ if (attribute.kind === "dynamic" && CONSUMED_ATTRIBUTE_CHANNELS.has(attribute.channel)) continue;
816
+ if (!attributeAdmitted(state, attribute, span)) continue;
817
+ if (attribute.kind === "static") {
818
+ if (unsafeStreamFrameAttribute(attribute.name, attribute.value)) {
819
+ diag(state, "OEIR_LEAF_UNSAFE_ATTRIBUTE", `static URL attribute '${attribute.name}' carries a script-bearing scheme`, span);
820
+ continue;
821
+ }
822
+ const staticValue = BOOLEAN_ATTRIBUTES.has(attribute.name.toLowerCase()) ? "" : attribute.value;
823
+ push(state, `${varName}.setAttribute(${JSON.stringify(attribute.name)}, ${JSON.stringify(staticValue)});`, attribute.span);
824
+ continue;
825
+ }
826
+ if (CLIENT_ONLY_ATTRIBUTE_CHANNELS.has(attribute.channel)) {
827
+ if (!clientOnlyAttributeAdmitted(state, node.tag, attribute.name, span)) continue;
828
+ const record = recordFor(state, attribute.expression, records);
829
+ if (record === null || !declareReads(state, record)) continue;
830
+ emitClientOnlyInstall(state, varName, attribute, record);
831
+ continue;
832
+ }
833
+ const record = recordFor(state, attribute.expression, records);
834
+ if (record === null || !declareReads(state, record)) continue;
835
+ const dependencies = dependencyNames(state, record);
836
+ if (dependencies === null) continue;
837
+ if (dependencies.length === 0) {
838
+ if (!readsEntryLocal(state, record)) {
839
+ emitAttributeWrite(state, varName, attribute.name, attribute.channel, record, attribute.span);
840
+ continue;
841
+ }
842
+ registerRefresh(state, emitLocalRefresh(state, attribute.span, () => emitAttributeWrite(state, varName, attribute.name, attribute.channel, record, attribute.span)));
843
+ continue;
844
+ }
845
+ const refresh = nextName(state, "refresh");
846
+ push(state, `function ${refresh}() {`, attribute.span);
847
+ state.indent += 1;
848
+ emitAttributeWrite(state, varName, attribute.name, attribute.channel, record, attribute.span);
849
+ state.indent -= 1;
850
+ push(state, `}`);
851
+ push(state, `${refresh}();`);
852
+ emitDependencySubscriptions(state, dependencies, refresh, attribute.span);
853
+ registerRefresh(state, refresh);
854
+ }
855
+ const ranges = allowDirectory && state.emitDirectoryMarkers && directoryParentAllowed(node.tag) ? staticElementDirectory(node.children) : null;
856
+ if (ranges !== null) emitMountedDirectory(state, ranges, records, {
857
+ kind: "element",
858
+ name: varName
859
+ });
860
+ else {
861
+ const childNamespace = elementNamespace;
862
+ for (const child of emissionNodes(state, node.children)) {
863
+ const childSink = [];
864
+ emitNode(state, child, childSink, records, allowDirectory, childNamespace);
865
+ appendSinkToElement(state, childSink, varName);
866
+ }
867
+ }
868
+ sink.push({
869
+ kind: "node",
870
+ name: varName
871
+ });
872
+ return;
873
+ }
874
+ if (node.kind === "dynamic-text") {
875
+ const record = recordFor(state, node.expression, records);
876
+ if (record === null || !declareReads(state, record)) return;
877
+ const dependencies = dependencyNames(state, record);
878
+ if (dependencies === null) return;
879
+ let boundaryEnd = null;
880
+ if (state.emitTextBoundaries) {
881
+ const ordinal = textOrdinal(state, node);
882
+ if (ordinal === null) return;
883
+ const boundaryStart = nextName(state, "textStart");
884
+ push(state, `const ${boundaryStart} = emitComment(textSlotMarker(${ordinal}));`, node.span);
885
+ sink.push({
886
+ kind: "node",
887
+ name: boundaryStart
888
+ });
889
+ boundaryEnd = nextName(state, "textEnd");
890
+ const textVar = nextName(state, "text");
891
+ push(state, `const ${textVar} = document.createTextNode("");`, node.span);
892
+ sink.push({
893
+ kind: "node",
894
+ name: textVar
895
+ });
896
+ push(state, `const ${boundaryEnd} = emitComment(textSlotMarker(${ordinal}, true));`, node.span);
897
+ sink.push({
898
+ kind: "node",
899
+ name: boundaryEnd
900
+ });
901
+ if (dependencies.length === 0) {
902
+ if (!readsEntryLocal(state, record)) {
903
+ emitTextWrite(state, textVar, record);
904
+ return;
905
+ }
906
+ registerRefresh(state, emitLocalRefresh(state, node.span, () => emitTextWrite(state, textVar, record)));
907
+ return;
908
+ }
909
+ const refresh = nextName(state, "refresh");
910
+ push(state, `function ${refresh}() {`, node.span);
911
+ state.indent += 1;
912
+ emitTextWrite(state, textVar, record);
913
+ state.indent -= 1;
914
+ push(state, `}`);
915
+ push(state, `${refresh}();`);
916
+ emitDependencySubscriptions(state, dependencies, refresh, node.span);
917
+ registerRefresh(state, refresh);
918
+ return;
919
+ }
920
+ const textVar = nextName(state, "text");
921
+ push(state, `const ${textVar} = document.createTextNode("");`, node.span);
922
+ sink.push({
923
+ kind: "node",
924
+ name: textVar
925
+ });
926
+ if (dependencies.length === 0) {
927
+ if (!readsEntryLocal(state, record)) {
928
+ emitTextWrite(state, textVar, record);
929
+ return;
930
+ }
931
+ registerRefresh(state, emitLocalRefresh(state, node.span, () => emitTextWrite(state, textVar, record)));
932
+ return;
933
+ }
934
+ const refresh = nextName(state, "refresh");
935
+ push(state, `function ${refresh}() {`, node.span);
936
+ state.indent += 1;
937
+ emitTextWrite(state, textVar, record);
938
+ state.indent -= 1;
939
+ push(state, `}`);
940
+ push(state, `${refresh}();`);
941
+ emitDependencySubscriptions(state, dependencies, refresh, node.span);
942
+ registerRefresh(state, refresh);
943
+ return;
944
+ }
945
+ if (node.kind === "component-ref") {
946
+ emitComponentRef(state, node, sink, records);
947
+ return;
948
+ }
949
+ if (node.kind === "insertion-site") {
950
+ emitInsertionSite(state, node, sink);
951
+ return;
952
+ }
953
+ if (node.kind === "children-projection") {
954
+ emitChildrenProjection(state, node, sink);
955
+ return;
956
+ }
957
+ diag(state, "OEIR_LEAF_UNSUPPORTED_NODE", `node '${node.kind}' is outside the generated-leaf slice`, span);
958
+ }
959
+ //#endregion
960
+ export { appendSinkToArray, clientOnlyAttributeAdmitted, emitBranchRelease, emitBranchRollback, emitClientOnlyInstall, emitEntryBuilder, emitListKeyFunction, emitMountedDirectory, emitNode, emitNodesBuilder, emitRecipes, emitsRetentionReleaseText };