pathsIn

tcmts.cli.main.pathsIn · #pewlb6fw · term · runs on the server

uses tcmts.core.ts.parse, which uses tcmts.core.ts.compiler, which uses process

The codebase paths a scratch file's declarations go to: its namespace and their names, or a // tcm: path.

import { joinName } from "#codebase/tcmts/cli/mirror";
import { scratchNamespace } from "#codebase/tcmts/core/analyze";
import { parse } from "#codebase/tcmts/core/ts";
import {
  isClassDeclaration as ts_isClassDeclaration, isFunctionDeclaration as ts_isFunctionDeclaration,
  isIdentifier as ts_isIdentifier, isInterfaceDeclaration as ts_isInterfaceDeclaration,
  isTypeAliasDeclaration as ts_isTypeAliasDeclaration, isVariableStatement as ts_isVariableStatement,
} from "typescript/ast";

export function pathsIn(file: string, text: string, into: string | undefined): string[] {
  const ns = into ?? scratchNamespace(text) ?? "";
  const out: string[] = [];
  for (const st of parse(file, text).statements) {
    const pragma = /\/\/\s*tcm:\s*path\s+(\S+)/.exec(text.slice(st.pos, st.getStart()))?.[1];
    const names = ts_isVariableStatement(st)
      ? st.declarationList.declarations.flatMap((d) => (ts_isIdentifier(d.name) ? [d.name.text] : []))
      : (ts_isFunctionDeclaration(st) || ts_isClassDeclaration(st) || ts_isInterfaceDeclaration(st) || ts_isTypeAliasDeclaration(st)) && st.name ? [st.name.text] : [];
    if (pragma && names.length) out.push(pragma);
    else for (const n of names) out.push(joinName(ns, n));
  }
  return out;
}

uses

used by