Skip to content

Commit

Permalink
Declare consts with interfaces in d.ts files
Browse files Browse the repository at this point in the history
  • Loading branch information
jaalah committed Apr 19, 2024
1 parent 90c8376 commit cedba7e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
3 changes: 1 addition & 2 deletions config/formats/registerJavascriptNested.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ export const registerJavascriptNested = () =>
formatter(formatterArguments) {
const tokens = formatterArguments.dictionary.tokens;
const transformedTokens = convertTokensToFlatObject(tokens);
const transformedOutputParts = generateNestedOutput({
const transformedOutput = generateNestedOutput({
transformedTokens,
formatterType: 'javascript'
});

const transformedOutput = transformedOutputParts.join('\n\n');
return `
/**
* Do not edit directly
Expand Down
3 changes: 1 addition & 2 deletions config/formats/registerTypescriptNestedDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ export const registerTypescriptNestedDefinitions = () =>
formatter(formatterArguments) {
const tokens = formatterArguments.dictionary.tokens;
const transformedTokens = convertTokensToFlatObject(tokens);
const transformedOutputParts = generateNestedOutput({
const transformedOutput = generateNestedOutput({
transformedTokens,
formatterType: 'typescript'
});

const transformedOutput = transformedOutputParts.join('\n\n');
return `
/**
* Do not edit directly
Expand Down
43 changes: 28 additions & 15 deletions config/utilities/generateNestedOutput.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
// Helper function to generate transformed output parts
export function generateNestedOutput({transformedTokens, formatterType}) {
export function generateNestedOutput({ transformedTokens, formatterType }: { transformedTokens: any, formatterType: 'typescript' | 'javascript' }) {
return Object.keys(transformedTokens).map(key => {
// Generate JSON string of the object, then format it for JS object syntax
const topLevelObjectString = JSON.stringify(
transformedTokens[key],
null,
2
transformedTokens[key], null, 2
).replace(/"([^"]+)":/g, (match, key) => `${key}:`);

const exports: string[] = [];
const isInterface = formatterType === 'typescript';
const declarationOrExport = formatterType === 'typescript' ? `export interface ${key}Types` : `export const ${key} =`;
exports.push(`${declarationOrExport} ${topLevelObjectString}${isInterface ? '' : ';'}`);

// Handling one level deeper
if (isInterface) {
// Create TypeScript interface
const interfaceName = `${key}Types`;
exports.push(`export interface ${interfaceName} ${topLevelObjectString}`);

// Declare const with the interface type
exports.push(`declare const ${key}: ${interfaceName};`);
} else {
// JavaScript export statement
exports.push(`export const ${key} = ${topLevelObjectString};`);
}

// Handling one level deeper for JavaScript objects or TypeScript interfaces
Object.entries(transformedTokens[key]).forEach(([subKey, subValue]) => {
const subObjectString = JSON.stringify(
subValue,
null,
2
subValue, null, 2
).replace(/"([^"]+)":/g, (match, key) => `${key}:`);

const subDeclarationOrExport = formatterType === 'typescript' ? `export interface ${subKey}Types` : `export const ${subKey} =`;
exports.push(`${subDeclarationOrExport} ${subObjectString}${isInterface ? '' : ';'}`);
if (isInterface) {
// Create sub-interface and declare const for each subkey
const subInterfaceName = `${subKey}Types`;
exports.push(`export interface ${subInterfaceName} ${subObjectString}`);
exports.push(`declare const ${subKey}: ${subInterfaceName};`);
} else {
// JavaScript export for subkeys
exports.push(`export const ${subKey} = ${subObjectString};`);
}
});

return exports.join('\n\n');
});
}
}).join('\n\n');
}

0 comments on commit cedba7e

Please sign in to comment.