@@ -19,6 +19,10 @@ async function runTest(
1919 expect ( allVars . get ( '--carousel-bg' ) . symbol . value ) . toEqual (
2020 'var(--main-bg-color)'
2121 ) ;
22+ // After resolution, --carousel-bg should have a color property
23+ expect ( allVars . get ( '--carousel-bg' ) . color ) . toBeDefined ( ) ;
24+ expect ( allVars . get ( '--carousel-bg' ) . color ) . toEqual ( allVars . get ( '--main-bg-color' ) . color ) ;
25+
2226 expect ( allVars . get ( '--child-main-bg-color' ) . symbol . value ) . toEqual ( 'brown' ) ;
2327 expect ( allVars . get ( '--child-h1' ) . symbol . value ) . toEqual ( '26px' ) ;
2428 expect ( allVars . get ( '--child-h2' ) . symbol . value ) . toEqual ( '22px' ) ;
@@ -27,6 +31,9 @@ async function runTest(
2731 expect ( allVars . get ( '--child-carousel-bg' ) . symbol . value ) . toEqual (
2832 'var(--main-bg-color)'
2933 ) ;
34+ // After resolution, --child-carousel-bg should have a color property
35+ expect ( allVars . get ( '--child-carousel-bg' ) . color ) . toBeDefined ( ) ;
36+ expect ( allVars . get ( '--child-carousel-bg' ) . color ) . toEqual ( allVars . get ( '--main-bg-color' ) . color ) ;
3037
3138 if ( typeof additionalChecks === 'function' ) {
3239 await additionalChecks ( allVars ) ;
@@ -108,4 +115,93 @@ describe('CSS Variable Manager', () => {
108115 expect ( h1Var . symbol . value ) . toEqual ( '26px' ) ;
109116 expect ( h1Var . color ) . toBeUndefined ( ) ;
110117 } ) ;
118+
119+ test ( 'can resolve nested variable references and detect colors' , async ( ) => {
120+ const cssManager = new CSSVariableManager ( ) ;
121+
122+ await cssManager . parseAndSyncVariables ( [
123+ path . join ( __dirname , '../fixtures/nested-var-resolution' ) ,
124+ ] ) ;
125+
126+ const allVars = cssManager . getAll ( ) ;
127+
128+ // Test direct color values
129+ expect ( allVars . get ( '--color-red' ) . symbol . value ) . toEqual ( 'rgb(255, 0, 0)' ) ;
130+ expect ( allVars . get ( '--color-red' ) . color ) . toBeDefined ( ) ;
131+
132+ expect ( allVars . get ( '--color-blue' ) . symbol . value ) . toEqual ( '#0000ff' ) ;
133+ expect ( allVars . get ( '--color-blue' ) . color ) . toBeDefined ( ) ;
134+
135+ expect ( allVars . get ( '--color-green' ) . symbol . value ) . toEqual ( 'hsl(120, 100%, 50%)' ) ;
136+ expect ( allVars . get ( '--color-green' ) . color ) . toBeDefined ( ) ;
137+
138+ // Test single-level nesting - value is still var() but should have color property
139+ expect ( allVars . get ( '--color-red-alias' ) . symbol . value ) . toEqual ( 'var(--color-red)' ) ;
140+ expect ( allVars . get ( '--color-red-alias' ) . color ) . toBeDefined ( ) ;
141+ expect ( allVars . get ( '--color-red-alias' ) . color ) . toEqual ( allVars . get ( '--color-red' ) . color ) ;
142+
143+ expect ( allVars . get ( '--color-blue-alias' ) . symbol . value ) . toEqual ( 'var(--color-blue)' ) ;
144+ expect ( allVars . get ( '--color-blue-alias' ) . color ) . toBeDefined ( ) ;
145+ expect ( allVars . get ( '--color-blue-alias' ) . color ) . toEqual ( allVars . get ( '--color-blue' ) . color ) ;
146+
147+ // Test two-level nesting
148+ expect ( allVars . get ( '--color-red-alias-2' ) . symbol . value ) . toEqual ( 'var(--color-red-alias)' ) ;
149+ expect ( allVars . get ( '--color-red-alias-2' ) . color ) . toBeDefined ( ) ;
150+ expect ( allVars . get ( '--color-red-alias-2' ) . color ) . toEqual ( allVars . get ( '--color-red' ) . color ) ;
151+
152+ // Test three-level nesting
153+ expect ( allVars . get ( '--color-red-alias-3' ) . symbol . value ) . toEqual ( 'var(--color-red-alias-2)' ) ;
154+ expect ( allVars . get ( '--color-red-alias-3' ) . color ) . toBeDefined ( ) ;
155+ expect ( allVars . get ( '--color-red-alias-3' ) . color ) . toEqual ( allVars . get ( '--color-red' ) . color ) ;
156+
157+ // Test four-level nesting
158+ expect ( allVars . get ( '--color-red-alias-4' ) . symbol . value ) . toEqual ( 'var(--color-red-alias-3)' ) ;
159+ expect ( allVars . get ( '--color-red-alias-4' ) . color ) . toBeDefined ( ) ;
160+ expect ( allVars . get ( '--color-red-alias-4' ) . color ) . toEqual ( allVars . get ( '--color-red' ) . color ) ;
161+
162+ // Test five-level nesting (max depth)
163+ expect ( allVars . get ( '--color-red-alias-5' ) . symbol . value ) . toEqual ( 'var(--color-red-alias-4)' ) ;
164+ expect ( allVars . get ( '--color-red-alias-5' ) . color ) . toBeDefined ( ) ;
165+ expect ( allVars . get ( '--color-red-alias-5' ) . color ) . toEqual ( allVars . get ( '--color-red' ) . color ) ;
166+
167+ // Test six-level nesting (should stop at depth 5, cannot resolve to a color)
168+ expect ( allVars . get ( '--color-red-alias-6' ) . symbol . value ) . toEqual ( 'var(--color-red-alias-5)' ) ;
169+ // At 6 levels, we hit the depth limit and cannot fully resolve, so no color
170+ expect ( allVars . get ( '--color-red-alias-6' ) . color ) . toBeUndefined ( ) ;
171+
172+ // Test fallback values
173+ expect ( allVars . get ( '--undefined-with-fallback' ) . symbol . value ) . toEqual ( 'var(--does-not-exist, #ff00ff)' ) ;
174+ expect ( allVars . get ( '--undefined-with-fallback' ) . color ) . toBeDefined ( ) ;
175+
176+ expect ( allVars . get ( '--nested-with-fallback' ) . symbol . value ) . toEqual ( 'var(--also-undefined, var(--color-green))' ) ;
177+ expect ( allVars . get ( '--nested-with-fallback' ) . color ) . toBeDefined ( ) ;
178+ expect ( allVars . get ( '--nested-with-fallback' ) . color ) . toEqual ( allVars . get ( '--color-green' ) . color ) ;
179+
180+ // Test circular references (should not have color)
181+ expect ( allVars . get ( '--circular-a' ) . symbol . value ) . toEqual ( 'var(--circular-b)' ) ;
182+ expect ( allVars . get ( '--circular-a' ) . color ) . toBeUndefined ( ) ;
183+
184+ expect ( allVars . get ( '--circular-b' ) . symbol . value ) . toEqual ( 'var(--circular-a)' ) ;
185+ expect ( allVars . get ( '--circular-b' ) . color ) . toBeUndefined ( ) ;
186+
187+ // Test non-color variables (should not have color)
188+ expect ( allVars . get ( '--spacing' ) . symbol . value ) . toEqual ( '16px' ) ;
189+ expect ( allVars . get ( '--spacing' ) . color ) . toBeUndefined ( ) ;
190+
191+ expect ( allVars . get ( '--font-size' ) . symbol . value ) . toEqual ( '14px' ) ;
192+ expect ( allVars . get ( '--font-size' ) . color ) . toBeUndefined ( ) ;
193+
194+ // Test cross-file resolution
195+ expect ( allVars . get ( '--child-color-red' ) . symbol . value ) . toEqual ( 'var(--color-red)' ) ;
196+ expect ( allVars . get ( '--child-color-red' ) . color ) . toBeDefined ( ) ;
197+ expect ( allVars . get ( '--child-color-red' ) . color ) . toEqual ( allVars . get ( '--color-red' ) . color ) ;
198+
199+ expect ( allVars . get ( '--child-color-blue-alias' ) . symbol . value ) . toEqual ( 'var(--color-blue-alias)' ) ;
200+ expect ( allVars . get ( '--child-color-blue-alias' ) . color ) . toBeDefined ( ) ;
201+ expect ( allVars . get ( '--child-color-blue-alias' ) . color ) . toEqual ( allVars . get ( '--color-blue' ) . color ) ;
202+
203+ expect ( allVars . get ( '--child-nested' ) . symbol . value ) . toEqual ( 'var(--color-red-alias-3)' ) ;
204+ expect ( allVars . get ( '--child-nested' ) . color ) . toBeDefined ( ) ;
205+ expect ( allVars . get ( '--child-nested' ) . color ) . toEqual ( allVars . get ( '--color-red' ) . color ) ;
206+ } ) ;
111207} ) ;
0 commit comments