syntaxError

tcmts.cli.main.syntaxError · #ahl6vwlm · term · runs on the server

uses node:util

The first syntax error in some code, said alone (what the checker says besides follows from reading the code wrong): where it is, as the checker says it (scratch.ts:12:5), and the lines around it, the place marked. A long line (a template literal's, say) is shown around the place.

import { red } from "#codebase/tcmts/cli/style";
import { stripVTControlCharacters } from "node:util";

export function syntaxError(
  d: { code: number; message: string; formatted: string },
  text: string,
): string {
  const at = /^(.*):(\d+):(\d+) - /.exec(stripVTControlCharacters(d.formatted).split("\n")[0]!);
  const head = `${at ? `${at[1]}:${at[2]}:${at[3]} - ` : ""}${red("error")} TS${d.code}: ${d.message}`;
  if (!at) return head;
  const line = Number(at[2]) - 1;
  const col = Number(at[3]) - 1;
  const lines = text.split("\n");
  const width = 100;
  const start = Math.max(0, col - width / 2);
  const shown = (l: string) =>
    `${start && l.length > start ? "…" : ""}${l.slice(start, start + width)}${l.length > start + width ? "…" : ""}`;
  const out = [head, ""];
  for (let i = Math.max(0, line - 2); i <= Math.min(lines.length - 1, line + 2); i++) {
    out.push(`${i === line ? ">" : " "} ${shown(lines[i]!)}`);
    if (i === line) out.push(`  ${" ".repeat(col - start + (start ? 1 : 0))}${red("^")}`);
  }
  return out.join("\n");
}

uses

used by