diff --git a/scripts/ad-hoc/strip-derived-from-pr.mjs b/scripts/ad-hoc/strip-derived-from-pr.mjs index 1ea86600571..1f6b0baaa1d 100644 --- a/scripts/ad-hoc/strip-derived-from-pr.mjs +++ b/scripts/ad-hoc/strip-derived-from-pr.mjs @@ -52,6 +52,8 @@ export function classifyFile(file, { claimFiles, instructionFiles }) { /** * Each managed value the head changed goes back to the base value of the same claim (same check, * same position). A check whose claim count differs between base and head is left to a human. + * The replacement is the base's own text for that claim, not the parsed value: a mirror that + * writes "۲۳۳" must get "۲۳۳" back, or the line still differs from the base and keeps conflicting. */ export function restoreClaims(base, head, checks) { const edits = []; @@ -72,6 +74,7 @@ export function restoreClaims(base, head, checks) { label: check.label, from: hv[i], to: bv[i], + text: b[i].start >= 0 ? (base ?? "").slice(b[i].start, b[i].end) : bv[i], start: claim.start, end: claim.end, }); @@ -84,7 +87,7 @@ export function restoreClaims(base, head, checks) { const key = `${e.start}:${e.end}`; if (seen.has(key)) continue; seen.add(key); - content = content.slice(0, e.start) + e.to + content.slice(e.end); + content = content.slice(0, e.start) + e.text + content.slice(e.end); } for (const check of checks) { if (!edits.some((e) => e.label === check.label)) continue; diff --git a/tests/unit/strip-derived-from-pr.test.ts b/tests/unit/strip-derived-from-pr.test.ts index 543d38d6ccb..a1f7e412e8f 100644 --- a/tests/unit/strip-derived-from-pr.test.ts +++ b/tests/unit/strip-derived-from-pr.test.ts @@ -84,6 +84,28 @@ test("claims: each value the PR changed goes back to the base value; prose edits assert.deepEqual(r.manual, []); }); +test("claims: the base's own digits come back, not a re-rendered number", () => { + // A translated mirror writes the count in Persian digits; the head rewrote it in ASCII. + const toAscii = (t: string) => t.replace(/[۰-۹]/g, (d) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(d))); + const nativeCheck: Check = { + label: "DB migrations count (README mirrors)", + extract: (content) => + [...content.matchAll(/([0-9۰-۹]+) مهاجرت/dg)].map((m) => ({ + value: Number(toAscii(m[1])), + start: (m as unknown as { indices: [number, number][] }).indices[1][0], + end: (m as unknown as { indices: [number, number][] }).indices[1][1], + })), + }; + const base = "۱۴۷ ماژول، ۲۳۳ مهاجرت\n"; + const head = "۱۴۷ ماژول، 234 مهاجرت\n"; + const r = strip.restoreClaims(base, head, [nativeCheck]); + assert.equal(r.content, base, "byte-identical to the base line"); + assert.deepEqual(r.edits, [ + { label: "DB migrations count (README mirrors)", from: "234", to: "233" }, + ]); + assert.deepEqual(r.manual, []); +}); + test("claims: a PR that adds or removes a claim is left to a human", () => { const r = strip.restoreClaims("387 providers.\n", "388 providers. Also 388 providers.\n", [ providerCheck,