Skip to content

Commit 4013652

Browse files
fix(compiler): fold a variable read inside a colour function
lightningcss parses `hsl(0 100% 50% / var(--a))`, `rgb(… / var(--a))` and `light-dark(#123, var(--b))` as an unresolved colour whose channels are token lists, and the inliner walked `var` and `function` tokens only. A variable declared once therefore lost its declaration while a read inside a colour function kept naming it, so the alpha, or the arm, came out empty.
1 parent a547588 commit 4013652

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎src/__tests__/compiler/compiler.test.tsx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ test("preserves excluded CSS variables using the public compiler option", () =>
9999
expect(rule?.v?.some(([name]) => name === "red")).toBe(false);
100100
});
101101

102+
test.each([
103+
[
104+
"an hsl() alpha",
105+
`:root { --alpha: 0.5; } .test { color: hsl(0 100% 50% / var(--alpha)); }`,
106+
`.test { color: hsl(0 100% 50% / 0.5); }`,
107+
],
108+
[
109+
"an rgb() alpha",
110+
`:root { --alpha: 0.5; } .test { color: rgb(255 0 0 / var(--alpha)); }`,
111+
`.test { color: rgb(255 0 0 / 0.5); }`,
112+
],
113+
[
114+
"a light-dark() arm",
115+
`:root { --dark: #654321; } .test { color: light-dark(#123456, var(--dark)); }`,
116+
`.test { color: light-dark(#123456, #654321); }`,
117+
],
118+
])("folds a variable read inside %s", (_name, css, folded) => {
119+
expect(compile(css).stylesheet()).toStrictEqual(compile(folded).stylesheet());
120+
});
121+
102122
test("multiple rules with same selector", () => {
103123
const compiled = compile(`
104124
.redOrGreen:hover {

‎src/compiler/inline-variables.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@ function flattenPart(
151151
part.value.arguments = part.value.arguments.flatMap((arg) => {
152152
return flattenPart(arg, vars);
153153
});
154+
} else if (part.type === "unresolved-color") {
155+
const color = part.value;
156+
157+
switch (color.type) {
158+
case "rgb":
159+
case "hsl":
160+
color.alpha = color.alpha.flatMap((arg) => flattenPart(arg, vars));
161+
break;
162+
case "light-dark":
163+
color.light = color.light.flatMap((arg) => flattenPart(arg, vars));
164+
color.dark = color.dark.flatMap((arg) => flattenPart(arg, vars));
165+
break;
166+
default:
167+
color satisfies never;
168+
}
154169
}
155170

156171
return part;

0 commit comments

Comments
 (0)