Skip to content

Commit c5f2652

Browse files
committed
Refactor
1 parent 5878193 commit c5f2652

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

packages/tailwindcss-language-service/src/diagnostics/getInvalidConfigPathDiagnostics.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { closest, distance } from '../util/closest'
77
import { combinations } from '../util/combinations'
88
import dlv from 'dlv'
99
import type { TextDocument } from 'vscode-languageserver-textdocument'
10+
import type { DesignSystem } from '../util/v4'
1011

1112
type ValidationResult =
1213
| { isValid: true; value: any }
@@ -205,23 +206,39 @@ export function getInvalidConfigPathDiagnostics(
205206
return diagnostics
206207
}
207208

208-
function validateV4ThemePath(state: State, path: string): ValidationResult {
209+
function resolveThemeValue(design: DesignSystem, path: string) {
210+
let candidate = `[--custom:theme(${path})]`
211+
209212
// Compile a dummy candidate that uses the theme function with the given path.
210213
//
211214
// We'll get a rule with a declaration from which we read the value. No rule
212215
// will be generated and the root will be empty if the path is invalid.
213216
//
214217
// Non-CSS representable values are not a concern here because the validation
215218
// only happens for calls in a CSS context.
216-
let [root] = state.designSystem.compile([`[--custom:theme(${path})]`])
219+
let [root] = design.compile([candidate])
217220

218221
let value: string | null = null
219222

220223
root.walkDecls((decl) => {
221224
value = decl.value
222225
})
223226

224-
if (value !== null) {
227+
return value
228+
}
229+
230+
function resolveKnownThemeKeys(design: DesignSystem): string[] {
231+
let validThemeKeys = Array.from(design.theme.entries(), ([key]) => key)
232+
233+
return validThemeKeys
234+
}
235+
236+
function validateV4ThemePath(state: State, path: string): ValidationResult {
237+
let prefix = state.designSystem.theme.prefix ?? null
238+
239+
let value = resolveThemeValue(state.designSystem, path)
240+
241+
if (value !== null && value !== undefined) {
225242
return { isValid: true, value }
226243
}
227244

@@ -251,14 +268,10 @@ function suggestAlternativeThemeKeys(state: State, path: string): string[] {
251268
// exposed in any v4 API
252269
if (!path.startsWith('--')) return []
253270

254-
// This is an older version of v4 and it does not have an `entries()` method
255-
// TODO: Check this against old versions — might be unncessary
256-
if (!('entries' in state.designSystem.theme)) return []
257-
258271
let parts = path.slice(2).split('-')
259272
parts[0] = `--${parts[0]}`
260273

261-
let validThemeKeys = Array.from(state.designSystem.theme.entries(), ([key]) => key)
274+
let validThemeKeys = resolveKnownThemeKeys(state.designSystem)
262275
let potentialThemeKey: string | null = null
263276

264277
while (parts.length > 1) {

0 commit comments

Comments
 (0)