@@ -7,6 +7,7 @@ import { closest, distance } from '../util/closest'
7
7
import { combinations } from '../util/combinations'
8
8
import dlv from 'dlv'
9
9
import type { TextDocument } from 'vscode-languageserver-textdocument'
10
+ import type { DesignSystem } from '../util/v4'
10
11
11
12
type ValidationResult =
12
13
| { isValid : true ; value : any }
@@ -205,23 +206,39 @@ export function getInvalidConfigPathDiagnostics(
205
206
return diagnostics
206
207
}
207
208
208
- function validateV4ThemePath ( state : State , path : string ) : ValidationResult {
209
+ function resolveThemeValue ( design : DesignSystem , path : string ) {
210
+ let candidate = `[--custom:theme(${ path } )]`
211
+
209
212
// Compile a dummy candidate that uses the theme function with the given path.
210
213
//
211
214
// We'll get a rule with a declaration from which we read the value. No rule
212
215
// will be generated and the root will be empty if the path is invalid.
213
216
//
214
217
// Non-CSS representable values are not a concern here because the validation
215
218
// only happens for calls in a CSS context.
216
- let [ root ] = state . designSystem . compile ( [ `[--custom:theme( ${ path } )]` ] )
219
+ let [ root ] = design . compile ( [ candidate ] )
217
220
218
221
let value : string | null = null
219
222
220
223
root . walkDecls ( ( decl ) => {
221
224
value = decl . value
222
225
} )
223
226
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 ) {
225
242
return { isValid : true , value }
226
243
}
227
244
@@ -251,14 +268,10 @@ function suggestAlternativeThemeKeys(state: State, path: string): string[] {
251
268
// exposed in any v4 API
252
269
if ( ! path . startsWith ( '--' ) ) return [ ]
253
270
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
-
258
271
let parts = path . slice ( 2 ) . split ( '-' )
259
272
parts [ 0 ] = `--${ parts [ 0 ] } `
260
273
261
- let validThemeKeys = Array . from ( state . designSystem . theme . entries ( ) , ( [ key ] ) => key )
274
+ let validThemeKeys = resolveKnownThemeKeys ( state . designSystem )
262
275
let potentialThemeKey : string | null = null
263
276
264
277
while ( parts . length > 1 ) {
0 commit comments