import { ASTAliasedTypes, ASTEnum, ASTIntersection, ASTNode, ASTNodes, ASTObject, ASTUnion, TExtendedTypeKind, Zod2X } from "../../core"; import { IZod2DartOpt } from "./options"; /** * Transpiler for Zod schemas to Dart classes using package:json_annotation. * * The `.g.dart` part file is NOT generated by this transpiler — it must be produced * externally via `dart run build_runner build`. */ export declare class Zod2Dart extends Zod2X { protected readonly commentKey = "//"; protected lib: { jsonAnnotation: string; }; /** * Maps a local struct/class name to the discriminated union names it participates in. * Populated by a pre-scan of the AST before the normal transpile pass. */ private _unionMembers; /** * Names of locally-defined discriminated union abstract classes. * Used to emit @JsonKey(fromJson/toJson) when such a type appears as a field. */ private _discriminatedUnionTypes; /** * Names of typedef-emitted types (alias, union typedef, struct extends). * When these appear as field types in @JsonSerializable classes, we emit * @JsonKey(fromJson: TypeName.fromJson) to prevent json_serializable from * following the typedef chain and referencing transitive imports. */ private _typedefNames; /** * Maps a disc union typedef name (e.g. "NewDiscriminatedUnionItem") to the * fromJson function name of the underlying entity-level union * (e.g. "discriminatedUnionItemFromJson"). * Populated during pre-scan when an ASTUnion node has aliasOf set. */ private _discriminatedUnionDelegate; /** * Types used for the templates of typedefs of generic types (local or imported), by typedef * name. E.g. "AdminUserEntity" → [{ aliasOf: "AdminUserMetadata", kind: "ASTObject" }]. */ private _typedefTemplates; /** * Types used for the templates of fields of generic types of other files, by helper key. */ private _usedCustomFromJsonTemplates; /** * Unified set of type names needing custom @JsonKey annotations (discriminated unions use * their own FromJson and ToJson functions). * - Typedef types: fromJson = _FromJson (standalone wrapper) * - TypedefList: list fromJson/toJson helpers * _toJson helpers are emitted in runAfter() for all entries. * Keyed by helper (`` or `[]` for list helpers), so a type used both * directly and in a list gets both helpers. */ private _usedCustomFromJsonTypes; /** * Maps a type name (e.g. "GenericUserEntity") to its full Dart type with * template args (e.g. "GenericUserEntity"). Used in runAfter() * to generate helpers with correct generic return types and factory calls. */ private _usedCustomFromJsonFullTypes; /** * Maps a typedef name to its full extended type (the right-hand side of the * typedef). E.g. "AdminUserEntity" → "GenericUserEntity". * Populated in addExtendedType() when the extended type has generic args. * Used in runAfter() to generate 2-arg fromJson calls for generic classes. */ private _typedefToExtendedType; constructor(opt?: IZod2DartOpt); /** * Dart libraries can import each other. */ supportsCircularImports(): boolean; /** * Overrides transpile() to pre-scan discriminated union memberships before the normal pass. * This allows transpileStruct() to emit `implements UnionName` on member classes. */ transpile(transpilerQueue: ASTNodes): string; /** * Dart imports are not prefixed, so a local type cannot share its name with an imported one. */ private _checkImportedNamesClash; protected runBefore(): void; protected runAfter(): void; protected addImportFromFile(filename: string, _namespace: string): string; protected getTypeFromExternalNamespace(_namespace: string, typeName: string): string; /** * For Layered Modeling. Language options used to declare a type that extends another one. */ protected getExtendedTypeOptions(data: ASTNode, type?: TExtendedTypeKind): { type: "union" | "alias" | undefined; templates: string | undefined; declaredTemplates: string | undefined; }; protected addExtendedType(name: string, parentNamespace: string, aliasOf: string, opt?: { type?: "union" | "alias"; isInternal?: boolean; templates?: string; declaredTemplates?: string; }): void; protected getStringType: () => string; protected getBooleanType: () => string; protected getAnyType: () => string; protected getDateType: () => string; protected getNumberType(isInt: boolean, _range: { min?: number; max?: number; }): string; /** * Returns the Dart type for a literal value. * Dart has no structural literal types, so we return the underlying primitive type. * When a parent enum is provided, returns the enum type name (not a member expression), * since Dart enum members cannot be used as types. */ protected getLiteralStringType(value: string | number | boolean, parentEnumNameKey?: [string, string]): string | number; /** Ex: List> for 2D array */ protected getArrayType(arrayType: string, arrayDeep: number): string; /** Ex: Set */ protected getSetType: (itemType: string) => string; /** Ex: Map */ protected getMapType: (keyType: string, valueType: string) => string; /** Ex: Map (record key is always String) */ protected getRecordType: (keyType: string, valueType: string) => string; /** Ex: (T1, T2, T3) — Dart 3 positional record type */ protected getTupleType: (itemsType: string[]) => string; /** Dart has no native union type; use Object */ protected getUnionType: (_itemsType: string[]) => string; /** Dart has no native intersection type; use Object */ protected getIntersectionType: (_itemsType: string[]) => string; protected transpileAliasedType(data: ASTAliasedTypes): void; /** * Emits a Dart enhanced enum with a typed `value` field and `@JsonEnum` annotation. * * All-string values: * @JsonEnum(valueField: 'value') * enum EnumItem { * enum1('Enum1'), * enum2('Enum2'); * * const EnumItem(this.value); * final String value; * } * * Mixed (int + string): typedef alias with comment. */ protected transpileEnum(data: ASTEnum): void; protected transpileStruct(data: ASTObject): void; /** * Dart intersection: * - If the AST produced a merged `newObject`, emit it as a flat class. * - Otherwise emit a typedef alias to Object with a comment. */ protected transpileIntersection(data: ASTIntersection): void; /** * Dart union: * - Non-discriminated: typedef alias to Object with a comment listing member types. * - Discriminated: abstract base class + factory unmarshal function using Dart 3 switch, * and a function that serializes it. * * Classes of other files cannot implement the union, so these variants are wrapped in * local classes that do (`class AnyEventCreated implements AnyEvent { final Created value; }`). */ protected transpileUnion(data: ASTUnion): void; /** * Returns the functions that deserialize and serialize the type used for a template of a * generic type (the `fromJsonT` and `toJsonT` arguments of json_serializable). */ private _templateJson; /** Ex: Message.fromJson(json, (j) => ..., (j) => ...) */ private _genericFromJson; /** Ex: value.toJson((v) => ..., (v) => ...) */ private _genericToJson; /** * Returns the types used for the templates of a type that is an instance of a generic type * (directly or through a typedef), if any. */ private _getInstanceTemplates; /** Renders the full body of a Dart class for an ASTObject. */ private _transpileStructBody; /** Renders a single class field declaration with optional `@JsonKey` annotation. */ private _transpileMember; /** * Emits `fromJson` factory and `toJson` method delegation stubs. * For generic classes, extra `fromJsonT` / `toJsonT` function parameters are added * as required by `json_serializable` with `genericArgumentFactories: true`. */ private _emitFromToJson; }