From 456ccf5ef05a8e1ac063124179e745eb934086e6 Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Tue, 31 Jan 2023 13:30:12 +0000 Subject: [PATCH] Made an interface for commentable TypeScript nodes --- .../typescript/commentable-typescript-node.ts | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/utils/typescript/commentable-typescript-node.ts diff --git a/src/utils/typescript/commentable-typescript-node.ts b/src/utils/typescript/commentable-typescript-node.ts new file mode 100644 index 0000000..df6fe6c --- /dev/null +++ b/src/utils/typescript/commentable-typescript-node.ts @@ -0,0 +1,60 @@ +import { TypeScriptComment } from "./typescript-comment"; +import { TypeScriptFormattingOptions } from "./typescript-formatting-options"; +import { TypeScriptNode } from "./typescript-node"; + +/** + * Represents a TypeScript node that can contain comments. + */ +export interface CommentableTypeScriptNode extends TypeScriptNode { + /** + * Returns an iterable of comments associated with this node. + */ + comments(): Iterable; + + /** + * Adds a comment to this node. + * + * @param comment - The comment to add to this node. + * + * @returns The comment that was added. + */ + addComment(comment: TypeScriptComment): TypeScriptComment; + + /** + * Deletes a previously added comment from this node. + * + * @param comment - The comment to delete from this node. + * + * @returns `true` if the comment was deleted successfully; otherwise, `false`. + */ + deleteComment(comment: TypeScriptComment): boolean; + + /** + * Formats the comments associated with this node as a string according to the specified formatting options. + * + * @param options - Formatting options that determine how the output should be formatted. + * + * @returns A string representation of the comments associated with this node. + */ + formatComments(options?: TypeScriptFormattingOptions): string; + + /** + * Formats the content of this node as a string according to the specified formatting options. + * + * @param options - Formatting options that determine how the output should be formatted. + * + * @returns A string representation of the content of this node. + */ + formatContent(options?: TypeScriptFormattingOptions): string; +} + +/** + * Determines whether the given node is a {@link CommentableTypeScriptNode}. + * + * @param node - The node to check. + * + * @returns `true` if the given node is a {@link CommentableTypeScriptNode}; otherwise, `false`. + */ +export function isCommentableTypeScriptNode(node: TypeScriptNode): node is CommentableTypeScriptNode { + return typeof (node as CommentableTypeScriptNode).comments === "function"; +}