import { nodeKinds } from '../constants.ts' import { toDeprecatedExport, toDeprecatedImport } from '../utils/moduleNames.ts' import type { ExportInput, ExportNode, ImportInput, ImportNode, ModuleName } from './file.ts' type Override = { value: TValue | null | undefined; derived: TValue | null | undefined } function overrideSpace(names: Array, { value, derived }: Override): Array { if (value == null || value === derived) return names const space = value ? 'type' : 'value' return names.map((item) => ({ ...item, space })) } const isOverride = ({ value, derived }: Override): boolean => value !== undefined && JSON.stringify(value) !== JSON.stringify(derived) // Object spread copies symbol keys, so `{ ...imp, names }` carries the fields `imp` derived and they can be told apart from an override. const DERIVED = Symbol.for('kubb.ast.derived') // `name` is stored as JSON, so changing the node's `name` array in place still counts as an override. type Derived = { name: string | undefined; isTypeOnly?: boolean | null } type WithDerived = { [DERIVED]?: Derived } function ownFields(input: { name?: TName; isTypeOnly?: boolean | null } & WithDerived): { name?: TName; isTypeOnly?: boolean | null } { const snapshot = input[DERIVED] if (!snapshot) return { name: input.name, isTypeOnly: input.isTypeOnly } return { name: JSON.stringify(input.name) === snapshot.name ? undefined : input.name, isTypeOnly: input.isTypeOnly === snapshot.isTypeOnly ? undefined : input.isTypeOnly, } } function withoutDerived(input: T): T { const { [DERIVED]: _, ...rest } = input as T & WithDerived return rest as T } /** * Creates an `ImportNode` and fills the deprecated `name` and `isTypeOnly` from `names`. A deprecated field * that differs from the one `names` gives overrides it. * * A spread keeps the record of the derived fields only when the original node has one, which a node made * from `names` does. For a node made without `names`, or a JSON or `structuredClone` copy, leave out `name` * and `isTypeOnly` before you pass new `names`, or the old `name` replaces the new `names` and the old * `isTypeOnly` sets their space. */ export function createImport(input: ImportInput): ImportNode { if (!input.names) return { ...withoutDerived(input), name: input.name ?? [], kind: nodeKinds.import } const own = ownFields(input) const derived = toDeprecatedImport(input.names) if (isOverride({ value: own.name, derived: derived.name })) return { ...withoutDerived(input), names: null, name: own.name ?? [], kind: nodeKinds.import } const names = overrideSpace(input.names, { value: own.isTypeOnly, derived: derived.isTypeOnly }) const fields = toDeprecatedImport(names) const node: ImportNode & WithDerived = { ...input, ...fields, names, [DERIVED]: { name: JSON.stringify(fields.name), isTypeOnly: fields.isTypeOnly }, kind: nodeKinds.import, } return node } /** * Creates an `ExportNode`, with the deprecated fields of {@link createImport}. For a node made without * `names`, or a JSON or `structuredClone` copy, leave out `name` and `isTypeOnly` before you pass new `names`. */ export function createExport(input: ExportInput): ExportNode { if (!input.names) return { ...withoutDerived(input), kind: nodeKinds.export } const own = ownFields(input) const derived = toDeprecatedExport(input.names) if (isOverride({ value: own.name, derived: derived.name })) return { ...withoutDerived(input), name: own.name, names: null, kind: nodeKinds.export } const names = overrideSpace(input.names, { value: own.isTypeOnly, derived: derived.isTypeOnly }) const fields = toDeprecatedExport(names) const node: ExportNode & WithDerived = { ...input, ...fields, name: fields.name, names, [DERIVED]: { name: JSON.stringify(fields.name), isTypeOnly: fields.isTypeOnly }, kind: nodeKinds.export, } return node }