All files / transpiler/output/codegen CommentFormatter.ts

100% Statements 52/52
96.77% Branches 30/31
100% Functions 6/6
100% Lines 49/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156                                    181x   173x     5x     3x                     10x   9x 4x       5x 5x   5x 12x 12x 11x   1x       5x 5x                     68x   67x 67x   67x 179x 7x     172x 1x 1x   172x         67x 4x     67x                 4x     2x       2x 1x     1x               5x     5x 3x       2x 2x 2x   2x 3x     2x 2x     2x             7x 7x 1x   6x          
import ECommentType from "../../types/ECommentType";
import IComment from "../../types/IComment";
 
/**
 * Formats comments for C output (ADR-043)
 *
 * - Line comments: Keep as // (C99+ mode)
 * - Block comments: Keep as block comments
 * - Doc comments: Convert /// to Doxygen format
 */
class CommentFormatter {
  /**
   * Format a single comment for C output
   * @param comment The comment to format
   * @param indent Indentation string (spaces)
   * @returns Formatted comment string
   */
  format(comment: IComment, indent: string = ""): string {
    switch (comment.type) {
      case ECommentType.Line:
        return `${indent}//${comment.content}`;
 
      case ECommentType.Block:
        return this.formatBlockComment(comment, indent);
 
      case ECommentType.Doc:
        return this.formatDocComment(comment, indent);
    }
  }
 
  /**
   * Format consecutive doc comments as a single Doxygen block
   * @param comments Array of consecutive doc comments
   * @param indent Indentation string
   * @returns Formatted Doxygen comment block
   */
  formatDocCommentGroup(comments: IComment[], indent: string = ""): string {
    if (comments.length === 0) return "";
 
    if (comments.length === 1) {
      return this.formatDocComment(comments[0], indent);
    }
 
    // Multiple doc comments -> single /** ... */ block
    const lines: string[] = [];
    lines.push(`${indent}/**`);
 
    for (const comment of comments) {
      const content = comment.content.trim();
      if (content) {
        lines.push(`${indent} * ${content}`);
      } else {
        lines.push(`${indent} *`);
      }
    }
 
    lines.push(`${indent} */`);
    return lines.join("\n");
  }
 
  /**
   * Format leading comments (comments that appear before code)
   * Groups consecutive doc comments into Doxygen blocks
   * @param comments Array of comments
   * @param indent Indentation string
   * @returns Array of formatted comment strings
   */
  formatLeadingComments(comments: IComment[], indent: string = ""): string[] {
    if (comments.length === 0) return [];
 
    const result: string[] = [];
    let docGroup: IComment[] = [];
 
    for (const comment of comments) {
      if (comment.type === ECommentType.Doc) {
        docGroup.push(comment);
      } else {
        // Flush any accumulated doc comments
        if (docGroup.length > 0) {
          result.push(this.formatDocCommentGroup(docGroup, indent));
          docGroup = [];
        }
        result.push(this.format(comment, indent));
      }
    }
 
    // Flush remaining doc comments
    if (docGroup.length > 0) {
      result.push(this.formatDocCommentGroup(docGroup, indent));
    }
 
    return result;
  }
 
  /**
   * Format a trailing/inline comment
   * @param comment The comment
   * @returns Formatted inline comment (with leading spaces)
   */
  formatTrailingComment(comment: IComment): string {
    switch (comment.type) {
      case ECommentType.Line:
      case ECommentType.Doc:
        return `  //${comment.content}`;
 
      case ECommentType.Block:
        // For inline block comments, keep on single line if possible
        if (!comment.content.includes("\n")) {
          return `  /*${comment.content}*/`;
        }
        // Multi-line block comment shouldn't be inline
        return `  /*${comment.content}*/`;
    }
  }
 
  /**
   * Format a block comment preserving internal structure
   */
  private formatBlockComment(comment: IComment, indent: string): string {
    const content = comment.content;
 
    // Single-line block comment
    if (!content.includes("\n")) {
      return `${indent}/*${content}*/`;
    }
 
    // Multi-line block comment - preserve structure
    const lines = content.split("\n");
    const result: string[] = [];
    result.push(`${indent}/*${lines[0]}`);
 
    for (let i = 1; i < lines.length - 1; i++) {
      result.push(`${indent}${lines[i]}`);
    }
 
    Eif (lines.length > 1) {
      result.push(`${indent}${lines.at(-1)}*/`);
    }
 
    return result.join("\n");
  }
 
  /**
   * Format a single doc comment as Doxygen
   */
  private formatDocComment(comment: IComment, indent: string): string {
    const content = comment.content.trim();
    if (!content) {
      return `${indent}/** */`;
    }
    return `${indent}/** ${content} */`;
  }
}
 
export default CommentFormatter;