@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
@@ -10,7 +10,8 @@
10
10
  * text. This registry is how an edge travels from the compiled-element
11
11
  * transform (the one writer) to the intercepting plugin (the one reader)
12
12
  * within a build process — keyed by the edge's resolved module id (the
13
- * importing module's directory plus the sheet's file name), the same id the
13
+ * importing module's directory plus the sheet's file name, plus the
14
+ * component's light-root scope tag when it has one), the same id the
14
15
  * bundler's resolver lands on. A `.css` import from a module with no
15
16
  * registered edges is a plain app stylesheet and rides vite's own CSS
16
17
  * channel; a `.css` import from a tracked module with no edge entry is a
@@ -26,6 +27,15 @@ export interface RegisteredStyleRequest {
26
27
  readonly specifier: string;
27
28
  /** The sheet file's absolute path — where the intercepting build reads the bytes. */
28
29
  readonly file: string;
30
+ /**
31
+ * The light-root component tag this edge's emitted asset is scoped to
32
+ * (alpha15 S lane, S-C1.2). A light root has no shadow boundary, so its
33
+ * sheets leave the build wrapped in native `@scope (<tag>)`; the tag is
34
+ * therefore part of the edge's identity, not a detail of its bytes — two
35
+ * components sharing one sheet file are two resources with two scopes.
36
+ * Absent for a shadow-root edge, whose bytes ship exactly as authored.
37
+ */
38
+ readonly scopeTag?: string;
29
39
  }
30
40
  /** Register one generated edge (idempotent per module id — recompiles overwrite). */
31
41
  export declare function registerStyleRequest(request: RegisteredStyleRequest): void;
@@ -35,19 +45,37 @@ export declare function getStyleRequest(moduleId: string): RegisteredStyleReques
35
45
  export declare function hasStyleImporter(importerId: string): boolean;
36
46
  /** Every edge whose sheet file is the given path — the dev HMR reverse lookup. */
37
47
  export declare function styleEdgesForFile(file: string): RegisteredStyleRequest[];
48
+ /**
49
+ * Every edge one importing module declared. The host build uses it to emit the
50
+ * style resources of modules it admitted for their edges alone (S-C1.4: a Page
51
+ * or Layout the server renders and no island imports).
52
+ */
53
+ export declare function styleRequestsForImporter(importerId: string): RegisteredStyleRequest[];
38
54
  /** Drop every registered edge (a build's bookkeeping is its own lifetime). */
39
55
  export declare function clearStyleRequests(): void;
40
56
  /**
41
- * The registry key for one authored edge: the sheet file's resolved path.
42
- * Query suffixes on the importer id (dev-time cache busting) are stripped,
43
- * so recompiles re-register under one key; the full relative path keeps
44
- * same-named sheets in sibling subdirectories distinct edges.
57
+ * The registry key for one authored edge: the sheet file's resolved path,
58
+ * plus the importing component's light-root scope tag when it has one. Query
59
+ * suffixes on the importer id (dev-time cache busting) are stripped, so
60
+ * recompiles re-register under one key; the full relative path keeps
61
+ * same-named sheets in sibling subdirectories distinct edges. The scope
62
+ * tag rides the key because the emitted asset is a different resource per
63
+ * scope: a light root's sheet is served wrapped in `@scope (<tag>)`.
64
+ *
65
+ * Registering an edge passes the tag explicitly (the caller is the compile
66
+ * that just proved it); the intercept resolves a `.css` import from
67
+ * `(importer, specifier)` alone, so it reads the same fact back through the
68
+ * importer index. Both paths land on one key for one edge, and a disagreement
69
+ * can only fail closed at the intercept — never emit an unscoped asset.
45
70
  */
46
- export declare function styleRequestModuleId(importerId: string, specifier: string): string;
71
+ export declare function styleRequestModuleId(importerId: string, specifier: string, scopeTag?: string): string;
47
72
  /**
48
73
  * The sheet file's absolute path for one authored edge: the importing
49
74
  * module's directory joined with the authored specifier. Relative specifiers
50
- * only (the compiler refuses bare ones), so the join is total.
75
+ * only (the compiler refuses bare ones), so the join is total. Parent
76
+ * segments (`../theme.css`) resolve lexically against the directory — the key
77
+ * must be the path the bundler's resolver lands on, never a verbatim `..`
78
+ * id no lookup can match.
51
79
  */
52
80
  export declare function styleRequestFile(importerId: string, specifier: string): string;
53
81
  //#endregion
@@ -2,10 +2,20 @@
2
2
  const requests = /* @__PURE__ */ new Map();
3
3
  /** Second index: which importing modules carry edges (the intercept's gate). */
4
4
  const importers = /* @__PURE__ */ new Set();
5
+ /**
6
+ * Third index: the light-root scope tag an importing module's edges carry.
7
+ * The intercept resolves a `.css` import from `(importer, specifier)` alone,
8
+ * so the scope must be readable from the importer — the registry key and the
9
+ * intercept's key derivation would otherwise disagree.
10
+ */
11
+ const importerScopes = /* @__PURE__ */ new Map();
5
12
  /** Register one generated edge (idempotent per module id — recompiles overwrite). */
6
13
  function registerStyleRequest(request) {
7
14
  requests.set(request.moduleId, request);
8
- importers.add(cleanImporterId(request.importer));
15
+ const importer = cleanImporterId(request.importer);
16
+ importers.add(importer);
17
+ if (request.scopeTag === void 0) importerScopes.delete(importer);
18
+ else importerScopes.set(importer, request.scopeTag);
9
19
  }
10
20
  /** The edge a resolved sheet module id carries, if any. */
11
21
  function getStyleRequest(moduleId) {
@@ -18,33 +28,69 @@ function hasStyleImporter(importerId) {
18
28
  /** Every edge whose sheet file is the given path — the dev HMR reverse lookup. */
19
29
  function styleEdgesForFile(file) {
20
30
  const clean = file.split("?", 1)[0];
21
- return [...requests.values()].filter((request) => request.moduleId === clean);
31
+ return [...requests.values()].filter((request) => request.file === clean);
32
+ }
33
+ /**
34
+ * Every edge one importing module declared. The host build uses it to emit the
35
+ * style resources of modules it admitted for their edges alone (S-C1.4: a Page
36
+ * or Layout the server renders and no island imports).
37
+ */
38
+ function styleRequestsForImporter(importerId) {
39
+ const clean = importerId.split("?", 1)[0];
40
+ return [...requests.values()].filter((request) => request.importer === clean);
22
41
  }
23
42
  /** Drop every registered edge (a build's bookkeeping is its own lifetime). */
24
43
  function clearStyleRequests() {
25
44
  requests.clear();
26
45
  importers.clear();
46
+ importerScopes.clear();
27
47
  }
28
48
  function cleanImporterId(importerId) {
29
49
  return importerId.split("?", 1)[0];
30
50
  }
31
51
  /**
32
- * The registry key for one authored edge: the sheet file's resolved path.
33
- * Query suffixes on the importer id (dev-time cache busting) are stripped,
34
- * so recompiles re-register under one key; the full relative path keeps
35
- * same-named sheets in sibling subdirectories distinct edges.
52
+ * The separator between a sheet file and the light-root scope tag in one
53
+ * edge's registry key. `\0` can never appear in a path or a tag name, so a
54
+ * scoped key can never collide with a plain file key.
55
+ */
56
+ const SCOPE_KEY_SEPARATOR = "\0";
57
+ /**
58
+ * The registry key for one authored edge: the sheet file's resolved path,
59
+ * plus the importing component's light-root scope tag when it has one. Query
60
+ * suffixes on the importer id (dev-time cache busting) are stripped, so
61
+ * recompiles re-register under one key; the full relative path keeps
62
+ * same-named sheets in sibling subdirectories distinct edges. The scope
63
+ * tag rides the key because the emitted asset is a different resource per
64
+ * scope: a light root's sheet is served wrapped in `@scope (<tag>)`.
65
+ *
66
+ * Registering an edge passes the tag explicitly (the caller is the compile
67
+ * that just proved it); the intercept resolves a `.css` import from
68
+ * `(importer, specifier)` alone, so it reads the same fact back through the
69
+ * importer index. Both paths land on one key for one edge, and a disagreement
70
+ * can only fail closed at the intercept — never emit an unscoped asset.
36
71
  */
37
- function styleRequestModuleId(importerId, specifier) {
38
- return styleRequestFile(importerId, specifier);
72
+ function styleRequestModuleId(importerId, specifier, scopeTag) {
73
+ const file = styleRequestFile(importerId, specifier);
74
+ const scope = scopeTag ?? importerScopes.get(cleanImporterId(importerId));
75
+ return scope === void 0 ? file : `${file}${SCOPE_KEY_SEPARATOR}${scope}`;
39
76
  }
40
77
  /**
41
78
  * The sheet file's absolute path for one authored edge: the importing
42
79
  * module's directory joined with the authored specifier. Relative specifiers
43
- * only (the compiler refuses bare ones), so the join is total.
80
+ * only (the compiler refuses bare ones), so the join is total. Parent
81
+ * segments (`../theme.css`) resolve lexically against the directory — the key
82
+ * must be the path the bundler's resolver lands on, never a verbatim `..`
83
+ * id no lookup can match.
44
84
  */
45
85
  function styleRequestFile(importerId, specifier) {
46
86
  const clean = cleanImporterId(importerId);
47
- return `${clean.slice(0, clean.lastIndexOf("/"))}/${specifier.replace(/^\.\//, "")}`;
87
+ const segments = clean.slice(0, clean.lastIndexOf("/")).split("/");
88
+ for (const segment of specifier.split("/")) {
89
+ if (segment === "" || segment === ".") continue;
90
+ if (segment === "..") segments.pop();
91
+ else segments.push(segment);
92
+ }
93
+ return segments.join("/");
48
94
  }
49
95
  //#endregion
50
- export { clearStyleRequests, getStyleRequest, hasStyleImporter, registerStyleRequest, styleEdgesForFile, styleRequestFile, styleRequestModuleId };
96
+ export { clearStyleRequests, getStyleRequest, hasStyleImporter, registerStyleRequest, styleEdgesForFile, styleRequestFile, styleRequestModuleId, styleRequestsForImporter };
@@ -0,0 +1,84 @@
1
+ //#region src/internal/compiler/tailwind-candidates.d.ts
2
+ /**
3
+ * Tailwind class-candidate channel (alpha15 S lane).
4
+ *
5
+ * The compiler never interprets Tailwind's class grammar: a compiled module's
6
+ * compile-time `class` values are collected as OPAQUE STRINGS by the lowering
7
+ * (static literals, primitive-literal attributes, both branches of an admitted
8
+ * conditional class, `each` item-template literals) and travel to the build
9
+ * layer through this registry — the same one-writer/one-reader shape as the
10
+ * style-edge registry. The build layer hands the list to the OFFICIAL Tailwind
11
+ * compiler as explicit candidates (`@source inline(…)`), which owns candidate
12
+ * syntax, utility generation, theme compilation and optimization.
13
+ *
14
+ * Root scope is part of the fact, because it decides which sheet an engine can
15
+ * use: a light-root component's classes are covered by the document entry's own
16
+ * pipeline when its DOM is the document's DOM ({@linkcode
17
+ * ClassCandidateRegistry.allLight} — the document-sheet input for an engine
18
+ * that generates its own document CSS), while a shadow-root component's
19
+ * classes can only reach its tree through the component stylesheet.
20
+ *
21
+ * The COMPONENT sheet compiles from every admitted candidate
22
+ * ({@linkcode ClassCandidateRegistry.all}), not from the shadow-root subset:
23
+ * a light-root component's DOM does not always end up in the document. The
24
+ * layout convention puts every page inside the layout's shadow template, and a
25
+ * shadow component may render a light-root child, so a document-level class
26
+ * can be rendered inside a shadow tree where only the component sheet reaches
27
+ * it. The sheet is emitted only when a shadow root exists, so an app without
28
+ * one pays nothing for the wider set.
29
+ *
30
+ * OWNERSHIP (defect C): the registry is an explicit object with a lifetime its
31
+ * owner chooses — one per build, one per dev server — instead of a
32
+ * process-global map. Two builds (or two dev servers) in one process therefore
33
+ * cannot inherit each other's facts, and a caller never has to clear shared
34
+ * state another live build owns. Registering a module's CURRENT facts is
35
+ * authoritative: a module that now carries no class token drops its previous
36
+ * facts, so an emptied or edited module cannot leave a stale candidate behind
37
+ * (and a deleted file is forgotten by registering its absence —
38
+ * {@linkcode ClassCandidateRegistry.remove}).
39
+ *
40
+ * The registry only carries FACTS the compiler could prove statically. A
41
+ * `class={this.<field>}` binding contributes nothing — the compiler cannot
42
+ * know its runtime value, and it must not guess. Completeness for such
43
+ * bindings is the author's explicit declaration through Tailwind's own
44
+ * `@source` mechanisms in the app's CSS entry.
45
+ */
46
+ /**
47
+ * One owner's class-candidate facts: the build (or dev server) that created it
48
+ * is the only writer, its stylesheet emitter the only reader.
49
+ */
50
+ export interface ClassCandidateRegistry {
51
+ /**
52
+ * Register ONE module's statically admitted class candidates. The call is
53
+ * authoritative for that module: an empty list means the module now carries
54
+ * no statically admitted class token, so its previous facts are dropped
55
+ * rather than left to inflate the next stylesheet.
56
+ */
57
+ register(moduleId: string, candidates: readonly string[], options: {
58
+ shadow: boolean;
59
+ }): void;
60
+ /** Forget one module (a deleted file, a module that left the graph). */
61
+ remove(moduleId: string): void;
62
+ /** The candidates one module contributed, or an empty list. */
63
+ forModule(moduleId: string): readonly string[];
64
+ /** Every candidate registered so far, first-seen ordered and deduplicated. */
65
+ all(): readonly string[];
66
+ /**
67
+ * The SHADOW-root candidates only. The COMPONENT sheet no longer compiles
68
+ * from this view (a light-root class can render inside a shadow tree, so the
69
+ * sheet takes {@linkcode ClassCandidateRegistry.all}); it stays as the
70
+ * shadow-only view of the facts, for a consumer that wants exactly that.
71
+ */
72
+ allShadow(): readonly string[];
73
+ /**
74
+ * The LIGHT-root candidates only — the document sheet's input for an engine
75
+ * that generates its own document CSS (a shadow component's classes are
76
+ * delivered by the component stylesheet and never inflate the document one).
77
+ */
78
+ allLight(): readonly string[];
79
+ /** True when any module contributed candidates. */
80
+ has(): boolean;
81
+ }
82
+ /** Create one owner's registry (see the lifetime note above). */
83
+ export declare function createClassCandidateRegistry(): ClassCandidateRegistry;
84
+ //#endregion
@@ -0,0 +1,54 @@
1
+ //#region src/internal/compiler/tailwind-candidates.ts
2
+ /** Create one owner's registry (see the lifetime note above). */
3
+ function createClassCandidateRegistry() {
4
+ const byModule = /* @__PURE__ */ new Map();
5
+ const union = (include) => {
6
+ const seen = /* @__PURE__ */ new Set();
7
+ for (const entry of byModule.values()) {
8
+ if (!include(entry)) continue;
9
+ for (const candidate of entry.tokens) seen.add(candidate);
10
+ }
11
+ return [...seen];
12
+ };
13
+ return {
14
+ register(moduleId, candidates, options) {
15
+ const id = cleanModuleId(moduleId);
16
+ if (candidates.length === 0) {
17
+ byModule.delete(id);
18
+ return;
19
+ }
20
+ byModule.set(id, {
21
+ shadow: options.shadow,
22
+ tokens: [...candidates]
23
+ });
24
+ },
25
+ remove(moduleId) {
26
+ byModule.delete(cleanModuleId(moduleId));
27
+ },
28
+ forModule(moduleId) {
29
+ return byModule.get(cleanModuleId(moduleId))?.tokens ?? [];
30
+ },
31
+ all() {
32
+ return union(() => true);
33
+ },
34
+ allShadow() {
35
+ return union((entry) => entry.shadow);
36
+ },
37
+ allLight() {
38
+ return union((entry) => !entry.shadow);
39
+ },
40
+ has() {
41
+ return byModule.size > 0;
42
+ }
43
+ };
44
+ }
45
+ /**
46
+ * The module-id normalization every lookup shares: query suffixes on the module
47
+ * id (dev-time cache busting) are stripped, so the authored and dev-served
48
+ * spellings answer identically.
49
+ */
50
+ function cleanModuleId(moduleId) {
51
+ return moduleId.split("?", 1)[0];
52
+ }
53
+ //#endregion
54
+ export { createClassCandidateRegistry };
@@ -1,39 +0,0 @@
1
- import { SemanticCoreOptions } from "./module-analysis.js";
2
- import { CompiledElementSourceMap } from "./source-map.js";
3
- import { CompiledElementError, ElementCompilerDiagnostic } from "./compiler-diagnostics.js";
4
- import { PartProgramV1 } from "@openelement/protocol/part-program";
5
- //#region src/internal/compiler/semantic-core/compile.d.ts
6
- /**
7
- * The compiled module the transform emits for one authored TSX source: the
8
- * generated `code`, its Source Map v3, the Part Program facts the runtime
9
- * consumes, and the ordered diagnostics that did not fail the compile.
10
- */
11
- export interface CompileElementResult {
12
- code: string;
13
- /**
14
- * Real Source Map v3 for the emitted module (VLQ line+column segments
15
- * derived from the compiler's span records and emission provenance). The
16
- * same map is embedded as the module's inline map; the Vite shell returns it
17
- * as its `map` output for downstream composition (#1210).
18
- */
19
- map: CompiledElementSourceMap;
20
- program: PartProgramV1;
21
- /**
22
- * The module's style edges (#1558): the authored `.css` specifiers the
23
- * class's `static styles` arrays, in authored order. The host build's
24
- * style-asset plugin intercepts these edges — a build with no intercepting
25
- * plugin fails at import resolution, never by silently inlining.
26
- */
27
- styleRequests?: readonly string[];
28
- }
29
- /**
30
- * Compile one authored TSX module into the compiled Part Program module.
31
- * `fileName` is used for diagnostics and the emitted source map only —
32
- * filesystem resolution is the caller's job. Fails closed with a
33
- * {@link CompiledElementError} carrying the ordered OEC diagnostics.
34
- * `options.staticSidecars` admits host-declared sidecar policy statements
35
- * (e.g. an island delivery descriptor); the default core admits none.
36
- */
37
- export declare function compileElementProgram(source: string, fileName: string, options?: SemanticCoreOptions): CompileElementResult;
38
- //#endregion
39
- export { CompiledElementError, type ElementCompilerDiagnostic };
@@ -1,98 +0,0 @@
1
- import { createModuleIntrinsicBindings } from "./module-analysis.js";
2
- import { analyzeCompiledModule } from "./analyze-module.js";
3
- import { CompiledElementError, failAt, parseDiagnostic } from "./compiler-diagnostics.js";
4
- import { buildPartProgram } from "./emit-program.js";
5
- import { Lowering } from "./lower-program.js";
6
- import { emitCompiledModule } from "./emit-module.js";
7
- import { typescriptParser } from "./parse-module.js";
8
- import ts from "typescript";
9
- //#region src/internal/compiler/semantic-core/compile.ts
10
- /**
11
- * @openelement/element — TSX-to-Part Program v1 compiler (#1161–#1163).
12
- *
13
- * The compiler accepts one deliberately bounded authoring grammar. It lowers
14
- * every accepted dynamic expression to a typed fixed Part or Region with a
15
- * stable compiler-owned location. Anything outside that grammar produces a
16
- * source-located diagnostic; no runtime discovery or fallback renderer is
17
- * emitted.
18
- *
19
- * Single-duty pipeline modules behind this facade (#1473): parse-module.ts
20
- * (the one TypeScript parse seam), analyze-module.ts (module/class shape),
21
- * lower-program.ts (JSX → Program tree), emit-program.ts (Part Program
22
- * assembly + validation), emit-module.ts (generated module text + Source Map
23
- * v3), compiler-diagnostics.ts (error dialect + source locations). This file
24
- * stays the internal entry: it wires the stages in order and re-exports the
25
- * boundary types the Vite plugin and the compiler tests import.
26
- */
27
- /**
28
- * The authored module's canonical `computed` local binding (aliases
29
- * followed), or null when the module never binds it. Synthesized
30
- * conditional-class factories (#1556) reference the authored binding when it
31
- * exists; otherwise the emitter adds a module-local import under the
32
- * synthesized name. Same resolution discipline as the ReadonlySignal scan in
33
- * the emitter: the canonical specifier is fixed because computed factories
34
- * already require it.
35
- */
36
- function canonicalComputedLocal(sf, intrinsics) {
37
- for (const statement of sf.statements) {
38
- if (!ts.isImportDeclaration(statement)) continue;
39
- if (!ts.isStringLiteral(statement.moduleSpecifier) || statement.moduleSpecifier.text !== "@openelement/element") continue;
40
- const bindings = statement.importClause?.namedBindings;
41
- if (!bindings || !ts.isNamedImports(bindings)) continue;
42
- for (const element of bindings.elements) if (intrinsics.isRuntimeNamedImport(element.name.text, "@openelement/element", "computed")) return element.name.text;
43
- }
44
- return null;
45
- }
46
- /**
47
- * Compile one authored TSX module into the compiled Part Program module.
48
- * `fileName` is used for diagnostics and the emitted source map only —
49
- * filesystem resolution is the caller's job. Fails closed with a
50
- * {@link CompiledElementError} carrying the ordered OEC diagnostics.
51
- * `options.staticSidecars` admits host-declared sidecar policy statements
52
- * (e.g. an island delivery descriptor); the default core admits none.
53
- */
54
- function compileElementProgram(source, fileName, options = {}) {
55
- const parsed = typescriptParser.parseModule(source, fileName);
56
- const sf = parsed.sourceFile;
57
- const syntaxDiagnostics = parsed.syntaxDiagnostics;
58
- if (syntaxDiagnostics.length > 0) throw new CompiledElementError([parseDiagnostic(sf, syntaxDiagnostics[0])]);
59
- const fail = failAt(sf);
60
- const intrinsics = createModuleIntrinsicBindings(sf, options);
61
- const analyzed = analyzeCompiledModule(sf, intrinsics, fail);
62
- const methodNames = analyzed.methods.map((method) => method.name.text);
63
- const lowering = new Lowering(sf, analyzed.fields, methodNames, { computedLocal: canonicalComputedLocal(sf, intrinsics) });
64
- const renderStatements = analyzed.render.body?.statements ?? [];
65
- if (renderStatements.length !== 1 || !ts.isReturnStatement(renderStatements[0]) || !renderStatements[0].expression) fail(analyzed.render, "OEC9007", "render() must be a single return of one JSX element");
66
- const returned = renderStatements[0];
67
- const root = lowering.lowerRoot(returned.expression);
68
- if (root.k !== "el") fail(analyzed.render, "OEC9007", "render() must return one intrinsic JSX element");
69
- analyzed.fields.push(...lowering.synthesizedFields);
70
- const { program, programJson, propertiesJson, metadataJson, observedJson } = buildPartProgram({
71
- sf,
72
- fileName,
73
- analyzed,
74
- root,
75
- lowering
76
- });
77
- const { code, map } = emitCompiledModule({
78
- sf,
79
- source,
80
- fileName,
81
- analyzed,
82
- lowering,
83
- program,
84
- programJson,
85
- propertiesJson,
86
- metadataJson,
87
- observedJson
88
- });
89
- const { styleRequests } = analyzed;
90
- return {
91
- code,
92
- map,
93
- program,
94
- ...styleRequests.length === 0 ? {} : { styleRequests }
95
- };
96
- }
97
- //#endregion
98
- export { CompiledElementError, compileElementProgram };