Skip to content

Commit a7e6e7f

Browse files
committed
chore: wip
1 parent 1ccd221 commit a7e6e7f

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

fixtures/output/example-0001.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,5 @@ export declare type ComplexUnionIntersection =
8989
& {
9090
metadata: Record<string, unknown>
9191
}
92+
93+
export default dts;

src/extract.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ export function processLine(line: string, state: ProcessingState): void {
311311
}
312312

313313
if (trimmedLine.startsWith('export default')) {
314-
state.defaultExport = `\n${trimmedLine};`
314+
state.defaultExport = trimmedLine.endsWith(';') ? trimmedLine : `${trimmedLine};`
315315
return
316316
}
317317

@@ -590,6 +590,16 @@ export function processDeclarationBlock(
590590
return
591591
}
592592

593+
if (
594+
declarationWithoutComments.startsWith('export default')
595+
) {
596+
// Handle export default statements
597+
state.defaultExport = declarationWithoutComments.endsWith(';')
598+
? declarationWithoutComments
599+
: `${declarationWithoutComments};`
600+
return
601+
}
602+
593603
if (
594604
declarationWithoutComments.startsWith('export const')
595605
|| declarationWithoutComments.startsWith('const')
@@ -1964,18 +1974,24 @@ function getDeclarationType(line: string): 'interface' | 'type' | 'const' | 'fun
19641974
* Format the final output with proper spacing and organization
19651975
*/
19661976
function formatOutput(state: ProcessingState): string {
1967-
const output = state.dtsLines
1977+
const outputLines = state.dtsLines
19681978
// Remove more than two consecutive empty lines
19691979
.reduce((acc, line, index, arr) => {
19701980
if (line === '' && arr[index - 1] === '' && arr[index - 2] === '') {
19711981
return acc
19721982
}
19731983
return [...acc, line]
19741984
}, [] as string[])
1975-
.join('\n')
19761985

1977-
// Ensure file ends with single newline
1978-
return `${output.trim()}\n`
1986+
// Ensure file ends with a single newline
1987+
let output = outputLines.join('\n').trim()
1988+
1989+
// Append default export at the end if it exists
1990+
if (state.defaultExport) {
1991+
output += `\n\n${state.defaultExport.trim()}`
1992+
}
1993+
1994+
return `${output}\n`
19791995
}
19801996

19811997
function getIndentation(line: string): string {

0 commit comments

Comments
 (0)