@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.
- package/package.json +4 -2
- package/src/compiler.d.ts +12 -6
- package/src/compiler.js +11 -6
- package/src/internal/compiler/compiled-artifact-class-candidates.d.ts +11 -0
- package/src/internal/compiler/compiled-artifact-class-candidates.js +46 -0
- package/src/internal/compiler/compiled-artifact-style-requests.d.ts +26 -7
- package/src/internal/compiler/compiled-artifact-style-requests.js +57 -9
- package/src/internal/compiler/plugin.d.ts +68 -49
- package/src/internal/compiler/plugin.js +93 -82
- package/src/internal/compiler/semantic-core/analyze-module.d.ts +73 -0
- package/src/internal/compiler/semantic-core/analyze-module.js +29 -379
- package/src/internal/compiler/semantic-core/attribute-classification.js +92 -0
- package/src/internal/compiler/semantic-core/compiler-diagnostics.js +2 -16
- package/src/internal/compiler/semantic-core/diagnostics/index.d.ts +21 -1
- package/src/internal/compiler/semantic-core/diagnostics/index.js +32 -1
- package/src/internal/compiler/semantic-core/expression-lowering.js +469 -0
- package/src/internal/compiler/semantic-core/expression-registry.js +165 -0
- package/src/internal/compiler/semantic-core/function-frontend.js +1229 -0
- package/src/internal/compiler/semantic-core/island-policy-statement.js +52 -0
- package/src/internal/compiler/semantic-core/jsx-module-id.d.ts +4 -0
- package/src/internal/compiler/semantic-core/jsx-module-id.js +7 -0
- package/src/internal/compiler/semantic-core/lower-program.d.ts +220 -0
- package/src/internal/compiler/semantic-core/lower-program.js +350 -142
- package/src/internal/compiler/semantic-core/module-analysis.d.ts +82 -4
- package/src/internal/compiler/semantic-core/module-analysis.js +88 -19
- package/src/internal/compiler/semantic-core/parse-module.js +16 -16
- package/src/internal/compiler/semantic-core/scope-resolution.d.ts +208 -0
- package/src/internal/compiler/semantic-core/scope-resolution.js +1121 -0
- package/src/internal/compiler/semantic-core/server-component.d.ts +129 -0
- package/src/internal/compiler/semantic-core/server-component.js +587 -0
- package/src/internal/compiler/semantic-core/type-check.js +1 -1
- package/src/internal/compiler/semantic-core/type-erasure.js +156 -0
- package/src/internal/compiler/semantic-ir/generated-element/compile.d.ts +61 -0
- package/src/internal/compiler/semantic-ir/generated-element/compile.js +137 -0
- package/src/internal/compiler/semantic-ir/generated-element/contract.d.ts +201 -0
- package/src/internal/compiler/semantic-ir/generated-element/contract.js +147 -0
- package/src/internal/compiler/semantic-ir/generated-element/emit.js +452 -0
- package/src/internal/compiler/semantic-ir/generated-element/site.js +1195 -0
- package/src/internal/compiler/semantic-ir/generated-leaf/claim-emission.js +1372 -0
- package/src/internal/compiler/semantic-ir/generated-leaf/codegen-literals.js +31 -0
- package/src/internal/compiler/semantic-ir/generated-leaf/component-emission.js +647 -0
- package/src/internal/compiler/semantic-ir/generated-leaf/emission.js +388 -0
- package/src/internal/compiler/semantic-ir/generated-leaf/generate.js +395 -0
- package/src/internal/compiler/semantic-ir/generated-leaf/instance-emission.js +865 -0
- package/src/internal/compiler/semantic-ir/generated-leaf/mount-emission.js +960 -0
- package/src/internal/compiler/semantic-ir/generated-leaf/server-emission.js +1011 -0
- package/src/internal/compiler/semantic-ir/generated-leaf/structure.js +714 -0
- package/src/internal/compiler/style-requests.d.ts +35 -7
- package/src/internal/compiler/style-requests.js +57 -11
- package/src/internal/compiler/tailwind-candidates.d.ts +84 -0
- package/src/internal/compiler/tailwind-candidates.js +54 -0
- package/src/internal/compiler/semantic-core/compile.d.ts +0 -39
- package/src/internal/compiler/semantic-core/compile.js +0 -98
- package/src/internal/compiler/semantic-core/emit-module.js +0 -313
- package/src/internal/compiler/semantic-core/emit-program.js +0 -121
- package/src/internal/compiler/semantic-core/style-imports.js +0 -87
|
@@ -1,313 +0,0 @@
|
|
|
1
|
-
import { isCompileTimeOnlyImport } from "./module-analysis.js";
|
|
2
|
-
import { SYNTHESIZED_COMPUTED_LOCAL } from "./lower-program.js";
|
|
3
|
-
import { SourceMapSegmentBuilder } from "./source-map.js";
|
|
4
|
-
import ts from "typescript";
|
|
5
|
-
import { COMPILED_MODULE_ABI_VERSION, PART_PROGRAM_VERSION } from "@openelement/protocol/part-program";
|
|
6
|
-
//#region src/internal/compiler/semantic-core/emit-module.ts
|
|
7
|
-
/**
|
|
8
|
-
* Generated-module emission for the compiled grammar (#1473 split): renders
|
|
9
|
-
* the compiled module text with
|
|
10
|
-
* verbatim copies, synthesized statics and forwarding handlers, each mapped
|
|
11
|
-
* through a real Source Map v3 segment.
|
|
12
|
-
*/
|
|
13
|
-
/**
|
|
14
|
-
* Copy one source import statement into the generated module. Compile-time-
|
|
15
|
-
* only intrinsic bindings (element/property from '@openelement/element') are
|
|
16
|
-
* stripped: the decorators they powered are erased by compilation and the
|
|
17
|
-
* runtime package exports neither, so keeping them would emit an unresolvable
|
|
18
|
-
* runtime import. A statement whose entire clause stripped away is dropped.
|
|
19
|
-
*/
|
|
20
|
-
function rewriteImportForGeneratedModule(sf, statement) {
|
|
21
|
-
if (!ts.isStringLiteral(statement.moduleSpecifier)) return statement.getText(sf);
|
|
22
|
-
const module = statement.moduleSpecifier.text;
|
|
23
|
-
const clause = statement.importClause;
|
|
24
|
-
if (!clause) return statement.getText(sf);
|
|
25
|
-
const bindings = clause.namedBindings;
|
|
26
|
-
if (!bindings || !ts.isNamedImports(bindings)) return statement.getText(sf);
|
|
27
|
-
const kept = bindings.elements.filter((element) => !isCompileTimeOnlyImport(module, element.propertyName?.text ?? element.name.text));
|
|
28
|
-
if (kept.length === bindings.elements.length) return statement.getText(sf);
|
|
29
|
-
const parts = [];
|
|
30
|
-
if (clause.name) parts.push(clause.name.text);
|
|
31
|
-
if (kept.length > 0) {
|
|
32
|
-
const specifiers = kept.map((element) => `${element.isTypeOnly ? "type " : ""}${element.propertyName ? `${element.propertyName.text} as ` : ""}${element.name.text}`);
|
|
33
|
-
parts.push(`{ ${specifiers.join(", ")} }`);
|
|
34
|
-
}
|
|
35
|
-
if (parts.length === 0) return null;
|
|
36
|
-
return `import ${clause.isTypeOnly ? "type " : ""}${parts.join(", ")} from '${module}';`;
|
|
37
|
-
}
|
|
38
|
-
function encodeBase64(value) {
|
|
39
|
-
const bytes = new TextEncoder().encode(value);
|
|
40
|
-
let binary = "";
|
|
41
|
-
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
42
|
-
return btoa(binary);
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* The platform lifecycle callbacks whose authored bodies the compiler wraps
|
|
46
|
-
* in the error-boundary capture contract. These are the callbacks the platform
|
|
47
|
-
* invokes outside any application stack — a throw inside them would otherwise
|
|
48
|
-
* escape straight to window.onerror with no boundary the way a render throw
|
|
49
|
-
* in React reaches the nearest error boundary. Everything else (event
|
|
50
|
-
* handlers, plain author methods) keeps the verbatim emission: those already
|
|
51
|
-
* run inside caller stacks, and the runtime capture points (the facade
|
|
52
|
-
* property setter, the kernel update sink) own their failure modes.
|
|
53
|
-
*/
|
|
54
|
-
const CAPTURE_WRAPPED_LIFECYCLE_CALLBACKS = /* @__PURE__ */ new Set([
|
|
55
|
-
"connectedCallback",
|
|
56
|
-
"disconnectedCallback",
|
|
57
|
-
"adoptedCallback",
|
|
58
|
-
"attributeChangedCallback",
|
|
59
|
-
"formAssociatedCallback",
|
|
60
|
-
"formResetCallback",
|
|
61
|
-
"formStateRestoreCallback"
|
|
62
|
-
]);
|
|
63
|
-
function generatedHandlerText(handler) {
|
|
64
|
-
const action = handler.action;
|
|
65
|
-
if (action.kind === "method") return ` ${handler.name}(): void { this.${action.name}(); }`;
|
|
66
|
-
if (action.kind === "call") return ` ${handler.name}(): void { this.${action.name}(); }`;
|
|
67
|
-
if (action.kind === "increment") return ` ${handler.name}(): void { this.${action.signal}++; }`;
|
|
68
|
-
if (action.kind === "decrement") return ` ${handler.name}(): void { this.${action.signal}--; }`;
|
|
69
|
-
if (action.kind === "add") return ` ${handler.name}(): void { this.${action.signal} += ${action.value}; }`;
|
|
70
|
-
if (action.kind === "subtract") return ` ${handler.name}(): void { this.${action.signal} -= ${action.value}; }`;
|
|
71
|
-
if (action.kind === "assign") return ` ${handler.name}(): void { this.${action.signal} = ${JSON.stringify(action.value)}; }`;
|
|
72
|
-
return ` ${handler.name}(): void {}`;
|
|
73
|
-
}
|
|
74
|
-
/** Render the compiled module text and its inline Source Map v3. */
|
|
75
|
-
function emitCompiledModule(input) {
|
|
76
|
-
const { sf, source, fileName, analyzed, lowering, program, programJson, propertiesJson, metadataJson, observedJson } = input;
|
|
77
|
-
const { fields, methods, render, stylesText, stylesNode, stylesTypeText, passthroughStatements, decorator, classNode, tag, className, isDefaultExport, openElementLocalName, delegatesFocus, formAssociated } = analyzed;
|
|
78
|
-
const segments = new SourceMapSegmentBuilder();
|
|
79
|
-
const codeLines = [];
|
|
80
|
-
const nodePosition = (node) => {
|
|
81
|
-
const position = sf.getLineAndCharacterOfPosition(node.getStart(sf));
|
|
82
|
-
return {
|
|
83
|
-
line: position.line + 1,
|
|
84
|
-
column: position.character
|
|
85
|
-
};
|
|
86
|
-
};
|
|
87
|
-
const mapLineAt = (generatedLine, generatedColumn, node, name) => {
|
|
88
|
-
const position = nodePosition(node);
|
|
89
|
-
segments.add({
|
|
90
|
-
generatedLine,
|
|
91
|
-
generatedColumn,
|
|
92
|
-
sourceLine: position.line,
|
|
93
|
-
sourceColumn: position.column,
|
|
94
|
-
...name === void 0 ? {} : { name }
|
|
95
|
-
});
|
|
96
|
-
};
|
|
97
|
-
/**
|
|
98
|
-
* Continuation lines of a verbatim-copied block: text line i is authored
|
|
99
|
-
* line (start + i) verbatim, so generated column (prefix + whitespace) maps
|
|
100
|
-
* to authored column (whitespace) — the first non-whitespace character.
|
|
101
|
-
*/
|
|
102
|
-
const mapContinuationLines = (text, firstGeneratedLine, node, prefix) => {
|
|
103
|
-
const position = nodePosition(node);
|
|
104
|
-
const lines = text.split("\n");
|
|
105
|
-
for (let index = 1; index < lines.length; index++) {
|
|
106
|
-
const whitespace = /^\s*/.exec(lines[index])[0].length;
|
|
107
|
-
segments.add({
|
|
108
|
-
generatedLine: firstGeneratedLine + index,
|
|
109
|
-
generatedColumn: prefix + whitespace,
|
|
110
|
-
sourceLine: position.line + index,
|
|
111
|
-
sourceColumn: whitespace
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
/** Push a line or block: multiline text always splits so codeLines.length tracks emitted lines. */
|
|
116
|
-
const push = (text) => {
|
|
117
|
-
for (const line of text.split("\n")) codeLines.push(line);
|
|
118
|
-
};
|
|
119
|
-
/** Push a verbatim copy of a node's text (optionally line-prefixed). */
|
|
120
|
-
const pushVerbatim = (text, node, prefix = "", name) => {
|
|
121
|
-
const firstGeneratedLine = codeLines.length + 1;
|
|
122
|
-
push(text.split("\n").map((line) => prefix + line).join("\n"));
|
|
123
|
-
mapLineAt(firstGeneratedLine, prefix.length, node, name);
|
|
124
|
-
mapContinuationLines(text, firstGeneratedLine, node, prefix.length);
|
|
125
|
-
};
|
|
126
|
-
/** Push one synthesized line embedding a verbatim value, mapping both. */
|
|
127
|
-
const pushDerivedLine = (head, valueText, tail, nameNode, nameColumn, name, valueNode) => {
|
|
128
|
-
const generatedLine = codeLines.length + 1;
|
|
129
|
-
push(`${head}${valueText}${tail}`);
|
|
130
|
-
mapLineAt(generatedLine, nameColumn, nameNode, name);
|
|
131
|
-
if (valueNode !== void 0) {
|
|
132
|
-
mapLineAt(generatedLine, head.length, valueNode);
|
|
133
|
-
mapContinuationLines(valueText, generatedLine, valueNode, 0);
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
/** Push a synthesized block whose first line traces to `node`. */
|
|
137
|
-
const pushDerivedBlock = (text, node, name) => {
|
|
138
|
-
const firstGeneratedLine = codeLines.length + 1;
|
|
139
|
-
push(text);
|
|
140
|
-
mapLineAt(firstGeneratedLine, 0, node, name);
|
|
141
|
-
};
|
|
142
|
-
push(`// <auto-generated by open:compiled-element; part-program format ${PART_PROGRAM_VERSION} / module ABI ${COMPILED_MODULE_ABI_VERSION} - do not edit>`);
|
|
143
|
-
for (const statement of sf.statements) {
|
|
144
|
-
if (!ts.isImportDeclaration(statement)) continue;
|
|
145
|
-
const rewritten = rewriteImportForGeneratedModule(sf, statement);
|
|
146
|
-
if (rewritten !== null) pushVerbatim(rewritten, statement);
|
|
147
|
-
}
|
|
148
|
-
let readonlySignalLocal = null;
|
|
149
|
-
for (const statement of sf.statements) {
|
|
150
|
-
if (!ts.isImportDeclaration(statement)) continue;
|
|
151
|
-
if (!ts.isStringLiteral(statement.moduleSpecifier) || statement.moduleSpecifier.text !== "@openelement/element") continue;
|
|
152
|
-
const bindings = statement.importClause?.namedBindings;
|
|
153
|
-
if (!bindings || !ts.isNamedImports(bindings)) continue;
|
|
154
|
-
for (const element of bindings.elements) if ((element.propertyName?.text ?? element.name.text) === "ReadonlySignal") readonlySignalLocal = element.name.text;
|
|
155
|
-
}
|
|
156
|
-
const needsComputedTypes = fields.some((field) => field.computed);
|
|
157
|
-
const signalType = readonlySignalLocal ?? "ReadonlySignal";
|
|
158
|
-
if (needsComputedTypes && readonlySignalLocal === null) push(`import type { ReadonlySignal } from '@openelement/element';`);
|
|
159
|
-
if (lowering.synthesizedFields.length > 0 && !lowering.sourceBindsComputed) push(`import { computed as ${SYNTHESIZED_COMPUTED_LOCAL} } from '@openelement/element';`);
|
|
160
|
-
push("");
|
|
161
|
-
for (const statement of passthroughStatements) pushVerbatim(statement.getText(sf), statement);
|
|
162
|
-
if (passthroughStatements.length > 0) push("");
|
|
163
|
-
const programStartLine = codeLines.length + 1;
|
|
164
|
-
pushDerivedBlock(`const __partProgram = ${programJson};`, render);
|
|
165
|
-
const programJsonPosition = (offset) => {
|
|
166
|
-
const before = programJson.slice(0, offset);
|
|
167
|
-
return {
|
|
168
|
-
line: programStartLine + before.split("\n").length - 1,
|
|
169
|
-
column: offset - (before.lastIndexOf("\n") + 1)
|
|
170
|
-
};
|
|
171
|
-
};
|
|
172
|
-
const tagOffset = programJson.indexOf(`"tag": ${JSON.stringify(tag)}`);
|
|
173
|
-
if (tagOffset >= 0) {
|
|
174
|
-
const at = programJsonPosition(tagOffset);
|
|
175
|
-
mapLineAt(at.line, at.column, decorator);
|
|
176
|
-
}
|
|
177
|
-
push("");
|
|
178
|
-
pushDerivedBlock(`const __compiledProperties = ${propertiesJson};`, classNode.name);
|
|
179
|
-
push("");
|
|
180
|
-
pushDerivedBlock(`const __elementMetadata = ${metadataJson};`, classNode.name);
|
|
181
|
-
push("");
|
|
182
|
-
pushDerivedBlock(`const __observedAttributes: string[] = ${observedJson};`, classNode.name);
|
|
183
|
-
push("");
|
|
184
|
-
pushDerivedBlock("const __compiledProps = {", classNode.name);
|
|
185
|
-
for (const field of fields) {
|
|
186
|
-
if (field.computed) {
|
|
187
|
-
const computedLine = codeLines.length + 1;
|
|
188
|
-
push(` ${field.name}: { type: Object, default: undefined, reflect: false, attribute: false },`);
|
|
189
|
-
mapLineAt(computedLine, 2, field.node.name, field.name);
|
|
190
|
-
continue;
|
|
191
|
-
}
|
|
192
|
-
const attribute = field.attribute === null ? "false" : JSON.stringify(field.attribute);
|
|
193
|
-
pushDerivedLine(` ${field.name}: { type: ${field.typeConstructor}, default: `, field.initializerText, `, reflect: ${field.reflect}, attribute: ${attribute} },`, field.node.name, 2, field.name, field.node.initializer);
|
|
194
|
-
}
|
|
195
|
-
push("};");
|
|
196
|
-
push("");
|
|
197
|
-
const classLine = `export ${isDefaultExport ? "default " : ""}class ${className} extends ${openElementLocalName} {`;
|
|
198
|
-
push(classLine);
|
|
199
|
-
mapLineAt(codeLines.length, classLine.indexOf(className), classNode.name, className);
|
|
200
|
-
push([
|
|
201
|
-
" static __partProgram: typeof __partProgram = __partProgram;",
|
|
202
|
-
" static __compiledProperties: typeof __compiledProperties = __compiledProperties;",
|
|
203
|
-
" static __elementMetadata: typeof __elementMetadata = __elementMetadata;",
|
|
204
|
-
" static props: typeof __compiledProps = __compiledProps;",
|
|
205
|
-
" static observedAttributes: typeof __observedAttributes = __observedAttributes;"
|
|
206
|
-
].join("\n"));
|
|
207
|
-
if (delegatesFocus) push(" static override delegatesFocus: boolean = true;");
|
|
208
|
-
if (formAssociated) push(" static override formAssociated: boolean = true;");
|
|
209
|
-
const computedFields = fields.filter((field) => field.computed);
|
|
210
|
-
if (computedFields.length > 0) {
|
|
211
|
-
const plainFieldByName = new Map(fields.filter((field) => !field.computed).map((field) => [field.name, field]));
|
|
212
|
-
const signalValueType = (name) => {
|
|
213
|
-
const plain = plainFieldByName.get(name);
|
|
214
|
-
const annotated = plain?.typeText.replace(/^:\s*/, "").trim() ?? "";
|
|
215
|
-
if (annotated) return annotated;
|
|
216
|
-
switch (plain?.typeConstructor) {
|
|
217
|
-
case "String": return "string";
|
|
218
|
-
case "Number": return "number";
|
|
219
|
-
case "Boolean": return "boolean";
|
|
220
|
-
case "Array": return "unknown[]";
|
|
221
|
-
case "Object": return "Record<string, unknown>";
|
|
222
|
-
default: return "unknown";
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
const computedReturnType = (field) => {
|
|
226
|
-
return field.typeText.replace(/^:\s*/, "").trim() || `${signalType}<unknown>`;
|
|
227
|
-
};
|
|
228
|
-
push(" static __computedFields: {");
|
|
229
|
-
for (const field of computedFields) {
|
|
230
|
-
const params = field.computed.deps.map((dep) => `${dep}: ${signalType}<${signalValueType(dep)}>`).join(", ");
|
|
231
|
-
push(` ${field.name}: (__s: { ${params} }) => ${computedReturnType(field)};`);
|
|
232
|
-
}
|
|
233
|
-
push(" } = {");
|
|
234
|
-
for (const field of computedFields) {
|
|
235
|
-
const factoryLine = codeLines.length + 1;
|
|
236
|
-
push(` ${field.name}: ${field.computed.factoryText},`);
|
|
237
|
-
mapLineAt(factoryLine, 4, field.node.name, field.name);
|
|
238
|
-
mapLineAt(factoryLine, 4 + field.name.length + 2, field.node.initializer);
|
|
239
|
-
mapContinuationLines(field.computed.factoryText, factoryLine, field.computed.body, 0);
|
|
240
|
-
}
|
|
241
|
-
push(" };");
|
|
242
|
-
}
|
|
243
|
-
if (stylesText !== void 0 && stylesNode !== void 0) {
|
|
244
|
-
const stylesHead = ` static override styles${stylesTypeText ?? ""} = `;
|
|
245
|
-
const stylesLine = codeLines.length + 1;
|
|
246
|
-
push(`${stylesHead}${stylesText};`);
|
|
247
|
-
mapLineAt(stylesLine, stylesHead.length, stylesNode);
|
|
248
|
-
mapContinuationLines(stylesText, stylesLine, stylesNode, 0);
|
|
249
|
-
}
|
|
250
|
-
for (const field of fields) {
|
|
251
|
-
if (field.computed) continue;
|
|
252
|
-
const accessibility = field.accessibility ? `${field.accessibility} ` : "";
|
|
253
|
-
pushDerivedLine(` ${accessibility}${field.name}${field.typeText} = `, field.initializerText, ";", field.node.name, 2 + accessibility.length, field.name, field.node.initializer);
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Emit one platform lifecycle callback with the boundary-capture wrapper
|
|
257
|
-
* (P2: the wrapper is a compile-time cost; the runtime delta is one method
|
|
258
|
-
* call on the failure path only). The body bytes and their columns are
|
|
259
|
-
* emitted UNCHANGED from the verbatim copy — body line i keeps the authored
|
|
260
|
-
* text at the authored column, so the continuation mapping stays
|
|
261
|
-
* byte-identical to what `pushVerbatim` produced; the inserted `try {` and
|
|
262
|
-
* `} catch` lines are pure scaffolding and stay unmapped (consumers fall
|
|
263
|
-
* through to the nearest real construct, the method's own first-line
|
|
264
|
-
* mapping). Prior art (P7): React error boundaries capture lifecycle and
|
|
265
|
-
* render throws at runtime; the compiled difference is that this capture
|
|
266
|
-
* point is materialized once, at compile time, around exactly the platform
|
|
267
|
-
* entry points — `_captureError` on the base class then performs the same
|
|
268
|
-
* nearest-boundary climb React's component stack does.
|
|
269
|
-
*/
|
|
270
|
-
const pushLifecycleCapture = (method, name) => {
|
|
271
|
-
const body = method.body;
|
|
272
|
-
const methodText = method.getText(sf);
|
|
273
|
-
const header = methodText.slice(0, body.getStart(sf) - method.getStart(sf));
|
|
274
|
-
const bodyText = methodText.slice(header.length + 1, methodText.length - 1).replace(/\n[ \t]*$/, "");
|
|
275
|
-
const firstGeneratedLine = codeLines.length + 1;
|
|
276
|
-
push(` ${header}{`);
|
|
277
|
-
mapLineAt(firstGeneratedLine, 2, method, name);
|
|
278
|
-
push(` try {${bodyText}`);
|
|
279
|
-
mapContinuationLines(bodyText, firstGeneratedLine + 1, body, 0);
|
|
280
|
-
push(` } catch (error) { if (!this._captureError(error)) throw error; }`);
|
|
281
|
-
push(` }`);
|
|
282
|
-
};
|
|
283
|
-
for (const method of methods) {
|
|
284
|
-
const name = method.name.text;
|
|
285
|
-
if (CAPTURE_WRAPPED_LIFECYCLE_CALLBACKS.has(name)) {
|
|
286
|
-
pushLifecycleCapture(method, name);
|
|
287
|
-
continue;
|
|
288
|
-
}
|
|
289
|
-
pushVerbatim(method.getText(sf), method, " ", name);
|
|
290
|
-
}
|
|
291
|
-
for (const handler of lowering.generatedHandlers) {
|
|
292
|
-
const handlerLine = codeLines.length + 1;
|
|
293
|
-
push(generatedHandlerText(handler));
|
|
294
|
-
mapLineAt(handlerLine, 2, handler.node);
|
|
295
|
-
}
|
|
296
|
-
push(" render(): never {");
|
|
297
|
-
mapLineAt(codeLines.length, 2, render, "render");
|
|
298
|
-
push(` throw new Error(
|
|
299
|
-
'[open:compiled-element] ${tag} is compiled to a Part Program; the runtime JSX render path is not available.',
|
|
300
|
-
);
|
|
301
|
-
}`);
|
|
302
|
-
push("}");
|
|
303
|
-
push("");
|
|
304
|
-
push("export { __partProgram, __elementMetadata };");
|
|
305
|
-
const map = segments.build(fileName, source, program.sourceMap);
|
|
306
|
-
push(`//# sourceMappingURL=data:application/json;base64,${encodeBase64(JSON.stringify(map))}`);
|
|
307
|
-
return {
|
|
308
|
-
code: codeLines.join("\n") + "\n",
|
|
309
|
-
map
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
//#endregion
|
|
313
|
-
export { emitCompiledModule };
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { CompiledElementError, sourceRange } from "./compiler-diagnostics.js";
|
|
2
|
-
import { validatePartProgram } from "@openelement/protocol/part-program";
|
|
3
|
-
//#region src/internal/compiler/semantic-core/emit-program.ts
|
|
4
|
-
/**
|
|
5
|
-
* Path-addressed fixed Parts can use DOM child indexes only before any dynamic
|
|
6
|
-
* anchor. An anchor expands to multiple DOM nodes, so accepting a fixed sink
|
|
7
|
-
* after one would make server output, fresh creation and claim address
|
|
8
|
-
* different nodes. The v1 location id remains the identity; this guard keeps
|
|
9
|
-
* the path retained for seed consumers safe until they consume that id.
|
|
10
|
-
*/
|
|
11
|
-
function assertPathSafety(program) {
|
|
12
|
-
for (const part of program.parts) {
|
|
13
|
-
if (part.k === "text" || part.k === "when" || part.k === "each") continue;
|
|
14
|
-
let nodes = program.template;
|
|
15
|
-
for (const target of part.path) {
|
|
16
|
-
for (let sibling = 0; sibling < target; sibling++) if (nodes[sibling]?.k === "part") {
|
|
17
|
-
const source = program.sourceMap.records.find((record) => record.id === `p${part.index}`)?.source;
|
|
18
|
-
throw new CompiledElementError([{
|
|
19
|
-
code: "OEC9015",
|
|
20
|
-
message: `${part.k} part path [${part.path.join(",")}] is preceded by a dynamic anchor; path-addressed fixed sinks must appear before any dynamic anchor sibling`,
|
|
21
|
-
file: source?.file ?? program.sourceMap.file,
|
|
22
|
-
line: source?.start.line ?? 1,
|
|
23
|
-
character: source?.start.column ?? 1,
|
|
24
|
-
start: source?.start.offset ?? 0,
|
|
25
|
-
end: source?.end.offset ?? 0
|
|
26
|
-
}]);
|
|
27
|
-
}
|
|
28
|
-
const next = nodes[target];
|
|
29
|
-
nodes = next?.k === "el" ? next.children : [];
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
/** Assemble, path-check and validate the Part Program; derive the serialized payloads. */
|
|
34
|
-
function buildPartProgram(input) {
|
|
35
|
-
const { sf, fileName, analyzed, root, lowering } = input;
|
|
36
|
-
const { tag, className, fields, render, rootKind } = analyzed;
|
|
37
|
-
const metadata = {
|
|
38
|
-
tag,
|
|
39
|
-
className,
|
|
40
|
-
sourceFile: fileName,
|
|
41
|
-
properties: fields.map((field) => ({
|
|
42
|
-
name: field.name,
|
|
43
|
-
attribute: field.attribute,
|
|
44
|
-
type: field.type,
|
|
45
|
-
converter: field.converter,
|
|
46
|
-
reflect: field.reflect,
|
|
47
|
-
default: field.defaultValue,
|
|
48
|
-
...field.computed ? {
|
|
49
|
-
computed: true,
|
|
50
|
-
deps: field.computed.deps
|
|
51
|
-
} : {}
|
|
52
|
-
})),
|
|
53
|
-
observedAttributes: fields.flatMap((field) => field.attribute === null ? [] : [field.attribute]),
|
|
54
|
-
cem: {
|
|
55
|
-
tagName: tag,
|
|
56
|
-
className,
|
|
57
|
-
declaration: {
|
|
58
|
-
name: className,
|
|
59
|
-
module: fileName
|
|
60
|
-
},
|
|
61
|
-
attributes: fields.flatMap((field) => field.attribute === null ? [] : [{
|
|
62
|
-
name: field.attribute,
|
|
63
|
-
fieldName: field.name,
|
|
64
|
-
type: field.type,
|
|
65
|
-
reflect: field.reflect
|
|
66
|
-
}]),
|
|
67
|
-
members: fields.map((field) => ({
|
|
68
|
-
name: field.name,
|
|
69
|
-
fieldName: field.name,
|
|
70
|
-
type: field.type,
|
|
71
|
-
attribute: field.attribute,
|
|
72
|
-
reflect: field.reflect
|
|
73
|
-
}))
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
const sourceRecords = [
|
|
77
|
-
{
|
|
78
|
-
id: "root",
|
|
79
|
-
kind: "root",
|
|
80
|
-
source: sourceRange(sf, render)
|
|
81
|
-
},
|
|
82
|
-
...fields.map((field) => ({
|
|
83
|
-
id: `property:${field.name}`,
|
|
84
|
-
kind: "property",
|
|
85
|
-
source: sourceRange(sf, field.node)
|
|
86
|
-
})),
|
|
87
|
-
...lowering.sourceRecords
|
|
88
|
-
];
|
|
89
|
-
const program = {
|
|
90
|
-
version: 1,
|
|
91
|
-
tag,
|
|
92
|
-
root: {
|
|
93
|
-
id: "root",
|
|
94
|
-
kind: rootKind,
|
|
95
|
-
nodes: [root.id]
|
|
96
|
-
},
|
|
97
|
-
template: [root],
|
|
98
|
-
parts: lowering.parts,
|
|
99
|
-
regions: lowering.regions,
|
|
100
|
-
dependencies: lowering.dependencies,
|
|
101
|
-
locations: lowering.locations,
|
|
102
|
-
sourceMap: {
|
|
103
|
-
version: 1,
|
|
104
|
-
file: fileName,
|
|
105
|
-
records: sourceRecords
|
|
106
|
-
},
|
|
107
|
-
metadata
|
|
108
|
-
};
|
|
109
|
-
assertPathSafety(program);
|
|
110
|
-
validatePartProgram(program);
|
|
111
|
-
const { sourceMap: _provenance, ...wireProgram } = program;
|
|
112
|
-
return {
|
|
113
|
-
program,
|
|
114
|
-
programJson: JSON.stringify(wireProgram, null, 2),
|
|
115
|
-
propertiesJson: JSON.stringify(metadata.properties, null, 2),
|
|
116
|
-
metadataJson: JSON.stringify(metadata, null, 2),
|
|
117
|
-
observedJson: JSON.stringify(metadata.observedAttributes, null, 2)
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
//#endregion
|
|
121
|
-
export { buildPartProgram };
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
//#region src/internal/compiler/semantic-core/style-imports.ts
|
|
3
|
-
/**
|
|
4
|
-
* Sheet-import admission (#1558) — the one style authoring form.
|
|
5
|
-
*
|
|
6
|
-
* A compiled module's sheets are real `.css` files imported into the module
|
|
7
|
-
* and arrayed in the class's `static styles`. The generated module copies the
|
|
8
|
-
* authored imports and the initializer verbatim (emit-module), the sheet's
|
|
9
|
-
* bytes stay in exactly one place — the authored file (P6) — and the compile
|
|
10
|
-
* result carries the import edges so the host build's style-asset plugin can
|
|
11
|
-
* intercept them (style-requests.ts). Nothing here reads sheet bytes and
|
|
12
|
-
* nothing here synthesizes sheet text: the compiler polices only the
|
|
13
|
-
* import/initializer shape, and every other sheet spelling fails closed with
|
|
14
|
-
* OEC9029 plus a migration hint — a silent inline path would put stylesheet
|
|
15
|
-
* bytes back into JS chunks and void the zero-inline guarantee (#1553).
|
|
16
|
-
*/
|
|
17
|
-
/**
|
|
18
|
-
* The OEC9029 diagnostic: the one style authoring form is `.css` file
|
|
19
|
-
* imports arrayed in `static styles`; the message carries the migration hint.
|
|
20
|
-
*/
|
|
21
|
-
function failStyleShape(fail, node, message) {
|
|
22
|
-
fail(node, "OEC9029", `${message} — write the sheet to a .css file, import it (\`import sheet from './<component>.css'\`) and array the binding in static styles; inline sheet strings are retired (#1558)`);
|
|
23
|
-
}
|
|
24
|
-
/** Strip parens and type assertions — the initializer's expression core. */
|
|
25
|
-
function unwrap(expr) {
|
|
26
|
-
let current = expr;
|
|
27
|
-
while (ts.isParenthesizedExpression(current) || ts.isAsExpression(current) || ts.isTypeAssertionExpression(current)) current = current.expression;
|
|
28
|
-
return current;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Collect the module's `.css` imports and police their shape: every sheet
|
|
32
|
-
* import is a relative specifier bound through a default import — the
|
|
33
|
-
* side-effect form has no binding for `static styles` to adopt, and a bare
|
|
34
|
-
* package specifier does not resolve inside the authoring tree (the style
|
|
35
|
-
* edge is per-module: the intercept keys on the importing module's directory
|
|
36
|
-
* plus the file name). Named or type-only forms are refused for the same
|
|
37
|
-
* dangling-binding reason.
|
|
38
|
-
*/
|
|
39
|
-
function collectSheetImports(sf, fail) {
|
|
40
|
-
const imports = /* @__PURE__ */ new Map();
|
|
41
|
-
for (const statement of sf.statements) {
|
|
42
|
-
if (!ts.isImportDeclaration(statement)) continue;
|
|
43
|
-
if (!ts.isStringLiteral(statement.moduleSpecifier)) continue;
|
|
44
|
-
const specifier = statement.moduleSpecifier.text;
|
|
45
|
-
if (!specifier.endsWith(".css")) continue;
|
|
46
|
-
if (!specifier.startsWith("./") && !specifier.startsWith("../")) failStyleShape(fail, statement, `the .css import specifier "${specifier}" must be relative; a bare package specifier does not resolve inside the authoring tree`);
|
|
47
|
-
const clause = statement.importClause;
|
|
48
|
-
if (!clause || clause.isTypeOnly || !clause.name) failStyleShape(fail, statement, clause ? `the .css import of "${specifier}" must bind its sheet as a default import; named and type-only forms have no sheet binding for static styles to adopt` : `the .css import of "${specifier}" must bind its sheet — the side-effect form has no binding for static styles to adopt`);
|
|
49
|
-
const name = clause.name.text;
|
|
50
|
-
if (imports.has(name)) fail(statement, "OEC9029", `duplicate sheet import binding "${name}" — a compiled module's .css imports bind distinct names so static styles can array them individually`);
|
|
51
|
-
imports.set(name, {
|
|
52
|
-
name,
|
|
53
|
-
specifier,
|
|
54
|
-
node: statement
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
return imports;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Admit the class's authored `static styles` initializer: an array literal of
|
|
61
|
-
* the module's sheet-import bindings, in authored order. Returns the edge
|
|
62
|
-
* specifiers in that order; the admitted initializer emits verbatim, so the
|
|
63
|
-
* generated module keeps the authored import edges the host build intercepts.
|
|
64
|
-
* Every other shape fails closed with OEC9029, and every collected sheet
|
|
65
|
-
* import must be arrayed — an unadopted edge would dangle in the generated
|
|
66
|
-
* module with no style channel to ride.
|
|
67
|
-
*/
|
|
68
|
-
function admitStylesInitializer(stylesNode, sheetImports, sf, fail) {
|
|
69
|
-
const arrayNode = stylesNode === void 0 ? void 0 : unwrap(stylesNode);
|
|
70
|
-
if (arrayNode !== void 0 && !ts.isArrayLiteralExpression(arrayNode)) failStyleShape(fail, arrayNode, `static styles must array the module's .css file imports; "${arrayNode.getText(sf)}" is not an array of sheet imports`);
|
|
71
|
-
const edges = [];
|
|
72
|
-
const arrayed = /* @__PURE__ */ new Set();
|
|
73
|
-
if (arrayNode !== void 0) for (const element of arrayNode.elements) {
|
|
74
|
-
if (ts.isSpreadElement(element) || !ts.isIdentifier(element)) failStyleShape(fail, element, `static styles must array the module's .css file imports as plain bindings; "${element.getText(sf)}" is not a sheet-import binding`);
|
|
75
|
-
const name = element.text;
|
|
76
|
-
const sheet = sheetImports.get(name);
|
|
77
|
-
if (sheet === void 0) failStyleShape(fail, element, `static styles must array the module's .css file imports; "${name}" is not a .css import binding of this module`);
|
|
78
|
-
if (!arrayed.has(name)) {
|
|
79
|
-
arrayed.add(name);
|
|
80
|
-
edges.push(sheet.specifier);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
for (const sheet of sheetImports.values()) if (!arrayed.has(sheet.name)) fail(sheet.node, "OEC9029", `the .css import "${sheet.name}" ("${sheet.specifier}") is never arrayed in static styles — every sheet import of a compiled module is adopted by its class`);
|
|
84
|
-
return edges;
|
|
85
|
-
}
|
|
86
|
-
//#endregion
|
|
87
|
-
export { admitStylesInitializer, collectSheetImports };
|