namespacePragma

tcmts.core.extract.namespacePragma · #xby3gyww · term · runs anywhere

A // tcm: namespace a.b line among the comments before a statement: the namespace it names.

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

export function namespacePragma(node: ts_Node, sf: ts_SourceFile): string | undefined {
  let found: string | undefined;
  for (const r of ts_getLeadingCommentRanges(sf.text, node.getFullStart()) ?? []) {
    const m = /^\/\/\s*tcm:\s*namespace\s+(\S+)\s*$/.exec(sf.text.slice(r.pos, r.end));
    if (m) found = m[1];
  }
  return found;
}

used by