testRespelled
tcmts.cli.main.testRespelled · #thos7lot · term
import { respelled } from "#codebase/tcmts/cli/main";
import type { DefRecord } from "#codebase/tcmts/core/types";
export const testRespelled = () => {
const def = {
sourceText: "const find_1 = (xs: number[]) => find(xs, 1) + find_1.length;",
spans: [
{ start: 6, end: 12, hash: "self", pos: "value" },
{ start: 33, end: 37, hash: "other", pos: "value" },
{ start: 47, end: 53, hash: "self", pos: "value" },
],
} as unknown as DefRecord;
const out = respelled(
def,
new Map([
["find_1", "find"],
["find", "find_1"],
]),
);
const want = "const find = (xs: number[]) => find_1(xs, 1) + find.length;";
if (out.sourceText !== want) throw new Error(out.sourceText);
const at = out.spans.map((s) => out.sourceText.slice(s.start, s.end));
if (at.join() !== "find,find_1,find") throw new Error(at.join());
// a shorthand the file wrote out is one again; a type's property, or a comment's words, aren't
const text =
"const o = { find: find_1, n: g(1), m: find_1 } as { find: find_1 }; // { find: find_1 }";
const refs = [...text.matchAll(/find_1|\bg\b/g)].slice(0, 4).map((m) => m.index);
const two = respelled(
{
sourceText: text,
spans: refs.map((start, i) => ({
start,
end: start + (i === 1 ? 1 : 6),
hash: i === 3 ? "type" : "other",
pos: i === 3 ? "type" : "value",
})),
} as unknown as DefRecord,
new Map([["find_1", "find"]]),
);
const want2 = "const o = { find, n: g(1), m: find } as { find: find }; // { find: find_1 }";
if (two.sourceText !== want2) throw new Error(two.sourceText);
const got = two.spans.map(
(s) => `${two.sourceText.slice(s.start, s.end)}${s.shorthand ? `(${s.shorthand})` : ""}`,
);
if (got.join() !== "find(find),g,find,find") throw new Error(got.join());
}
uses