testApplyPatchKeepsTemplateLiteralsWhole

tcmts.cli.main.testApplyPatchKeepsTemplateLiteralsWhole · #kuriaqtx · term

In code, an edit inside a template literal that would end or split it (a bare backtick or ${ in new) isn't made, and why is said, with the literal's line; one that keeps it whole (an escaped backtick, a whole substitution), or takes in its backtick on purpose, is.

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

export const testApplyPatchKeepsTemplateLiteralsWhole = () => {
  const src = 'export const G = `one ${x} two`;\nconst s = "a";\n';
  const patched = (old: string, nu: string, from = 0) =>
    applyPatch(src, [{ old, new: nu }], false, from, "x.ts");
  const problem = (r: ReturnType<typeof applyPatch>) => (r as { problem: string }).problem;
  assert.match(
    problem(patched("two", "t`wo")),
    /bare backtick inside the template literal at line 1, which would end the literal there: escape it as \\` \(in JSON, "\\\\`"\)/,
  );
  assert.match(problem(patched("two", "t`wo", 5)), /at line 6/);
  assert.match(problem(patched("two", "t${wo")), /bare \$\{ inside the template literal at line 1/);
  for (const [old, nu] of [
    ["two", "${y} two"],
    ["two", "t\\`wo"],
    ["two`", "two` + `three`"],
    ['"a"', '"`"'],
  ])
    assert.ok("text" in patched(old!, nu!), `${old} → ${nu}`);
  // a page's text isn't code
  assert.ok("text" in applyPatch(src, [{ old: "two", new: "t`wo" }]));
}

uses