testImportsUsingWhatChangesSavedWithIt

tcmts.cli.test.e2e.editing.testImportsUsingWhatChangesSavedWithIt · #5aqsrioh · term

A scratch file whose constant uses one it imports, which uses a type the file changes: the import is re-derived against the new type and saved with it, though written after what uses it (rather than refused as "used before its declaration"); and a type error in the file's own code is said as one, not as something it imports that can't be saved with it.

import { okIn, tcmIn, 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 testImportsUsingWhatChangesSavedWithIt = () => {
  const dir = tempDir("imports using what changes");
  const ok = (...args: string[]) => okIn(dir, ...args);
  ok("init");
  fs.writeFileSync(
    path.join(dir, "lib.ts"),
    `// tcm: namespace lib\nexport type Body = { n: number };\nexport const BASE: Body = { n: 1 };\nexport const USER = BASE.n + 1;\n`,
  );
  ok("add", "lib.ts");
  const scratch = path.join(dir, "scratch.ts");
  const write = (user: string) =>
    fs.writeFileSync(
      scratch,
      `// tcm: namespace lib\nimport { BASE } from "#codebase/lib";\nexport type Body = { n: number; m?: number };\n${user}\n`,
    );
  write("export const USER: string = BASE.n + 2;");
  const bad = tcmIn(dir, "update");
  assert.notEqual(bad.status, 0);
  assert.match(bad.out, /TS2322/, bad.out);
  assert.doesNotMatch(bad.out, /can't be saved with it/, bad.out);
  assert.match(bad.out, /scratch\.ts has problems/, bad.out);
  write("export const USER = BASE.n + 2;");
  const out = ok("update");
  assert.match(out, /~ update +lib\.Body/, out);
  assert.match(out, /~ update +lib\.USER/, out);
  assert.equal(ok("run", "lib.USER").trim(), "3");
  assert.match(ok("todo"), /nothing to do/);
}

uses