namespacedClientApi

tcmts.cli.placement.namespacedClientApi · #f3enpo6l · term · runs anywhere

React.useState(…) through import * as React or import React: the same hooks, used as properties.

import { isClientReactApi } from "#codebase/tcmts/cli/placement";
import type { IR } from "#codebase/tcmts/core/hash";

export function namespacedClientApi(ir: IR): string | undefined {
  if (typeof ir === "string") return undefined;
  if (ir[0] === "PropertyAccessExpression") {
    const object = (ir.find((x) => Array.isArray(x) && x[0] === "expression") as IR[] | undefined)?.[1] as IR[] | undefined;
    const name = (ir.find((x) => Array.isArray(x) && x[0] === "name") as IR[] | undefined)?.[1] as IR[] | undefined;
    if (object?.[0] === "Ext" && typeof object[1] === "string" && (object[3] === "*" || object[3] === "default") && name?.[0] === "N" && typeof name[1] === "string") {
      const api = { module: object[1], export: name[1] };
      if (isClientReactApi(api)) return `${object[1].replace(/^npm:(.+)@[^@]+$/, "$1")}.${name[1]}`;
    }
  }
  for (const c of ir) {
    const found = namespacedClientApi(c);
    if (found) return found;
  }
  return undefined;
}

uses

used by