pathPragma

tcmts.core.extract.pathPragma · #cdigumnj · term · runs anywhere

import { DOTTED, PATH_PRAGMA, TcmError } from "#codebase/tcmts/core/extract";
import { getLeadingCommentRanges as ts_getLeadingCommentRanges } from "typescript/ast";
import type { Node as ts_Node, SourceFile as ts_SourceFile } from "typescript/ast";

export function pathPragma(node: ts_Node, sf: ts_SourceFile): string | undefined {
  let found: string | undefined;
  for (const r of ts_getLeadingCommentRanges(sf.text, node.getFullStart()) ?? []) {
    const m = PATH_PRAGMA.exec(sf.text.slice(r.pos, r.end));
    if (!m) continue;
    if (!DOTTED.test(m[1]!)) throw new TcmError(`'${m[1]}' is not a valid codebase path`, node);
    found = m[1];
  }
  return found;
}

uses

used by