mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-22 08:20:58 -05:00
Made class that represents a TypeScript interface
This commit is contained in:
parent
5b17642f5b
commit
a0ed649c8a
1 changed files with 69 additions and 0 deletions
69
src/utils/typescript/typescript-interface.ts
Normal file
69
src/utils/typescript/typescript-interface.ts
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue