Skip to content

Commit

Permalink
Merge pull request #1404 from tkgroot/fix/dtcg-es6Constants-formatter
Browse files Browse the repository at this point in the history
fix(formats): handle DTCG-format tokens in javascript/es6
  • Loading branch information
jorenbroekema authored Dec 10, 2024
2 parents e03f5f2 + fd8cdb4 commit beea34f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-carrots-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

handle DTCG-format tokens in javascript/es6 formatter
11 changes: 10 additions & 1 deletion __tests__/formats/__snapshots__/es6Constants.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ snapshots["formats javascript/es6 should be a valid JS file and match snapshot"]
* Do not edit directly, this file was auto-generated.
*/
export const red = "#EF5350";
export const red = "#EF5350"; // comment
`;
/* end snapshot formats javascript/es6 should be a valid JS file and match snapshot */

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

55 changes: 42 additions & 13 deletions __tests__/formats/es6Constants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,29 @@ const file = {
const tokens = {
color: {
red: {
comment: 'comment',
name: 'red',
value: '#EF5350',
original: {
value: '#EF5350',
},
path: ['color', 'base', 'red', '400'],
path: ['color', 'red'],
type: 'color',
value: '#EF5350',
},
},
};

const DTCGTokens = {
color: {
red: {
$description: 'comment',
name: 'red',
original: {
$value: '#EF5350',
},
path: ['color', 'red'],
$type: 'color',
$value: '#EF5350',
},
},
};
Expand All @@ -43,18 +60,30 @@ const format = formats[javascriptEs6];

describe('formats', () => {
describe(javascriptEs6, () => {
it('should be a valid JS file and match snapshot', async () => {
await expect(
await format(
createFormatArgs({
dictionary: { tokens, allTokens: convertTokenData(tokens, { output: 'array' }) },
file,
platform: {},
const formatArgs = (usesDtcg) =>
createFormatArgs({
dictionary: {
tokens: usesDtcg ? DTCGTokens : tokens,
allTokens: convertTokenData(usesDtcg ? DTCGTokens : tokens, {
output: 'array',
usesDtcg,
}),
{},
file,
),
).to.matchSnapshot();
},
file,
platform: {},
options: { usesDtcg },
});

it('should be a valid JS file and match snapshot', async () => {
const output = await format(formatArgs(false));

await expect(output).to.matchSnapshot();
});

it('should handle DTCG token format, be a valid JS file and matches snapshot', async () => {
const output = await format(formatArgs(true));

await expect(output).to.matchSnapshot();
});
});
});
27 changes: 12 additions & 15 deletions lib/common/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,21 +633,18 @@ const formats = {
formatting: getFormattingCloneWithoutPrefix(formatting),
options,
});
const content =
header +
dictionary.allTokens
.map(function (token) {
let to_ret =
'export const ' +
token.name +
' = ' +
JSON.stringify(options.usesDtcg ? token.$value : token.value) +
';';
if (token.comment) to_ret = to_ret.concat(' // ' + token.comment);
return to_ret;
})
.join('\n') +
'\n';
const content = [
header,
dictionary.allTokens.map((token) => {
const value = JSON.stringify(options.usesDtcg ? token.$value : token.value);
const comment = options.usesDtcg ? token.$description : token.comment;
const to_ret = `export const ${token.name} = ${value};`;

return comment ? to_ret.concat(`// ${comment}`) : to_ret;
}),
]
.flat()
.join('\n');
return formatJS(content);
},

Expand Down

0 comments on commit beea34f

Please sign in to comment.