From 755c51fa6107d8ece646f68ac21350906be69df4 Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Sat, 4 Feb 2023 12:03:22 +0000 Subject: [PATCH] Implemented class that represents type alias --- src/utils/typescript/typescript-type-alias.ts | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/utils/typescript/typescript-type-alias.ts diff --git a/src/utils/typescript/typescript-type-alias.ts b/src/utils/typescript/typescript-type-alias.ts new file mode 100644 index 0000000..d42a1ed --- /dev/null +++ b/src/utils/typescript/typescript-type-alias.ts @@ -0,0 +1,104 @@ +import { AbstractTypeScriptNode } from "./abstract-typescript-node"; +import { getIndentation, TypeScriptFormattingOptions } from "./typescript-formatting-options"; +import { TypeScriptTypeDefinition } from "./typescript-type-definition"; + +/** + * Represents a type alias in a TypeScript module. + */ +export class TypeScriptTypeAlias extends AbstractTypeScriptNode implements TypeScriptTypeDefinition { + /** + * The name of the type alias. + */ + private readonly _name: string; + + /** + * The type definition of the type alias. + */ + private readonly _type: TypeScriptTypeDefinition; + + /** + * Constructs a new {@link TypeScriptTypeAlias} instance. + * + * @param name - The name of the type alias. + * @param type - The type definition of the type alias. + */ + private constructor(name: string, type: TypeScriptTypeDefinition) { + super(); + this._name = name; + this._type = type; + } + + /** + * Creates a new {@link TypeScriptTypeAlias} instance. + * + * @param name - The name of the type alias. + * @param type - The type definition of the type alias. + * + * @returns A new {@link TypeScriptTypeAlias} instance. + */ + static create(name: string, type: TypeScriptTypeDefinition): TypeScriptTypeAlias { + return new TypeScriptTypeAlias(name, type); + } + + /** + * Gets the name of the type alias. + */ + get name(): string { + return this._name; + } + + /** + * Gets the type definition of the type alias. + */ + get type(): TypeScriptTypeDefinition { + return this._type; + } + + /** + * @inheritdoc + */ + get isComposite(): true { + return true; + } + + /** + * @inheritdoc + */ + get isUnion(): false { + return false; + } + + /** + * @inheritdoc + */ + get isIntersection(): boolean { + return false; + } + + /** + * @inheritdoc + */ + get isAlias(): true { + return true; + } + + /** + * @inheritdoc + */ + composingTypes(): Iterable { + return [this._type]; + } + + /** + * @inheritdoc + */ + formatContent(options?: TypeScriptFormattingOptions): string { + const indent = getIndentation(options); + + const formattedName = this._name; + const formattedType = this._type.format(options).trimStart(); + const formattedTypeAlias = `${indent}type ${formattedName} = ${formattedType};`; + + return formattedTypeAlias; + } +}