Skip to content

Commit

Permalink
fix: indent generated augmentation interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 15, 2022
1 parent acd01ee commit 9af1021
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ export const genTypeObject = (obj: TypeObject, indent = '') => {
}), indent, '{}', false)
}

export const genInterface = (name: string, contents?: TypeObject, options: GenInterfaceOptions = {}) => {
export const genInterface = (name: string, contents?: TypeObject, options: GenInterfaceOptions = {}, indent = '') => {
const result = [
options.export && 'export',
`interface ${name}`,
options.extends && `extends ${Array.isArray(options.extends) ? options.extends.join(', ') : options.extends}`,
contents ? genTypeObject(contents) : '{}'
contents ? genTypeObject(contents, indent) : '{}'
].filter(Boolean).join(' ')
return result
}

export const genAugmentation = (specifier: string, interfaces?: Record<string, TypeObject | [TypeObject, Omit<GenInterfaceOptions, 'export'>]>) => {
return `declare module ${genString(specifier)} ${wrapInDelimiters(
Object.entries(interfaces || {}).map(
([key, entry]) => ' ' + (Array.isArray(entry) ? genInterface(key, ...entry) : genInterface(key, entry))
([key, entry]) => ' ' + (Array.isArray(entry) ? genInterface(key, ...entry, ' ') : genInterface(key, entry, {}, ' '))
)
)}`
}
10 changes: 8 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,16 @@ describe('genInterface', () => {
const genAugmentationTests: Array<{ input: Parameters<typeof genAugmentation>, code: string }> = [
{ input: ['@nuxt/utils'], code: 'declare module "@nuxt/utils" {}' },
{
input: ['@nuxt/utils', { MyInterface: {} }],
input: ['@nuxt/utils', {
MyInterface: {
'test?': 'string'
}
}],
code:
`declare module "@nuxt/utils" {
interface MyInterface {}
interface MyInterface {
test?: string
}
}`
},
{
Expand Down

0 comments on commit 9af1021

Please sign in to comment.