Skip to content

Commit

Permalink
refactor(ts): cleanup output and use as const and type extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
casaper committed Jun 19, 2022
1 parent 6b03793 commit 0baf509
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 387 deletions.
5 changes: 4 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export const DEFAULT_OPTIONS: Omit<RunnerOptions, 'inputDir' | 'outputDir'> = {
OtherAssetType.JSON,
OtherAssetType.TS
],
formatOptions: { json: { indent: 4 } },
formatOptions: {
json: { indent: 4 },
ts: { types: ['stringLiteral', 'enum'] }
},
pathOptions: {},
templates: {},
codepoints: {},
Expand Down
133 changes: 1 addition & 132 deletions src/generators/asset-types/__tests__/__snapshots__/ts.ts.snap
Original file line number Diff line number Diff line change
@@ -1,134 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`\`TS\` asset generator generates constant only 1`] = `
"export const MY_ICONS_SET_CODEPOINTS: Record<string, string> = {
\\"foo\\": \\"4265\\",
\\"bar\\": \\"1231\\",
};
"
`;

exports[`\`TS\` asset generator generates constant with literalId if no enum generated 1`] = `
"export type MyIconsSetId =
| \\"foo\\"
| \\"bar\\";
export const MY_ICONS_SET_CODEPOINTS: { [key in MyIconsSetId]: string } = {
\\"foo\\": \\"4265\\",
\\"bar\\": \\"1231\\",
};
"
`;

exports[`\`TS\` asset generator generates constant with literalKey if no enum generated 1`] = `
"export type MyIconsSetKey =
| \\"Foo\\"
| \\"Bar\\";
export const MY_ICONS_SET_CODEPOINTS: { [key in MyIconsSetKey]: string } = {
\\"foo\\": \\"4265\\",
\\"bar\\": \\"1231\\",
};
"
`;

exports[`\`TS\` asset generator generates no key string literal type if option passed like that 1`] = `
"export enum MyIconsSet {
Foo = \\"foo\\",
Bar = \\"bar\\",
}
export const MY_ICONS_SET_CODEPOINTS: { [key in MyIconsSet]: string } = {
[MyIconsSet.Foo]: \\"4265\\",
[MyIconsSet.Bar]: \\"1231\\",
};
"
`;

exports[`\`TS\` asset generator generates single quotes if format option passed 1`] = `
"export type MyIconsSetId =
| 'foo'
| 'bar';
export type MyIconsSetKey =
| 'Foo'
| 'Bar';
export enum MyIconsSet {
Foo = 'foo',
Bar = 'bar',
}
export const MY_ICONS_SET_CODEPOINTS: { [key in MyIconsSet]: string } = {
[MyIconsSet.Foo]: '4265',
[MyIconsSet.Bar]: '1231',
};
"
`;

exports[`\`TS\` asset generator prevents enum keys that start with digits 1`] = `
"export type MyIconsSetId =
| \\"1234\\"
| \\"5678\\";
export type MyIconsSetKey =
| \\"i1234\\"
| \\"i5678\\";
export enum MyIconsSet {
i1234 = \\"1234\\",
i5678 = \\"5678\\",
}
export const MY_ICONS_SET_CODEPOINTS: { [key in MyIconsSet]: string } = {
[MyIconsSet.i1234]: \\"undefined\\",
[MyIconsSet.i5678]: \\"undefined\\",
};
"
`;

exports[`\`TS\` asset generator prevents enum keys that start with digits when digits and chars 1`] = `
"export type MyIconsSetId =
| \\"1234asdf\\"
| \\"5678ab\\"
| \\"foo\\";
export type MyIconsSetKey =
| \\"i1234asdf\\"
| \\"i5678ab\\"
| \\"Foo\\";
export enum MyIconsSet {
i1234asdf = \\"1234asdf\\",
i5678ab = \\"5678ab\\",
Foo = \\"foo\\",
}
export const MY_ICONS_SET_CODEPOINTS: { [key in MyIconsSet]: string } = {
[MyIconsSet.i1234asdf]: \\"undefined\\",
[MyIconsSet.i5678ab]: \\"undefined\\",
[MyIconsSet.Foo]: \\"4265\\",
};
"
`;

exports[`\`TS\` asset generator renders expected TypeScript module content 1`] = `
"export type MyIconsSetId =
| \\"foo\\"
| \\"bar\\";
export type MyIconsSetKey =
| \\"Foo\\"
| \\"Bar\\";
export enum MyIconsSet {
Foo = \\"foo\\",
Bar = \\"bar\\",
}
export const MY_ICONS_SET_CODEPOINTS: { [key in MyIconsSet]: string } = {
[MyIconsSet.Foo]: \\"4265\\",
[MyIconsSet.Bar]: \\"1231\\",
};
"
`;
exports[`\`TS\` asset generator renders expected TypeScript module content 1`] = `"export enum MyIconsSet { Foo = \\"foo\\", Bar = \\"bar\\", } export type MyIconsSetId = \`MyIconsSet\`; export const MY_ICONS_SET_CODEPOINTS: { [key in MyIconsSetId]: string } = { \\"foo\\": \\"4265\\", \\"bar\\": \\"1231\\", }; "`;
Loading

0 comments on commit 0baf509

Please sign in to comment.