testSaveTakesOutUnusedImports

tcmts.cli.test.e2e.editing.testSaveTakesOutUnusedImports · #2yhq3a4r · term

Saving a scratch file takes out the imports nothing in it uses, as tcm patch does: one the code stopped using, and one of a name the codebase doesn't have any more (which would stop it typechecking). What a comment's {@link} names stays.

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 testSaveTakesOutUnusedImports = () => {
  const dir = tempDir("save prunes imports");
  const ok = (...args: string[]) => okIn(dir, ...args);
  ok("init");
  const scratch = path.join(dir, "scratch.ts");
  fs.writeFileSync(
    scratch,
    `// tcm: namespace lib\nexport const one = (): number => 1;\nexport const two = (): number => 2;\n`,
  );
  ok("update");
  fs.writeFileSync(
    scratch,
    `// tcm: namespace app\nimport { randomBytes } from "node:crypto";\nimport { gone, one, two } from "#codebase/lib";\n\n` +
      `/** Like {@link two}, but one. */\nexport const f = (): number => one();\n`,
  );
  const out = ok("update");
  assert.match(out, /took out imports nothing uses: randomBytes, gone/, out);
  assert.match(out, /\+ new +app\.f/, out);
  // (the file, kept, as it was saved)
  assert.equal(
    fs.readFileSync(scratch, "utf8"),
    `// tcm: namespace app\nimport { one, two } from "#codebase/lib";\n\n/** Like {@link two}, but one. */\nexport const f = (): number => one();\n`,
  );
  assert.equal(ok("run", "app.f").trim(), "1");
}

uses