@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,73 @@
1
+ import { CompilerFail } from "./compiler-diagnostics.js";
2
+ import ts from "typescript";
3
+ import { PropertyValueType, SerializableValue } from "@openelement/protocol/part-program";
4
+ //#region src/internal/compiler/semantic-core/analyze-module.d.ts
5
+ /** The property inventory a caller hands the lowering walk. */
6
+ export interface CompiledField {
7
+ name: string;
8
+ reflect: boolean;
9
+ attribute: string | null;
10
+ type: PropertyValueType;
11
+ converter: PropertyValueType;
12
+ typeConstructor: 'String' | 'Number' | 'Boolean' | 'Array' | 'Object';
13
+ accessibility: 'public' | 'protected' | 'private' | '';
14
+ typeText: string;
15
+ initializerText: string;
16
+ defaultValue: SerializableValue;
17
+ node: ts.PropertyDeclaration;
18
+ /**
19
+ * Computed field (alpha.8): the source initializer is
20
+ * `computed(() => <expr>)` (optionally behind a type assertion). The
21
+ * compiled accessor returns the derived value. The compiler records the
22
+ * source-signal dependencies and emits a `__computedFields` factory over the
23
+ * instance's signal record; no field initializer runs on the generated class.
24
+ */
25
+ computed?: {
26
+ deps: string[];
27
+ factoryText: string;
28
+ body: ts.Expression;
29
+ };
30
+ }
31
+ /** The compiler face of the one canonical attribute-name predicate. */
32
+ declare function isSafeAttributeName(value: string): boolean;
33
+ declare function unwrapExpression(expr: ts.Expression): ts.Expression;
34
+ /** Return a JSON-safe literal, or undefined when the expression is not literal. */
35
+ declare function literalValue(expr: ts.Expression, sf: ts.SourceFile): SerializableValue | undefined;
36
+ declare function primitiveText(value: SerializableValue): string | null;
37
+ /**
38
+ * Message actors for computed-body diagnostics. One validation walk serves
39
+ * both computed-field initializers and the inline conditional class (#1556);
40
+ * the subjects keep each caller's diagnostic prose precise while the shared
41
+ * defaults reproduce the computed-field messages byte-identically.
42
+ */
43
+ export interface ComputedBodySubjects {
44
+ /** Actor for shape violations ("X may not nest non-arrow functions"). */
45
+ readonly actor: string;
46
+ /** Actor for computed-field reads ("X may not read computed field ..."). */
47
+ readonly single: string;
48
+ /** Actor for undeclared reads ("X may only read this.<declared property>"). */
49
+ readonly plural: string;
50
+ }
51
+ /**
52
+ * Validate one computed-body expression and derive its factory text (#1556
53
+ * split of parseComputedInitializer). The body may read `this.<field>` of any
54
+ * NON-computed declared field (rewritten to the signal-record read) and use
55
+ * any module-scope values; `this` in any other position, reads of computed
56
+ * fields, and nested non-arrow functions fail closed (OEC9024). Returns the
57
+ * source-signal dependency list (declaration order) and the body text with
58
+ * every `this.<field>` read rewritten to `__s.<field>.value`.
59
+ *
60
+ * `binding` (alpha15 server lane) switches the read accessor from
61
+ * `this.<field>` to `<parameter>.<field>` (a server function component's
62
+ * props parameter): the same declared-field table and the same messages —
63
+ * only the accessor spelling differs, and no signal-record rewrite applies
64
+ * (the generated constructor reads the props object directly). The default
65
+ * keeps the class path's behavior and messages byte-identical.
66
+ */
67
+ declare function validateComputedExpressionBody(body: ts.Expression, sf: ts.SourceFile, plainFieldNames: ReadonlySet<string>, computedFieldNames: ReadonlySet<string>, fail: CompilerFail, subjects?: ComputedBodySubjects, binding?: {
68
+ readonly parameter: string;
69
+ }): {
70
+ deps: string[];
71
+ bodyText: string;
72
+ };
73
+ //#endregion
@@ -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 { camelToKebab, isSafeAttributeName } from "@openelement/protocol/forbidden-sinks";
2
+ import { isSafeAttributeName } from "@openelement/protocol/forbidden-sinks";
5
3
  //#region src/internal/compiler/semantic-core/analyze-module.ts
6
4
  /**
7
- * Module/class-shape analysis for the compiled grammar (#1473 split): the
8
- * admitted statement shapes,
9
- * the one `@element` class with its decorator options and canonical heritage,
10
- * and the `@property` field/method/render inventory the later lowering and
11
- * emission stages consume.
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",
@@ -105,15 +78,31 @@ const COMPUTED_FIELD_SUBJECTS = {
105
78
  * fields, and nested non-arrow functions fail closed (OEC9024). Returns the
106
79
  * source-signal dependency list (declaration order) and the body text with
107
80
  * every `this.<field>` read rewritten to `__s.<field>.value`.
81
+ *
82
+ * `binding` (alpha15 server lane) switches the read accessor from
83
+ * `this.<field>` to `<parameter>.<field>` (a server function component's
84
+ * props parameter): the same declared-field table and the same messages —
85
+ * only the accessor spelling differs, and no signal-record rewrite applies
86
+ * (the generated constructor reads the props object directly). The default
87
+ * keeps the class path's behavior and messages byte-identical.
108
88
  */
109
- function validateComputedExpressionBody(body, sf, plainFieldNames, computedFieldNames, fail, subjects = COMPUTED_FIELD_SUBJECTS) {
89
+ function validateComputedExpressionBody(body, sf, plainFieldNames, computedFieldNames, fail, subjects = COMPUTED_FIELD_SUBJECTS, binding) {
110
90
  const deps = [];
111
91
  const replacements = [];
112
92
  const visit = (node) => {
113
- if (ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) fail(node, "OEC9024", `${subjects.actor} may not nest non-arrow functions`);
93
+ if (ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) fail(node, "OEC9024", `${subjects.actor} may not nest non-arrow functions — hoist the function to module scope and import it`);
94
+ if (binding !== void 0) {
95
+ if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === binding.parameter) {
96
+ const name = node.name.text;
97
+ if (computedFieldNames.has(name)) fail(node, "OEC9024", `${subjects.single} may not read computed field "${name}" — read its source ${binding.parameter}.<property> inputs instead (computed fields cannot chain)`);
98
+ if (!plainFieldNames.has(name)) fail(node, "OEC9024", `${subjects.plural} may only read ${binding.parameter}.<declared property> (found ${binding.parameter}.${name})`);
99
+ if (!deps.includes(name)) deps.push(name);
100
+ return;
101
+ }
102
+ }
114
103
  if (ts.isPropertyAccessExpression(node) && node.expression.kind === ts.SyntaxKind.ThisKeyword) {
115
104
  const name = node.name.text;
116
- if (computedFieldNames.has(name)) fail(node, "OEC9024", `${subjects.single} may not read computed field "${name}"`);
105
+ if (computedFieldNames.has(name)) fail(node, "OEC9024", `${subjects.single} may not read computed field "${name}" — read its source this.<property> inputs instead (computed fields cannot chain)`);
117
106
  if (!plainFieldNames.has(name)) fail(node, "OEC9024", `${subjects.plural} may only read this.<declared property> (found this.${name})`);
118
107
  if (!deps.includes(name)) deps.push(name);
119
108
  replacements.push({
@@ -137,344 +126,5 @@ function validateComputedExpressionBody(body, sf, plainFieldNames, computedField
137
126
  bodyText
138
127
  };
139
128
  }
140
- /**
141
- * Parse a computed field initializer (alpha.8): `computed(() => ...)`
142
- * behind optional `as`-casts, where the callee binds the canonical `computed`
143
- * import (#1209 — a same-name spelling bound to anything else fails closed
144
- * with OEC9025). The arrow body may read `this.<field>` of any NON-computed
145
- * declared field (rewritten to the signal-record read) and use any
146
- * module-scope values; `this` in any other position, reads of computed
147
- * fields, and nested non-arrow functions fail closed (OEC9024). Returns null
148
- * for non-computed initializers.
149
- */
150
- function parseComputedInitializer(initializer, sf, intrinsics, plainFieldNames, computedFieldNames, fail) {
151
- const value = unwrapExpression(initializer);
152
- if (!ts.isCallExpression(value)) return null;
153
- const resolution = intrinsics.resolveIntrinsic(value.expression, "computed");
154
- if (!resolution.canonical) {
155
- 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})` : ""));
156
- return null;
157
- }
158
- if (value.arguments.length !== 1 || !ts.isArrowFunction(value.arguments[0])) fail(value, "OEC9024", "computed fields take exactly one zero-argument arrow function");
159
- const arrow = value.arguments[0];
160
- if (arrow.parameters.length > 0) fail(arrow, "OEC9024", "computed field arrows may not declare parameters");
161
- 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)");
162
- else fail(arrow.body, "OEC9024", "computed field arrows use an expression body, not a block");
163
- const body = arrow.body;
164
- const { deps, bodyText } = validateComputedExpressionBody(body, sf, plainFieldNames, computedFieldNames, fail);
165
- return {
166
- deps,
167
- factoryText: `(__s) => ${resolution.localName}(() => ${bodyText})`,
168
- body
169
- };
170
- }
171
- function inferPropertyType(member, defaultValue, sf) {
172
- const typeText = member.type?.getText(sf).replace(/[\s?]/g, "") ?? "";
173
- if (typeText === "string" || typeText === "String") return {
174
- label: "string",
175
- constructorName: "String"
176
- };
177
- if (typeText === "number" || typeText === "Number") return {
178
- label: "number",
179
- constructorName: "Number"
180
- };
181
- if (typeText === "boolean" || typeText === "Boolean") return {
182
- label: "boolean",
183
- constructorName: "Boolean"
184
- };
185
- if (/^(Array|ReadonlyArray|Set)</.test(typeText) || typeText.endsWith("[]")) return {
186
- label: "array",
187
- constructorName: "Array"
188
- };
189
- if (/^(Record|Map|Object)</.test(typeText)) return {
190
- label: "object",
191
- constructorName: "Object"
192
- };
193
- if (Array.isArray(defaultValue)) return {
194
- label: "array",
195
- constructorName: "Array"
196
- };
197
- if (defaultValue !== null && typeof defaultValue === "object") return {
198
- label: "object",
199
- constructorName: "Object"
200
- };
201
- if (typeof defaultValue === "number") return {
202
- label: "number",
203
- constructorName: "Number"
204
- };
205
- if (typeof defaultValue === "boolean") return {
206
- label: "boolean",
207
- constructorName: "Boolean"
208
- };
209
- return {
210
- label: "string",
211
- constructorName: "String"
212
- };
213
- }
214
- function propertyFields(sf, classNode, intrinsics, fail) {
215
- const fields = [];
216
- const methods = [];
217
- let render = null;
218
- let stylesText;
219
- let stylesNode;
220
- let stylesTypeText;
221
- const names = /* @__PURE__ */ new Set();
222
- const propertyAttributeNames = /* @__PURE__ */ new Set();
223
- for (const member of classNode.members) {
224
- if (ts.isPropertyDeclaration(member)) {
225
- const modifiers = ts.getModifiers(member) ?? [];
226
- if (modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword) && ts.isIdentifier(member.name) && member.name.text === "styles" && (ts.getDecorators(member) ?? []).length === 0) {
227
- if (!member.initializer) fail(member, "OEC9005", "static styles requires an initializer");
228
- if (stylesText !== void 0) fail(member, "OEC9005", "compiled classes may declare static styles only once");
229
- stylesText = member.initializer.getText(sf);
230
- stylesNode = member.initializer;
231
- stylesTypeText = member.type ? `: ${member.type.getText(sf)}` : void 0;
232
- continue;
233
- }
234
- const accessibilityModifiers = modifiers.filter((modifier) => modifier.kind === ts.SyntaxKind.PublicKeyword || modifier.kind === ts.SyntaxKind.ProtectedKeyword || modifier.kind === ts.SyntaxKind.PrivateKeyword);
235
- 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");
236
- const decorators = ts.getDecorators(member) ?? [];
237
- if (decorators.length !== 1) fail(member, "OEC9005", "compiled fields must carry exactly one @property decorator");
238
- const decorator = decorators[0];
239
- const call = decorator.expression;
240
- if (!ts.isCallExpression(call)) fail(decorator, "OEC9004", "unknown decorator on an OpenElement member; use only @property");
241
- const decoratorResolution = intrinsics.resolveIntrinsic(call.expression, "property");
242
- if (!decoratorResolution.canonical) {
243
- if (decoratorResolution.unsupported) fail(decorator, "OEC9027", `unsupported @property provenance: ${decoratorResolution.unsupported}`);
244
- fail(decorator, "OEC9004", "unknown decorator on an OpenElement member; use only @property imported from '@openelement/element'");
245
- }
246
- if (!ts.isIdentifier(member.name)) fail(member, "OEC9005", "property names must be identifiers");
247
- if (names.has(member.name.text)) fail(member, "OEC9005", `duplicate property "${member.name.text}"`);
248
- names.add(member.name.text);
249
- if (!member.initializer) fail(member, "OEC9005", "compiled fields require a literal initializer");
250
- const computedFieldNames = new Set(fields.filter((f) => f.computed).map((f) => f.name));
251
- const computedInit = parseComputedInitializer(member.initializer, sf, intrinsics, new Set(fields.filter((f) => !f.computed).map((f) => f.name)), computedFieldNames, fail);
252
- const trustedHtmlInit = !computedInit && isTrustedHtmlInitializer(member.initializer, intrinsics, fail);
253
- const defaultValue = computedInit || trustedHtmlInit ? null : literalValue(member.initializer, sf);
254
- if (defaultValue === void 0) fail(member.initializer, "OEC9020", "property defaults must be serializable literals");
255
- const options = call.arguments.length === 1 ? call.arguments[0] : void 0;
256
- if (!options || !ts.isObjectLiteralExpression(options)) fail(decorator, "OEC9005", "@property requires one options object literal");
257
- let reflect = false;
258
- let attribute;
259
- let explicitType;
260
- let explicitConverter;
261
- const optionNames = /* @__PURE__ */ new Set();
262
- for (const entry of options.properties) {
263
- if (!ts.isPropertyAssignment(entry) || entry.name.getText(sf).length === 0) fail(entry, "OEC9022", "@property options must be named literal assignments");
264
- const optionName = entry.name.getText(sf);
265
- if (optionNames.has(optionName)) fail(entry, "OEC9022", `@property option "${optionName}" may appear only once`);
266
- optionNames.add(optionName);
267
- if (optionName === "reflect") {
268
- if (entry.initializer.kind !== ts.SyntaxKind.TrueKeyword && entry.initializer.kind !== ts.SyntaxKind.FalseKeyword) fail(entry, "OEC9005", "@property reflect must be a boolean literal");
269
- reflect = entry.initializer.kind === ts.SyntaxKind.TrueKeyword;
270
- continue;
271
- }
272
- if (optionName === "attribute") {
273
- if (entry.initializer.kind === ts.SyntaxKind.FalseKeyword) attribute = null;
274
- else if (ts.isStringLiteral(entry.initializer)) {
275
- attribute = camelToKebab(entry.initializer.text);
276
- if (!isSafeAttributeName$1(attribute)) fail(entry, "OEC9023", "property attribute name is unsafe");
277
- } else fail(entry, "OEC9023", "property attribute must be a string literal or false");
278
- continue;
279
- }
280
- if (optionName === "type" || optionName === "converter") {
281
- const type = propertyTypeFromConstructor(entry.initializer, sf, entry, fail);
282
- if (optionName === "type") explicitType = type;
283
- else explicitConverter = type.label;
284
- continue;
285
- }
286
- fail(entry, "OEC9022", `unsupported @property option "${optionName}"`);
287
- }
288
- const inferred = explicitType ?? inferPropertyType(member, defaultValue, sf);
289
- const converter = explicitConverter ?? inferred.label;
290
- if (attribute === void 0) attribute = camelToKebab(member.name.text);
291
- if (trustedHtmlInit) {
292
- if (inferred.label !== "object" || attribute !== null || reflect) fail(member, "OEC9026", "trustedHtml field defaults require type: Object, reflect: false, and attribute: false");
293
- }
294
- if (computedInit) {
295
- if (attribute !== null || reflect) fail(member, "OEC9025", "computed fields require @property({ reflect: false, attribute: false }) — derived signals have no attribute channel");
296
- }
297
- if (attribute !== null) {
298
- const attributeKey = attribute.toLowerCase();
299
- if (propertyAttributeNames.has(attributeKey)) fail(member, "OEC9023", `duplicate property attribute "${attribute}"`);
300
- propertyAttributeNames.add(attributeKey);
301
- }
302
- if (attribute === null && reflect) fail(member, "OEC9023", "a property with attribute: false cannot reflect");
303
- fields.push({
304
- name: member.name.text,
305
- reflect,
306
- attribute,
307
- type: inferred.label,
308
- converter,
309
- typeConstructor: inferred.constructorName,
310
- accessibility: accessibilityModifiers.length === 0 ? "" : accessibilityModifiers[0].kind === ts.SyntaxKind.PublicKeyword ? "public" : accessibilityModifiers[0].kind === ts.SyntaxKind.ProtectedKeyword ? "protected" : "private",
311
- typeText: member.type ? `: ${member.type.getText(sf)}` : "",
312
- initializerText: member.initializer.getText(sf),
313
- defaultValue,
314
- node: member,
315
- ...computedInit ? { computed: computedInit } : {}
316
- });
317
- continue;
318
- }
319
- if (ts.isMethodDeclaration(member)) {
320
- const modifiers = ts.getModifiers(member) ?? [];
321
- 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");
322
- if ((ts.getDecorators(member) ?? []).length > 0) fail(member, "OEC9004", "methods may not carry decorators in the compiler grammar");
323
- if (member.parameters.some((parameter) => (ts.getDecorators(parameter) ?? []).length > 0)) fail(member, "OEC9004", "method parameters may not carry decorators in the compiler grammar");
324
- if (!ts.isIdentifier(member.name)) fail(member, "OEC9006", "method names must be identifiers");
325
- if (member.name.text === "render") {
326
- if (render) fail(member, "OEC9007", "compiled classes may declare only one render() method");
327
- if (member.parameters.length !== 0 || modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword)) fail(member, "OEC9007", "render() must be a synchronous zero-argument method");
328
- render = member;
329
- } else methods.push(member);
330
- continue;
331
- }
332
- fail(member, "OEC9006", "constructors, accessors and undecorated fields are outside the compiler grammar");
333
- }
334
- if (!render) fail(classNode, "OEC9007", "compiled classes must declare render()");
335
- return {
336
- fields,
337
- methods,
338
- render,
339
- stylesText,
340
- stylesNode,
341
- stylesTypeText
342
- };
343
- }
344
- function isDeclareStatement(statement) {
345
- return ts.canHaveModifiers(statement) && (ts.getModifiers(statement) ?? []).some((modifier) => modifier.kind === ts.SyntaxKind.DeclareKeyword);
346
- }
347
- /**
348
- * The one extra runtime statement a compiled module may carry: the island
349
- * delivery policy colocated with the class
350
- * (`export const openElement = defineIslandConfig({ ... })`). The callee must
351
- * bind a static-sidecar descriptor the host injected through the compile
352
- * options (#1209); the default core knows none, so an unconfigured compiler
353
- * rejects the statement as OEC9008. A same-name spelling bound to anything
354
- * else is not the policy statement and falls through to the same rejection.
355
- * The statement is validated here and copied verbatim into the generated
356
- * module; the runtime factory validates the descriptor itself at module
357
- * evaluation. Anything else stays outside the compiled module grammar
358
- * (OEC9008).
359
- */
360
- function isIslandConfigStatement(statement, intrinsics) {
361
- if (!ts.isVariableStatement(statement)) return false;
362
- const modifiers = ts.getModifiers(statement) ?? [];
363
- if (modifiers.length !== 1 || modifiers[0].kind !== ts.SyntaxKind.ExportKeyword) return false;
364
- const declarations = statement.declarationList.declarations;
365
- if (declarations.length !== 1) return false;
366
- const declaration = declarations[0];
367
- if (!ts.isIdentifier(declaration.name) || declaration.name.text !== "openElement") return false;
368
- const initializer = declaration.initializer;
369
- if (!initializer || !ts.isCallExpression(initializer)) return false;
370
- if (!intrinsics.isStaticSidecarCallee(initializer.expression)) return false;
371
- if (initializer.arguments.length !== 1 || !ts.isObjectLiteralExpression(initializer.arguments[0])) return false;
372
- return true;
373
- }
374
- /**
375
- * Admit one parsed module to the compiled grammar and lift its shape
376
- * (#1473 split): the top-level
377
- * statement admission (OEC9008 fail-closed, island policy passthrough), the
378
- * exactly-one-`@element`-class rule with its decorator options and canonical
379
- * heritage provenance, and the {@link propertyFields} inventory. Component
380
- * styles are admitted in one form only (#1558): `.css` file imports arrayed
381
- * in `static styles` — any inline sheet shape fails closed with OEC9029.
382
- */
383
- function analyzeCompiledModule(sf, intrinsics, fail) {
384
- const sheetImports = collectSheetImports(sf, fail);
385
- const passthroughStatements = [];
386
- for (const statement of sf.statements) {
387
- if (ts.isImportDeclaration(statement) || ts.isClassDeclaration(statement) || ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement) || ts.isModuleDeclaration(statement) || isDeclareStatement(statement)) continue;
388
- if (isIslandConfigStatement(statement, intrinsics)) {
389
- passthroughStatements.push(statement);
390
- continue;
391
- }
392
- fail(statement, "OEC9008", "runtime top-level statements are outside the compiled module grammar");
393
- }
394
- const classes = sf.statements.filter(ts.isClassDeclaration);
395
- const decorated = classes.filter((node) => (ts.getDecorators(node) ?? []).some((decorator) => {
396
- const expr = decorator.expression;
397
- return ts.isCallExpression(expr) && intrinsics.resolveIntrinsic(expr.expression, "element").canonical;
398
- }));
399
- if (classes.length !== 1 || decorated.length !== 1) {
400
- for (const node of classes) for (const decorator of ts.getDecorators(node) ?? []) {
401
- const expr = decorator.expression;
402
- if (!ts.isCallExpression(expr)) continue;
403
- const resolution = intrinsics.resolveIntrinsic(expr.expression, "element");
404
- if (resolution.unsupported) fail(decorator, "OEC9027", `unsupported @element provenance: ${resolution.unsupported}`);
405
- }
406
- fail(sf, "OEC9001", "expected exactly one @element(...) class per compiled module (the decorator must bind the canonical element import from '@openelement/element')");
407
- }
408
- const classNode = decorated[0];
409
- const classDecorators = ts.getDecorators(classNode) ?? [];
410
- if (classDecorators.length !== 1) fail(classNode, "OEC9004", "compiled OpenElement classes must carry exactly one @element decorator");
411
- const classModifiers = ts.getModifiers(classNode) ?? [];
412
- const isDefaultExport = classModifiers.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword);
413
- 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");
414
- let tag = "";
415
- const decorator = classDecorators[0];
416
- const expr = decorator.expression;
417
- if (!ts.isCallExpression(expr) || !intrinsics.resolveIntrinsic(expr.expression, "element").canonical) fail(decorator, "OEC9004", "unknown decorator on an OpenElement class; use only @element");
418
- 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");
419
- tag = expr.arguments[0].text;
420
- if (!/^[a-z][a-z0-9]*(-[a-z0-9]+)+$/.test(tag)) fail(decorator, "OEC9002", `@element tag "${tag}" is not a custom-element name`);
421
- let rootKind = "light";
422
- let delegatesFocus = false;
423
- let formAssociated = false;
424
- if (expr.arguments.length === 2) {
425
- const options = expr.arguments[1];
426
- if (!ts.isObjectLiteralExpression(options)) fail(decorator, "OEC9002", "@element options must be an object literal");
427
- const seenOptions = /* @__PURE__ */ new Set();
428
- for (const entry of options.properties) {
429
- if (!ts.isPropertyAssignment(entry)) fail(entry, "OEC9002", "@element options must be named literal assignments");
430
- const optionName = entry.name.getText(sf);
431
- if (seenOptions.has(optionName)) fail(entry, "OEC9002", `@element option "${optionName}" may appear only once`);
432
- seenOptions.add(optionName);
433
- if (optionName === "root") {
434
- if (!ts.isStringLiteral(entry.initializer)) fail(entry, "OEC9002", "@element root must be a string literal");
435
- const mode = entry.initializer.text;
436
- if (mode !== "light" && mode !== "shadow-open" && mode !== "shadow-closed") fail(entry, "OEC9002", `@element root "${mode}" is unsupported; use "light", "shadow-open" or "shadow-closed"`);
437
- rootKind = mode;
438
- continue;
439
- }
440
- if (optionName === "delegatesFocus" || optionName === "formAssociated") {
441
- if (entry.initializer.kind !== ts.SyntaxKind.TrueKeyword && entry.initializer.kind !== ts.SyntaxKind.FalseKeyword) fail(entry, "OEC9002", `@element ${optionName} must be a boolean literal`);
442
- const value = entry.initializer.kind === ts.SyntaxKind.TrueKeyword;
443
- if (optionName === "delegatesFocus") delegatesFocus = value;
444
- else formAssociated = value;
445
- continue;
446
- }
447
- fail(entry, "OEC9002", "@element options support only root, delegatesFocus and formAssociated");
448
- }
449
- }
450
- const heritage = classNode.heritageClauses?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword);
451
- const heritageExpression = heritage?.types.length === 1 ? unwrapExpression(heritage.types[0].expression) : void 0;
452
- const heritageResolution = resolveCanonicalBaseClass(intrinsics, heritageExpression);
453
- 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}` : "") + ")");
454
- const openElementLocalName = heritageResolution.localName;
455
- if (!classNode.name) fail(classNode, "OEC9003", "compiled classes must be named");
456
- const className = classNode.name.text;
457
- const { fields, methods, render, stylesText, stylesNode, stylesTypeText } = propertyFields(sf, classNode, intrinsics, fail);
458
- const styleRequests = admitStylesInitializer(stylesNode, sheetImports, sf, fail);
459
- return {
460
- passthroughStatements,
461
- classNode,
462
- decorator,
463
- tag,
464
- rootKind,
465
- delegatesFocus,
466
- formAssociated,
467
- isDefaultExport,
468
- openElementLocalName,
469
- className,
470
- fields,
471
- methods,
472
- render,
473
- stylesText,
474
- stylesNode,
475
- stylesTypeText,
476
- styleRequests
477
- };
478
- }
479
129
  //#endregion
480
- export { analyzeCompiledModule, isSafeAttributeName$1 as isSafeAttributeName, literalValue, primitiveText, unwrapExpression, validateComputedExpressionBody };
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, diagnosticMessage } from "./diagnostics/index.js";
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, parseDiagnostic, sourceRange };
36
+ export { CompiledElementError, failAt, sourceRange };