fix: reject ambiguous color candidates before literal matching

This commit is contained in:
Miguel Angel Simon Sierra
2026-09-21 20:02:39 -04:00
parent 3dd6462423
commit c2d7947f41
2 changed files with 18 additions and 9 deletions
+12 -9
View File
@@ -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);
+6
View File
@@ -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);
});