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

feat(web): Make type definitions of objects static #42

Merged
merged 3 commits into from
Sep 29, 2023
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/canvas-tokens/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const config = setConfig({
level: ['brand', 'sys'],
extensions: ['d.ts'],
format: 'merge/types',
combineWith: ['merge/objects', '{platform}/objects'],
combineWith: ['merge/objects', '{platform}/types'],
},
{
level: ['base'],
Expand Down
13 changes: 13 additions & 0 deletions packages/canvas-tokens/utils/formatters/formatJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ export const formatES6ToObjects: Formatter = ({dictionary, file}) => {
}, headerContent);
};

/**
* Style Dictionary format function that create token type definitions.
* @param {*} FormatterArguments - Style Dictionary formatter object containing `dictionary`, `options`, `file` and `platform` properties.
* @returns file content as a string
*/
export const formatES6ToTypes: Formatter = ({dictionary, file}) => {
const headerContent = formatHelpers.fileHeader({file});
return Object.entries(dictionary.properties).reduce((acc: string, [key, values]) => {
return (acc +=
`export declare const ${key} = ` + JSON.stringify(values, null, 2) + ' as const;\n\n');
}, headerContent);
};

/**
* Style Dictionary format function that create the export index file for es6 folder.
* @param {*} FormatterArguments - Style Dictionary formatter object containing `dictionary`, `options`, `file` and `platform` properties.
Expand Down
4 changes: 4 additions & 0 deletions packages/canvas-tokens/utils/formatters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
formatCommonToObjects,
formatES6Exports,
formatCommonJSExports,
formatES6ToTypes,
} from './formatJS';
import {formatCSSComposite, formatLessComposite, formatSassComposite} from './formatStyles';
import {mergeObjects} from './mergeObjects';
Expand All @@ -18,6 +19,9 @@ export const formats: Record<string, Formatter> = {
// 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`
'es6/types': formatES6ToTypes,
'common-js/types': formatES6ToTypes,
// formatter creating the common-js file structure
// with tokens united in objects
'common-js/objects': formatCommonToObjects,
Expand Down
52 changes: 52 additions & 0 deletions packages/canvas-tokens/utils/spec/formats.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,58 @@ describe('formats', () => {
});
});

describe('es6/types', () => {
it('should return correct file structure for es6', () => {
const result = formats['es6/types']({
...defaultArgs,
options: {
formats: ['javascript/es6'],
level: 'sys',
},
dictionary: {
properties: {
opacity: {
disabled: '--cnvs-base-opacity-300',
},
},
},
});

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

expect(result).toBe(expected);
});
});

describe('common-js/types', () => {
it('should return correct file structure for es6', () => {
const result = formats['es6/types']({
...defaultArgs,
options: {
formats: ['javascript/common-js'],
level: 'sys',
},
dictionary: {
properties: {
opacity: {
disabled: '--cnvs-base-opacity-300',
},
},
},
});

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

expect(result).toBe(expected);
});
});

describe('merge/refs', () => {
it('should return correct file structure for es6', () => {
const result = formats['merge/refs']({
Expand Down