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: Fix type file generation to export types instead of declarations #120

Merged
merged 6 commits into from
Jul 3, 2024
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
4 changes: 2 additions & 2 deletions packages/canvas-tokens/utils/formatters/formatJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ export const formatToInlineES6Module: Formatter = ({dictionary, file}) => {

/**
* Style Dictionary format function that creates ts file structure.
* This structure contains separated exports of each token with `as const`.
* This structure contains separated type exports of each token.
* @param {*} FormatterArguments - Style Dictionary formatter object containing `dictionary`, `options`, `file` and `platform` properties.
* @returns file content as a string
*/
export const formatInlineTypes: Formatter = ({dictionary, file}) => {
const headerContent = formatHelpers.fileHeader({file});
return dictionary.allTokens.reduce((acc: string, {name, path}) => {
const cssVarName = path.join('-');
acc += `export declare const ${name} = "--cnvs-${cssVarName}" as const;\n`;
acc += `export declare const ${name}: "--cnvs-${cssVarName}";\n`;
return acc;
}, headerContent);
};
Expand Down
5 changes: 2 additions & 3 deletions packages/canvas-tokens/utils/formatters/formatTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const formatJSToTypes: Formatter = ({dictionary, file, options}) => {
};

const startingText = 'export declare const';
const endingText = 'as const;\n';

type ReplaceFn = (pattern: string, newText: string) => string;

Expand Down Expand Up @@ -62,7 +61,7 @@ const recursivelyCreateFileStructure = ({

const innerText = depth
? `${spaces}"${key}": "${values}",`
: `${startingText} ${key} = "${values}" ${endingText}`;
: `${startingText} ${key}: "${values}";\n`;
const fullInnerText = jsDocText + innerText;

updatedContent = replaceInContent(`**${key}**`, fullInnerText);
Expand All @@ -74,7 +73,7 @@ const recursivelyCreateFileStructure = ({
.join('\n');

const innerText = !depth
? `${startingText} ${key} = {\n${placeholders}\n} ${endingText}`
? `${startingText} ${key}: {\n${placeholders}\n};\n`
: `${spaces}"${key}": {\n${placeholders}\n${spaces}},`;

updatedContent = replaceInContent(`**${key}**`, innerText);
Expand Down
4 changes: 2 additions & 2 deletions packages/canvas-tokens/utils/formatters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export const formats: Record<string, Formatter> = {
// formatter creating the inline es6 file structure
// with separated variables of tokens
'js/es6': formatToInlineES6Module,
// formatter creating the es6 and common-js inline types including the `as const`
// formatter creating the es6 and common-js inline types
'ts/inline': formatInlineTypes,
// formatter creating the es6 file structure
// with tokens united in objects
'es6/objects': formatES6ToObjects,
// formatter creating the es6 and common-js types
// including the `as const` and JSDoc description
// including types and JSDoc description
'ts/jsdoc-object': formatJSToTypes,
// formatter creating the common-js file structure
// with tokens united in objects
Expand Down
6 changes: 3 additions & 3 deletions packages/canvas-tokens/utils/spec/formats.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('formats', () => {
const result = formats['ts/inline'](defaultArgs);
const expected =
headerContent +
`export declare const cinnamon100 = "--cnvs-base-palette-cinnamon-100" as const;\nexport declare const cinnamon200 = "--cnvs-base-palette-cinnamon-200" as const;\n`;
`export declare const cinnamon100: "--cnvs-base-palette-cinnamon-100";\nexport declare const cinnamon200: "--cnvs-base-palette-cinnamon-200";\n`;

expect(result).toBe(expected);
});
Expand Down Expand Up @@ -374,7 +374,7 @@ describe('formats', () => {

const expected =
headerContent +
'export declare const opacity = {\n /**\n * 0.4\n * \n * token: base.opacity.400\n * \n * Test JSDoc\n */\n "disabled": "--cnvs-base-opacity-300",\n} as const;\n';
'export declare const opacity: {\n /**\n * 0.4\n * \n * token: base.opacity.400\n * \n * Test JSDoc\n */\n "disabled": "--cnvs-base-opacity-300",\n};\n';

expect(result).toBe(expected);
});
Expand Down Expand Up @@ -402,7 +402,7 @@ describe('formats', () => {

const expected =
headerContent +
'export declare const opacity = {\n /** 0.4 */\n "disabled": "--cnvs-base-opacity-300",\n} as const;\n';
'export declare const opacity: {\n /** 0.4 */\n "disabled": "--cnvs-base-opacity-300",\n};\n';

expect(result).toBe(expected);
});
Expand Down
Loading