Skip to content

Commit 1e8e48e

Browse files
authored
fix: calculate node.length correctly for nested var() (#70)
1 parent 39ea228 commit 1e8e48e

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/parse-value.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,24 @@ describe('Value Node Types', () => {
130130
expect(value?.line).toBe(3)
131131
expect(value?.column).toBe(10)
132132
})
133+
134+
it('should have correct offset and length for var()', () => {
135+
const value = getValue('div { color: var(--item); }')
136+
expect(value?.start).toBe(13)
137+
expect(value?.length).toBe(11)
138+
expect(value?.end).toBe(24)
139+
expect(value?.line).toBe(1)
140+
expect(value?.column).toBe(14)
141+
})
142+
143+
it('should have correct offset and length for var() with fallback', () => {
144+
const value = getValue('div { color: var(--item1, var(--item2)); }')
145+
expect(value?.start).toBe(13)
146+
expect(value?.length).toBe(26)
147+
expect(value?.end).toBe(39)
148+
expect(value?.line).toBe(1)
149+
expect(value?.column).toBe(14)
150+
})
133151
})
134152

135153
describe('OPERATOR', () => {
@@ -513,6 +531,18 @@ describe('Value Node Types', () => {
513531
expect(func?.has_children).toBe(true)
514532
})
515533

534+
it('should provide node.value for var() function with fallback', () => {
535+
const root = parse('body { color: var(--primary-color, 1); }')
536+
const decl = root.first_child?.first_child?.next_sibling?.first_child
537+
const func = decl?.values[0]
538+
539+
expect(func?.type).toBe(FUNCTION)
540+
expect(func?.name).toBe('var')
541+
expect(func?.text).toBe('var(--primary-color, 1)')
542+
expect(func?.value).toBe('--primary-color, 1')
543+
expect(func?.has_children).toBe(true)
544+
})
545+
516546
it('should parse transform value', () => {
517547
const root = parse('body { transform: translateX(10px) rotate(45deg); }')
518548
const decl = root.first_child?.first_child?.next_sibling?.first_child

src/parse-value.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ export class ValueParser {
247247
if (token_type === TOKEN_EOF) break
248248
if (this.lexer.token_start >= this.value_end) break
249249

250-
// Track parentheses depth
251-
if (token_type === TOKEN_LEFT_PAREN || token_type === TOKEN_FUNCTION) {
252-
paren_depth++
253-
} else if (token_type === TOKEN_RIGHT_PAREN) {
250+
// Check for closing paren
251+
// Note: We don't track paren_depth for TOKEN_LEFT_PAREN or TOKEN_FUNCTION here
252+
// because parse_value_node() will recursively handle them
253+
if (token_type === TOKEN_RIGHT_PAREN) {
254254
paren_depth--
255255
if (paren_depth === 0) {
256256
content_end = this.lexer.token_start // Position of ')'

0 commit comments

Comments
 (0)