-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Declare consts with interfaces in d.ts files
- Loading branch information
Showing
3 changed files
with
30 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |