Skip to content

Commit 86e8ae1

Browse files
committed
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 <[email protected]>
1 parent 5aad797 commit 86e8ae1

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed

__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
@@ -26,12 +26,29 @@ const file = {
2626
const tokens = {
2727
color: {
2828
red: {
29+
comment: 'comment',
2930
name: 'red',
30-
value: '#EF5350',
3131
original: {
3232
value: '#EF5350',
3333
},
34-
path: ['color', 'base', 'red', '400'],
34+
path: ['color', 'red'],
35+
type: 'color',
36+
value: '#EF5350',
37+
},
38+
},
39+
};
40+
41+
const DTCGTokens = {
42+
color: {
43+
red: {
44+
$description: 'comment',
45+
name: 'red',
46+
original: {
47+
$value: '#EF5350',
48+
},
49+
path: ['color', 'red'],
50+
$type: 'color',
51+
$value: '#EF5350',
3552
},
3653
},
3754
};
@@ -40,18 +57,30 @@ const format = formats['javascript/es6'];
4057

4158
describe('formats', () => {
4259
describe('javascript/es6', () => {
43-
it('should be a valid JS file and match snapshot', async () => {
44-
await expect(
45-
await format(
46-
createFormatArgs({
47-
dictionary: { tokens, allTokens: convertTokenData(tokens, { output: 'array' }) },
48-
file,
49-
platform: {},
60+
const formatArgs = (usesDtcg) =>
61+
createFormatArgs({
62+
dictionary: {
63+
tokens: usesDtcg ? DTCGTokens : tokens,
64+
allTokens: convertTokenData(usesDtcg ? DTCGTokens : tokens, {
65+
output: 'array',
66+
usesDtcg,
5067
}),
51-
{},
52-
file,
53-
),
54-
).to.matchSnapshot();
68+
},
69+
file,
70+
platform: {},
71+
options: { usesDtcg },
72+
});
73+
74+
it('should be a valid JS file and match snapshot', async () => {
75+
const output = await format(formatArgs(false));
76+
77+
await expect(output).to.matchSnapshot();
78+
});
79+
80+
it('should handle DTCG token format, be a valid JS file and matches snapshot', async () => {
81+
const output = await format(formatArgs(true));
82+
83+
await expect(output).to.matchSnapshot();
5584
});
5685
});
5786
});

lib/common/formats.js

+12-15
Original file line numberDiff line numberDiff line change
@@ -565,21 +565,18 @@ const formats = {
565565
formatting: getFormattingCloneWithoutPrefix(formatting),
566566
options,
567567
});
568-
const content =
569-
header +
570-
dictionary.allTokens
571-
.map(function (token) {
572-
let to_ret =
573-
'export const ' +
574-
token.name +
575-
' = ' +
576-
JSON.stringify(options.usesDtcg ? token.$value : token.value) +
577-
';';
578-
if (token.comment) to_ret = to_ret.concat(' // ' + token.comment);
579-
return to_ret;
580-
})
581-
.join('\n') +
582-
'\n';
568+
const content = [
569+
header,
570+
dictionary.allTokens.map((token) => {
571+
const value = JSON.stringify(options.usesDtcg ? token.$value : token.value);
572+
const comment = options.usesDtcg ? token.$description : token.comment;
573+
const to_ret = `export const ${token.name} = ${value};`;
574+
575+
return comment ? to_ret.concat(`// ${comment}`) : to_ret;
576+
}),
577+
]
578+
.flat()
579+
.join('\n');
583580
return formatJS(content);
584581
},
585582

0 commit comments

Comments
 (0)