Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(formats): handle DTCG-format tokens in typescript/es6-declarations #1406

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/beige-hounds-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

handle DTCG-format tokens in typescript/es6-declarations formatter
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,36 @@ export const fontFamily: '"Source Sans Pro", Arial, sans-serif';
`;
/* end snapshot formats typescript/es6-declarations with outputStringLiterals should match snapshot */

snapshots["formats typescript/es6-declarations without outputStringLiterals should match snapshot"] =
`/**
* Do not edit directly, this file was auto-generated.
*/

/** Used for errors */
export const colorRed: string;
export const fontFamily: string;
`;
/* end snapshot formats typescript/es6-declarations without outputStringLiterals should match snapshot */

snapshots["formats typescript/es6-declarations with DTCG tokens and outputStringLiterals should match snapshot"] =
`/**
* Do not edit directly, this file was auto-generated.
*/

/** Used for errors */
export const colorRed: "#FF0000";
export const fontFamily: '"Source Sans Pro", Arial, sans-serif';
`;
/* end snapshot formats typescript/es6-declarations with DTCG tokens and outputStringLiterals should match snapshot */

snapshots["formats typescript/es6-declarations with DTCG tokens and without outputStringLiterals should match snapshot"] =
`/**
* Do not edit directly, this file was auto-generated.
*/

/** Used for errors */
export const colorRed: string;
export const fontFamily: string;
`;
/* end snapshot formats typescript/es6-declarations with DTCG tokens and without outputStringLiterals should match snapshot */

83 changes: 69 additions & 14 deletions __tests__/formats/typeScriptEs6Declarations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,44 @@ const tokens = {
},
};

const DTCGTokens = {
color: {
red: {
$description: 'Used for errors',
$type: 'color',
$value: '#FF0000',
name: 'colorRed',
},
},
font: {
family: {
$type: 'fontFamily',
$value: '"Source Sans Pro", Arial, sans-serif',
name: 'fontFamily',
},
},
};

const format = formats[typescriptEs6Declarations];

describe('formats', () => {
describe(typescriptEs6Declarations, () => {
const formatArgs = (usesDtcg, customFile) =>
createFormatArgs({
dictionary: {
tokens: usesDtcg ? DTCGTokens : tokens,
allTokens: convertTokenData(usesDtcg ? DTCGTokens : tokens, {
output: 'array',
usesDtcg,
}),
},
file: customFile ?? file,
platform: {},
options: { usesDtcg },
});

it('should be a valid TS file', async () => {
const output = await format(
createFormatArgs({
dictionary: { tokens, allTokens: convertTokenData(tokens, { output: 'array' }) },
file,
platform: {},
}),
);
const output = await format(formatArgs(false));

// get all lines that begin with export
const lines = output.split('\n').filter((l) => l.indexOf('export') >= 0);
Expand All @@ -61,21 +87,50 @@ describe('formats', () => {
});
});

it('without outputStringLiterals should match snapshot', async () => {
const customFile = {
...file,
options: {
outputStringLiterals: false,
},
};
const output = await format(formatArgs(false, customFile));

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

it('with outputStringLiterals should match snapshot', async () => {
const customFile = {
...file,
options: {
outputStringLiterals: true,
},
};
const output = await format(formatArgs(false, customFile));

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

const output = await format(
createFormatArgs({
dictionary: { tokens, allTokens: convertTokenData(tokens, { output: 'array' }) },
file: customFile,
platform: {},
}),
);
it('with DTCG tokens and outputStringLiterals should match snapshot', async () => {
const customFile = {
...file,
options: {
outputStringLiterals: true,
},
};
const output = await format(formatArgs(true, customFile));

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

it('with DTCG tokens and without outputStringLiterals should match snapshot', async () => {
const customFile = {
...file,
options: {
outputStringLiterals: false,
},
};
const output = await format(formatArgs(true, customFile));

await expect(output).to.matchSnapshot();
});
Expand Down
33 changes: 17 additions & 16 deletions lib/common/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,22 +730,23 @@ const formats = {
formatting: getFormattingCloneWithoutPrefix(formatting),
options,
});
const content =
header +
dictionary.allTokens
.map(function (token) {
let to_ret_token = '';
if (token.comment) to_ret_token += '/** ' + token.comment + ' */\n';
to_ret_token +=
'export const ' +
token.name +
' : ' +
getTypeScriptType(options.usesDtcg ? token.$value : token.value, options) +
';';
return to_ret_token;
})
.join('\n') +
'\n';
const content = [
header,
dictionary.allTokens.map((token) => {
const typescriptType = getTypeScriptType(
options.usesDtcg ? token.$value : token.value,
options,
);
const comment = options.usesDtcg ? token.$description : token.comment;

return [
`${comment ? `/** ${comment} */` : ''}`,
`export const ${token.name} : ${typescriptType};`,
].join('\n');
}),
]
.flat()
.join('');
return formatJS(content, true);
},

Expand Down
Loading