Skip to content

Commit 7722959

Browse files
authored
Merge pull request #115 from oBusk/fix-culori-fail
Don't crash server if culori fails to parse a variable
2 parents aa080fa + 2ad8e41 commit 7722959

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

.changeset/full-lions-tease.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"css-variables-language-server": patch
3+
"vscode-css-variables": patch
4+
---
5+
6+
Prevent crashing when css variables have unexpected format such as `--font: var(--font-size)/var(--line-height);`

packages/css-variables-language-server/src/CSSVariableManager.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,14 @@ export default class CSSVariableManager {
201201

202202
// If the value was resolved (changed), try to parse it as a color
203203
if (resolvedValue !== originalValue) {
204-
const culoriColor = culori.parse(resolvedValue);
204+
let culoriColor: culori.Color;
205+
206+
try {
207+
// Culori will throw on some invalid variables, we should not crash the server
208+
culoriColor = culori.parse(resolvedValue);
209+
} catch {
210+
return;
211+
}
205212

206213
if (culoriColor) {
207214
// Update the color property for this variable

packages/css-variables-language-server/src/tests/fixtures/nested-var-resolution/main.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@
4141
/* Non-color variables (should not get color property) */
4242
--spacing: 16px;
4343
--font-size: 14px;
44+
--font: 700 14px/16px 'Helvetica Neue', sans-serif;
4445
}

packages/css-variables-language-server/src/tests/unit-tests/CSSVariableManager.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ describe('CSS Variable Manager', () => {
191191
expect(allVars.get('--font-size').symbol.value).toEqual('14px');
192192
expect(allVars.get('--font-size').color).toBeUndefined();
193193

194+
expect(allVars.get('--font').symbol.value).toEqual("700 14px/16px 'Helvetica Neue', sans-serif");
195+
expect(allVars.get('--font').color).toBeUndefined();
196+
194197
// Test cross-file resolution
195198
expect(allVars.get('--child-color-red').symbol.value).toEqual('var(--color-red)');
196199
expect(allVars.get('--child-color-red').color).toBeDefined();

0 commit comments

Comments
 (0)