From a0ed649c8a4d94b232423b73a1605183166ba7f7 Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Wed, 8 Feb 2023 09:46:31 +0000 Subject: [PATCH] Made class that represents a TypeScript interface --- src/utils/typescript/typescript-interface.ts | 69 ++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/utils/typescript/typescript-interface.ts diff --git a/src/utils/typescript/typescript-interface.ts b/src/utils/typescript/typescript-interface.ts new file mode 100644 index 0000000..9b1085c --- /dev/null +++ b/src/utils/typescript/typescript-interface.ts @@ -0,0 +1,69 @@ +import { AbstractTypeScriptNode } from "./abstract-typescript-node"; +import { getIndentation, TypeScriptFormattingOptions } from "./typescript-formatting-options"; +import { TypeScriptObject } from "./typescript-object"; + +/** + * Represents an interface in a TypeScript module. + */ +export class TypeScriptInterface extends AbstractTypeScriptNode { + /** + * The name of the interface. + */ + private readonly _name: string; + + /** + * The object definition of the interface. + */ + private readonly _definition: TypeScriptObject; + + /** + * Constructs a new {@link TypeScriptInterface} instance. + * + * @param name - The name of the interface. + * @param definition - The object definition of the interface. + */ + private constructor(name: string, definition: TypeScriptObject) { + super(); + this._name = name; + this._definition = definition; + } + + /** + * Creates a new {@link TypeScriptInterface} instance. + * + * @param name - The name of the interface. + * @param definition - The object definition of the interface. + * + * @returns A new {@link TypeScriptInterface} instance. + */ + static create(name: string, definition?: TypeScriptObject): TypeScriptInterface { + return new TypeScriptInterface(name, definition || TypeScriptObject.create()); + } + + /** + * Gets the name of the interface. + */ + get name(): string { + return this._name; + } + + /** + * Gets the object definition of the interface. + */ + get definition(): TypeScriptObject { + return this._definition; + } + + /** + * @inheritdoc + */ + formatContent(options?: TypeScriptFormattingOptions): string { + const indent = getIndentation(options); + + const formattedName = this._name; + const formattedDefinition = this._definition.format(options).trimStart(); + const formattedInterface = `${indent}interface ${formattedName} ${formattedDefinition}`; + + return formattedInterface; + } +}