testSameNamesAndRenamed

tcmts.cli.main.testSameNamesAndRenamed · #cnwuv7kg · term

import { renamed, sameNames } from "#codebase/tcmts/cli/main";

export const testSameNamesAndRenamed = () => {
  const names = new Map<string, string>();
  sameNames(
    "  nodeOf(handle: ts_NodeHandle): ts_Node | undefined;",
    "  nodeOf(handle: ts_NodeHandle): ts_Node_1 | undefined;",
    names,
  );
  sameNames(
    "const at = (x: number) => ({ at: x });",
    "const at_1 = (x: number) => ({ at: x });",
    names,
  );
  // a shorthand written out says nothing of the name
  sameNames("f({ y })", "f({ y: y_1 })", names);
  const eq = (a: unknown, b: unknown) => {
    if (JSON.stringify(a) !== JSON.stringify(b))
      throw new Error(`${JSON.stringify(a)} isn't ${JSON.stringify(b)}`);
  };
  eq(
    [...names],
    [
      ["ts_Node", "ts_Node_1"],
      ["at", "at_1"],
    ],
  );
  // what's declared, and what's imported, by those names; not a local (a parameter) by one, a property, or words
  eq(
    renamed(
      'export const at = (n: ts_Node): ts_Node => {\n  // at, ts_Node\n  const o = { at: /at/, s: `at ${at}`, k: "at" };\n  return x.at(1) + o?.at + { at: 2, b: at, at } + ((at: number) => at)(1);\n};',
      names,
    ),
    'export const at_1 = (n: ts_Node_1): ts_Node_1 => {\n  // at, ts_Node\n  const o = { at: /at/, s: `at ${at_1}`, k: "at" };\n  return x.at(1) + o?.at + { at: 2, b: at_1, at: at_1 } + ((at: number) => at)(1);\n};',
  );
  // in JSX, a component's name, not its text, an attribute's name or an element's (`<at>` is no reference)
  eq(
    renamed(
      "export const v = <Row at={at}>at Row <at /></Row>;",
      new Map([
        ["Row", "Row_1"],
        ["at", "at_1"],
      ]),
      true,
    ),
    "export const v = <Row_1 at={at_1}>at Row <at /></Row_1>;",
  );
}

uses