jsDocText

tcmts.core.extract.jsDocText · #wraoxk4s · term · runs anywhere

import { getLeadingCommentRanges as ts_getLeadingCommentRanges } from "typescript/ast";
import type { Node as ts_Node, SourceFile as ts_SourceFile } from "typescript/ast";

export function jsDocText(node: ts_Node, sf: ts_SourceFile): string | undefined {
  const ranges = ts_getLeadingCommentRanges(sf.text, node.getFullStart()) ?? [];
  const docs = ranges.filter((r) => sf.text.startsWith("/**", r.pos));
  const last = docs.at(-1);
  if (!last) return undefined;
  return sf.text
    .slice(last.pos + 3, last.end - 2)
    .split("\n")
    .map((l) => l.replace(/^\s*\* ?/, ""))
    .join("\n")
    .trim();
}

used by