Skip to content

Commit e2ff36d

Browse files
committed
more fixes
1 parent a5051a0 commit e2ff36d

File tree

4 files changed

+29
-56
lines changed

4 files changed

+29
-56
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@projectwallace/css-code-quality": "^3.0.2",
2828
"@projectwallace/css-design-tokens": "^0.10.0",
2929
"@projectwallace/css-layer-tree": "^2.0.2",
30-
"@projectwallace/css-parser": "^0.8.1",
30+
"@projectwallace/css-parser": "^0.8.3",
3131
"@projectwallace/format-css": "^2.1.1",
3232
"@sveltejs/enhanced-img": "0.4.4",
3333
"@sveltejs/kit": "^2.49.0",

src/lib/components/custom-property-inspector/analyze-custom-properties.test.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { test, expect } from 'vitest';
2-
import { analyze } from './analyze-custom-properties';
1+
import { test, expect } from 'vitest'
2+
import { analyze } from './analyze-custom-properties'
33

44
test('unused property', () => {
55
let actual = analyze(`
@@ -22,9 +22,7 @@ test('undeclared property', () => {
2222
expect(actual.undeclared_with_fallback.size).toBe(0)
2323
expect(actual.unused.size).toBe(0)
2424
expect(actual.all.size).toBe(1)
25-
expect(actual.all.get('--undeclared')).toEqual([
26-
{ column: 3, length: 25, line: 3, offset: 8 }
27-
])
25+
expect(actual.all.get('--undeclared')).toEqual([{ column: 10, length: 17, line: 3, offset: 15 }])
2826
})
2927

3028
test('undeclared property with fallback', () => {
@@ -36,9 +34,7 @@ test('undeclared property with fallback', () => {
3634
expect(actual.undeclared.size).toBe(0)
3735
expect(actual.unused.size).toBe(0)
3836
expect(actual.all.size).toBe(1)
39-
expect(actual.all.get('--undeclared')).toEqual([
40-
{ column: 3, length: 30, line: 3, offset: 8 }
41-
])
37+
expect(actual.all.get('--undeclared')).toEqual([{ column: 10, length: 22, line: 3, offset: 15 }])
4238
})
4339

4440
test('undeclared property with undefined custom property as fallback', () => {
@@ -50,9 +46,8 @@ test('undeclared property with undefined custom property as fallback', () => {
5046
expect(actual.undeclared.size).toBe(1)
5147
expect(actual.unused.size).toBe(0)
5248
expect(actual.all.size).toBe(2)
53-
expect(actual.all.get('--undeclared')).toEqual([
54-
{ column: 3, length: 46, line: 3, offset: 8 }
55-
])
49+
expect(actual.all.get('--undeclared')).toEqual([{ column: 10, length: 38, line: 3, offset: 15 }])
50+
expect(actual.all.get('--undeclared-2')).toEqual([{ column: 28, length: 19, line: 3, offset: 33 }])
5651
})
5752

5853
test('undeclared property with defined custom property as fallback', () => {
@@ -65,9 +60,7 @@ test('undeclared property with defined custom property as fallback', () => {
6560
expect(actual.undeclared.size).toBe(0)
6661
expect(actual.unused.size).toBe(0)
6762
expect(actual.all.size).toBe(2)
68-
expect(actual.all.get('--undeclared')).toEqual([
69-
{ column: 3, length: 42, line: 4, offset: 25 }
70-
])
63+
expect(actual.all.get('--undeclared')).toEqual([{ column: 10, length: 34, line: 4, offset: 32 }])
7164
})
7265

7366
test('undeclared property with empty fallback', () => {
@@ -79,9 +72,7 @@ test('undeclared property with empty fallback', () => {
7972
expect(actual.undeclared.size).toBe(0)
8073
expect(actual.unused.size).toBe(0)
8174
expect(actual.all.size).toBe(1)
82-
expect(actual.all.get('--undeclared')).toEqual([
83-
{ column: 3, length: 27, line: 3, offset: 8 }
84-
])
75+
expect(actual.all.get('--undeclared')).toEqual([{ column: 10, length: 19, line: 3, offset: 15 }])
8576
})
8677

8778
test('unused @property', () => {
@@ -94,9 +85,7 @@ test('unused @property', () => {
9485
expect(actual.undeclared.size).toBe(0)
9586
expect(actual.undeclared_with_fallback.size).toBe(0)
9687
expect(actual.all.size).toBe(1)
97-
expect(actual.all.get('--unused')).toEqual([
98-
{ column: 2, length: 66, line: 2, offset: 2 }
99-
])
88+
expect(actual.all.get('--unused')).toEqual([{ column: 2, length: 66, line: 2, offset: 2 }])
10089
})
10190

10291
test('used @property', () => {
@@ -114,6 +103,6 @@ test('used @property', () => {
114103
expect(actual.all.size).toBe(1)
115104
expect(actual.all.get('--used')).toEqual([
116105
{ column: 2, length: 64, line: 2, offset: 2 },
117-
{ column: 3, length: 15, line: 7, offset: 74 }
106+
{ column: 6, length: 11, line: 7, offset: 77 }
118107
])
119-
})
108+
})

src/lib/components/custom-property-inspector/analyze-custom-properties.ts

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,27 @@ export function analyze(css: string) {
1818
let all_properties = new Map<string, CssLocation[]>()
1919
let declared_with_fallback = new Set<string>()
2020

21-
// Helper to recursively walk values and find all var() functions
22-
function walk_values(value_node: CSSNode, declaration_node: CSSNode) {
23-
if (value_node.type === FUNCTION && value_node.name === 'var') {
24-
let first_child = value_node.first_child
25-
if (first_child !== null && first_child.type === IDENTIFIER && first_child.name.startsWith('--')) {
26-
let loc = to_loc(declaration_node)
27-
let name = first_child.name
28-
used_properties.add(name)
29-
all_properties.set(name, (all_properties.get(name) ?? []).concat(loc))
30-
31-
// check if it has a fallback value that is a custom property
32-
let children = value_node.children
33-
let second_child = children[1]
34-
if (second_child !== undefined && !(second_child.type === FUNCTION && second_child.name === 'var')) {
35-
declared_with_fallback.add(name)
36-
}
37-
}
38-
}
39-
40-
// Recursively walk children to find nested var() calls
41-
for (let child of value_node.children) {
42-
walk_values(child, declaration_node)
43-
}
44-
}
45-
4621
walk(ast, (node: CSSNode) => {
4722
if (node.type === DECLARATION) {
4823
if (node.property.startsWith('--')) {
49-
let loc = to_loc(node)
5024
let name = node.property
25+
let loc = to_loc(node)
5126
declared_properties.add(name)
5227
all_properties.set(name, (all_properties.get(name) ?? []).concat(loc))
5328
}
29+
} else if (node.type === FUNCTION && node.name === 'var') {
30+
let [first_child, second_child] = node.children
31+
32+
if (first_child?.type === IDENTIFIER && first_child.name.startsWith('--')) {
33+
let loc = to_loc(node)
34+
let name = first_child.name
35+
used_properties.add(name)
36+
all_properties.set(name, (all_properties.get(name) ?? []).concat(loc))
5437

55-
// Check for var() usage in declaration values (recursively)
56-
for (let value of node.values) {
57-
walk_values(value, node)
38+
// check if it has a fallback value that is a custom property
39+
if (second_child !== undefined && !(second_child.type === FUNCTION && second_child.name === 'var')) {
40+
declared_with_fallback.add(name)
41+
}
5842
}
5943
} else if (node.type === AT_RULE && node.name === 'property') {
6044
let name = node.prelude!

0 commit comments

Comments
 (0)