From 0f35dcb6fe922ac6d95a5a46b4dd8a38241f74fe Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Tue, 13 May 2025 11:32:10 -0400 Subject: [PATCH 1/4] Ignore errors thrown by Culori --- .../src/util/color.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/tailwindcss-language-service/src/util/color.ts b/packages/tailwindcss-language-service/src/util/color.ts index a1a99d66..0ea56852 100644 --- a/packages/tailwindcss-language-service/src/util/color.ts +++ b/packages/tailwindcss-language-service/src/util/color.ts @@ -61,7 +61,7 @@ function getColorsInString(state: State, str: string): (culori.Color | KeywordCo function toColor(match: RegExpMatchArray) { let color = match[1].replace(/var\([^)]+\)/, '1') - return getKeywordColor(color) ?? culori.parse(color) + return getKeywordColor(color) ?? tryParseColor(color) } str = replaceCssVarsWithFallbacks(state, str) @@ -275,7 +275,7 @@ export function getColorFromValue(value: unknown): culori.Color | KeywordColor | ) { return null } - const color = culori.parse(trimmedValue) + const color = tryParseColor(trimmedValue) return color ?? null } @@ -296,11 +296,21 @@ export function formatColor(color: culori.Color): string { const COLOR_MIX_REGEX = /color-mix\(in [^,]+,\s*(.*?)\s*(\d+|\.\d+|\d+\.\d+)%,\s*transparent\)/g +function tryParseColor(color: string) { + try { + return culori.parse(color) + } catch (err) { + console.error('Error parsing color', color) + console.error(err) + return null + } +} + function removeColorMixWherePossible(str: string) { return str.replace(COLOR_MIX_REGEX, (match, color, percentage) => { if (color.startsWith('var(')) return match - let parsed = culori.parse(color) + let parsed = tryParseColor(color) if (!parsed) return match let alpha = Number(percentage) / 100 From 9d28020089f0f02ef2541d4068002a0f43c161ba Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Tue, 13 May 2025 11:35:12 -0400 Subject: [PATCH 2/4] Update changelog --- packages/vscode-tailwindcss/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/vscode-tailwindcss/CHANGELOG.md b/packages/vscode-tailwindcss/CHANGELOG.md index e57ef565..8e28ad34 100644 --- a/packages/vscode-tailwindcss/CHANGELOG.md +++ b/packages/vscode-tailwindcss/CHANGELOG.md @@ -6,6 +6,7 @@ - Ignore Python virtual env directories by default ([#1336](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1336)) - Ignore Yarn v2+ metadata & cache directories by default ([#1336](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1336)) - Ignore some build caches by default ([#1336](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1336)) +- Gracefully handle color parsing failures ([#1363](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1363)) # 0.14.16 From 30d80735abe75dfbd5f879ff7eadc44ffefc9604 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Tue, 13 May 2025 11:41:59 -0400 Subject: [PATCH 3/4] update --- packages/tailwindcss-language-service/src/util/color.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/tailwindcss-language-service/src/util/color.ts b/packages/tailwindcss-language-service/src/util/color.ts index 0ea56852..d7e9172e 100644 --- a/packages/tailwindcss-language-service/src/util/color.ts +++ b/packages/tailwindcss-language-service/src/util/color.ts @@ -275,8 +275,8 @@ export function getColorFromValue(value: unknown): culori.Color | KeywordColor | ) { return null } - const color = tryParseColor(trimmedValue) - return color ?? null + + return tryParseColor(trimmedValue) ?? null } let toRgb = culori.converter('rgb') From 35537eab25a37e0face6eb06fd38ce6b9e5b8f50 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Tue, 13 May 2025 11:45:15 -0400 Subject: [PATCH 4/4] move `??` --- packages/tailwindcss-language-service/src/util/color.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/tailwindcss-language-service/src/util/color.ts b/packages/tailwindcss-language-service/src/util/color.ts index d7e9172e..4d17a9ea 100644 --- a/packages/tailwindcss-language-service/src/util/color.ts +++ b/packages/tailwindcss-language-service/src/util/color.ts @@ -276,7 +276,7 @@ export function getColorFromValue(value: unknown): culori.Color | KeywordColor | return null } - return tryParseColor(trimmedValue) ?? null + return tryParseColor(trimmedValue) } let toRgb = culori.converter('rgb') @@ -298,7 +298,7 @@ const COLOR_MIX_REGEX = /color-mix\(in [^,]+,\s*(.*?)\s*(\d+|\.\d+|\d+\.\d+)%,\s function tryParseColor(color: string) { try { - return culori.parse(color) + return culori.parse(color) ?? null } catch (err) { console.error('Error parsing color', color) console.error(err)