@openelement/compiler 1.0.0-alpha.14 → 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 (56) hide show
  1. package/package.json +4 -2
  2. package/src/compiler.d.ts +12 -6
  3. package/src/compiler.js +11 -6
  4. package/src/internal/compiler/compiled-artifact-class-candidates.d.ts +11 -0
  5. package/src/internal/compiler/compiled-artifact-class-candidates.js +46 -0
  6. package/src/internal/compiler/compiled-artifact-style-requests.d.ts +26 -7
  7. package/src/internal/compiler/compiled-artifact-style-requests.js +57 -9
  8. package/src/internal/compiler/plugin.d.ts +68 -49
  9. package/src/internal/compiler/plugin.js +93 -82
  10. package/src/internal/compiler/semantic-core/analyze-module.d.ts +73 -0
  11. package/src/internal/compiler/semantic-core/analyze-module.js +29 -379
  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/diagnostics/index.d.ts +21 -1
  15. package/src/internal/compiler/semantic-core/diagnostics/index.js +32 -1
  16. package/src/internal/compiler/semantic-core/expression-lowering.js +469 -0
  17. package/src/internal/compiler/semantic-core/expression-registry.js +165 -0
  18. package/src/internal/compiler/semantic-core/function-frontend.js +1229 -0
  19. package/src/internal/compiler/semantic-core/island-policy-statement.js +52 -0
  20. package/src/internal/compiler/semantic-core/jsx-module-id.d.ts +4 -0
  21. package/src/internal/compiler/semantic-core/jsx-module-id.js +7 -0
  22. package/src/internal/compiler/semantic-core/lower-program.d.ts +220 -0
  23. package/src/internal/compiler/semantic-core/lower-program.js +350 -142
  24. package/src/internal/compiler/semantic-core/module-analysis.d.ts +82 -4
  25. package/src/internal/compiler/semantic-core/module-analysis.js +88 -19
  26. package/src/internal/compiler/semantic-core/parse-module.js +16 -16
  27. package/src/internal/compiler/semantic-core/scope-resolution.d.ts +208 -0
  28. package/src/internal/compiler/semantic-core/scope-resolution.js +1121 -0
  29. package/src/internal/compiler/semantic-core/server-component.d.ts +129 -0
  30. package/src/internal/compiler/semantic-core/server-component.js +587 -0
  31. package/src/internal/compiler/semantic-core/type-check.js +1 -1
  32. package/src/internal/compiler/semantic-core/type-erasure.js +156 -0
  33. package/src/internal/compiler/semantic-ir/generated-element/compile.d.ts +61 -0
  34. package/src/internal/compiler/semantic-ir/generated-element/compile.js +137 -0
  35. package/src/internal/compiler/semantic-ir/generated-element/contract.d.ts +201 -0
  36. package/src/internal/compiler/semantic-ir/generated-element/contract.js +147 -0
  37. package/src/internal/compiler/semantic-ir/generated-element/emit.js +452 -0
  38. package/src/internal/compiler/semantic-ir/generated-element/site.js +1195 -0
  39. package/src/internal/compiler/semantic-ir/generated-leaf/claim-emission.js +1372 -0
  40. package/src/internal/compiler/semantic-ir/generated-leaf/codegen-literals.js +31 -0
  41. package/src/internal/compiler/semantic-ir/generated-leaf/component-emission.js +647 -0
  42. package/src/internal/compiler/semantic-ir/generated-leaf/emission.js +388 -0
  43. package/src/internal/compiler/semantic-ir/generated-leaf/generate.js +395 -0
  44. package/src/internal/compiler/semantic-ir/generated-leaf/instance-emission.js +865 -0
  45. package/src/internal/compiler/semantic-ir/generated-leaf/mount-emission.js +960 -0
  46. package/src/internal/compiler/semantic-ir/generated-leaf/server-emission.js +1011 -0
  47. package/src/internal/compiler/semantic-ir/generated-leaf/structure.js +714 -0
  48. package/src/internal/compiler/style-requests.d.ts +35 -7
  49. package/src/internal/compiler/style-requests.js +57 -11
  50. package/src/internal/compiler/tailwind-candidates.d.ts +84 -0
  51. package/src/internal/compiler/tailwind-candidates.js +54 -0
  52. package/src/internal/compiler/semantic-core/compile.d.ts +0 -39
  53. package/src/internal/compiler/semantic-core/compile.js +0 -98
  54. package/src/internal/compiler/semantic-core/emit-module.js +0 -313
  55. package/src/internal/compiler/semantic-core/emit-program.js +0 -121
  56. package/src/internal/compiler/semantic-core/style-imports.js +0 -87
@@ -0,0 +1,395 @@
1
+ import { SourceMapSegmentBuilder } from "../../semantic-core/source-map.js";
2
+ import { fragmentLineCount, importContexts } from "./emission.js";
3
+ import { collectStaticClassCandidates, moduleUsesSharedClaimCursor } from "./structure.js";
4
+ import { findMountCycle, moduleUsesContext, planRetention } from "./instance-emission.js";
5
+ import { CLAIM_ACTIVATION } from "./claim-emission.js";
6
+ import { emitComponent } from "./component-emission.js";
7
+ import ts from "typescript";
8
+ //#region src/internal/compiler/semantic-ir/generated-leaf/generate.ts
9
+ /** Coordinate code generation for a validated function-component module. */
10
+ /** Compiler-private runtime entry the emitted module imports from. */
11
+ const GENERATED_LEAF_RUNTIME_SPECIFIER = "@openelement/element/internal/compiled/leaf-runtime";
12
+ const RUNTIME_IMPORTS = [
13
+ "assertSafeUrlAttribute",
14
+ "attributeValueOf",
15
+ "bindSignalWrites",
16
+ "createRecipe",
17
+ "displayValue",
18
+ "insertRecipe",
19
+ "removeNodes"
20
+ ];
21
+ /**
22
+ * Runtime symbols a generated module imports only when it uses them. The
23
+ * predicate reads the emitted body, so a new emission site cannot forget to
24
+ * register its import (the symbol is named in the call it emits).
25
+ */
26
+ const OPTIONAL_RUNTIME_IMPORTS = [
27
+ {
28
+ symbol: "claimRecipe",
29
+ predicate: (_body, usesClaimRecipe) => usesClaimRecipe
30
+ },
31
+ {
32
+ symbol: "leafClaimMismatch",
33
+ predicate: (body) => body.includes("leafClaimMismatch(")
34
+ },
35
+ {
36
+ symbol: "leafRuntimeError",
37
+ predicate: (body) => body.includes("leafRuntimeError(")
38
+ },
39
+ {
40
+ symbol: "selectOptionSelected",
41
+ predicate: (body) => body.includes("selectOptionSelected(")
42
+ },
43
+ {
44
+ symbol: "selectOptionValue",
45
+ predicate: (body) => body.includes("selectOptionValue(")
46
+ },
47
+ {
48
+ symbol: "nestedHost",
49
+ predicate: (body) => body.includes("nestedHost(")
50
+ },
51
+ {
52
+ symbol: "reconcileEachEntries",
53
+ predicate: (body) => body.includes("reconcileEachEntries(")
54
+ },
55
+ {
56
+ symbol: "signal",
57
+ predicate: (body) => body.includes("= signal((")
58
+ },
59
+ {
60
+ symbol: "computed",
61
+ predicate: (body) => body.includes("= computed(")
62
+ },
63
+ {
64
+ symbol: "assertElementInHtmlNamespace",
65
+ predicate: (body) => body.includes("assertElementInHtmlNamespace(")
66
+ },
67
+ {
68
+ symbol: "assertElementInNamespace",
69
+ predicate: (body) => body.includes("assertElementInNamespace(")
70
+ },
71
+ {
72
+ symbol: "assertStaticAttributeSeed",
73
+ predicate: (body) => body.includes("assertStaticAttributeSeed(")
74
+ },
75
+ {
76
+ symbol: "assertNestedHostStaticAttributeSeed",
77
+ predicate: (body) => body.includes("assertNestedHostStaticAttributeSeed(")
78
+ },
79
+ {
80
+ symbol: "isExpandedLightRootHost",
81
+ predicate: (body) => body.includes("isExpandedLightRootHost(")
82
+ },
83
+ {
84
+ symbol: "emitComment",
85
+ predicate: (body) => body.includes("emitComment(")
86
+ },
87
+ {
88
+ symbol: "escapeAttrValue",
89
+ predicate: (body) => body.includes("escapeAttrValue(")
90
+ },
91
+ {
92
+ symbol: "escapeText",
93
+ predicate: (body) => body.includes("escapeText(")
94
+ },
95
+ {
96
+ symbol: "rangeMarker",
97
+ predicate: (body) => body.includes("rangeMarker(")
98
+ },
99
+ {
100
+ symbol: "MARKERS",
101
+ predicate: (body) => body.includes("MARKERS.")
102
+ },
103
+ {
104
+ symbol: "markerText",
105
+ predicate: (body) => body.includes("markerText(")
106
+ },
107
+ {
108
+ symbol: "assertTextSlotMarker",
109
+ predicate: (body) => body.includes("assertTextSlotMarker(")
110
+ },
111
+ {
112
+ symbol: "textSlotMarker",
113
+ predicate: (body) => body.includes("textSlotMarker(")
114
+ },
115
+ {
116
+ symbol: "slotAnchorMarker",
117
+ predicate: (body) => body.includes("slotAnchorMarker(")
118
+ },
119
+ {
120
+ symbol: "assertWhenAnchor",
121
+ predicate: (body) => body.includes("assertWhenAnchor(")
122
+ },
123
+ {
124
+ symbol: "assertWhenEndMarker",
125
+ predicate: (body) => body.includes("assertWhenEndMarker(")
126
+ },
127
+ {
128
+ symbol: "isWhenEndMarker",
129
+ predicate: (body) => body.includes("isWhenEndMarker(")
130
+ },
131
+ {
132
+ symbol: "assertEachAnchor",
133
+ predicate: (body) => body.includes("assertEachAnchor(")
134
+ },
135
+ {
136
+ symbol: "assertEachEndMarker",
137
+ predicate: (body) => body.includes("assertEachEndMarker(")
138
+ },
139
+ {
140
+ symbol: "parseEntryKeyMarker",
141
+ predicate: (body) => body.includes("parseEntryKeyMarker(")
142
+ },
143
+ {
144
+ symbol: "readEntryKeyMarker",
145
+ predicate: (body) => body.includes("readEntryKeyMarker(")
146
+ },
147
+ {
148
+ symbol: "entrySeedMismatch",
149
+ predicate: (body) => body.includes("entrySeedMismatch(")
150
+ },
151
+ {
152
+ symbol: "entryKeyMarker",
153
+ predicate: (body) => body.includes("entryKeyMarker(")
154
+ },
155
+ {
156
+ symbol: "encodeListKey",
157
+ predicate: (body) => body.includes("encodeListKey(")
158
+ },
159
+ {
160
+ symbol: "tryEncodeListKey",
161
+ predicate: (body) => body.includes("tryEncodeListKey(")
162
+ },
163
+ {
164
+ symbol: "runUntracked",
165
+ predicate: (body) => body.includes("runUntracked(")
166
+ },
167
+ {
168
+ symbol: "guardUpdate",
169
+ predicate: (body) => body.includes("guardUpdate(")
170
+ },
171
+ {
172
+ symbol: "claimStaticSiblingRange",
173
+ predicate: (_body, _usesClaimRecipe, usesDirectory) => usesDirectory
174
+ },
175
+ {
176
+ symbol: "openStaticSiblingDirectory",
177
+ predicate: (_body, _usesClaimRecipe, usesDirectory) => usesDirectory
178
+ },
179
+ {
180
+ symbol: "retainChildInstance",
181
+ predicate: (body) => body.includes("retainChildInstance(")
182
+ },
183
+ {
184
+ symbol: "releaseChildInstance",
185
+ predicate: (body) => body.includes("releaseChildInstance(")
186
+ },
187
+ {
188
+ symbol: "releaseUnderStructuralScope",
189
+ predicate: (body) => body.includes("releaseUnderStructuralScope(")
190
+ },
191
+ {
192
+ symbol: "beginInstancePass",
193
+ predicate: (body) => body.includes("beginInstancePass(")
194
+ },
195
+ {
196
+ symbol: "endInstancePass",
197
+ predicate: (body) => body.includes("endInstancePass(")
198
+ },
199
+ {
200
+ symbol: "joinInstancePass",
201
+ predicate: (body) => body.includes("joinInstancePass(")
202
+ },
203
+ {
204
+ symbol: "createRenderContextScope",
205
+ predicate: (body) => body.includes("createRenderContextScope(")
206
+ }
207
+ ];
208
+ /** Fail-closed parse check of the emitted module text (no execution). */
209
+ function parsesAsModule(text) {
210
+ return (ts.createSourceFile("generated-leaf.js", text, ts.ScriptTarget.ES2022, true, ts.ScriptKind.JS).parseDiagnostics ?? []).length === 0;
211
+ }
212
+ /**
213
+ * Emit the generated leaf module for one M03 `FunctionModelModule`.
214
+ * Deterministic for the same model/registry; unsupported shapes fail closed.
215
+ */
216
+ /** The import table of one module, with the used entries recorded during emit. */
217
+ function createImportContext(module) {
218
+ const byLocal = /* @__PURE__ */ new Map();
219
+ for (const entry of module.imports ?? []) if (!byLocal.has(entry.local)) byLocal.set(entry.local, entry);
220
+ return {
221
+ byLocal,
222
+ used: /* @__PURE__ */ new Map()
223
+ };
224
+ }
225
+ /** One re-emitted static import, verbatim in form and specifier. */
226
+ function emitAuthoredImport(entry) {
227
+ const from = JSON.stringify(entry.specifier);
228
+ switch (entry.form) {
229
+ case "default": return `import ${entry.local} from ${from};`;
230
+ case "namespace": return `import * as ${entry.local} from ${from};`;
231
+ default: return entry.imported === entry.local ? `import { ${entry.local} } from ${from};` : `import { ${entry.imported} as ${entry.local} } from ${from};`;
232
+ }
233
+ }
234
+ function generateLeafModule(module, options = {}) {
235
+ const records = new Map(module.expressions.expressions.map((record) => [record.id, record]));
236
+ if (module.components.length === 0) return {
237
+ module: null,
238
+ sourceMap: null,
239
+ placements: [],
240
+ rootInstanceFactory: null,
241
+ hostFeatures: null,
242
+ requiredBindings: [],
243
+ requiredProps: [],
244
+ requiresPropsChanged: false,
245
+ classCandidates: [],
246
+ diagnostics: [{
247
+ severity: "error",
248
+ code: "OEIR_LEAF_MISSING_COMPONENT",
249
+ message: "the module has no function component to generate",
250
+ span: {
251
+ file: module.file,
252
+ start: 0,
253
+ end: 0,
254
+ line: 1,
255
+ column: 1
256
+ },
257
+ origin: "module"
258
+ }]
259
+ };
260
+ const componentByName = new Map(module.components.map((component) => [component.name, component]));
261
+ const importContext = createImportContext(module);
262
+ for (const component of module.components) importContexts.set(component, importContext);
263
+ const cycle = findMountCycle(module);
264
+ if (cycle !== null) return {
265
+ module: null,
266
+ sourceMap: null,
267
+ placements: [],
268
+ rootInstanceFactory: null,
269
+ hostFeatures: null,
270
+ requiredBindings: [],
271
+ requiredProps: [],
272
+ requiresPropsChanged: false,
273
+ classCandidates: [],
274
+ diagnostics: [cycle]
275
+ };
276
+ const chunks = [];
277
+ const diagnostics = [];
278
+ const requiredBindings = /* @__PURE__ */ new Set();
279
+ const requiredProps = /* @__PURE__ */ new Set();
280
+ const segments = [];
281
+ const placements = [];
282
+ let requiresPropsChanged = false;
283
+ let usesStaticSiblingDirectoryConsumer = false;
284
+ let usesClaimRecipe = false;
285
+ let usesLeafClaimMismatch = false;
286
+ let rootClaim = false;
287
+ let rootInstanceFactory = null;
288
+ const sharedClaimCursor = moduleUsesSharedClaimCursor(module, componentByName);
289
+ const retention = planRetention(module, componentByName, records, options.rootInstance);
290
+ for (const component of module.components) {
291
+ const emitted = emitComponent(module.file, component, records, componentByName, sharedClaimCursor, options.rootInstance, retention, options.claimStyleLinkPrefix === true, options.lightRoot === true && component.name === options.rootInstance);
292
+ diagnostics.push(...emitted.diagnostics);
293
+ if (emitted.diagnostics.length > 0) continue;
294
+ rootInstanceFactory ??= emitted.rootInstanceFactory;
295
+ if (component.name === options.rootInstance) rootClaim = emitted.hasClaim === true;
296
+ for (const name of emitted.requiredBindings) requiredBindings.add(name);
297
+ for (const name of emitted.requiredProps) requiredProps.add(name);
298
+ requiresPropsChanged ||= emitted.requiresPropsChanged;
299
+ usesStaticSiblingDirectoryConsumer ||= emitted.usesStaticSiblingDirectoryConsumer === true;
300
+ usesClaimRecipe ||= emitted.usesClaimRecipe === true;
301
+ usesLeafClaimMismatch ||= emitted.usesLeafClaimMismatch === true;
302
+ const lineOffset = fragmentLineCount(chunks);
303
+ segments.push(...emitted.segments.map((segment) => ({
304
+ ...segment,
305
+ generatedLine: segment.generatedLine + lineOffset
306
+ })));
307
+ placements.push(...emitted.placements.map((placement) => ({
308
+ ...placement,
309
+ generatedLine: placement.generatedLine + lineOffset
310
+ })));
311
+ chunks.push(...emitted.lines);
312
+ }
313
+ if (diagnostics.length > 0) return {
314
+ module: null,
315
+ sourceMap: null,
316
+ placements: [...placements],
317
+ rootInstanceFactory: null,
318
+ hostFeatures: null,
319
+ classCandidates: [],
320
+ requiredBindings: [...requiredBindings].sort(),
321
+ requiredProps: [...requiredProps].sort(),
322
+ requiresPropsChanged,
323
+ diagnostics
324
+ };
325
+ const body = chunks.join("\n");
326
+ const runtimeImports = [...RUNTIME_IMPORTS];
327
+ for (const optional of OPTIONAL_RUNTIME_IMPORTS) if (optional.predicate(body, usesClaimRecipe, usesStaticSiblingDirectoryConsumer)) runtimeImports.push(optional.symbol);
328
+ if (runtimeImports.length !== RUNTIME_IMPORTS.length) runtimeImports.sort();
329
+ const authoredImports = [...importContext.used.values()].sort((left, right) => left.specifier === right.specifier ? left.imported < right.imported ? -1 : left.imported > right.imported ? 1 : 0 : left.specifier < right.specifier ? -1 : 1).map(emitAuthoredImport);
330
+ const headerLines = [
331
+ "// <generated by openelement M07a leaf-claim (mount M06b2a); do not edit>",
332
+ `import { ${runtimeImports.join(", ")} } from ${JSON.stringify(GENERATED_LEAF_RUNTIME_SPECIFIER)};`,
333
+ ...authoredImports,
334
+ ""
335
+ ];
336
+ if (sharedClaimCursor) headerLines.push("let __oe_claimCursor = null;", `let ${CLAIM_ACTIVATION} = null;`, "");
337
+ const text = [...headerLines, ...chunks].join("\n");
338
+ if (!parsesAsModule(text)) return {
339
+ module: null,
340
+ sourceMap: null,
341
+ placements: [...placements],
342
+ rootInstanceFactory: null,
343
+ hostFeatures: null,
344
+ classCandidates: [],
345
+ requiredBindings: [...requiredBindings].sort(),
346
+ requiredProps: [...requiredProps].sort(),
347
+ requiresPropsChanged,
348
+ diagnostics: [{
349
+ severity: "error",
350
+ code: "OEIR_LEAF_INVALID_ARTIFACT",
351
+ message: "the emitted module does not parse as JavaScript",
352
+ span: {
353
+ file: module.file,
354
+ start: 0,
355
+ end: 0,
356
+ line: 1,
357
+ column: 1
358
+ },
359
+ origin: "module"
360
+ }]
361
+ };
362
+ const headerShift = headerLines.length;
363
+ const moduleSegments = segments.map((segment) => ({
364
+ ...segment,
365
+ generatedLine: segment.generatedLine + headerShift
366
+ }));
367
+ const modulePlacements = placements.map((placement) => ({
368
+ ...placement,
369
+ generatedLine: placement.generatedLine + headerShift
370
+ }));
371
+ let sourceMap = null;
372
+ if (options.authoredSource !== void 0) {
373
+ const builder = new SourceMapSegmentBuilder();
374
+ for (const segment of moduleSegments) builder.add(segment);
375
+ sourceMap = builder.build(module.file, options.authoredSource);
376
+ }
377
+ return {
378
+ module: text,
379
+ sourceMap,
380
+ placements: modulePlacements,
381
+ rootInstanceFactory,
382
+ hostFeatures: {
383
+ context: moduleUsesContext({ componentByName }),
384
+ ledger: runtimeImports.includes("beginInstancePass"),
385
+ rootClaim
386
+ },
387
+ requiredBindings: [...requiredBindings].sort(),
388
+ requiredProps: [...requiredProps].sort(),
389
+ requiresPropsChanged,
390
+ classCandidates: collectStaticClassCandidates(module),
391
+ diagnostics: []
392
+ };
393
+ }
394
+ //#endregion
395
+ export { GENERATED_LEAF_RUNTIME_SPECIFIER, collectStaticClassCandidates, generateLeafModule };