From c2d7947f414d7a43c0c7daf2bee56e6774f16b3e Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Mon, 21 Sep 2026 20:02:39 -0400 Subject: [PATCH] fix: reject ambiguous color candidates before literal matching --- scripts/check-token-parity.mjs | 21 ++++++++++++--------- scripts/check-token-parity.test.mjs | 6 ++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/scripts/check-token-parity.mjs b/scripts/check-token-parity.mjs index 08d17d03f0..f8d44133cd 100644 --- a/scripts/check-token-parity.mjs +++ b/scripts/check-token-parity.mjs @@ -147,22 +147,25 @@ function slots(line) { return result.length ? result : [{ key: "line", value: line }]; } +function literalSlot(slot) { + return /#[\da-f]{3,8}\b|\b(?:rgba?|hsla?)\(|-(?:white|black)(?:\/\d+)?\b/i.test(slot.value); +} +function replacementMatch(before, candidates, slot, values, names) { + const ambiguous = new Set(candidates.map((old) => canonical(old.value))).size > 1; + const literal = candidates.find(literalSlot); + if (!literal) return undefined; + if (ambiguous) return { before: literal, equal: false, names }; + const expanded = canonical(expandTokens(slot.value, values)); + return { before, equal: canonical(before.value) === expanded, names }; +} function matchReplacement(slot, oldSlots, values) { const candidates = oldSlots.filter((old) => old.key === slot.key); - const ambiguous = new Set(candidates.map((old) => canonical(old.value))).size > 1; const index = oldSlots.findIndex((old) => old.key === slot.key); if (index < 0) return undefined; const [before] = oldSlots.splice(index, 1); const names = [...slot.value.matchAll(TOKEN)].map((match) => match[1]); if (!names.length) return undefined; - if (!/#[\da-f]{3,8}\b|\b(?:rgba?|hsla?)\(|-(?:white|black)(?:\/\d+)?\b/i.test(before.value)) - return undefined; - const expanded = canonical(expandTokens(slot.value, values)); - return { - before, - equal: [!ambiguous, canonical(before.value) === expanded].every(Boolean), - names, - }; + return replacementMatch(before, candidates, slot, values, names); } function compareSlot(file, line, slot, oldSlots, values, allowlist) { const match = matchReplacement(slot, oldSlots, values); diff --git a/scripts/check-token-parity.test.mjs b/scripts/check-token-parity.test.mjs index 9af9c30cb7..96905e4ac9 100644 --- a/scripts/check-token-parity.test.mjs +++ b/scripts/check-token-parity.test.mjs @@ -114,3 +114,9 @@ test("a deleted declaration cannot supply another selector's old colour", () => "--- a/a.css\n+++ b/a.css\n@@ -1,2 +1 @@\n-.a { color: #fff; }\n-.b { color: #000; }\n+.b { color: var(--white); }"; assert.equal(findTokenParityIssues(change, "--white: #fff;").length, 1); }); + +test("a deleted nonliteral declaration cannot hide an ambiguous colour migration", () => { + const change = + "--- a/a.css\n+++ b/a.css\n@@ -1,6 +1,3 @@\n-.a {\n- color: inherit;\n-}\n-.b {\n- color: #000;\n-}\n+.b {\n+ color: var(--white);\n+}"; + assert.equal(findTokenParityIssues(change, "--white: #fff;").length, 1); +});