testPatchSeveralRenamesOnlyNames

tcmts.cli.test.e2e.editing.testPatchSeveralRenamesOnlyNames · #xfoudgn5 · term

tcm patch --patches of definitions that, written together, read with another name (session_1: another has a local session, so the constant they use is given another): what an edit writes takes it only where it's the name, declared or referred to, not where the word is in a comment, a string, a regular expression, a template or a property's name; a shorthand keeps its key, and is saved as one.

import { okIn, tempDir } from "#codebase/tcmts/cli/test/e2e/harness";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

export const testPatchSeveralRenamesOnlyNames = () => {
  const dir = tempDir("patch renames only names");
  const ok = (...args: string[]) => okIn(dir, ...args);
  ok("init");
  fs.writeFileSync(
    path.join(dir, "lib.ts"),
    `// tcm: namespace lib\nexport const session = "s";\nexport const use = (): string => session + "!";\n` +
      `export const other = (): number => {\n  const session = 1;\n  return session;\n};\n`,
  );
  ok("add", "lib.ts");
  fs.rmSync(path.join(dir, "lib.ts"));
  const out = ok(
    "patch",
    "--patches",
    JSON.stringify([
      { name: "lib.session", edits: [{ old: '"s"', new: '/* session */ "session"' }] },
      {
        name: "lib.use",
        edits: [
          {
            old: 'session + "!"',
            new:
              '{\n  // the session, "session"\n  const o = { session: /session/.source, s: `session ${session}`, k: "session", n: { session } };\n' +
              "  return session + o.session + o.s + o.k + o.n.session;\n}",
          },
        ],
      },
      { name: "lib.other", edits: [{ old: "return session;", new: "return session + 1;" }] },
    ]),
  );
  assert.match(out, /~ update +lib\.use/, out);
  const use = ok("view", "lib.use");
  assert.doesNotMatch(use, /session_1/, use);
  assert.match(use, /\/\/ the session, "session"/, use);
  assert.match(use, /session: \/session\/\.source/, use);
  assert.match(use, /`session \$\{session\}`/, use);
  assert.match(use, /k: "session"/, use);
  assert.match(use, /n: \{ session \}/, use);
  const session = ok("view", "lib.session");
  assert.match(session, /export const session = \/\* session \*\/ "session"/, session);
  assert.match(ok("view", "lib.other"), /const session = 1;\s*return session \+ 1;/);
  assert.equal(ok("run", "lib.use").trim(), "sessionsessionsession sessionsessionsession");
  assert.match(ok("todo"), /nothing to do/);
}

uses