Repository: babel/babel
File: packages/babel-code-frame/src/index.ts (TypeScript)

Code before:
  } else {
    if (startColumn === endColumn) {
      if (startColumn) {
        markerLines[startLine] = [startColumn, 0];
      } else {
        markerLines[startLine] = true;
      }
    } else {
      markerLines[startLine] = [startColumn, endColumn - startColumn];
    }
  }

  return { start, end, markerLines };
}

export function codeFrameColumns(
  rawLines: string,
  loc: NodeLocation,
  opts: Options = {},
): string {
  const shouldHighlight =
    opts.forceColor || (isColorSupported() && opts.highlightCode);
  const defs = getDefs(shouldHighlight);

  const lines = rawLines.split(NEWLINE);
  const { start, end, markerLines } = getMarkerLines(loc, lines, opts);
  const hasColumns = loc.start && typeof loc.start.column === "number";

  const numberMaxWidth = String(end).length;

  const highlightedLines = shouldHighlight ? highlight(rawLines) : rawLines;

  let frame = highlightedLines
    .split(NEWLINE, end)
    .slice(start, end)
    .map((line, index) => {
      const number = start + 1 + index;
      const paddedNumber = ` ${number}`.slice(-numberMaxWidth);
      const gutter = ` ${paddedNumber} |`;
      const hasMarker = markerLines[number];
      const lastMarkerLine = !markerLines[number + 1];
      if (hasMarker) {
        let markerLine = "";
        if (Array.isArray(hasMarker)) {
          const markerSpacing = line
            .slice(0, Math.max(hasMarker[0] - 1, 0))
            .replace(/[^\t]/g, " ");
          const numberOfMarkers = hasMarker[1] || 1;

          markerLine = [
            "\n ",

Review comment (suggestion):
If I pass `startLine: 0` in the options, I get this to be `startLineBaseZero: 0`. I think it's good, as the first line is line 1 so 0 is likely a mistake that meant "first line", but I wanted to point it out.

Code after:
        markerLines[lineNumber] = [0, sourceLength];
      }
    }
  } else {
    if (startColumn === endColumn) {
      if (startColumn) {
        markerLines[startLine] = [startColumn, 0];
      } else {
        markerLines[startLine] = true;
      }
    } else {
      markerLines[startLine] = [startColumn, endColumn - startColumn];
    }
  }

  return { start, end, markerLines };
}

export function codeFrameColumns(
  rawLines: string,
  loc: NodeLocation,
  opts: Options = {},
): string {
  const shouldHighlight =
    opts.forceColor || (isColorSupported() && opts.highlightCode);
  const startLineBaseZero = (opts.startLine || 1) - 1;
  const defs = getDefs(shouldHighlight);

  const lines = rawLines.split(NEWLINE);
  const { start, end, markerLines } = getMarkerLines(
    loc,
    lines,
    opts,
    startLineBaseZero,
  );
  const hasColumns = loc.start && typeof loc.start.column === "number";

  const numberMaxWidth = String(end + startLineBaseZero).length;

  const highlightedLines = shouldHighlight ? highlight(rawLines) : rawLines;

  let frame = highlightedLines
    .split(NEWLINE, end)
    .slice(start, end)
    .map((line, index) => {
      const number = start + 1 + index;
      const paddedNumber = ` ${number + startLineBaseZero}`.slice(
        -numberMaxWidth,
      );
      const gutter = ` ${paddedNumber} |`;
      const hasMarker = markerLines[number];