testPackageReexportsHashAsImports

tcmts.core.test.hash.testPackageReexportsHashAsImports · #5we3746e · term

A package's export used through a project module that re-exports it (export * from "pkg", export { x } from "pkg", export * as ns from "pkg") hashes as an import of it from the package.

import { dir, hashes, project } from "#codebase/tcmts/core/test/hash";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

export const testPackageReexportsHashAsImports = () => {
  const pkg = path.join(dir, "node_modules", "geom");
  fs.mkdirSync(pkg, { recursive: true });
  fs.writeFileSync(
    path.join(pkg, "package.json"),
    JSON.stringify({ name: "geom", version: "1.2.0", types: "./index.d.ts" }),
  );
  fs.writeFileSync(
    path.join(pkg, "index.d.ts"),
    "export declare function area(r: number): number;\nexport interface Point { x: number; y: number }\n",
  );
  const direct = hashes(`import { area } from "geom";
import type { Point } from "geom";
import * as geom from "geom";
export const a = (r: number) => area(r);
export const b = (r: number) => area(r);
export const c = (r: number) => area(r);
export const d = (r: number) => area(r);
export type At = Point;
export type Through = Point;
export const lib = () => geom;`);
  const p = project({
    "star.ts": `export * from "geom";`,
    "named.ts": `export { area } from "geom";`,
    "ns.ts": `export * as g from "geom";`,
    "star_use.ts": `import { area } from "./star.ts";\nexport const a = (r: number) => area(r);`,
    "named_use.ts": `import * as m from "./named.ts";\nexport const b = (r: number) => m.area(r);`,
    "ns_use.ts": `import { g } from "./ns.ts";
export const c = (r: number) => g.area(r);
export type At = g.Point;
export const lib = () => g;`,
    "ns_through.ts": `import * as m from "./ns.ts";\nexport const d = (r: number) => m.g.area(r);\nexport type Through = m.g.Point;`,
  });
  for (const name of ["a", "b", "c", "d"]) {
    assert.equal(p[name]!.hash, direct[name], `${name} hashes as an import of area`);
    assert.deepEqual(
      p[name]!.deps.map((x) => x.ref),
      ["npm:geom@1#area"],
      name,
    );
  }
  assert.equal(p.At!.hash, direct.At, "in a type position too");
  assert.equal(p.Through!.hash, direct.Through, "in a type position through a namespace import");
  assert.equal(p.lib!.hash, direct.lib, "the namespace itself is the package's");
}

uses