|
| 1 | +// FR-019 — shared + externally-provided enum resolution. |
| 2 | +// |
| 3 | +// A reusable enum is a ROOT/package-level abstract `field.enum` (a sibling of |
| 4 | +// object.entity) that concrete entity fields `extends`. Per ADR-0026 such a |
| 5 | +// declaration is materialized ONCE per port and referenced, instead of being |
| 6 | +// redeclared inline in every consuming entity file. |
| 7 | +// |
| 8 | +// Two cases on the abstract declaration: |
| 9 | +// • non-@provided → metaobjects MATERIALIZES the type once (a shared module), |
| 10 | +// and consuming fields reference it. |
| 11 | +// • @provided: true → metaobjects emits NOTHING for the type; consuming fields |
| 12 | +// reference an existing hand-written/third-party type, resolved via per-port |
| 13 | +// codegen config (the namespace/module never lives in metadata — ADR-0001). |
| 14 | +// |
| 15 | +// The common inline case (a concrete `field.enum` with @values declared directly, |
| 16 | +// no root-extends) is UNCHANGED — it stays a per-entity `<Entity><Field>` union. |
| 17 | + |
| 18 | +import type { MetaField, MetaRoot } from "@metaobjectsdev/metadata"; |
| 19 | +import { |
| 20 | + FIELD_SUBTYPE_ENUM, |
| 21 | + FIELD_ATTR_PROVIDED, |
| 22 | + TYPE_METADATA, |
| 23 | +} from "@metaobjectsdev/metadata"; |
| 24 | +import { toPascalCase } from "./naming.js"; |
| 25 | +import { enumValues } from "./enum-meta.js"; |
| 26 | + |
| 27 | +/** A shared (root-level abstract) enum declaration the codegen must reason about. */ |
| 28 | +export interface SharedEnum { |
| 29 | + /** The materialized type name — the abstract declaration's PascalCase name (cross-port identity). */ |
| 30 | + readonly name: string; |
| 31 | + /** Member symbols (the @values SSOT). */ |
| 32 | + readonly values: string[]; |
| 33 | + /** True when the declaration carries `@provided: true` (reference, don't materialize). */ |
| 34 | + readonly provided: boolean; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * The root-level abstract `field.enum` a concrete enum field resolves to via |
| 39 | + * `extends`, or undefined when the field is an inline enum (no root-abstract |
| 40 | + * super). A super qualifies only when it is (a) an abstract field, AND (b) a |
| 41 | + * direct child of the metadata root (a package-level declaration) — NOT an |
| 42 | + * abstract field nested inside another object. |
| 43 | + */ |
| 44 | +export function resolveSharedEnumDecl(field: MetaField): MetaField | undefined { |
| 45 | + if (field.subType !== FIELD_SUBTYPE_ENUM) return undefined; |
| 46 | + const sup = field.resolveSuper(); |
| 47 | + if (sup === undefined) return undefined; |
| 48 | + if (!sup.isAbstract) return undefined; |
| 49 | + // The declaration must sit at the package/root level (sibling of object.entity), |
| 50 | + // i.e. its parent node is the metadata root. |
| 51 | + const parent = sup.parent; |
| 52 | + if (parent === undefined || parent.type !== TYPE_METADATA) return undefined; |
| 53 | + return sup; |
| 54 | +} |
| 55 | + |
| 56 | +/** The shared-enum descriptor a field resolves to, or undefined for inline enums. */ |
| 57 | +export function sharedEnumForField(field: MetaField): SharedEnum | undefined { |
| 58 | + const decl = resolveSharedEnumDecl(field); |
| 59 | + if (decl === undefined) return undefined; |
| 60 | + const values = enumValues(field); |
| 61 | + if (values === undefined) return undefined; |
| 62 | + return { |
| 63 | + name: toPascalCase(decl.name), |
| 64 | + values, |
| 65 | + provided: decl.ownAttr(FIELD_ATTR_PROVIDED) === true, |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * All shared (root-level abstract) enum declarations in the loaded root that are |
| 71 | + * actually CONSUMED by at least one concrete entity field — keyed by the |
| 72 | + * materialized type name. A declaration nobody extends is not materialized (no |
| 73 | + * dangling type). Deterministic order: first-consumption order across entities. |
| 74 | + */ |
| 75 | +export function collectSharedEnums(root: MetaRoot): Map<string, SharedEnum> { |
| 76 | + const out = new Map<string, SharedEnum>(); |
| 77 | + for (const entity of root.objects()) { |
| 78 | + for (const field of entity.fields()) { |
| 79 | + const shared = sharedEnumForField(field); |
| 80 | + if (shared === undefined) continue; |
| 81 | + if (!out.has(shared.name)) out.set(shared.name, shared); |
| 82 | + } |
| 83 | + } |
| 84 | + return out; |
| 85 | +} |
| 86 | + |
| 87 | +/** The shared enums that metaobjects MATERIALIZES (non-@provided, consumed). */ |
| 88 | +export function materializedSharedEnums(root: MetaRoot): SharedEnum[] { |
| 89 | + return [...collectSharedEnums(root).values()].filter((e) => !e.provided); |
| 90 | +} |
| 91 | + |
| 92 | +/** Whether any shared enum (materialized or provided) is consumed in the model. */ |
| 93 | +export function hasSharedEnums(root: MetaRoot): boolean { |
| 94 | + return collectSharedEnums(root).size > 0; |
| 95 | +} |
0 commit comments