Skip to content

Commit c2b58fe

Browse files
bartvenemanBart Veneman
andauthored
feat: add list of css properties used per color (#24)
Co-authored-by: Bart Veneman <[email protected]>
1 parent a2e5e97 commit c2b58fe

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/index.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test, expect, describe } from 'vitest'
22
import { analysis_to_tokens, css_to_tokens } from './index.js'
3-
import { EXTENSION_AUTHORED_AS, EXTENSION_USAGE_COUNT } from './types.js'
3+
import { EXTENSION_AUTHORED_AS, EXTENSION_CSS_PROPERTIES, EXTENSION_USAGE_COUNT } from './types.js'
44
import { analyze } from '@projectwallace/css-analyzer'
55

66
describe('analysis_to_tokens', () => {
@@ -27,6 +27,7 @@ describe('analysis_to_tokens', () => {
2727
$extensions: {
2828
[EXTENSION_AUTHORED_AS]: 'green',
2929
[EXTENSION_USAGE_COUNT]: 1,
30+
[EXTENSION_CSS_PROPERTIES]: ['color']
3031
}
3132
},
3233
},
@@ -79,7 +80,8 @@ describe('css_to_tokens', () => {
7980
let actual = css_to_tokens(`
8081
.my-design-system {
8182
color: green;
82-
color: rgb(100 100 100 / 20%);
83+
background-color: rgb(100 100 100 / 20%);
84+
border-color: green;
8385
}
8486
`)
8587
expect(actual.color).toEqual({
@@ -92,7 +94,8 @@ describe('css_to_tokens', () => {
9294
},
9395
$extensions: {
9496
[EXTENSION_AUTHORED_AS]: 'green',
95-
[EXTENSION_USAGE_COUNT]: 1,
97+
[EXTENSION_USAGE_COUNT]: 2,
98+
[EXTENSION_CSS_PROPERTIES]: ['color', 'border-color'],
9699
}
97100
},
98101
'grey-8139d9b': {
@@ -105,6 +108,7 @@ describe('css_to_tokens', () => {
105108
$extensions: {
106109
[EXTENSION_AUTHORED_AS]: 'rgb(100 100 100 / 20%)',
107110
[EXTENSION_USAGE_COUNT]: 1,
111+
[EXTENSION_CSS_PROPERTIES]: ['background-color'],
108112
},
109113
}
110114
})
@@ -136,6 +140,7 @@ describe('css_to_tokens', () => {
136140
$extensions: {
137141
[EXTENSION_AUTHORED_AS]: 'transparent',
138142
[EXTENSION_USAGE_COUNT]: 1,
143+
[EXTENSION_CSS_PROPERTIES]: ['color'],
139144
}
140145
},
141146
})

src/index.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { destructure_line_height } from './destructure-line-height.js'
99
import { parse_length } from './parse-length.js'
1010
import {
1111
EXTENSION_AUTHORED_AS,
12+
EXTENSION_USAGE_COUNT,
13+
EXTENSION_CSS_PROPERTIES,
1214
type CubicBezierToken,
1315
type DimensionToken,
1416
type DurationToken,
@@ -18,7 +20,6 @@ import {
1820
type ColorToken,
1921
type CssAnalysis,
2022
type ShadowToken,
21-
EXTENSION_USAGE_COUNT,
2223
} from './types.js'
2324
import { color_to_token } from './colors.js'
2425

@@ -41,6 +42,11 @@ type Collection = {
4142
} | {
4243
uniqueWithLocations: UniqueWithLocations
4344
}
45+
type ItemsPerContext = Record<string, {
46+
unique: Record<string, number>,
47+
uniqueWithLocations?: UniqueWithLocations,
48+
}>
49+
4450
type TokenID = string
4551

4652
type Tokens = {
@@ -66,6 +72,10 @@ function get_unique(collection: Collection) {
6672
return collection.unique
6773
}
6874

75+
/**
76+
* Function to get the count of a specific value from a collection item regardless of whether the
77+
* analysis was run with locations enabled or not.
78+
*/
6979
function get_count(collection_item: number | CssLocation[]) {
7080
if (Array.isArray(collection_item)) {
7181
return collection_item.length
@@ -83,14 +93,25 @@ export function analysis_to_tokens(analysis: CssAnalysis): Tokens {
8393
for (let [group, group_colors] of color_groups) {
8494
for (let color of group_colors) {
8595
let color_token = color_to_token(color)
96+
8697
if (color_token !== null) {
8798
let name = `${color_dict.get(group)}-${hash(color)}`
99+
100+
let items_per_context = analysis.values.colors.itemsPerContext as ItemsPerContext
101+
let properties = Object.entries(items_per_context).reduce((acc, [property, collection]) => {
102+
if (color in collection.unique || (collection.uniqueWithLocations && color in collection.uniqueWithLocations)) {
103+
acc.push(property)
104+
}
105+
return acc
106+
}, [] as Array<string>)
107+
88108
colors[name] = {
89109
$type: 'color',
90110
$value: color_token,
91111
$extensions: {
92112
[EXTENSION_AUTHORED_AS]: color,
93-
[EXTENSION_USAGE_COUNT]: get_count(unique[color]!)
113+
[EXTENSION_USAGE_COUNT]: get_count(unique[color]!),
114+
[EXTENSION_CSS_PROPERTIES]: properties,
94115
}
95116
}
96117
}

src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type CssAnalysis = ReturnType<typeof analyze>
55

66
export const EXTENSION_AUTHORED_AS = 'com.projectwallace.css-authored-as'
77
export const EXTENSION_USAGE_COUNT = 'com.projectwallace.usage-count'
8+
export const EXTENSION_CSS_PROPERTIES = 'com.projectwallace.css-properties'
89

910
export type Easing = [number, number, number, number]
1011

@@ -43,7 +44,10 @@ export type ColorValue = {
4344

4445
export type ColorToken = BaseToken & {
4546
$type: 'color'
46-
$value: ColorValue
47+
$value: ColorValue,
48+
$extensions: {
49+
[EXTENSION_CSS_PROPERTIES]: Array<string>
50+
}
4751
}
4852

4953
export type DimensionValue = {

0 commit comments

Comments
 (0)