{"draft":null,"from":191,"to":192,"files":[{"path":"scripts/autosettle/rules.ts","added":35,"removed":0,"diff":"--- /dev/null\n+++ b/scripts/autosettle/rules.ts\n@@ -0,0 +1,35 @@\n+// Checks in code around the model's pick (#19). What code can decide, code\n+// decides; what the model picks is turned down when it would plainly break\n+// the file. Both run before anything is settled automatically.\n+\n+import type { Hold, Pick } from \"./jev.ts\";\n+\n+const solid = (lines: readonly string[]) => lines.map((l) => l.trim()).filter((l) => /[A-Za-z0-9]/.test(l));\n+\n+/** A pick code can make without the model, with why; undefined when it takes judgment. */\n+export function ruled(h: Hold): { pick: Pick; reason: string } | undefined {\n+\tconst yours = solid(h.yours);\n+\tconst theirs = solid(h.theirs);\n+\tif (yours.join(\"\\n\") === theirs.join(\"\\n\")) return { pick: \"theirs\", reason: \"both sides wrote the same lines\" };\n+\tif (yours.length > 0 && yours.every((l) => theirs.includes(l)))\n+\t\treturn { pick: \"theirs\", reason: \"every line of yours is already in theirs\" };\n+\tif (theirs.length > 0 && theirs.every((l) => yours.includes(l)))\n+\t\treturn { pick: \"yours\", reason: \"every line of theirs is already in yours\" };\n+\treturn undefined;\n+}\n+\n+/** Why a model's pick can't be applied as it is, or undefined when it can. */\n+export function refused(h: Hold, pick: Pick): string | undefined {\n+\tif (pick !== \"both\") return undefined;\n+\tconst theirs = new Set(solid(h.theirs));\n+\tconst twice = solid(h.yours).filter((l) => theirs.has(l));\n+\tif (twice.length > 0)\n+\t\treturn `keeping both would repeat ${twice.length === 1 ? \"a line\" : `${twice.length} lines`}: ${twice[0]}`;\n+\t// The same key or name set on both sides: `timeoutMs:`, `limit =`.\n+\tconst key = (l: string) => /^([A-Za-z_$\"'][\\w$\"'-]*)\\s*[:=]/.exec(l)?.[1];\n+\tconst theirKeys = new Set(solid(h.theirs).map(key).filter(Boolean));\n+\tconst clash = solid(h.yours)\n+\t\t.map(key)\n+\t\t.find((k) => k && theirKeys.has(k));\n+\treturn clash ? `keeping both would set ${clash} twice` : undefined;\n+}"}],"unmatched":[]}