File tree 3 files changed +50
-12
lines changed
3 files changed +50
-12
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' style-dictionary ' : patch
3
+ ---
4
+
5
+ Fix convertToDTCG for sets that are already (partially) DTCG.
Original file line number Diff line number Diff line change @@ -247,6 +247,41 @@ describe('utils', () => {
247
247
} ) ;
248
248
} ) ;
249
249
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
+
250
285
it ( 'should work with any number of nestings' , ( ) => {
251
286
const result = convertToDTCG ( {
252
287
colors : {
Original file line number Diff line number Diff line change @@ -25,20 +25,18 @@ function recurse(slice, opts) {
25
25
let types = new Set ( ) ;
26
26
27
27
// 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' ) ) {
29
30
const token = /** @type {DesignToken } */ ( slice ) ;
30
- // convert to $ prefixed properties
31
31
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 ] ;
42
40
}
43
41
} ) ;
44
42
return types ;
You can’t perform that action at this time.
0 commit comments