testPatchADocWrittenOnOneLine

tcmts.cli.main.testPatchADocWrittenOnOneLine · #ji3wi2uh · term

A doc comment can be patched as it was written, on one line, though it's stored on several (each led by *)

import { applyPatch } from "#codebase/tcmts/cli/main";
import assert from "node:assert/strict";

export const testPatchADocWrittenOnOneLine = () => {
  const text = `/**\n * How many definitions calling each other are worth a word: two or three that\n * recurse together are fine.\n */\nexport const BIG = 5;\n`;
  const r = applyPatch(text, [
    {
      old: "/** How many definitions calling each other are worth a word: two or three that recurse together are fine. */",
      new: "/** Up to four that recurse together are fine. */",
    },
  ]);
  assert.ok("text" in r, JSON.stringify(r));
  assert.equal(
    (r as { text: string }).text,
    "/** Up to four that recurse together are fine. */\nexport const BIG = 5;\n",
  );
  // a part of it, across the line break
  const part = applyPatch(text, [
    { old: "two or three that recurse together", new: "up to four that recurse together" },
  ]);
  assert.ok(
    "text" in part &&
      (part as { text: string }).text.includes("up to four that recurse together are fine."),
    JSON.stringify(part),
  );
  // code isn't loosened: a line starting with `*` that multiplies is still code
  assert.ok(
    "problem" in applyPatch("const x = a\n  * b;\n", [{ old: "a b", new: "c" }]),
    "a missing operator isn't spacing",
  );
}

uses