importBindings

tcmts.cli.main.importBindings · #pt3jbzli · term · runs on the server

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

What a module's imports bind: local name → the module, and whether only as a type.

import { isTypeOnlyImport, parse } from "#codebase/tcmts/core/ts";
import {
  isImportDeclaration as ts_isImportDeclaration, isNamespaceImport as ts_isNamespaceImport,
  isStringLiteral as ts_isStringLiteral,
} from "typescript/ast";

export function importBindings(file: string, text: string): Map<string, { module: string; type: boolean }> {
  const out = new Map<string, { module: string; type: boolean }>();
  for (const st of parse(file, text).statements) {
    if (!ts_isImportDeclaration(st) || !st.importClause || !ts_isStringLiteral(st.moduleSpecifier)) continue;
    const module = st.moduleSpecifier.text;
    const type = isTypeOnlyImport(st.importClause);
    if (st.importClause.name) out.set(st.importClause.name.text, { module, type });
    const b = st.importClause.namedBindings;
    if (b && ts_isNamespaceImport(b)) out.set(b.name.text, { module, type });
    else if (b) for (const e of b.elements) out.set(e.name.text, { module, type: type || e.isTypeOnly });
  }
  return out;
}

uses

used by