testWrittenOutCodeIsNotRun

tcmts.cli.placement.testWrittenOutCodeIsNotRun · #fhvzhflf · term

A function whose source code only writes out as text (${f}, f.toString(), String(f)), for something else to run, doesn't place that code with it, or give it its effects: it stays where its own code puts it. Calling the function, or passing it along, still does; and a constant's value, written into text, was computed by its code.

import { reaches } from "#codebase/tcmts/cli/effects";
import { Placement } from "#codebase/tcmts/cli/placement";
import { codebaseOf } from "#codebase/tcmts/cli/test/shapes";
import { analyzeFile } from "#codebase/tcmts/core/analyze";
import { writtenOutRefs } from "#codebase/tcmts/core/ir";
import { compilerOptions } from "#codebase/tcmts/core/program";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

export const testWrittenOutCodeIsNotRun = () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "tcm written out "));
  fs.writeFileSync(path.join(dir, "package.json"), JSON.stringify({ type: "module" }));
  const file = path.join(dir, "written.ts");
  fs.writeFileSync(
    file,
    `export function observe(el: object) { new IntersectionObserver(() => {}).observe(el as never); return 1; }
export const cwd = () => process.cwd();
export const stamp = () => Date.now();
export const SEEN = typeof IntersectionObserver;
export const templated = () => \`(\${observe})(document.body)\`;
export const stringified = () => observe.toString() + String(cwd) + String(stamp);
export const calls = (el: object) => observe(el);
export const passes = () => [observe];
export const both = (el: object) => \`\${observe}\` + observe(el);
export const constant = () => \`\${SEEN}\`;
export const runsHere = () => cwd() + \`(\${observe})(document.body)\`;
export const testWritten = () => templated();
export const testCalls = () => passes().length;
`,
  );
  const analyzed = analyzeFile(file, {
    cacheDir: path.join(dir, ".tcm", "cache"),
    resolveName: (name) => {
      throw new Error(`unknown ${name}`);
    },
    compilerOptions: { ...compilerOptions(), lib: ["lib.esnext.d.ts", "lib.dom.d.ts"] },
  });
  assert.deepEqual([...analyzed.errors, ...analyzed.diagnostics.map((d) => d.message)], []);
  const cb = codebaseOf(analyzed.defs);
  const hash = new Map(analyzed.defs.map((d) => [d.nameHint, d.hash]));
  const h = (name: string) => hash.get(name)!;
  const rec = (name: string) => analyzed.defs.find((d) => d.nameHint === name)!;
  assert.deepEqual([...writtenOutRefs(rec("templated").ir)], [h("observe")]);
  assert.deepEqual(
    [...writtenOutRefs(rec("stringified").ir)].sort(),
    [h("observe"), h("cwd"), h("stamp")].sort(),
  );
  assert.deepEqual([...writtenOutRefs(rec("both").ir)], [], "used too: it runs");

  const placement = new Placement(cb);
  const where = (name: string) => placement.placeOf(h(name)).place;
  assert.equal(where("observe"), "browser");
  assert.equal(where("cwd"), "server");
  assert.equal(where("templated"), "anywhere", "it only writes observe out");
  assert.equal(where("stringified"), "anywhere");
  assert.equal(where("calls"), "browser");
  assert.equal(where("passes"), "browser");
  assert.equal(where("both"), "browser");
  assert.equal(where("constant"), "browser", "a constant written out was computed by its code");
  assert.deepEqual(placement.placeOf(h("runsHere")), {
    place: "server",
    why: "uses cwd, which uses process",
  });

  assert.equal(placement.testBrowserWhy(h("testWritten")), undefined);
  assert.equal(placement.testNeedsBrowser(h("testWritten")), false);
  assert.equal(
    placement.testBrowserWhy(h("testCalls")),
    "uses passes, which uses observe, which uses IntersectionObserver",
  );

  const effects = reaches(cb, [h("stamp"), h("stringified"), h("both")]);
  assert.deepEqual(effects.get(h("stamp")), ["clock"]);
  assert.deepEqual(effects.get(h("stringified")), [], "stamp's source is written out, not run");
}

uses