@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.
- package/package.json +3 -2
- package/src/compiler.d.ts +8 -6
- package/src/compiler.js +7 -6
- package/src/internal/compiler/compiled-artifact-class-candidates.d.ts +5 -30
- package/src/internal/compiler/compiled-artifact-class-candidates.js +18 -32
- package/src/internal/compiler/compiled-artifact-style-requests.d.ts +9 -7
- package/src/internal/compiler/compiled-artifact-style-requests.js +9 -7
- package/src/internal/compiler/plugin.d.ts +31 -50
- package/src/internal/compiler/plugin.js +41 -108
- package/src/internal/compiler/semantic-core/analyze-module.d.ts +1 -45
- package/src/internal/compiler/semantic-core/analyze-module.js +10 -405
- 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/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 +51 -0
- package/src/internal/compiler/semantic-core/lower-program.js +186 -90
- package/src/internal/compiler/semantic-core/module-analysis.d.ts +28 -5
- package/src/internal/compiler/semantic-core/module-analysis.js +16 -17
- 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 +22 -7
- package/src/internal/compiler/semantic-core/server-component.js +163 -57
- 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 +0 -8
- package/src/internal/compiler/style-requests.js +1 -14
- package/src/internal/compiler/semantic-core/compile.d.ts +0 -47
- package/src/internal/compiler/semantic-core/compile.js +0 -100
- package/src/internal/compiler/semantic-core/emit-module.js +0 -317
- package/src/internal/compiler/semantic-core/emit-program.js +0 -141
- package/src/internal/compiler/semantic-core/style-imports.js +0 -87
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { resolveCanonicalBaseClass } from "./module-analysis.js";
|
|
2
|
-
import { admitStylesInitializer, collectSheetImports } from "./style-imports.js";
|
|
3
1
|
import ts from "typescript";
|
|
4
|
-
import {
|
|
2
|
+
import { isSafeAttributeName } from "@openelement/protocol/forbidden-sinks";
|
|
5
3
|
//#region src/internal/compiler/semantic-core/analyze-module.ts
|
|
6
4
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
5
|
+
* The expression-and-field primitives the two surviving lanes share with the
|
|
6
|
+
* lowering walk (#1473 split).
|
|
7
|
+
*
|
|
8
|
+
* This module once also carried the `@element` class-shape analysis
|
|
9
|
+
* (decorator options, canonical heritage, the `@property` inventory). That
|
|
10
|
+
* lane is retired: with no parser feeding it, keeping the analysis here would
|
|
11
|
+
* leave a second, unreachable reading of the same module grammar that could
|
|
12
|
+
* drift from the grammar the generated lane actually admits.
|
|
12
13
|
*/
|
|
13
14
|
/** The compiler face of the one canonical attribute-name predicate. */
|
|
14
15
|
function isSafeAttributeName$1(value) {
|
|
@@ -59,39 +60,11 @@ function literalValue(expr, sf) {
|
|
|
59
60
|
return object;
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
|
-
/**
|
|
63
|
-
* The one non-serializable field default admitted by the compiled grammar.
|
|
64
|
-
* The wrapper is the runtime capability; limiting the argument to a literal
|
|
65
|
-
* keeps compiled metadata deterministic and prevents arbitrary initializer
|
|
66
|
-
* execution from entering the property schema. Provenance (#1209): the callee
|
|
67
|
-
* must bind the canonical `trustedHtml` import; a same-name spelling bound to
|
|
68
|
-
* anything else fails closed (OEC9026) instead of being silently admitted.
|
|
69
|
-
*/
|
|
70
|
-
function isTrustedHtmlInitializer(expr, intrinsics, fail) {
|
|
71
|
-
const value = unwrapExpression(expr);
|
|
72
|
-
if (!ts.isCallExpression(value)) return false;
|
|
73
|
-
const resolution = intrinsics.resolveIntrinsic(value.expression, "trustedHtml");
|
|
74
|
-
if (!resolution.canonical) {
|
|
75
|
-
if (resolution.unsupported || ts.isIdentifier(value.expression) && value.expression.text === "trustedHtml") fail(value, "OEC9026", "trustedHtml field defaults require the canonical trustedHtml import: a runtime named import of trustedHtml from '@openelement/element'" + (resolution.unsupported ? ` (${resolution.unsupported})` : ""));
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
if (value.arguments.length !== 1) return false;
|
|
79
|
-
const html = unwrapExpression(value.arguments[0]);
|
|
80
|
-
return ts.isStringLiteral(html) || ts.isNoSubstitutionTemplateLiteral(html);
|
|
81
|
-
}
|
|
82
63
|
function primitiveText(value) {
|
|
83
64
|
if (value === null) return "";
|
|
84
65
|
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return String(value);
|
|
85
66
|
return null;
|
|
86
67
|
}
|
|
87
|
-
function propertyTypeFromConstructor(expr, sf, node, fail) {
|
|
88
|
-
const name = expr.getText(sf);
|
|
89
|
-
if (name !== "String" && name !== "Number" && name !== "Boolean" && name !== "Array" && name !== "Object") return fail(node, "OEC9021", "property type/converter must be one of String, Number, Boolean, Array or Object");
|
|
90
|
-
return {
|
|
91
|
-
label: name.toLowerCase(),
|
|
92
|
-
constructorName: name
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
68
|
const COMPUTED_FIELD_SUBJECTS = {
|
|
96
69
|
actor: "computed field arrows",
|
|
97
70
|
single: "computed field",
|
|
@@ -153,373 +126,5 @@ function validateComputedExpressionBody(body, sf, plainFieldNames, computedField
|
|
|
153
126
|
bodyText
|
|
154
127
|
};
|
|
155
128
|
}
|
|
156
|
-
/**
|
|
157
|
-
* Parse a computed field initializer (alpha.8): `computed(() => ...)`
|
|
158
|
-
* behind optional `as`-casts, where the callee binds the canonical `computed`
|
|
159
|
-
* import (#1209 — a same-name spelling bound to anything else fails closed
|
|
160
|
-
* with OEC9025). The arrow body may read `this.<field>` of any NON-computed
|
|
161
|
-
* declared field (rewritten to the signal-record read) and use any
|
|
162
|
-
* module-scope values; `this` in any other position, reads of computed
|
|
163
|
-
* fields, and nested non-arrow functions fail closed (OEC9024). Returns null
|
|
164
|
-
* for non-computed initializers.
|
|
165
|
-
*/
|
|
166
|
-
function parseComputedInitializer(initializer, sf, intrinsics, plainFieldNames, computedFieldNames, fail) {
|
|
167
|
-
const value = unwrapExpression(initializer);
|
|
168
|
-
if (!ts.isCallExpression(value)) return null;
|
|
169
|
-
const resolution = intrinsics.resolveIntrinsic(value.expression, "computed");
|
|
170
|
-
if (!resolution.canonical) {
|
|
171
|
-
if (resolution.unsupported || ts.isIdentifier(value.expression) && value.expression.text === "computed") fail(value, "OEC9025", "computed fields require the canonical computed import: a runtime named import of computed from '@openelement/element'" + (resolution.unsupported ? ` (${resolution.unsupported})` : ""));
|
|
172
|
-
return null;
|
|
173
|
-
}
|
|
174
|
-
if (value.arguments.length !== 1 || !ts.isArrowFunction(value.arguments[0])) fail(value, "OEC9024", "computed fields take exactly one zero-argument arrow function");
|
|
175
|
-
const arrow = value.arguments[0];
|
|
176
|
-
if (arrow.parameters.length > 0) fail(arrow, "OEC9024", "computed field arrows may not declare parameters");
|
|
177
|
-
if (!ts.isBlock(arrow.body)) {} else if (arrow.body.statements.length === 1 && ts.isReturnStatement(arrow.body.statements[0]) && arrow.body.statements[0].expression) fail(arrow.body, "OEC9024", "computed field arrows use an expression body, not a block (keep derived values atomic)");
|
|
178
|
-
else fail(arrow.body, "OEC9024", "computed field arrows use an expression body, not a block");
|
|
179
|
-
const body = arrow.body;
|
|
180
|
-
const { deps, bodyText } = validateComputedExpressionBody(body, sf, plainFieldNames, computedFieldNames, fail);
|
|
181
|
-
return {
|
|
182
|
-
deps,
|
|
183
|
-
factoryText: `(__s) => ${resolution.localName}(() => ${bodyText})`,
|
|
184
|
-
body
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
function inferPropertyType(member, defaultValue, sf) {
|
|
188
|
-
const typeText = member.type?.getText(sf).replace(/[\s?]/g, "") ?? "";
|
|
189
|
-
if (typeText === "string" || typeText === "String") return {
|
|
190
|
-
label: "string",
|
|
191
|
-
constructorName: "String"
|
|
192
|
-
};
|
|
193
|
-
if (typeText === "number" || typeText === "Number") return {
|
|
194
|
-
label: "number",
|
|
195
|
-
constructorName: "Number"
|
|
196
|
-
};
|
|
197
|
-
if (typeText === "boolean" || typeText === "Boolean") return {
|
|
198
|
-
label: "boolean",
|
|
199
|
-
constructorName: "Boolean"
|
|
200
|
-
};
|
|
201
|
-
if (/^(Array|ReadonlyArray|Set)</.test(typeText) || typeText.endsWith("[]")) return {
|
|
202
|
-
label: "array",
|
|
203
|
-
constructorName: "Array"
|
|
204
|
-
};
|
|
205
|
-
if (/^(Record|Map|Object)</.test(typeText)) return {
|
|
206
|
-
label: "object",
|
|
207
|
-
constructorName: "Object"
|
|
208
|
-
};
|
|
209
|
-
if (Array.isArray(defaultValue)) return {
|
|
210
|
-
label: "array",
|
|
211
|
-
constructorName: "Array"
|
|
212
|
-
};
|
|
213
|
-
if (defaultValue !== null && typeof defaultValue === "object") return {
|
|
214
|
-
label: "object",
|
|
215
|
-
constructorName: "Object"
|
|
216
|
-
};
|
|
217
|
-
if (typeof defaultValue === "number") return {
|
|
218
|
-
label: "number",
|
|
219
|
-
constructorName: "Number"
|
|
220
|
-
};
|
|
221
|
-
if (typeof defaultValue === "boolean") return {
|
|
222
|
-
label: "boolean",
|
|
223
|
-
constructorName: "Boolean"
|
|
224
|
-
};
|
|
225
|
-
return {
|
|
226
|
-
label: "string",
|
|
227
|
-
constructorName: "String"
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
function propertyFields(sf, classNode, intrinsics, fail) {
|
|
231
|
-
const fields = [];
|
|
232
|
-
const methods = [];
|
|
233
|
-
let render = null;
|
|
234
|
-
let stylesText;
|
|
235
|
-
let stylesNode;
|
|
236
|
-
let stylesTypeText;
|
|
237
|
-
const names = /* @__PURE__ */ new Set();
|
|
238
|
-
const propertyAttributeNames = /* @__PURE__ */ new Set();
|
|
239
|
-
for (const member of classNode.members) {
|
|
240
|
-
if (ts.isPropertyDeclaration(member)) {
|
|
241
|
-
const modifiers = ts.getModifiers(member) ?? [];
|
|
242
|
-
if (modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword) && ts.isIdentifier(member.name) && member.name.text === "styles" && (ts.getDecorators(member) ?? []).length === 0) {
|
|
243
|
-
if (!member.initializer) fail(member, "OEC9005", "static styles requires an initializer");
|
|
244
|
-
if (stylesText !== void 0) fail(member, "OEC9005", "compiled classes may declare static styles only once");
|
|
245
|
-
stylesText = member.initializer.getText(sf);
|
|
246
|
-
stylesNode = member.initializer;
|
|
247
|
-
stylesTypeText = member.type ? `: ${member.type.getText(sf)}` : void 0;
|
|
248
|
-
continue;
|
|
249
|
-
}
|
|
250
|
-
const accessibilityModifiers = modifiers.filter((modifier) => modifier.kind === ts.SyntaxKind.PublicKeyword || modifier.kind === ts.SyntaxKind.ProtectedKeyword || modifier.kind === ts.SyntaxKind.PrivateKeyword);
|
|
251
|
-
if (modifiers.some((modifier) => modifier.kind !== ts.SyntaxKind.PublicKeyword && modifier.kind !== ts.SyntaxKind.ProtectedKeyword && modifier.kind !== ts.SyntaxKind.PrivateKeyword) || accessibilityModifiers.length > 1 || member.questionToken || member.exclamationToken) fail(member, "OEC9005", "compiled @property fields must be ordinary initialized instance fields");
|
|
252
|
-
const decorators = ts.getDecorators(member) ?? [];
|
|
253
|
-
if (decorators.length !== 1) fail(member, "OEC9005", "compiled fields must carry exactly one @property decorator");
|
|
254
|
-
const decorator = decorators[0];
|
|
255
|
-
const call = decorator.expression;
|
|
256
|
-
if (!ts.isCallExpression(call)) fail(decorator, "OEC9004", "unknown decorator on an OpenElement member; use only @property");
|
|
257
|
-
const decoratorResolution = intrinsics.resolveIntrinsic(call.expression, "property");
|
|
258
|
-
if (!decoratorResolution.canonical) {
|
|
259
|
-
if (decoratorResolution.unsupported) fail(decorator, "OEC9027", `unsupported @property provenance: ${decoratorResolution.unsupported}`);
|
|
260
|
-
fail(decorator, "OEC9004", "unknown decorator on an OpenElement member; use only @property imported from '@openelement/element'");
|
|
261
|
-
}
|
|
262
|
-
if (!ts.isIdentifier(member.name)) fail(member, "OEC9005", "property names must be identifiers");
|
|
263
|
-
if (FORBIDDEN_PROPERTY_NAMES.has(member.name.text.toLowerCase())) fail(member, "OEC9005", `property name "${member.name.text}" is unsafe — prototype-pollution primitives are on the shared deny list; rename the property`);
|
|
264
|
-
if (names.has(member.name.text)) fail(member, "OEC9005", `duplicate property "${member.name.text}"`);
|
|
265
|
-
names.add(member.name.text);
|
|
266
|
-
if (!member.initializer) fail(member, "OEC9005", "compiled fields require an admitted initializer — a serializable literal, computed(...), or trustedHtml(...)");
|
|
267
|
-
const computedFieldNames = new Set(fields.filter((f) => f.computed).map((f) => f.name));
|
|
268
|
-
const computedInit = parseComputedInitializer(member.initializer, sf, intrinsics, new Set(fields.filter((f) => !f.computed).map((f) => f.name)), computedFieldNames, fail);
|
|
269
|
-
const trustedHtmlInit = !computedInit && isTrustedHtmlInitializer(member.initializer, intrinsics, fail);
|
|
270
|
-
const defaultValue = computedInit || trustedHtmlInit ? null : literalValue(member.initializer, sf);
|
|
271
|
-
if (defaultValue === void 0) fail(member.initializer, "OEC9020", "property defaults must be serializable literals");
|
|
272
|
-
const options = call.arguments.length === 1 ? call.arguments[0] : void 0;
|
|
273
|
-
if (!options || !ts.isObjectLiteralExpression(options)) fail(decorator, "OEC9005", "@property requires one options object literal");
|
|
274
|
-
let reflect = false;
|
|
275
|
-
let attribute;
|
|
276
|
-
let explicitType;
|
|
277
|
-
let explicitConverter;
|
|
278
|
-
const optionNames = /* @__PURE__ */ new Set();
|
|
279
|
-
for (const entry of options.properties) {
|
|
280
|
-
if (!ts.isPropertyAssignment(entry) || entry.name.getText(sf).length === 0) fail(entry, "OEC9022", "@property options must be named literal assignments");
|
|
281
|
-
const optionName = entry.name.getText(sf);
|
|
282
|
-
if (optionNames.has(optionName)) fail(entry, "OEC9022", `@property option "${optionName}" may appear only once`);
|
|
283
|
-
optionNames.add(optionName);
|
|
284
|
-
if (optionName === "reflect") {
|
|
285
|
-
if (entry.initializer.kind !== ts.SyntaxKind.TrueKeyword && entry.initializer.kind !== ts.SyntaxKind.FalseKeyword) fail(entry, "OEC9005", "@property reflect must be a boolean literal");
|
|
286
|
-
reflect = entry.initializer.kind === ts.SyntaxKind.TrueKeyword;
|
|
287
|
-
continue;
|
|
288
|
-
}
|
|
289
|
-
if (optionName === "attribute") {
|
|
290
|
-
if (entry.initializer.kind === ts.SyntaxKind.FalseKeyword) attribute = null;
|
|
291
|
-
else if (ts.isStringLiteral(entry.initializer)) {
|
|
292
|
-
attribute = camelToKebab(entry.initializer.text);
|
|
293
|
-
if (!isSafeAttributeName$1(attribute)) fail(entry, "OEC9023", `property attribute name "${attribute}" is unsafe — use a valid HTML attribute name (no on* handlers, no forbidden sinks)`);
|
|
294
|
-
} else fail(entry, "OEC9023", "property attribute must be a string literal or false");
|
|
295
|
-
continue;
|
|
296
|
-
}
|
|
297
|
-
if (optionName === "type" || optionName === "converter") {
|
|
298
|
-
const type = propertyTypeFromConstructor(entry.initializer, sf, entry, fail);
|
|
299
|
-
if (optionName === "type") explicitType = type;
|
|
300
|
-
else explicitConverter = type.label;
|
|
301
|
-
continue;
|
|
302
|
-
}
|
|
303
|
-
fail(entry, "OEC9022", `unsupported @property option "${optionName}" — admitted options are type, converter, reflect and attribute`);
|
|
304
|
-
}
|
|
305
|
-
const inferred = explicitType ?? inferPropertyType(member, defaultValue, sf);
|
|
306
|
-
const converter = explicitConverter ?? inferred.label;
|
|
307
|
-
if (attribute === void 0) attribute = camelToKebab(member.name.text);
|
|
308
|
-
if (trustedHtmlInit) {
|
|
309
|
-
if (inferred.label !== "object" || attribute !== null || reflect) fail(member, "OEC9026", "trustedHtml field defaults require type: Object, reflect: false, and attribute: false");
|
|
310
|
-
}
|
|
311
|
-
if (computedInit) {
|
|
312
|
-
if (attribute !== null || reflect) fail(member, "OEC9025", "computed fields require @property({ reflect: false, attribute: false }) — derived signals have no attribute channel");
|
|
313
|
-
}
|
|
314
|
-
if (attribute !== null) {
|
|
315
|
-
const attributeKey = attribute.toLowerCase();
|
|
316
|
-
if (propertyAttributeNames.has(attributeKey)) fail(member, "OEC9023", `duplicate property attribute "${attribute}"`);
|
|
317
|
-
propertyAttributeNames.add(attributeKey);
|
|
318
|
-
}
|
|
319
|
-
if (attribute === null && reflect) fail(member, "OEC9023", "a property with attribute: false cannot reflect");
|
|
320
|
-
fields.push({
|
|
321
|
-
name: member.name.text,
|
|
322
|
-
reflect,
|
|
323
|
-
attribute,
|
|
324
|
-
type: inferred.label,
|
|
325
|
-
converter,
|
|
326
|
-
typeConstructor: inferred.constructorName,
|
|
327
|
-
accessibility: accessibilityModifiers.length === 0 ? "" : accessibilityModifiers[0].kind === ts.SyntaxKind.PublicKeyword ? "public" : accessibilityModifiers[0].kind === ts.SyntaxKind.ProtectedKeyword ? "protected" : "private",
|
|
328
|
-
typeText: member.type ? `: ${member.type.getText(sf)}` : "",
|
|
329
|
-
initializerText: member.initializer.getText(sf),
|
|
330
|
-
defaultValue,
|
|
331
|
-
node: member,
|
|
332
|
-
...computedInit ? { computed: computedInit } : {}
|
|
333
|
-
});
|
|
334
|
-
continue;
|
|
335
|
-
}
|
|
336
|
-
if (ts.isMethodDeclaration(member)) {
|
|
337
|
-
const modifiers = ts.getModifiers(member) ?? [];
|
|
338
|
-
if (modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword || modifier.kind === ts.SyntaxKind.AbstractKeyword || modifier.kind === ts.SyntaxKind.DeclareKeyword || modifier.kind === ts.SyntaxKind.AccessorKeyword) || !member.body) fail(member, "OEC9006", "compiled methods must be concrete instance methods");
|
|
339
|
-
if ((ts.getDecorators(member) ?? []).length > 0) fail(member, "OEC9004", "methods may not carry decorators in the compiler grammar");
|
|
340
|
-
if (member.parameters.some((parameter) => (ts.getDecorators(parameter) ?? []).length > 0)) fail(member, "OEC9004", "method parameters may not carry decorators in the compiler grammar");
|
|
341
|
-
if (!ts.isIdentifier(member.name)) fail(member, "OEC9006", "method names must be identifiers");
|
|
342
|
-
if (member.name.text === "render") {
|
|
343
|
-
if (render) fail(member, "OEC9007", "compiled classes may declare only one render() method");
|
|
344
|
-
if (member.parameters.length !== 0 || modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword)) fail(member, "OEC9007", "render() must be a synchronous zero-argument method");
|
|
345
|
-
render = member;
|
|
346
|
-
} else methods.push(member);
|
|
347
|
-
continue;
|
|
348
|
-
}
|
|
349
|
-
fail(member, "OEC9006", "constructors, accessors and undecorated fields are outside the compiler grammar — declare state as @property fields and behavior as methods");
|
|
350
|
-
}
|
|
351
|
-
if (!render) fail(classNode, "OEC9007", "compiled classes must declare render()");
|
|
352
|
-
return {
|
|
353
|
-
fields,
|
|
354
|
-
methods,
|
|
355
|
-
render,
|
|
356
|
-
stylesText,
|
|
357
|
-
stylesNode,
|
|
358
|
-
stylesTypeText
|
|
359
|
-
};
|
|
360
|
-
}
|
|
361
|
-
function isDeclareStatement(statement) {
|
|
362
|
-
return ts.canHaveModifiers(statement) && (ts.getModifiers(statement) ?? []).some((modifier) => modifier.kind === ts.SyntaxKind.DeclareKeyword);
|
|
363
|
-
}
|
|
364
|
-
/**
|
|
365
|
-
* The one extra runtime statement a compiled module may carry: the island
|
|
366
|
-
* delivery policy colocated with the class
|
|
367
|
-
* (`export const openElement = defineIslandConfig({ ... })`). The callee must
|
|
368
|
-
* bind a static-sidecar descriptor the host injected through the compile
|
|
369
|
-
* options (#1209); the default core knows none, so an unconfigured compiler
|
|
370
|
-
* rejects the statement as OEC9008. A same-name spelling bound to anything
|
|
371
|
-
* else is not the policy statement and falls through to the same rejection.
|
|
372
|
-
* The statement is validated here and copied verbatim into the generated
|
|
373
|
-
* module; the runtime factory validates the descriptor itself at module
|
|
374
|
-
* evaluation. Anything else stays outside the compiled module grammar
|
|
375
|
-
* (OEC9008).
|
|
376
|
-
*/
|
|
377
|
-
function isIslandConfigStatement(statement, intrinsics) {
|
|
378
|
-
if (!ts.isVariableStatement(statement)) return false;
|
|
379
|
-
const modifiers = ts.getModifiers(statement) ?? [];
|
|
380
|
-
if (modifiers.length !== 1 || modifiers[0].kind !== ts.SyntaxKind.ExportKeyword) return false;
|
|
381
|
-
const declarations = statement.declarationList.declarations;
|
|
382
|
-
if (declarations.length !== 1) return false;
|
|
383
|
-
const declaration = declarations[0];
|
|
384
|
-
if (!ts.isIdentifier(declaration.name) || declaration.name.text !== "openElement") return false;
|
|
385
|
-
const initializer = declaration.initializer;
|
|
386
|
-
if (!initializer || !ts.isCallExpression(initializer)) return false;
|
|
387
|
-
if (!intrinsics.isStaticSidecarCallee(initializer.expression)) return false;
|
|
388
|
-
if (initializer.arguments.length !== 1 || !ts.isObjectLiteralExpression(initializer.arguments[0])) return false;
|
|
389
|
-
return true;
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* The root family an admitted island config declares, or `undefined` when the
|
|
393
|
-
* descriptor does not pin one. The config speaks the user-facing
|
|
394
|
-
* `'light' | 'shadow'` pair while the decorator keeps the precise
|
|
395
|
-
* `shadow-open`/`shadow-closed` modes. Validated here so the cross-check below
|
|
396
|
-
* can trust the value it compares.
|
|
397
|
-
*/
|
|
398
|
-
function islandConfigDeclaredRoot(statement, sf, fail) {
|
|
399
|
-
if (!ts.isVariableStatement(statement)) return void 0;
|
|
400
|
-
const initializer = statement.declarationList.declarations[0]?.initializer;
|
|
401
|
-
if (!initializer || !ts.isCallExpression(initializer)) return void 0;
|
|
402
|
-
const descriptor = initializer.arguments[0];
|
|
403
|
-
if (!descriptor || !ts.isObjectLiteralExpression(descriptor)) return void 0;
|
|
404
|
-
for (const entry of descriptor.properties) {
|
|
405
|
-
if (!ts.isPropertyAssignment(entry) || entry.name.getText(sf) !== "root") continue;
|
|
406
|
-
if (!ts.isStringLiteral(entry.initializer)) fail(entry, "OEC9030", "island config root must be a string literal");
|
|
407
|
-
const value = entry.initializer.text;
|
|
408
|
-
if (value !== "light" && value !== "shadow") fail(entry, "OEC9030", `island config root "${value}" is unsupported; use "light" or "shadow"`);
|
|
409
|
-
return value;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
/**
|
|
413
|
-
* Admit one parsed module to the compiled grammar and lift its shape
|
|
414
|
-
* (#1473 split): the top-level
|
|
415
|
-
* statement admission (OEC9008 fail-closed, island policy passthrough), the
|
|
416
|
-
* exactly-one-`@element`-class rule with its decorator options and canonical
|
|
417
|
-
* heritage provenance, and the {@link propertyFields} inventory. Component
|
|
418
|
-
* styles are admitted in one form only (#1558): `.css` file imports arrayed
|
|
419
|
-
* in `static styles` — any inline sheet shape fails closed with OEC9029.
|
|
420
|
-
*/
|
|
421
|
-
function analyzeCompiledModule(sf, intrinsics, fail) {
|
|
422
|
-
const sheetImports = collectSheetImports(sf, fail);
|
|
423
|
-
const passthroughStatements = [];
|
|
424
|
-
let islandConfigStatement;
|
|
425
|
-
for (const statement of sf.statements) {
|
|
426
|
-
if (ts.isImportDeclaration(statement) || ts.isClassDeclaration(statement) || ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement) || ts.isModuleDeclaration(statement) || isDeclareStatement(statement)) continue;
|
|
427
|
-
if (isIslandConfigStatement(statement, intrinsics)) {
|
|
428
|
-
passthroughStatements.push(statement);
|
|
429
|
-
islandConfigStatement = statement;
|
|
430
|
-
continue;
|
|
431
|
-
}
|
|
432
|
-
fail(statement, "OEC9008", "runtime top-level statements are outside the compiled module grammar — keep shared state in a plain module and import it; the island policy statement (export const openElement = defineIslandConfig(...)) is the one admitted runtime statement");
|
|
433
|
-
}
|
|
434
|
-
const classes = sf.statements.filter(ts.isClassDeclaration);
|
|
435
|
-
const decorated = classes.filter((node) => (ts.getDecorators(node) ?? []).some((decorator) => {
|
|
436
|
-
const expr = decorator.expression;
|
|
437
|
-
return ts.isCallExpression(expr) && intrinsics.resolveIntrinsic(expr.expression, "element").canonical;
|
|
438
|
-
}));
|
|
439
|
-
if (classes.length !== 1 || decorated.length !== 1) {
|
|
440
|
-
for (const node of classes) for (const decorator of ts.getDecorators(node) ?? []) {
|
|
441
|
-
const expr = decorator.expression;
|
|
442
|
-
if (!ts.isCallExpression(expr)) continue;
|
|
443
|
-
const resolution = intrinsics.resolveIntrinsic(expr.expression, "element");
|
|
444
|
-
if (resolution.unsupported) fail(decorator, "OEC9027", `unsupported @element provenance: ${resolution.unsupported}`);
|
|
445
|
-
}
|
|
446
|
-
fail(sf, "OEC9001", "expected exactly one @element(...) class per compiled module (the decorator must bind the canonical element import from '@openelement/element')");
|
|
447
|
-
}
|
|
448
|
-
const classNode = decorated[0];
|
|
449
|
-
const classDecorators = ts.getDecorators(classNode) ?? [];
|
|
450
|
-
if (classDecorators.length !== 1) fail(classNode, "OEC9004", "compiled OpenElement classes must carry exactly one @element decorator");
|
|
451
|
-
const classModifiers = ts.getModifiers(classNode) ?? [];
|
|
452
|
-
const isDefaultExport = classModifiers.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword);
|
|
453
|
-
if (!(isDefaultExport ? classModifiers.length === 2 && classModifiers.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword) : classModifiers.length === 1 && classModifiers[0].kind === ts.SyntaxKind.ExportKeyword)) fail(classNode, "OEC9006", "compiled @element classes must be exported (optionally as the default export) without other class modifiers");
|
|
454
|
-
let tag = "";
|
|
455
|
-
const decorator = classDecorators[0];
|
|
456
|
-
const expr = decorator.expression;
|
|
457
|
-
if (!ts.isCallExpression(expr) || !intrinsics.resolveIntrinsic(expr.expression, "element").canonical) fail(decorator, "OEC9004", "unknown decorator on an OpenElement class; use only @element");
|
|
458
|
-
if (expr.arguments.length < 1 || expr.arguments.length > 2 || !ts.isStringLiteral(expr.arguments[0])) fail(decorator, "OEC9002", "@element requires a string tag name plus an optional options object");
|
|
459
|
-
tag = expr.arguments[0].text;
|
|
460
|
-
if (!/^[a-z][a-z0-9]*(-[a-z0-9]+)+$/.test(tag)) fail(decorator, "OEC9002", `@element tag "${tag}" is not a custom-element name — use a lowercase name with at least one dash (my-element)`);
|
|
461
|
-
let rootKind = "light";
|
|
462
|
-
let delegatesFocus = false;
|
|
463
|
-
let formAssociated = false;
|
|
464
|
-
if (expr.arguments.length === 2) {
|
|
465
|
-
const options = expr.arguments[1];
|
|
466
|
-
if (!ts.isObjectLiteralExpression(options)) fail(decorator, "OEC9002", "@element options must be an object literal");
|
|
467
|
-
const seenOptions = /* @__PURE__ */ new Set();
|
|
468
|
-
for (const entry of options.properties) {
|
|
469
|
-
if (!ts.isPropertyAssignment(entry)) fail(entry, "OEC9002", "@element options must be named literal assignments");
|
|
470
|
-
const optionName = entry.name.getText(sf);
|
|
471
|
-
if (seenOptions.has(optionName)) fail(entry, "OEC9002", `@element option "${optionName}" may appear only once`);
|
|
472
|
-
seenOptions.add(optionName);
|
|
473
|
-
if (optionName === "root") {
|
|
474
|
-
if (!ts.isStringLiteral(entry.initializer)) fail(entry, "OEC9002", "@element root must be a string literal");
|
|
475
|
-
const mode = entry.initializer.text;
|
|
476
|
-
if (mode !== "light" && mode !== "shadow-open" && mode !== "shadow-closed") fail(entry, "OEC9002", `@element root "${mode}" is unsupported; use "light", "shadow-open" or "shadow-closed"`);
|
|
477
|
-
rootKind = mode;
|
|
478
|
-
continue;
|
|
479
|
-
}
|
|
480
|
-
if (optionName === "delegatesFocus" || optionName === "formAssociated") {
|
|
481
|
-
if (entry.initializer.kind !== ts.SyntaxKind.TrueKeyword && entry.initializer.kind !== ts.SyntaxKind.FalseKeyword) fail(entry, "OEC9002", `@element ${optionName} must be a boolean literal`);
|
|
482
|
-
const value = entry.initializer.kind === ts.SyntaxKind.TrueKeyword;
|
|
483
|
-
if (optionName === "delegatesFocus") delegatesFocus = value;
|
|
484
|
-
else formAssociated = value;
|
|
485
|
-
continue;
|
|
486
|
-
}
|
|
487
|
-
fail(entry, "OEC9002", "@element options support only root, delegatesFocus and formAssociated");
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
if (islandConfigStatement !== void 0) {
|
|
491
|
-
const declaredRoot = islandConfigDeclaredRoot(islandConfigStatement, sf, fail);
|
|
492
|
-
const compiledRoot = rootKind === "light" ? "light" : "shadow";
|
|
493
|
-
if (declaredRoot !== void 0 && declaredRoot !== compiledRoot) fail(islandConfigStatement, "OEC9030", `the island config declares root "${declaredRoot}" but @element('${tag}', { root: '${rootKind}' }) compiles a ${compiledRoot}-rooted program. The @element decorator owns the root — point the config at the light component (@element(tag, { root: 'light' })) or drop the config's root field so the two agree.`);
|
|
494
|
-
}
|
|
495
|
-
const heritage = classNode.heritageClauses?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword);
|
|
496
|
-
const heritageExpression = heritage?.types.length === 1 ? unwrapExpression(heritage.types[0].expression) : void 0;
|
|
497
|
-
const heritageResolution = resolveCanonicalBaseClass(intrinsics, heritageExpression);
|
|
498
|
-
if (!heritageResolution?.canonical) fail(classNode.name ?? classNode, "OEC9003", `compiled classes must extend a canonical import of OpenElement or ErrorBoundary from '@openelement/element' (found ${heritage?.types[0]?.expression.getText(sf) ?? "no base class"}` + (heritageResolution?.unsupported ? `; ${heritageResolution.unsupported}` : "") + ")");
|
|
499
|
-
const openElementLocalName = heritageResolution.localName;
|
|
500
|
-
if (!classNode.name) fail(classNode, "OEC9003", "compiled classes must be named");
|
|
501
|
-
const className = classNode.name.text;
|
|
502
|
-
const { fields, methods, render, stylesText, stylesNode, stylesTypeText } = propertyFields(sf, classNode, intrinsics, fail);
|
|
503
|
-
const styleRequests = admitStylesInitializer(stylesNode, sheetImports, sf, fail);
|
|
504
|
-
return {
|
|
505
|
-
passthroughStatements,
|
|
506
|
-
classNode,
|
|
507
|
-
decorator,
|
|
508
|
-
tag,
|
|
509
|
-
rootKind,
|
|
510
|
-
delegatesFocus,
|
|
511
|
-
formAssociated,
|
|
512
|
-
isDefaultExport,
|
|
513
|
-
openElementLocalName,
|
|
514
|
-
className,
|
|
515
|
-
fields,
|
|
516
|
-
methods,
|
|
517
|
-
render,
|
|
518
|
-
stylesText,
|
|
519
|
-
stylesNode,
|
|
520
|
-
stylesTypeText,
|
|
521
|
-
styleRequests
|
|
522
|
-
};
|
|
523
|
-
}
|
|
524
129
|
//#endregion
|
|
525
|
-
export {
|
|
130
|
+
export { isSafeAttributeName$1 as isSafeAttributeName, literalValue, primitiveText, unwrapExpression, validateComputedExpressionBody };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
//#region src/internal/compiler/semantic-core/attribute-classification.ts
|
|
2
|
+
/**
|
|
3
|
+
* Intrinsic attribute classification — the ONE shared classifier for a dynamic
|
|
4
|
+
* JSX attribute name on an intrinsic element.
|
|
5
|
+
*
|
|
6
|
+
* The production compiler (`semantic-core/lower-program.ts`) and the Semantic
|
|
7
|
+
* IR builder (`semantic-ir/build.ts`) both consume this module, so the question
|
|
8
|
+
* "is `class` a class binding or a JS property expando?" has exactly one answer
|
|
9
|
+
* on every path. The two name lists moved here verbatim from lower-program; the
|
|
10
|
+
* precedence below is the production precedence, and no consumer may invent a
|
|
11
|
+
* precedence of its own:
|
|
12
|
+
*
|
|
13
|
+
* key → ref → event (`/^on[A-Z]/`) → class → style → innerHTML → boolean
|
|
14
|
+
* (lowercased name) → value / DOM IDL property → attribute
|
|
15
|
+
*
|
|
16
|
+
* Classification is naming only: it says what the authored attribute *means*,
|
|
17
|
+
* not that the name or the value is admissible. Safety rejection (forbidden
|
|
18
|
+
* sinks, URL-carrying attributes, raw-text tags) stays with the consumers'
|
|
19
|
+
* canonical predicates — the compiler core runs `forbiddenSinkReason` /
|
|
20
|
+
* `isSafeAttributeName` on its Part paths, and a code generator applies the
|
|
21
|
+
* same predicates before it emits anything. Reading this module therefore never
|
|
22
|
+
* turns an unsafe name into a safe one.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Presence-only DOM attributes: the value is set/removed, never string-coerced
|
|
26
|
+
* (`<input disabled>`). Matched against the lowercased authored name, matching
|
|
27
|
+
* the HTML parser's name folding.
|
|
28
|
+
*/
|
|
29
|
+
const BOOLEAN_ATTRIBUTES = /* @__PURE__ */ new Set([
|
|
30
|
+
"allowfullscreen",
|
|
31
|
+
"async",
|
|
32
|
+
"autofocus",
|
|
33
|
+
"autoplay",
|
|
34
|
+
"checked",
|
|
35
|
+
"controls",
|
|
36
|
+
"default",
|
|
37
|
+
"defer",
|
|
38
|
+
"disabled",
|
|
39
|
+
"formnovalidate",
|
|
40
|
+
"hidden",
|
|
41
|
+
"inert",
|
|
42
|
+
"ismap",
|
|
43
|
+
"itemscope",
|
|
44
|
+
"loop",
|
|
45
|
+
"multiple",
|
|
46
|
+
"muted",
|
|
47
|
+
"nomodule",
|
|
48
|
+
"open",
|
|
49
|
+
"playsinline",
|
|
50
|
+
"readonly",
|
|
51
|
+
"required",
|
|
52
|
+
"reversed",
|
|
53
|
+
"selected"
|
|
54
|
+
]);
|
|
55
|
+
/**
|
|
56
|
+
* DOM IDL property names the compiler binds as JS properties (`el[name] =
|
|
57
|
+
* value`) instead of attributes. Matched case-sensitively, exactly as authored:
|
|
58
|
+
* the DOM property is the authored spelling.
|
|
59
|
+
*/
|
|
60
|
+
const DOM_PROPERTY_NAMES = /* @__PURE__ */ new Set([
|
|
61
|
+
"checked",
|
|
62
|
+
"files",
|
|
63
|
+
"indeterminate",
|
|
64
|
+
"readOnly",
|
|
65
|
+
"scrollLeft",
|
|
66
|
+
"scrollTop",
|
|
67
|
+
"selected",
|
|
68
|
+
"value"
|
|
69
|
+
]);
|
|
70
|
+
/**
|
|
71
|
+
* Classify one dynamic attribute name for an intrinsic element with `tag`.
|
|
72
|
+
*
|
|
73
|
+
* The precedence is production's, in order: `key` → `ref` → `/^on[A-Z]/` →
|
|
74
|
+
* `class` → `style` → `innerHTML` → boolean (lowercased name) → `value`/
|
|
75
|
+
* {@link DOM_PROPERTY_NAMES}/`<input value>` → plain attribute. Keep the order
|
|
76
|
+
* intact: `checked` and `selected` sit in both lists, and presence semantics
|
|
77
|
+
* must win (production's boolean branch precedes its property branch).
|
|
78
|
+
*/
|
|
79
|
+
function classifyIntrinsicAttribute(name, tag) {
|
|
80
|
+
const lowerName = name.toLowerCase();
|
|
81
|
+
if (name === "key") return "key";
|
|
82
|
+
if (name === "ref") return "ref";
|
|
83
|
+
if (/^on[A-Z]/.test(name)) return "event";
|
|
84
|
+
if (name === "class") return "class";
|
|
85
|
+
if (name === "style") return "style";
|
|
86
|
+
if (name === "innerHTML") return "html";
|
|
87
|
+
if (BOOLEAN_ATTRIBUTES.has(lowerName)) return "boolean";
|
|
88
|
+
if (name === "value" || DOM_PROPERTY_NAMES.has(name) || tag === "input" && name === "value") return "property";
|
|
89
|
+
return "attribute";
|
|
90
|
+
}
|
|
91
|
+
//#endregion
|
|
92
|
+
export { BOOLEAN_ATTRIBUTES, DOM_PROPERTY_NAMES, classifyIntrinsicAttribute };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CompilerDiagnosticError, diagnosticAt
|
|
1
|
+
import { CompilerDiagnosticError, diagnosticAt } from "./diagnostics/index.js";
|
|
2
2
|
//#region src/internal/compiler/semantic-core/compiler-diagnostics.ts
|
|
3
3
|
/** Error shape consumed by the Vite plugin and compiler tests. */
|
|
4
4
|
var CompiledElementError = class extends CompilerDiagnosticError {
|
|
@@ -32,19 +32,5 @@ function sourceRange(sf, node) {
|
|
|
32
32
|
}
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
|
-
function parseDiagnostic(sf, diagnostic) {
|
|
36
|
-
const start = diagnostic.start ?? 0;
|
|
37
|
-
const end = diagnostic.start === void 0 ? start : start + (diagnostic.length ?? 0);
|
|
38
|
-
const position = sf.getLineAndCharacterOfPosition(start);
|
|
39
|
-
return {
|
|
40
|
-
code: "OEC9000",
|
|
41
|
-
message: diagnosticMessage(diagnostic),
|
|
42
|
-
file: sf.fileName,
|
|
43
|
-
line: position.line + 1,
|
|
44
|
-
character: position.character + 1,
|
|
45
|
-
start,
|
|
46
|
-
end
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
35
|
//#endregion
|
|
50
|
-
export { CompiledElementError, failAt,
|
|
36
|
+
export { CompiledElementError, failAt, sourceRange };
|