Skip to content

Commit ea0ec73

Browse files
fix: convertDTCG for already DTCG input (#1445)
1 parent 1d4389a commit ea0ec73

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

.changeset/eight-cats-juggle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'style-dictionary': patch
3+
---
4+
5+
Fix convertToDTCG for sets that are already (partially) DTCG.

__tests__/utils/convertToDTCG.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,41 @@ describe('utils', () => {
247247
});
248248
});
249249

250+
it('should handle input that is DTCG syntax already properly', () => {
251+
const result = convertToDTCG({
252+
colors: {
253+
red: {
254+
$value: '#ff0000',
255+
$type: 'color',
256+
$extensions: {
257+
'com.example': {
258+
modify: {
259+
value: 0.5, // <- to check that it doesn't incorrectly identify this as a "token"
260+
type: 'transparentize',
261+
},
262+
},
263+
},
264+
},
265+
},
266+
});
267+
expect(result).to.eql({
268+
$type: 'color',
269+
colors: {
270+
red: {
271+
$value: '#ff0000',
272+
$extensions: {
273+
'com.example': {
274+
modify: {
275+
value: 0.5,
276+
type: 'transparentize',
277+
},
278+
},
279+
},
280+
},
281+
},
282+
});
283+
});
284+
250285
it('should work with any number of nestings', () => {
251286
const result = convertToDTCG({
252287
colors: {

lib/utils/convertToDTCG.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,18 @@ function recurse(slice, opts) {
2525
let types = new Set();
2626

2727
// this slice within the dictionary is a design token
28-
if (Object.hasOwn(slice, 'value')) {
28+
// Check for $value in case the input is already DTCG syntax
29+
if (Object.hasOwn(slice, 'value') || Object.hasOwn(slice, '$value')) {
2930
const token = /** @type {DesignToken} */ (slice);
30-
// convert to $ prefixed properties
3131
Object.keys(token).forEach((key) => {
32-
switch (key) {
33-
case 'type':
34-
// track the encountered types for this layer
35-
types.add(/** @type {string} */ (token[key]));
36-
// eslint-disable-next-line no-fallthrough
37-
case 'value':
38-
case 'description':
39-
token[`$${key}`] = token[key];
40-
delete token[key];
41-
// no-default
32+
if (['type', '$type'].includes(key)) {
33+
// track the encountered types for this layer
34+
types.add(/** @type {string} */ (token[key]));
35+
}
36+
// convert to $ prefixed properties
37+
if (['type', 'description', 'value'].includes(key)) {
38+
token[`$${key}`] = token[key];
39+
delete token[key];
4240
}
4341
});
4442
return types;

0 commit comments

Comments
 (0)