Skip to content

Commit fd8cdb4

Browse files
committedDec 9, 2024·
fix(formats): handle DTCG-format tokens in javascript/es6
- update snapshots - fix issue in javascript/es6 formatter and add handling DTCG tokens - update tests Signed-off-by: Tobias Kuppens Groot <tkuppensgroo@uos.de>
1 parent e03f5f2 commit fd8cdb4

File tree

4 files changed

+69
-29
lines changed

4 files changed

+69
-29
lines changed
 

‎.changeset/perfect-carrots-divide.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'style-dictionary': patch
3+
---
4+
5+
handle DTCG-format tokens in javascript/es6 formatter

‎__tests__/formats/__snapshots__/es6Constants.test.snap.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ snapshots["formats javascript/es6 should be a valid JS file and match snapshot"]
66
* Do not edit directly, this file was auto-generated.
77
*/
88
9-
export const red = "#EF5350";
9+
export const red = "#EF5350"; // comment
1010
`;
1111
/* end snapshot formats javascript/es6 should be a valid JS file and match snapshot */
1212

13+
snapshots["formats javascript/es6 should handle DTCG token format, be a valid JS file and matches snapshot"] =
14+
`/**
15+
* Do not edit directly, this file was auto-generated.
16+
*/
17+
18+
export const red = "#EF5350"; // comment
19+
`;
20+
/* end snapshot formats javascript/es6 should handle DTCG token format, be a valid JS file and matches snapshot */
21+

‎__tests__/formats/es6Constants.test.js

+42-13
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,29 @@ const file = {
2929
const tokens = {
3030
color: {
3131
red: {
32+
comment: 'comment',
3233
name: 'red',
33-
value: '#EF5350',
3434
original: {
3535
value: '#EF5350',
3636
},
37-
path: ['color', 'base', 'red', '400'],
37+
path: ['color', 'red'],
38+
type: 'color',
39+
value: '#EF5350',
40+
},
41+
},
42+
};
43+
44+
const DTCGTokens = {
45+
color: {
46+
red: {
47+
$description: 'comment',
48+
name: 'red',
49+
original: {
50+
$value: '#EF5350',
51+
},
52+
path: ['color', 'red'],
53+
$type: 'color',
54+
$value: '#EF5350',
3855
},
3956
},
4057
};
@@ -43,18 +60,30 @@ const format = formats[javascriptEs6];
4360

4461
describe('formats', () => {
4562
describe(javascriptEs6, () => {
46-
it('should be a valid JS file and match snapshot', async () => {
47-
await expect(
48-
await format(
49-
createFormatArgs({
50-
dictionary: { tokens, allTokens: convertTokenData(tokens, { output: 'array' }) },
51-
file,
52-
platform: {},
63+
const formatArgs = (usesDtcg) =>
64+
createFormatArgs({
65+
dictionary: {
66+
tokens: usesDtcg ? DTCGTokens : tokens,
67+
allTokens: convertTokenData(usesDtcg ? DTCGTokens : tokens, {
68+
output: 'array',
69+
usesDtcg,
5370
}),
54-
{},
55-
file,
56-
),
57-
).to.matchSnapshot();
71+
},
72+
file,
73+
platform: {},
74+
options: { usesDtcg },
75+
});
76+
77+
it('should be a valid JS file and match snapshot', async () => {
78+
const output = await format(formatArgs(false));
79+
80+
await expect(output).to.matchSnapshot();
81+
});
82+
83+
it('should handle DTCG token format, be a valid JS file and matches snapshot', async () => {
84+
const output = await format(formatArgs(true));
85+
86+
await expect(output).to.matchSnapshot();
5887
});
5988
});
6089
});

‎lib/common/formats.js

+12-15
Original file line numberDiff line numberDiff line change
@@ -633,21 +633,18 @@ const formats = {
633633
formatting: getFormattingCloneWithoutPrefix(formatting),
634634
options,
635635
});
636-
const content =
637-
header +
638-
dictionary.allTokens
639-
.map(function (token) {
640-
let to_ret =
641-
'export const ' +
642-
token.name +
643-
' = ' +
644-
JSON.stringify(options.usesDtcg ? token.$value : token.value) +
645-
';';
646-
if (token.comment) to_ret = to_ret.concat(' // ' + token.comment);
647-
return to_ret;
648-
})
649-
.join('\n') +
650-
'\n';
636+
const content = [
637+
header,
638+
dictionary.allTokens.map((token) => {
639+
const value = JSON.stringify(options.usesDtcg ? token.$value : token.value);
640+
const comment = options.usesDtcg ? token.$description : token.comment;
641+
const to_ret = `export const ${token.name} = ${value};`;
642+
643+
return comment ? to_ret.concat(`// ${comment}`) : to_ret;
644+
}),
645+
]
646+
.flat()
647+
.join('\n');
651648
return formatJS(content);
652649
},
653650

0 commit comments

Comments
 (0)
Please sign in to comment.