Skip to content

Commit

Permalink
fix: convertDTCG for already DTCG input (#1445)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema authored Feb 4, 2025
1 parent 1d4389a commit ea0ec73
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-cats-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

Fix convertToDTCG for sets that are already (partially) DTCG.
35 changes: 35 additions & 0 deletions __tests__/utils/convertToDTCG.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,41 @@ describe('utils', () => {
});
});

it('should handle input that is DTCG syntax already properly', () => {
const result = convertToDTCG({
colors: {
red: {
$value: '#ff0000',
$type: 'color',
$extensions: {
'com.example': {
modify: {
value: 0.5, // <- to check that it doesn't incorrectly identify this as a "token"
type: 'transparentize',
},
},
},
},
},
});
expect(result).to.eql({
$type: 'color',
colors: {
red: {
$value: '#ff0000',
$extensions: {
'com.example': {
modify: {
value: 0.5,
type: 'transparentize',
},
},
},
},
},
});
});

it('should work with any number of nestings', () => {
const result = convertToDTCG({
colors: {
Expand Down
22 changes: 10 additions & 12 deletions lib/utils/convertToDTCG.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,18 @@ function recurse(slice, opts) {
let types = new Set();

// this slice within the dictionary is a design token
if (Object.hasOwn(slice, 'value')) {
// Check for $value in case the input is already DTCG syntax
if (Object.hasOwn(slice, 'value') || Object.hasOwn(slice, '$value')) {
const token = /** @type {DesignToken} */ (slice);
// convert to $ prefixed properties
Object.keys(token).forEach((key) => {
switch (key) {
case 'type':
// track the encountered types for this layer
types.add(/** @type {string} */ (token[key]));
// eslint-disable-next-line no-fallthrough
case 'value':
case 'description':
token[`$${key}`] = token[key];
delete token[key];
// no-default
if (['type', '$type'].includes(key)) {
// track the encountered types for this layer
types.add(/** @type {string} */ (token[key]));
}
// convert to $ prefixed properties
if (['type', 'description', 'value'].includes(key)) {
token[`$${key}`] = token[key];
delete token[key];
}
});
return types;
Expand Down

0 comments on commit ea0ec73

Please sign in to comment.