import type * as SchemaRepresentation from "./SchemaRepresentation.ts"; /** * A reference to a declaration or import whose identity is independent of its printed name. * * **Gotchas** * * Bindings throw on string coercion. Embed them with `code` and render the containing module. * * @stability experimental * @category models * @since 4.1.5 */ export interface Binding { readonly _tag: "Binding"; readonly preferredName: string; readonly toString: () => never; } /** * A declaration binding with a name allocated in its owning module. * * @stability experimental * @category models * @since 4.1.5 */ export interface DeclarationBinding extends Binding { readonly name: string; } /** * Source text that retains declaration and import references until rendering. * * @stability experimental * @category models * @since 4.1.5 */ export type Fragment = string | Binding | { readonly _tag: "Parts"; readonly parts: ReadonlyArray; readonly toString: () => never; } | { readonly _tag: "Deferred"; readonly render: (render: Render) => string; readonly toString: () => never; }; /** * A function that resolves a fragment using the current module names and imports. * * @stability experimental * @category models * @since 4.1.5 */ export type Render = (fragment: Fragment) => string; /** * A fragment, primitive, or nested collection accepted by the code template helpers. * * @stability experimental * @category models * @since 4.1.5 */ export type CodeHole = Fragment | number | ReadonlyArray | undefined | null | false; /** * A structured import request with its preferred local name and optional import attributes. * * @stability experimental * @category models * @since 4.1.5 */ export interface Import { readonly module: string; readonly kind: "Namespace" | "Default" | "Named"; readonly imported: string; readonly local: string; readonly typeOnly: boolean; readonly attributes: string; } /** * Creates a namespace import binding that can be shared across generated modules. * * @stability experimental * @category constructors * @since 4.1.5 */ export declare function importNamespace(module: string, local: string, options?: { readonly typeOnly?: boolean | undefined; }): Binding; /** * Creates a named import binding that can be shared across generated modules. * * @stability experimental * @category constructors * @since 4.1.5 */ export declare function importNamed(module: string, name: string, options?: { readonly typeOnly?: boolean | undefined; }): Binding; /** * Creates a default import binding that can be shared across generated modules. * * @stability experimental * @category constructors * @since 4.1.5 */ export declare function importDefault(module: string, local: string, options?: { readonly typeOnly?: boolean | undefined; }): Binding; /** * Combines source text and bindings without resolving their names. * * @stability experimental * @category constructors * @since 4.1.5 */ export declare const code: (strings: TemplateStringsArray, ...values: ReadonlyArray) => Fragment; /** * Joins code fragments with a source-text separator. * * @stability experimental * @category combining * @since 4.1.5 */ export declare const join: (values: ReadonlyArray, separator: string) => Fragment; /** * Defers source construction until its bindings can be rendered. * * @stability experimental * @category constructors * @since 4.1.5 */ export declare const deferred: (render: (render: Render) => string) => Fragment; /** * Indents nonempty lines after the first line of a fragment by two spaces. * * @stability experimental * @category transforming * @since 4.1.5 */ export declare function indent(content: CodeHole): Fragment; /** * Formats a primitive as a JavaScript literal, qualifying non-finite numbers with globalThis. * * @stability experimental * @category formatting * @since 4.1.5 */ export declare const literal: (value: string | number | bigint | boolean | null | undefined) => string; /** * Formats an object property key, using a computed key for __proto__. * * @stability experimental * @category formatting * @since 4.1.5 */ export declare function propertyKey(name: string): string; /** * Formats text as a JSDoc comment and escapes embedded comment terminators. * * @stability experimental * @category formatting * @since 4.1.5 */ export declare function docComment(text: string | ReadonlyArray): string; /** * A declaration scope belonging to one generated module. * * @stability experimental * @category models * @since 4.1.5 */ export interface Scope { readonly _tag: "Scope"; } /** * A mutable builder for a TypeScript module, with shared names, imports, and declaration ordering. * * **Details** * * Mutation methods return this builder for chaining. Allocation methods return bindings for use in code fragments. * Printing produces a snapshot; the builder remains open for further declarations. * * @stability experimental * @category models * @since 4.1.5 */ export declare class Module { #private; /** * Creates an empty builder with optional names reserved for its surrounding code. */ constructor(reserved?: ReadonlyArray); /** * Creates a nested declaration scope in a module. */ scope(parent?: Scope): Scope; /** * Allocates a declaration binding, sanitizing and disambiguating its preferred name. * * **Details** * * Exact names throw when invalid or already reserved. Names also avoid enclosing and nested scopes. */ declare(preferredName: string, options?: { readonly exact?: boolean | undefined; readonly scope?: Scope | undefined; }): DeclarationBinding; /** * Registers a binding's declaration fragments and eager initialization dependencies. */ define(binding: Binding, fragments: ReadonlyArray, dependencies?: ReadonlyArray): this; /** * Adds named schema representations to a module and returns their declaration bindings by requested name. * * **When to use** * * Use when schemas share a generated module with other imports and declarations. * * **Details** * * Registers exported type aliases and runtime schemas, their referenced definitions, and auxiliary declarations. * Supply the shared reference table through `references`. Set `typeOnly` to emit types and ambient auxiliaries. * Returned keys preserve the requested names even when declaration names must change to avoid collisions. * A root that references a definition of the same name reuses that definition's binding. * The module remains open for further declarations; imports and initialization order are resolved when printing. * * **Gotchas** * * Recursive representations must contain explicit suspensions. Custom declarations and checks require supported * `toCode` callbacks. Each callback is called once and its exceptions pass through unchanged. * Callback output supports imports, qualified names, generic and structural types, API calls, * literal/object/array arguments, and zero-argument arrows. Unsupported syntax throws with its document path. * When another declaration eagerly uses a returned binding, include it in that declaration's dependencies. * * **Example** (Sharing names with generated schema declarations) * * ```ts import.meta.vitest * import { Codegen, Schema, SchemaRepresentation } from "effect" * * const module = new Codegen.Module() * const document = SchemaRepresentation.toRepresentation(Schema.String.ast) * const schemas = module.addSchemas({ User: document.representation }, { * references: document.references * }) * const user = schemas.get("User")! * const alias = module.declare("User") * const source = module * .define(alias, [Codegen.code`export const ${alias} = ${user}`], [user]) * .print() * * source.includes("export const User1 = User") // => true * ``` * * @see {@link Module.define} for adding surrounding declarations */ addSchemas(schemas: Readonly>, options?: { readonly references?: SchemaRepresentation.References | undefined; readonly typeOnly?: boolean | undefined; }): ReadonlyMap; /** * Registers a structured import request and returns its binding. */ addImport(input: Import): Binding; /** * Registers a fixed import binding supplied by surrounding code without emitting its import declaration. */ provideImport(input: Import): Binding; /** * Registers a side-effect import, preserving its first registration order. */ addSideEffect(module: string, attributes?: string): this; /** * Appends a source fragment after the module's declarations. */ add(fragment: Fragment): this; /** * Renders a snapshot of the module with declarations in dependency order and roots in input order. * * **Gotchas** * * Missing definitions and eager dependency cycles throw. Adding declarations later can change import * aliases, so render the complete module again after further changes. */ finalize(roots?: ReadonlyArray): { readonly names: ReadonlyMap; readonly declarations: ReadonlyArray<{ readonly binding: Binding; readonly fragments: ReadonlyArray; }>; readonly roots: ReadonlyArray; readonly imports: ReadonlyArray; readonly body: ReadonlyArray; }; /** * Prints a module's imports, ordered declarations, and appended source as TypeScript. */ print(): string; } //# sourceMappingURL=Codegen.d.ts.map