fix: preserve color migration slot pairing

This commit is contained in:
Miguel Angel Simon Sierra
2026-09-21 19:51:37 -04:00
parent 7cc1f4b796
commit d0d45b4ea1
2 changed files with 28 additions and 7 deletions
+8 -7
View File
@@ -125,7 +125,7 @@ function classSlots(line) {
const result = [];
const classes = [
...line.matchAll(
/([\w:-]+)-(white|black)(?:\/(\d+(?:\.\d+)?))?\b|([\w:-]+)-\[(var\([^)]*\))\]/g,
/([\w:-]+)-(white|black)(?:\/(\d+(?:\.\d+)?))?\b|([\w:-]+)-\[((?:var|rgba?|hsla?)\([^)]*\)|#[\da-f]{3,8})\]/g,
),
];
for (const match of classes) {
@@ -135,10 +135,11 @@ function classSlots(line) {
return result;
}
function slots(line) {
const classes = classSlots(line);
if (classes.length) return classes;
const properties = propertySlots(line);
return properties.length ? properties : [{ key: "line", value: line }];
const properties = propertySlots(line).filter(
(slot) => !["class", "className"].includes(slot.key),
);
const result = [...properties, ...classSlots(line)];
return result.length ? result : [{ key: "line", value: line }];
}
function matchReplacement(slot, oldSlots, values) {
@@ -151,8 +152,8 @@ function matchReplacement(slot, oldSlots, values) {
);
if (!candidates.length) return undefined;
const expanded = canonical(expandTokens(slot.value, values));
const exact = candidates.find((old) => canonical(old.value) === expanded);
return { before: exact ?? candidates[0], equal: Boolean(exact), names };
const before = candidates[0];
return { before, equal: canonical(before.value) === expanded, names };
}
function compareSlot(file, line, slot, oldSlots, values, allowlist) {
const match = matchReplacement(slot, oldSlots, values);
+20
View File
@@ -82,3 +82,23 @@ test("duplicate additions cannot hide a second wrong replacement", () => {
'--- a/packages/a.ts\n+++ b/packages/a.ts\n@@ -1,2 +1,2 @@\n-color: "#fff"\n-color: "#000"\n+color: "var(--x)"\n+color: "var(--x)"';
assert.equal(findTokenParityIssues(change, "--x: #f00;").length, 2);
});
test("arbitrary Tailwind literals are colour migrations", () => {
assert.equal(
findTokenParityIssues(diff('className="bg-[#fff]"', 'className="bg-[var(--x)]"'), "--x: #000;")
.length,
1,
);
});
test("class utilities do not hide inline colour slots", () => {
const old = '<div className="bg-black" style={{color: "#fff"}} />';
const next = '<div className="bg-black" style={{color: "var(--x)"}} />';
assert.equal(findTokenParityIssues(diff(old, next), "--x: #000;").length, 1);
});
test("swapping opposite token values between declarations fails", () => {
const change =
"--- a/packages/a.css\n+++ b/packages/a.css\n@@ -1,2 +1,2 @@\n-.a { color: #fff; }\n-.b { color: #000; }\n+.a { color: var(--black); }\n+.b { color: var(--white); }";
assert.equal(findTokenParityIssues(change, "--black: #000; --white: #fff;").length, 2);
});