Skip to content

Commit e478a00

Browse files
authored
Seperate walking Values from Declarations (#229)
1 parent 10a9e22 commit e478a00

File tree

2 files changed

+111
-39
lines changed

2 files changed

+111
-39
lines changed

src/index.js

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -222,69 +222,43 @@ const analyze = (css) => {
222222
}
223223
break
224224
}
225-
case 'Declaration': {
226-
totalDeclarations++
227-
228-
const declaration = stringifyNode(node)
229-
if (declarationsCache[declaration]) {
230-
declarationsCache[declaration]++
231-
} else {
232-
declarationsCache[declaration] = 1
233-
}
234-
235-
if (node.important) {
236-
importantDeclarations++
237-
238-
if (this.atrule && endsWith('keyframes', this.atrule.name)) {
239-
importantsInKeyframes++
240-
}
241-
}
225+
case 'Value': {
226+
values.push(node)
242227

243-
const { value, property } = node
244-
245-
properties.push(property)
246-
values.push(value)
247-
248-
if (hasVendorPrefix(property)) {
249-
propertyVendorPrefixes.push(property)
250-
} else if (isHack(property)) {
251-
propertyHacks.push(property)
252-
} else if (isCustom(property)) {
253-
customProperties.push(property)
254-
}
228+
const property = this.declaration.property
255229

256230
// Process properties first that don't have colors,
257231
// so we can avoid further walking them;
258232
if (isProperty('z-index', property)) {
259-
zindex.push(value)
233+
zindex.push(node)
260234
return this.skip
261235
} else if (isProperty('font', property)) {
262-
fontValues.push(value)
236+
fontValues.push(node)
263237
break
264238
} else if (isProperty('font-size', property)) {
265-
fontSizeValues.push(stringifyNode(value))
239+
fontSizeValues.push(stringifyNode(node))
266240
break
267241
} else if (isProperty('font-family', property)) {
268-
fontFamilyValues.push(stringifyNode(value))
242+
fontFamilyValues.push(stringifyNode(node))
269243
break
270244
} else if (isProperty('transition', property) || isProperty('animation', property)) {
271-
animations.push(value.children)
245+
animations.push(node.children)
272246
break
273247
} else if (isProperty('animation-duration', property) || isProperty('transition-duration', property)) {
274-
durations.push(stringifyNode(value))
248+
durations.push(stringifyNode(node))
275249
break
276250
} else if (isProperty('transition-timing-function', property) || isProperty('animation-timing-function', property)) {
277-
timingFunctions.push(stringifyNode(value))
251+
timingFunctions.push(stringifyNode(node))
278252
break
279253
} else if (isProperty('text-shadow', property)) {
280-
textShadows.push(value)
254+
textShadows.push(node)
281255
// no break here: potentially contains colors
282256
} else if (isProperty('box-shadow', property)) {
283-
boxShadows.push(value)
257+
boxShadows.push(node)
284258
// no break here: potentially contains colors
285259
}
286260

287-
walk(value, function (valueNode) {
261+
walk(node, function (valueNode) {
288262
switch (valueNode.type) {
289263
case 'Hash': {
290264
colors.push('#' + valueNode.value, property)
@@ -305,6 +279,10 @@ const analyze = (css) => {
305279
return this.skip
306280
}
307281
case 'Function': {
282+
// Don't walk var() multiple times
283+
if (strEquals('var', valueNode.name)) {
284+
return this.skip
285+
}
308286
if (colorFunctions[valueNode.name.toLowerCase()]) {
309287
colors.push(stringifyNode(valueNode), property)
310288
}
@@ -313,6 +291,38 @@ const analyze = (css) => {
313291
}
314292
}
315293
})
294+
break
295+
}
296+
case 'Declaration': {
297+
totalDeclarations++
298+
299+
const declaration = stringifyNode(node)
300+
if (declarationsCache[declaration]) {
301+
declarationsCache[declaration]++
302+
} else {
303+
declarationsCache[declaration] = 1
304+
}
305+
306+
if (node.important) {
307+
importantDeclarations++
308+
309+
if (this.atrule && endsWith('keyframes', this.atrule.name)) {
310+
importantsInKeyframes++
311+
}
312+
}
313+
314+
const { property } = node
315+
316+
properties.push(property)
317+
318+
if (hasVendorPrefix(property)) {
319+
propertyVendorPrefixes.push(property)
320+
} else if (isHack(property)) {
321+
propertyHacks.push(property)
322+
} else if (isCustom(property)) {
323+
customProperties.push(property)
324+
}
325+
break
316326
}
317327
}
318328
})

src/values/colors.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,68 @@ Colors.skip('ignores color names that are not actual colors', () => {
695695
assert.equal(actual, expected)
696696
})
697697

698+
Colors('finds colors in var() as fallback values', () => {
699+
const fixture = `
700+
test {
701+
--main: #aaa;
702+
color: var(--main, #eee);
703+
704+
/* from github */
705+
box-shadow: 0 3px 12px var(--color-fade-black-15), 0 0 1px rgba(27, 31, 35, .2);
706+
text-shadow: 0 0 var(--main, var(--sec, #000));
707+
}
708+
`
709+
const result = analyze(fixture)
710+
const actual = result.values.colors
711+
const expected = {
712+
total: 4,
713+
totalUnique: 4,
714+
unique: {
715+
'#aaa': 1,
716+
'#eee': 1,
717+
'rgba(27, 31, 35, .2)': 1,
718+
'#000': 1,
719+
},
720+
uniquenessRatio: 4 / 4,
721+
itemsPerContext: {
722+
'--main': {
723+
total: 1,
724+
totalUnique: 1,
725+
unique: {
726+
'#aaa': 1,
727+
},
728+
uniquenessRatio: 1,
729+
},
730+
'color': {
731+
total: 1,
732+
totalUnique: 1,
733+
unique: {
734+
'#eee': 1,
735+
},
736+
uniquenessRatio: 1,
737+
},
738+
'box-shadow': {
739+
total: 1,
740+
totalUnique: 1,
741+
unique: {
742+
'rgba(27, 31, 35, .2)': 1,
743+
},
744+
uniquenessRatio: 1,
745+
},
746+
'text-shadow': {
747+
total: 1,
748+
totalUnique: 1,
749+
unique: {
750+
'#000': 1,
751+
},
752+
uniquenessRatio: 1,
753+
},
754+
},
755+
}
756+
757+
assert.equal(actual, expected)
758+
})
759+
698760
Colors('ignores CSS keywords', () => {
699761
const fixture = `
700762
testColorKeywords {

0 commit comments

Comments
 (0)