@@ -209,70 +209,181 @@ const objectDictionary = createDictionary({
209
209
describe ( 'common' , ( ) => {
210
210
describe ( 'formatHelpers' , ( ) => {
211
211
describe ( 'createPropertyFormatter' , ( ) => {
212
- it ( 'should support outputReferences' , ( ) => {
213
- const propFormatter = createPropertyFormatter ( {
214
- outputReferences : true ,
215
- dictionary,
216
- format : 'css' ,
212
+ describe ( 'outputReferences' , ( ) => {
213
+ it ( 'should support outputReferences' , ( ) => {
214
+ const propFormatter = createPropertyFormatter ( {
215
+ outputReferences : true ,
216
+ dictionary,
217
+ format : 'css' ,
218
+ } ) ;
219
+ expect ( propFormatter ( dictionary . tokens . tokens . foo ) ) . toEqual ( ' --tokens-foo: 5px;' ) ;
220
+ expect ( propFormatter ( dictionary . tokens . tokens . ref ) ) . toEqual (
221
+ ' --tokens-ref: var(--tokens-foo);' ,
222
+ ) ;
217
223
} ) ;
218
- expect ( propFormatter ( dictionary . tokens . tokens . foo ) ) . toEqual ( ' --tokens-foo: 5px;' ) ;
219
- expect ( propFormatter ( dictionary . tokens . tokens . ref ) ) . toEqual (
220
- ' --tokens-ref: var(--tokens-foo);' ,
221
- ) ;
222
- } ) ;
223
224
224
- it ( 'should support outputReferences when values are transformed by (transitive) "value" transforms' , ( ) => {
225
- const propFormatter = createPropertyFormatter ( {
226
- outputReferences : true ,
227
- dictionary : transformedDictionary ,
228
- format : 'css' ,
225
+ it ( 'should support outputReferences when values are transformed by (transitive) "value" transforms' , ( ) => {
226
+ const propFormatter = createPropertyFormatter ( {
227
+ outputReferences : true ,
228
+ dictionary : transformedDictionary ,
229
+ format : 'css' ,
230
+ } ) ;
231
+ expect ( propFormatter ( transformedDictionary . tokens . tokens . foo ) ) . toEqual (
232
+ ' --tokens-foo: 5px;' ,
233
+ ) ;
234
+ expect ( propFormatter ( transformedDictionary . tokens . tokens . ref ) ) . toEqual (
235
+ ' --tokens-ref: var(--tokens-foo);' ,
236
+ ) ;
229
237
} ) ;
230
- expect ( propFormatter ( transformedDictionary . tokens . tokens . foo ) ) . toEqual (
231
- ' --tokens-foo: 5px;' ,
232
- ) ;
233
- expect ( propFormatter ( transformedDictionary . tokens . tokens . ref ) ) . toEqual (
234
- ' --tokens-ref: var(--tokens-foo);' ,
235
- ) ;
236
- } ) ;
237
238
238
- it ( 'should support number values for outputReferences' , ( ) => {
239
- const propFormatter = createPropertyFormatter ( {
240
- outputReferences : true ,
241
- dictionary : numberDictionary ,
242
- format : 'css' ,
239
+ it ( 'should support number values for outputReferences' , ( ) => {
240
+ const propFormatter = createPropertyFormatter ( {
241
+ outputReferences : true ,
242
+ dictionary : numberDictionary ,
243
+ format : 'css' ,
244
+ } ) ;
245
+ expect ( propFormatter ( numberDictionary . tokens . tokens . foo ) ) . toEqual ( ' --tokens-foo: 10;' ) ;
246
+ expect ( propFormatter ( numberDictionary . tokens . tokens . ref ) ) . toEqual (
247
+ ' --tokens-ref: var(--tokens-foo);' ,
248
+ ) ;
249
+ } ) ;
250
+
251
+ it ( 'should support multiple references for outputReferences' , ( ) => {
252
+ const propFormatter = createPropertyFormatter ( {
253
+ outputReferences : true ,
254
+ dictionary : multiDictionary ,
255
+ format : 'css' ,
256
+ } ) ;
257
+ expect ( propFormatter ( multiDictionary . tokens . tokens . foo ) ) . toEqual ( ' --tokens-foo: 10px;' ) ;
258
+ expect ( propFormatter ( multiDictionary . tokens . tokens . bar ) ) . toEqual ( ' --tokens-bar: 15px;' ) ;
259
+ expect ( propFormatter ( multiDictionary . tokens . tokens . ref ) ) . toEqual (
260
+ ' --tokens-ref: var(--tokens-foo) 5px var(--tokens-bar);' ,
261
+ ) ;
243
262
} ) ;
244
- expect ( propFormatter ( numberDictionary . tokens . tokens . foo ) ) . toEqual ( ' --tokens-foo: 10;' ) ;
245
- expect ( propFormatter ( numberDictionary . tokens . tokens . ref ) ) . toEqual (
246
- ' --tokens-ref: var(--tokens-foo);' ,
247
- ) ;
248
- } ) ;
249
263
250
- it ( 'should support multiple references for outputReferences' , ( ) => {
251
- const propFormatter = createPropertyFormatter ( {
252
- outputReferences : true ,
253
- dictionary : multiDictionary ,
254
- format : 'css' ,
264
+ it ( 'should support object value references for outputReferences' , ( ) => {
265
+ // The ref is an object type value, which means there will usually be some kind of transform (e.g. a CSS shorthand transform)
266
+ // to change it from an object to a string. In our example, we use a border CSS shorthand for border token.
267
+ // In this case, since it is an object value, we will run the transformation on the transformed (string) value.
268
+ const propFormatter = createPropertyFormatter ( {
269
+ outputReferences : true ,
270
+ dictionary : objectDictionary ,
271
+ format : 'css' ,
272
+ } ) ;
273
+ expect ( propFormatter ( objectDictionary . tokens . tokens . foo ) ) . toEqual ( ' --tokens-foo: 5px;' ) ;
274
+ expect ( propFormatter ( objectDictionary . tokens . tokens . ref ) ) . toEqual (
275
+ ' --tokens-ref: var(--tokens-foo) dashed #FF00FF;' ,
276
+ ) ;
255
277
} ) ;
256
- expect ( propFormatter ( multiDictionary . tokens . tokens . foo ) ) . toEqual ( ' --tokens-foo: 10px;' ) ;
257
- expect ( propFormatter ( multiDictionary . tokens . tokens . bar ) ) . toEqual ( ' --tokens-bar: 15px;' ) ;
258
- expect ( propFormatter ( multiDictionary . tokens . tokens . ref ) ) . toEqual (
259
- ' --tokens-ref: var(--tokens-foo) 5px var(--tokens-bar);' ,
260
- ) ;
261
278
} ) ;
262
279
263
- it ( 'should support object value references for outputReferences' , ( ) => {
264
- // The ref is an object type value, which means there will usually be some kind of transform (e.g. a CSS shorthand transform)
265
- // to change it from an object to a string. In our example, we use a border CSS shorthand for border token.
266
- // In this case, since it is an object value, we will run the transformation on the transformed (string) value.
267
- const propFormatter = createPropertyFormatter ( {
268
- outputReferences : true ,
269
- dictionary : objectDictionary ,
270
- format : 'css' ,
280
+ describe ( 'commentStyle' , ( ) => {
281
+ const commentProperties = {
282
+ color : {
283
+ red : {
284
+ name : 'color-red' ,
285
+ value : '#FF0000' ,
286
+ comment : 'Foo bar qux' ,
287
+ attributes : {
288
+ category : 'color' ,
289
+ type : 'red' ,
290
+ } ,
291
+ path : [ 'color' , 'red' ] ,
292
+ } ,
293
+ blue : {
294
+ name : 'color-blue' ,
295
+ value : '#0000FF' ,
296
+ comment : 'Foo\nbar\nqux' ,
297
+ attributes : {
298
+ category : 'color' ,
299
+ type : 'blue' ,
300
+ } ,
301
+ path : [ 'color' , 'blue' ] ,
302
+ } ,
303
+ green : {
304
+ name : 'color-green' ,
305
+ value : '#00FF00' ,
306
+ comment : 'Foo bar qux' ,
307
+ attributes : {
308
+ category : 'color' ,
309
+ type : 'green' ,
310
+ } ,
311
+ path : [ 'color' , 'green' ] ,
312
+ } ,
313
+ } ,
314
+ } ;
315
+
316
+ const commentDictionary = createDictionary ( {
317
+ properties : commentProperties ,
318
+ } ) ;
319
+
320
+ it ( 'should default to putting comment next to the output value' , ( ) => {
321
+ // long commentStyle
322
+ const cssFormatter = createPropertyFormatter ( {
323
+ format : 'css' ,
324
+ commentDictionary,
325
+ } ) ;
326
+ // short commentStyle
327
+ const sassFormatter = createPropertyFormatter ( {
328
+ format : 'sass' ,
329
+ commentDictionary,
330
+ } ) ;
331
+
332
+ // red = single-line comment, blue = multi-line comment
333
+ const cssRed = cssFormatter ( commentDictionary . tokens . color . red ) ;
334
+ const cssBlue = cssFormatter ( commentDictionary . tokens . color . blue ) ;
335
+ const sassRed = sassFormatter ( commentDictionary . tokens . color . red ) ;
336
+ const sassBlue = sassFormatter ( commentDictionary . tokens . color . blue ) ;
337
+
338
+ // Note that since CSS puts it inside a selector, there is an indentation of 2 spaces as well
339
+ // CSS also has commentStyle long, whereas sass uses short
340
+ expect ( cssRed ) . toMatchInlineSnapshot ( `" --color-red: #FF0000; /* Foo bar qux */"` ) ;
341
+
342
+ expect ( cssBlue ) . toMatchInlineSnapshot ( `
343
+ " /**
344
+ * Foo
345
+ * bar
346
+ * qux
347
+ */
348
+ --color-blue: #0000FF;"
349
+ ` ) ;
350
+
351
+ expect ( sassRed ) . toMatchInlineSnapshot ( `"$color-red: #FF0000; // Foo bar qux"` ) ;
352
+ expect ( sassBlue ) . toMatchInlineSnapshot ( `
353
+ "// Foo
354
+ // bar
355
+ // qux
356
+ $color-blue: #0000FF;"
357
+ ` ) ;
358
+ } ) ;
359
+
360
+ it ( 'allows overriding formatting commentStyle' , ( ) => {
361
+ // long commentStyle
362
+ const cssFormatter = createPropertyFormatter ( {
363
+ format : 'css' ,
364
+ commentDictionary,
365
+ formatting : { commentStyle : 'long' , commentPosition : 'above' } ,
366
+ } ) ;
367
+ // short commentStyle
368
+ const sassFormatter = createPropertyFormatter ( {
369
+ format : 'sass' ,
370
+ commentDictionary,
371
+ formatting : { commentStyle : 'short' , commentPosition : 'above' } ,
372
+ } ) ;
373
+
374
+ const cssRed = cssFormatter ( commentDictionary . tokens . color . green ) ;
375
+ const sassRed = sassFormatter ( commentDictionary . tokens . color . green ) ;
376
+
377
+ expect ( cssRed ) . toMatchInlineSnapshot ( `
378
+ " /* Foo bar qux */
379
+ --color-green: #00FF00;"
380
+ ` ) ;
381
+
382
+ expect ( sassRed ) . toMatchInlineSnapshot ( `
383
+ "// Foo bar qux
384
+ $color-green: #00FF00;"
385
+ ` ) ;
271
386
} ) ;
272
- expect ( propFormatter ( objectDictionary . tokens . tokens . foo ) ) . toEqual ( ' --tokens-foo: 5px;' ) ;
273
- expect ( propFormatter ( objectDictionary . tokens . tokens . ref ) ) . toEqual (
274
- ' --tokens-ref: var(--tokens-foo) dashed #FF00FF;' ,
275
- ) ;
276
387
} ) ;
277
388
} ) ;
278
389
} ) ;
0 commit comments