All files / cli DiagnosticText.ts

100% Statements 4/4
100% Branches 5/5
100% Functions 1/1
100% Lines 4/4

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                                        26x 26x     26x     26x          
import DeclarationSite from "../cnx/utils/DeclarationSite";
import type ITranspileError from "../cnx/types/ITranspileError";
 
/**
 * How the CLI writes one diagnostic: `file:line:col message`, with the advice
 * indented beneath it.
 *
 * #2165: errors (`ResultPrinter`) and 2.1's warnings (`Transpiler`) each spelled
 * this out, so the two could drift. Error fixtures are generated from this
 * output, so what a user reads and what the corpus asserts are the same text.
 */
class DiagnosticText {
  /**
   * @param sourcePath - Where the diagnostic is placed; a warning is placed at
   *   the file it was collected for
   */
  static of(
    diagnostic: ITranspileError,
    sourcePath: string | undefined = diagnostic.sourcePath,
  ): string {
    const position = `${diagnostic.line}:${diagnostic.column}`;
    const location = sourcePath
      ? `${DeclarationSite.displayPath(sourcePath)}:${position}`
      : position;
    const help = diagnostic.helpText
      ? `\n       help: ${diagnostic.helpText}`
      : "";
    return `${location} ${diagnostic.message}${help}`;
  }
}
 
export default DiagnosticText;