@@ -601,28 +601,48 @@ function formatOutput(state: ProcessingState): string {
601601 . filter ( line => line . startsWith ( 'import' ) )
602602 . forEach ( imp => imports . add ( imp . replace ( / ; + $ / , '' ) ) ) // Remove any existing semicolons
603603
604- // Get all non-import lines
604+ // Get all non-import lines and clean up semicolons
605605 const declarations = state . dtsLines
606606 . filter ( line => ! line . startsWith ( 'import' ) )
607- . map ( line => line . replace ( / ; + $ / , '' ) ) // Clean up any multiple semicolons
607+ . map ( ( line ) => {
608+ // Clean up any multiple semicolons and ensure all declarations end with one
609+ const trimmed = line . trim ( )
610+ if ( ! trimmed )
611+ return ''
612+
613+ // Don't add semicolons to export * statements or when one already exists
614+ if ( trimmed . startsWith ( 'export *' ) || trimmed . endsWith ( ';' ) ) {
615+ return trimmed
616+ }
617+
618+ // Add semicolon to type exports that don't have one
619+ if ( trimmed . startsWith ( 'export type' ) ) {
620+ return `${ trimmed } ;`
621+ }
622+
623+ return trimmed . replace ( / ; + $ / , ';' )
624+ } )
608625
609626 // Add default exports from state.defaultExports
610627 const defaultExports = Array . from ( state . defaultExports )
611- . map ( exp => exp . replace ( / ; + $ / , '' ) ) // Clean up any multiple semicolons
628+ . map ( exp => exp . trim ( ) . replace ( / ; + $ / , '; ' ) ) // Ensure single semicolon
612629
613- // Reconstruct the output with single semicolons where needed
630+ // Reconstruct the output with proper line breaks and semicolons
614631 const output = [
632+ // Add semicolons to imports
615633 ...Array . from ( imports ) . map ( imp => `${ imp } ;` ) ,
616634 '' ,
617- ...declarations . map ( decl => decl . trim ( ) !== '' ? `${ decl } ;` : '' ) ,
635+ // Filter empty lines and join declarations
636+ ...declarations . filter ( Boolean ) ,
618637 '' ,
619- ...defaultExports . map ( exp => `${ exp } ;` ) ,
638+ // Add default export
639+ ...defaultExports ,
620640 ]
621641
622- // Remove comments and normalize whitespace
642+ // Remove comments, normalize whitespace, and ensure single trailing newline
623643 return `${ output
624644 . map ( line => line . replace ( / \/ \* [ \s \S ] * ?\* \/ | \/ \/ .* / g, '' ) )
625- . filter ( Boolean )
645+ . filter ( line => line . trim ( ) || line === '' ) // Keep empty lines for spacing
626646 . join ( '\n' )
627647 } \n`
628648}
0 commit comments