@atlaskit/tokens 1.36.0 → 1.37.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/codemods/css-to-design-tokens/__tests__/css-to-design-tokens.test.tsx +489 -0
  3. package/codemods/css-to-design-tokens/__tests__/utils.test.tsx +145 -0
  4. package/codemods/css-to-design-tokens/lib/colors.tsx +71 -0
  5. package/codemods/css-to-design-tokens/lib/declaration.tsx +43 -0
  6. package/codemods/css-to-design-tokens/lib/legacy-colors.tsx +336 -0
  7. package/codemods/css-to-design-tokens/lib/meta.tsx +173 -0
  8. package/codemods/css-to-design-tokens/lib/tokens.tsx +54 -0
  9. package/codemods/css-to-design-tokens/lib/value.tsx +85 -0
  10. package/codemods/css-to-design-tokens/transform.tsx +99 -0
  11. package/codemods/hypermod.config.tsx +9 -0
  12. package/codemods/theme-to-design-tokens/__tests__/theme-to-design-tokens.test.tsx +1104 -0
  13. package/codemods/theme-to-design-tokens/transform.tsx +628 -0
  14. package/codemods/theme-to-design-tokens/utils/ast-meta.tsx +159 -0
  15. package/codemods/theme-to-design-tokens/utils/ast.tsx +46 -0
  16. package/codemods/theme-to-design-tokens/utils/color.tsx +45 -0
  17. package/codemods/theme-to-design-tokens/utils/css-utils.tsx +38 -0
  18. package/codemods/theme-to-design-tokens/utils/fuzzy-search.tsx +326 -0
  19. package/codemods/theme-to-design-tokens/utils/legacy-colors.tsx +232 -0
  20. package/codemods/theme-to-design-tokens/utils/named-colors.tsx +150 -0
  21. package/codemods/theme-to-design-tokens/utils/string-utils.tsx +22 -0
  22. package/codemods/utils/tokens.tsx +376 -0
  23. package/dist/cjs/artifacts/palettes-raw/typography-palette.js +3 -3
  24. package/dist/cjs/artifacts/themes/atlassian-typography-adg3.js +2 -2
  25. package/dist/cjs/artifacts/themes/atlassian-typography-minor3.js +2 -2
  26. package/dist/cjs/artifacts/token-default-values.js +15 -15
  27. package/dist/cjs/artifacts/tokens-raw/atlassian-typography-adg3.js +15 -15
  28. package/dist/cjs/artifacts/tokens-raw/atlassian-typography-minor3.js +16 -16
  29. package/dist/cjs/get-token-value.js +1 -1
  30. package/dist/cjs/get-token.js +1 -1
  31. package/dist/es2019/artifacts/palettes-raw/typography-palette.js +3 -3
  32. package/dist/es2019/artifacts/themes/atlassian-typography-adg3.js +15 -15
  33. package/dist/es2019/artifacts/themes/atlassian-typography-minor3.js +20 -20
  34. package/dist/es2019/artifacts/token-default-values.js +15 -15
  35. package/dist/es2019/artifacts/tokens-raw/atlassian-typography-adg3.js +15 -15
  36. package/dist/es2019/artifacts/tokens-raw/atlassian-typography-minor3.js +16 -16
  37. package/dist/es2019/get-token-value.js +1 -1
  38. package/dist/es2019/get-token.js +1 -1
  39. package/dist/esm/artifacts/palettes-raw/typography-palette.js +3 -3
  40. package/dist/esm/artifacts/themes/atlassian-typography-adg3.js +2 -2
  41. package/dist/esm/artifacts/themes/atlassian-typography-minor3.js +2 -2
  42. package/dist/esm/artifacts/token-default-values.js +15 -15
  43. package/dist/esm/artifacts/tokens-raw/atlassian-typography-adg3.js +15 -15
  44. package/dist/esm/artifacts/tokens-raw/atlassian-typography-minor3.js +16 -16
  45. package/dist/esm/get-token-value.js +1 -1
  46. package/dist/esm/get-token.js +1 -1
  47. package/dist/types/artifacts/palettes-raw/typography-palette.d.ts +1 -1
  48. package/dist/types/artifacts/themes/atlassian-typography-adg3.d.ts +2 -2
  49. package/dist/types/artifacts/themes/atlassian-typography-minor3.d.ts +2 -2
  50. package/dist/types/artifacts/token-default-values.d.ts +15 -15
  51. package/dist/types/artifacts/tokens-raw/atlassian-typography-adg3.d.ts +1 -1
  52. package/dist/types/artifacts/tokens-raw/atlassian-typography-minor3.d.ts +1 -1
  53. package/dist/types-ts4.5/artifacts/palettes-raw/typography-palette.d.ts +1 -1
  54. package/dist/types-ts4.5/artifacts/themes/atlassian-typography-adg3.d.ts +2 -2
  55. package/dist/types-ts4.5/artifacts/themes/atlassian-typography-minor3.d.ts +2 -2
  56. package/dist/types-ts4.5/artifacts/token-default-values.d.ts +15 -15
  57. package/dist/types-ts4.5/artifacts/tokens-raw/atlassian-typography-adg3.d.ts +1 -1
  58. package/dist/types-ts4.5/artifacts/tokens-raw/atlassian-typography-minor3.d.ts +1 -1
  59. package/package.json +5 -1
@@ -0,0 +1,71 @@
1
+ import { knownNamedColors, knownVariables } from './legacy-colors';
2
+
3
+ const NAMED_COLORS = Object.keys(knownNamedColors);
4
+ const GRADIENT_TYPES = ['linear', 'radial', 'conic'] as const;
5
+ const LESS_COLOR_FUNCTIONS = [
6
+ 'lighten',
7
+ 'darken',
8
+ 'saturate',
9
+ 'desaturate',
10
+ 'fadein',
11
+ 'fadeout',
12
+ 'fade',
13
+ 'spin',
14
+ 'mix',
15
+ 'greyscale',
16
+ 'contrast',
17
+ 'multiply',
18
+ 'screen',
19
+ 'overlay',
20
+ 'softlight',
21
+ 'hardlight',
22
+ 'difference',
23
+ 'exclusion',
24
+ 'average',
25
+ 'negation',
26
+ 'tint',
27
+ 'shade',
28
+ 'luma',
29
+ 'hue',
30
+ 'saturation',
31
+ 'lightness',
32
+ 'alpha',
33
+ 'red',
34
+ 'green',
35
+ 'blue',
36
+ ];
37
+
38
+ const REGEXES = {
39
+ // The CSS regular expression matches CSS variable declarations.
40
+ // It looks for the string "var(" followed by any characters except a closing parenthesis, and ending with a closing parenthesis.
41
+ CSS: /var\([^\)]+\)/g,
42
+ // The RAW_COLOR regular expression matches various CSS color formats including hexadecimal, RGB(A), HSL(A), LAB, LCH, and HWB.
43
+ // It allows for optional leading and trailing white spaces.
44
+ // For RGBA and HSLA, it allows any number (including negative numbers and numbers greater than 1) for the alpha channel.
45
+ // For RGB, HSL, LAB, LCH, and HWB, it expects three comma-separated values.
46
+ // It also allows optional white spaces around the commas and the values.
47
+ RAW_COLOR:
48
+ /^\s*(#([0-9a-f]{3}){1,2}|(rgba|hsla)\(\s*\d{1,3}%?\s*(,\s*\d{1,3}%?\s*){2},\s*-?\d*\.?\d+\s*\)|(rgb|hsl)\(\s*\d{1,3}%?\s*(,\s*\d{1,3}%?\s*){2}\)\s*|(lab|lch)\(\s*\d{1,3}%?\s+\d{1,3}%?\s+\d{1,3}%?\s*\)|hwb\(\s*\d{1,3}\s+\d{1,3}%?\s+\d{1,3}%?\s*\))\s*$/i,
49
+ };
50
+
51
+ export function isKnownCssVariable(value: string) {
52
+ return value in knownVariables;
53
+ }
54
+ export function isRawColor(value: string) {
55
+ return REGEXES.RAW_COLOR.test(value);
56
+ }
57
+ export function isNamedColor(value: string) {
58
+ return NAMED_COLORS.includes(value);
59
+ }
60
+ export function isGradient(value: string) {
61
+ return GRADIENT_TYPES.some((gradient) =>
62
+ value.startsWith(`${gradient}-gradient(`),
63
+ );
64
+ }
65
+ export function extractBetweenParentheses(value: string) {
66
+ const match = value.match(/\((.*?)\)/);
67
+ return match ? match[1] : '';
68
+ }
69
+ export function isLessFunction(value: string) {
70
+ return LESS_COLOR_FUNCTIONS.some((func) => value.startsWith(`${func}(`));
71
+ }
@@ -0,0 +1,43 @@
1
+ const COLOR_PROPERTIES = [
2
+ 'color',
3
+ 'background',
4
+ 'background-color',
5
+ 'box-shadow',
6
+ 'border',
7
+ 'border-left',
8
+ 'border-right',
9
+ 'border-top',
10
+ 'border-bottom',
11
+ 'border-color',
12
+ 'border-left-color',
13
+ 'border-right-color',
14
+ 'border-top-color',
15
+ 'border-bottom-color',
16
+ 'outline',
17
+ 'outline-color',
18
+ 'accent-color',
19
+ 'caret-color',
20
+ 'scrollbar-color',
21
+ 'text-stroke',
22
+ ] as const;
23
+
24
+ export function isColorRelatedProperty(prop: string) {
25
+ return COLOR_PROPERTIES.some((property) => property === prop);
26
+ }
27
+
28
+ export function isCssDeclaration(prop: string) {
29
+ return prop.startsWith('--');
30
+ }
31
+
32
+ export function extractCssVarName(prop: string) {
33
+ return prop.substring(prop.indexOf('(') + 1).split(/\,|\)/)[0];
34
+ }
35
+
36
+ export function extractLessVarName(prop: string) {
37
+ return prop.substring(1);
38
+ }
39
+
40
+ export function splitCssValue(value: string) {
41
+ const regex = /(?:[^\s()]+|\((?:[^()]+|\([^()]*\))*\))+/g;
42
+ return value.match(regex);
43
+ }
@@ -0,0 +1,336 @@
1
+ export const knownVariables: Record<string, string[]> = {
2
+ '--adg3-color-R50': ['danger'],
3
+ '--adg3-color-R75': ['danger'],
4
+ '--adg3-color-R100': ['danger'],
5
+ '--adg3-color-R200': ['danger'],
6
+ '--adg3-color-R300': ['danger'],
7
+ '--adg3-color-R400': ['danger'],
8
+ '--adg3-color-R500': ['danger'],
9
+ '--adg3-color-Y50': ['warning'],
10
+ '--adg3-color-Y75': ['warning'],
11
+ '--adg3-color-Y100': ['warning'],
12
+ '--adg3-color-Y200': ['warning'],
13
+ '--adg3-color-Y300': ['warning'],
14
+ '--adg3-color-Y400': ['warning'],
15
+ '--adg3-color-Y500': ['warning'],
16
+ '--adg3-color-G50': ['success'],
17
+ '--adg3-color-G75': ['success'],
18
+ '--adg3-color-G100': ['success'],
19
+ '--adg3-color-G200': ['success'],
20
+ '--adg3-color-G300': ['success'],
21
+ '--adg3-color-G400': ['success'],
22
+ '--adg3-color-G500': ['success'],
23
+ '--adg3-color-B50': ['brand'],
24
+ '--adg3-color-B75': ['brand'],
25
+ '--adg3-color-B100': ['brand'],
26
+ '--adg3-color-B200': ['brand'],
27
+ '--adg3-color-B300': ['brand'],
28
+ '--adg3-color-B400': ['brand'],
29
+ '--adg3-color-B500': ['brand'],
30
+ '--adg3-color-P50': ['discovery'],
31
+ '--adg3-color-P75': ['discovery'],
32
+ '--adg3-color-P100': ['discovery'],
33
+ '--adg3-color-P200': ['discovery'],
34
+ '--adg3-color-P300': ['discovery'],
35
+ '--adg3-color-P400': ['discovery'],
36
+ '--adg3-color-P500': ['discovery'],
37
+ '--adg3-color-T50': ['accent', 'teal'],
38
+ '--adg3-color-T75': ['accent', 'teal'],
39
+ '--adg3-color-T100': ['accent', 'teal'],
40
+ '--adg3-color-T200': ['accent', 'teal'],
41
+ '--adg3-color-T300': ['accent', 'teal'],
42
+ '--adg3-color-T400': ['accent', 'teal'],
43
+ '--adg3-color-T500': ['accent', 'teal'],
44
+ '--adg3-color-N0': ['inverse'],
45
+ '--adg3-color-N10': [],
46
+ '--adg3-color-N20': [],
47
+ '--adg3-color-N30': [],
48
+ '--adg3-color-N40': [],
49
+ '--adg3-color-N50': [],
50
+ '--adg3-color-N60': [],
51
+ '--adg3-color-N70': [],
52
+ '--adg3-color-N80': [],
53
+ '--adg3-color-N90': [],
54
+ '--adg3-color-N100': [],
55
+ '--adg3-color-N200': ['text', 'subtlest'],
56
+ '--adg3-color-N300': [],
57
+ '--adg3-color-N400': ['text', 'subtle'],
58
+ '--adg3-color-N500': [],
59
+ '--adg3-color-N600': [],
60
+ '--adg3-color-N700': ['text'],
61
+ '--adg3-color-N800': ['text'],
62
+ '--adg3-color-N900': ['text'],
63
+ '--adg3-color-N10A': [],
64
+ '--adg3-color-N20A': [],
65
+ '--adg3-color-N30A': [],
66
+ '--adg3-color-N40A': [],
67
+ '--adg3-color-N50A': [],
68
+ '--adg3-color-N60A': [],
69
+ '--adg3-color-N70A': [],
70
+ '--adg3-color-N80A': [],
71
+ '--adg3-color-N90A': [],
72
+ '--adg3-color-N100A': [],
73
+ '--adg3-color-N200A': [],
74
+ '--adg3-color-N300A': [],
75
+ '--adg3-color-N400A': [],
76
+ '--adg3-color-N500A': [],
77
+ '--adg3-color-N600A': [],
78
+ '--adg3-color-N700A': [],
79
+ '--adg3-color-N800A': [],
80
+ '--adg3-color-DN900': [],
81
+ '--adg3-color-DN800': [],
82
+ '--adg3-color-DN700': [],
83
+ '--adg3-color-DN600': [],
84
+ '--adg3-color-DN500': [],
85
+ '--adg3-color-DN400': [],
86
+ '--adg3-color-DN300': [],
87
+ '--adg3-color-DN200': [],
88
+ '--adg3-color-DN100': [],
89
+ '--adg3-color-DN90': [],
90
+ '--adg3-color-DN80': [],
91
+ '--adg3-color-DN70': [],
92
+ '--adg3-color-DN60': [],
93
+ '--adg3-color-DN50': [],
94
+ '--adg3-color-DN40': [],
95
+ '--adg3-color-DN30': [],
96
+ '--adg3-color-DN20': [],
97
+ '--adg3-color-DN10': [],
98
+ '--adg3-color-DN0': [],
99
+ '--adg3-color-DN800A': [],
100
+ '--adg3-color-DN700A': [],
101
+ '--adg3-color-DN600A': [],
102
+ '--adg3-color-DN500A': [],
103
+ '--adg3-color-DN400A': [],
104
+ '--adg3-color-DN300A': [],
105
+ '--adg3-color-DN200A': [],
106
+ '--adg3-color-DN100A': [],
107
+ '--adg3-color-DN90A': [],
108
+ '--adg3-color-DN80A': [],
109
+ '--adg3-color-DN70A': [],
110
+ '--adg3-color-DN60A': [],
111
+ '--adg3-color-DN50A': [],
112
+ '--adg3-color-DN40A': [],
113
+ '--adg3-color-DN30A': [],
114
+ '--adg3-color-DN20A': [],
115
+ '--adg3-color-DN10A': [],
116
+
117
+ '--adg3-color-background-light': ['elevation', 'surface'],
118
+ '--adg3-color-background-dark': ['elevation', 'surface'],
119
+ '--adg3-color-text-light': ['text'],
120
+ '--adg3-color-text-dark': ['text'],
121
+ '--adg3-color-subtleText-light': ['text', 'subtle'],
122
+ '--adg3-color-subtleText-dark': ['text', 'subtle'],
123
+ '--adg3-color-placeholderText-light': ['text', 'subtlest'],
124
+ '--adg3-color-placeholderText-dark': ['text', 'subtlest'],
125
+ '--adg3-color-heading-light': ['text'],
126
+ '--adg3-color-heading-dark': ['text'],
127
+ '--adg3-color-subtleHeading-light': ['text', 'subtle'],
128
+ '--adg3-color-subtleHeading-dark': ['text', 'subtle'],
129
+ '--adg3-color-codeBlock-light': ['elevation', 'surface', 'sunken'],
130
+ '--adg3-color-codeBlock-dark': ['elevation', 'surface', 'sunken'],
131
+ '--adg3-color-link-light': ['link'],
132
+ '--adg3-color-link-dark': ['link'],
133
+ '--adg3-color-linkHover-light': ['link'],
134
+ '--adg3-color-linkHover-dark': ['link'],
135
+ '--adg3-color-linkActive-light': ['link', 'pressed'],
136
+ '--adg3-color-linkActive-dark': ['link', 'pressed'],
137
+ '--adg3-color-linkOutline-light': ['link', 'focused'],
138
+ '--adg3-color-linkOutline-dark': ['link', 'focused'],
139
+ '--adg3-color-primary-light': ['brand'],
140
+ '--adg3-color-primary-dark': ['brand'],
141
+ '--adg3-color-blue-light': ['accent', 'subtler', 'blue'],
142
+ '--adg3-color-blue-dark': ['accent', 'bolder', 'blue'],
143
+ '--adg3-color-teal-light': ['accent', 'subtler', 'teal'],
144
+ '--adg3-color-teal-dark': ['accent', 'bolder', 'teal'],
145
+ '--adg3-color-purple-light': ['accent', 'subtler', 'purple'],
146
+ '--adg3-color-purple-dark': ['accent', 'bolder', 'purple'],
147
+ '--adg3-color-red-light': ['accent', 'subtler', 'red'],
148
+ '--adg3-color-red-dark': ['accent', 'bolder', 'red'],
149
+ '--adg3-color-yellow-light': ['accent', 'subtler', 'yellow'],
150
+ '--adg3-color-yellow-dark': ['accent', 'bolder', 'yellow'],
151
+ '--adg3-color-green-light': ['accent', 'subtler', 'green'],
152
+ '--adg3-color-green-dark': ['accent', 'bolder', 'green'],
153
+ '--adg3-color-N20-transparent': [],
154
+
155
+ /* Alias variables */
156
+ '--jpo-theme-color': ['brand'],
157
+ '--jpo-text-default-color': ['text'],
158
+ '--jpo-text-secondary-color': ['text', 'subtle'],
159
+ '--jpo-text-muted-color': ['text', 'disabled'],
160
+ '--jpo-text-error-color': ['text', 'danger'],
161
+ '--jpo-bg-default-color': ['elevation', 'surface'],
162
+ '--jpo-bg-reverse-color': ['background', 'inverse'],
163
+ '--jpo-bg-warning-color': ['background', 'warning', 'subtle'],
164
+ '--jpo-bg-dark-color': ['background', 'netural', 'bold'],
165
+ '--jpo-border-default-color': ['border'],
166
+ '--jpo-border-secondary-color': ['border', 'subtle'],
167
+ };
168
+
169
+ export const knownNamedColors: Record<string, string[]> = {
170
+ aliceblue: ['blue'],
171
+ antiquewhite: [],
172
+ aqua: ['teal'],
173
+ aquamarine: ['teal'],
174
+ azure: ['teal', 'subtlest'],
175
+ beige: [],
176
+ bisque: [],
177
+ black: [],
178
+ blanchedalmond: [],
179
+ blue: ['blue'],
180
+ blueviolet: ['blue'],
181
+ brown: [],
182
+ burlywood: [],
183
+ cadetblue: ['blue'],
184
+ chartreuse: [],
185
+ chocolate: [],
186
+ coral: [],
187
+ cornflowerblue: ['blue'],
188
+ cornsilk: [],
189
+ crimson: ['red'],
190
+ cyan: ['accent', 'teal', 'subtle'],
191
+ darkblue: ['accent', 'blue', 'bold'],
192
+ darkcyan: ['accent', 'teal', 'bold'],
193
+ darkgoldenrod: [],
194
+ darkgray: [],
195
+ darkgrey: [],
196
+ darkgreen: ['green'],
197
+ darkkhaki: [],
198
+ darkmagenta: ['magenta', 'bold'],
199
+ darkolivegreen: ['green'],
200
+ darkorange: [],
201
+ darkorchid: [],
202
+ darkred: ['accent', 'red', 'bold'],
203
+ darksalmon: [],
204
+ darkseagreen: ['green'],
205
+ darkslateblue: [],
206
+ darkslategray: [],
207
+ darkslategrey: [],
208
+ darkturquoise: ['teal', 'bold'],
209
+ darkviolet: ['purple', 'bold'],
210
+ deeppink: ['magenta'],
211
+ deepskyblue: ['blue'],
212
+ dimgray: [],
213
+ dimgrey: [],
214
+ dodgerblue: [],
215
+ firebrick: ['red', 'bold'],
216
+ floralwhite: [],
217
+ forestgreen: ['green'],
218
+ fuchsia: ['magenta', 'subtle'],
219
+ gainsboro: [],
220
+ ghostwhite: [],
221
+ gold: ['yellow'],
222
+ goldenrod: [],
223
+ gray: [],
224
+ grey: [],
225
+ green: ['green'],
226
+ greenyellow: [],
227
+ honeydew: [],
228
+ hotpink: ['magenta'],
229
+ indianred: [],
230
+ indigo: ['purple'],
231
+ ivory: [],
232
+ khaki: [],
233
+ lavender: ['purple'],
234
+ lavenderblush: ['purple'],
235
+ lawngreen: ['green'],
236
+ lemonchiffon: [],
237
+ lightblue: [],
238
+ lightcoral: [],
239
+ lightcyan: [],
240
+ lightgoldenrodyellow: [],
241
+ lightgray: [],
242
+ lightgrey: [],
243
+ lightgreen: ['green', 'accent'],
244
+ lightpink: ['magenta'],
245
+ lightsalmon: ['pink', 'subtler'],
246
+ lightseagreen: ['green'],
247
+ lightskyblue: ['blue', 'accent'],
248
+ lightslategray: [],
249
+ lightslategrey: [],
250
+ lightsteelblue: ['blue'],
251
+ lightyellow: [],
252
+ lime: ['green', 'subtler'],
253
+ limegreen: ['green'],
254
+ linen: [],
255
+ magenta: ['magenta'],
256
+ maroon: [],
257
+ mediumaquamarine: [],
258
+ mediumblue: [],
259
+ mediumorchid: [],
260
+ mediumpurple: [],
261
+ mediumseagreen: ['green'],
262
+ mediumslateblue: [],
263
+ mediumspringgreen: ['green'],
264
+ mediumturquoise: [],
265
+ mediumvioletred: [],
266
+ midnightblue: [],
267
+ mintcream: [],
268
+ mistyrose: [],
269
+ moccasin: [],
270
+ navajowhite: [],
271
+ navy: ['blue', 'bold'],
272
+ oldlace: [],
273
+ olive: [],
274
+ olivedrab: [],
275
+ orange: [],
276
+ orangered: [],
277
+ orchid: ['purple'],
278
+ palegoldenrod: [],
279
+ palegreen: ['green'],
280
+ paleturquoise: [],
281
+ palevioletred: [],
282
+ papayawhip: [],
283
+ peachpuff: [],
284
+ peru: [],
285
+ pink: ['magenta'],
286
+ plum: [],
287
+ powderblue: ['blue', 'subtle'],
288
+ purple: ['purple'],
289
+ rebeccapurple: ['purple', 'accent'],
290
+ red: ['red', 'accent'],
291
+ rosybrown: [],
292
+ royalblue: [],
293
+ saddlebrown: [],
294
+ salmon: [],
295
+ sandybrown: [],
296
+ seagreen: ['green'],
297
+ seashell: [],
298
+ sienna: [],
299
+ silver: [],
300
+ skyblue: ['blue', 'subtlest'],
301
+ slateblue: [],
302
+ slategray: [],
303
+ slategrey: [],
304
+ snow: [],
305
+ springgreen: ['green'],
306
+ steelblue: ['blue'],
307
+ tan: [],
308
+ teal: ['accent', 'teal'],
309
+ thistle: [],
310
+ tomato: ['red'],
311
+ turquoise: [],
312
+ violet: ['purple'],
313
+ wheat: [],
314
+ white: ['elevation', 'surface'],
315
+ whitesmoke: [],
316
+ yellow: ['yellow'],
317
+ yellowgreen: ['yellow'],
318
+ };
319
+
320
+ export const knownRawColors: Record<string, string[]> = {
321
+ '#cccccc': ['gray'],
322
+ '#aaaaaa': ['gray', 'subtlest'],
323
+ '#bbbbbb': ['gray', 'subtle'],
324
+ '#ffffff': ['elevation', 'surface'],
325
+ '#f0f0f0': ['elevation', 'surface'],
326
+ '#eeeeee': ['elevation', 'surface', 'sunken'],
327
+ '#ff0000': ['danger'],
328
+ '#d04437': ['danger'],
329
+ '#c00c00': ['danger'],
330
+ '#5243aa': ['discovery'],
331
+ '#ffc712': ['warning'],
332
+ '#00f00f': ['brand'],
333
+ '#3b73af': ['brand'],
334
+ '#326ca6': ['brand'],
335
+ '#0052cc': ['brand'],
336
+ };
@@ -0,0 +1,173 @@
1
+ import type { Declaration, Node, Rule } from 'postcss';
2
+
3
+ import { uniqueWordsFromTokens } from '../../utils/tokens';
4
+
5
+ import { extractCssVarName } from './declaration';
6
+ import {
7
+ knownNamedColors,
8
+ knownRawColors,
9
+ knownVariables,
10
+ } from './legacy-colors';
11
+
12
+ function filterDuplicateFoundations(meta: string[]) {
13
+ const foundations = ['text', 'background', 'shadow', 'border'];
14
+ let hasFoundation = false;
15
+
16
+ return meta.filter((val) => {
17
+ if (!hasFoundation && foundations.includes(val)) {
18
+ hasFoundation = true;
19
+ return true;
20
+ }
21
+
22
+ if (hasFoundation && foundations.includes(val)) {
23
+ return false;
24
+ }
25
+
26
+ return true;
27
+ });
28
+ }
29
+
30
+ const REPLACEMENTS: Record<string, string> = {
31
+ ':': '',
32
+ ',': '',
33
+ texts: 'text',
34
+ error: 'danger',
35
+ invalid: 'danger',
36
+ removed: 'danger',
37
+ removal: 'danger',
38
+ remove: 'danger',
39
+ focus: 'focused',
40
+ valid: 'success',
41
+ successful: 'success',
42
+ risk: 'warning',
43
+ caution: 'warning',
44
+ warn: 'warning',
45
+ primary: 'bold',
46
+ info: 'bold',
47
+ secondary: 'subtle',
48
+ hyperlink: 'link',
49
+ anchor: 'link',
50
+ active: 'pressed',
51
+ hover: 'hovered',
52
+ dragged: 'overlay',
53
+ dragging: 'overlay',
54
+ drag: 'overlay',
55
+ 'background-color': 'background',
56
+ color: 'text',
57
+ icons: 'icon',
58
+ arrow: 'icon',
59
+ glyph: 'icon',
60
+ stroke: 'border',
61
+ 'border-left': 'border',
62
+ 'border-right': 'border',
63
+ 'border-top': 'border',
64
+ 'border-bottom': 'border',
65
+ 'box-shadow': 'shadow',
66
+ };
67
+ const ADDITIONAL_META: Record<string, string> = {
68
+ grey: 'neutral',
69
+ red: 'danger',
70
+ };
71
+
72
+ export function cleanMeta(meta: string[]) {
73
+ const cleanMeta = meta
74
+ .reduce(
75
+ (accum: string[], val: string) => [
76
+ ...accum,
77
+ ...(typeof val === 'string'
78
+ ? val.split(/(?=[A-Z])/g).map((e) => e.toLowerCase())
79
+ : []),
80
+ ],
81
+ [],
82
+ )
83
+ .reduce((accum: string[], val: string) => {
84
+ if (val in ADDITIONAL_META) {
85
+ accum.push(val, ADDITIONAL_META[val]);
86
+ } else {
87
+ accum.push(val);
88
+ }
89
+ accum.push(val in REPLACEMENTS ? REPLACEMENTS[val] : val);
90
+
91
+ return accum;
92
+ }, [])
93
+ .filter((val: string) => uniqueWordsFromTokens.includes(val))
94
+ .reduce((accum: string[], val: string) => {
95
+ if (!accum.includes(val)) {
96
+ accum.push(val);
97
+ }
98
+
99
+ return accum;
100
+ }, []);
101
+
102
+ return filterDuplicateFoundations(cleanMeta);
103
+ }
104
+
105
+ export function getBaseDeclarationMeta(decl: Declaration): string[] {
106
+ const parentSelectors = getParentSelectors(decl)
107
+ .split(/\-|\.|\,|\ |\:|\&/)
108
+ .filter(Boolean);
109
+
110
+ return [getPropertyMeta(decl.prop), ...parentSelectors];
111
+ }
112
+
113
+ function getPropertyMeta(prop: string) {
114
+ if (prop === 'color') {
115
+ return 'text';
116
+ }
117
+
118
+ if (prop.startsWith('background')) {
119
+ return 'background';
120
+ }
121
+
122
+ if (prop.includes('shadow')) {
123
+ return 'shadow';
124
+ }
125
+
126
+ if (prop.includes('border')) {
127
+ return 'border';
128
+ }
129
+
130
+ return '';
131
+ }
132
+
133
+ function isRule(node: Node): node is Rule {
134
+ return node.type === 'rule';
135
+ }
136
+
137
+ function getParentSelectors(node: Node): string {
138
+ if (isRule(node)) {
139
+ // @ts-expect-error
140
+ return getParentSelectors(node.parent) + ' ' + node.selector;
141
+ }
142
+
143
+ if (node.parent) {
144
+ return getParentSelectors(node.parent);
145
+ }
146
+
147
+ return '';
148
+ }
149
+
150
+ export function getCssVarMeta(cssVariable: string) {
151
+ const tokenName = extractCssVarName(cssVariable);
152
+ const meta = knownVariables[tokenName];
153
+
154
+ if (!meta || meta.length === 0) {
155
+ return tokenName.split('-');
156
+ }
157
+
158
+ return meta;
159
+ }
160
+
161
+ export function getRawColorMeta(rawColor: string) {
162
+ let cleanColor = rawColor.toLowerCase();
163
+
164
+ if (cleanColor.length === 4) {
165
+ cleanColor = cleanColor + cleanColor.substring(cleanColor.indexOf('#') + 1);
166
+ }
167
+
168
+ return knownRawColors[cleanColor] ?? [];
169
+ }
170
+
171
+ export function getNamedColorMeta(namedColor: string) {
172
+ return knownNamedColors[namedColor] ?? [];
173
+ }
@@ -0,0 +1,54 @@
1
+ import Search from '../../theme-to-design-tokens/utils/fuzzy-search';
2
+ import { activeTokens } from '../../utils/tokens';
3
+
4
+ import { cleanMeta } from './meta';
5
+
6
+ const COLOR_TOKEN_SEARCH_TYPES = [
7
+ 'text',
8
+ 'link',
9
+ 'icon',
10
+ 'border',
11
+ 'background',
12
+ 'blanket',
13
+ 'skeleton',
14
+ 'chart',
15
+ 'surface',
16
+ 'shadow',
17
+ ];
18
+
19
+ function filterTokens(meta: string[]) {
20
+ const commonSearchTypes = COLOR_TOKEN_SEARCH_TYPES.filter((type) =>
21
+ meta.includes(type),
22
+ );
23
+ return commonSearchTypes.length !== 0
24
+ ? activeTokens.filter((token) =>
25
+ commonSearchTypes.some(
26
+ (type) =>
27
+ token.startsWith(`color.${type}`) ||
28
+ token.startsWith(`elevation.${type}`),
29
+ ),
30
+ )
31
+ : activeTokens;
32
+ }
33
+
34
+ function tokenToVar(token: string) {
35
+ return `--ds-${token
36
+ .replace(/\./g, '-')
37
+ .replace('color-', '')
38
+ .replace('elevation-', '')}`;
39
+ }
40
+
41
+ export default function findToken(meta: string[]) {
42
+ const filteredTokens = filterTokens(meta);
43
+ const tokenFuzzySearch = Search(filteredTokens, false);
44
+ const cleanSearchTerms = cleanMeta(meta).join(' ');
45
+ const results: string[] = tokenFuzzySearch.get(cleanSearchTerms);
46
+
47
+ if (!results || results.length === 0) {
48
+ return 'MISSING_TOKEN';
49
+ }
50
+
51
+ const candidates = results.map((result) => result[1]);
52
+
53
+ return tokenToVar(candidates[0]);
54
+ }