@@ -622,41 +622,24 @@ function processImports(line: string, state: ImportTrackingState): void {
622
622
}
623
623
}
624
624
625
- function processDeclarationBlock (
626
- lines : string [ ] ,
627
- comments : string [ ] ,
628
- state : ProcessingState ,
629
- ) : void {
630
- console . log ( 'Processing declaration block:' , { lines, comments } )
631
-
632
- // Only include JSDoc comments
633
- const jsdocComments = comments . filter ( isJSDocComment )
634
- console . log ( 'Filtered JSDoc comments:' , jsdocComments )
635
-
636
- // Add JSDoc comments directly before the declaration
637
- if ( jsdocComments . length > 0 ) {
638
- // Directly add the comments without the extra newline
639
- state . dtsLines . push ( ...jsdocComments . map ( comment => comment . trimEnd ( ) ) )
640
- }
641
-
642
- // Remove any non-JSDoc comments that might have slipped through
643
- const cleanedLines = lines . map ( ( line ) => {
644
- const commentIndex = line . indexOf ( '//' )
645
- return commentIndex !== - 1 ? line . substring ( 0 , commentIndex ) . trim ( ) : line
646
- } ) . filter ( Boolean )
647
-
648
- const declaration = cleanedLines . join ( '\n' ) . trim ( )
625
+ function processTypeDeclaration ( declaration : string , isExported = true ) : string {
626
+ const lines = declaration . split ( '\n' )
627
+ const firstLine = lines [ 0 ] . trim ( )
649
628
650
- if ( ! declaration ) {
651
- console . log ( 'Empty declaration, skipping' )
652
- return
629
+ // Preserve direct type exports
630
+ if ( firstLine . startsWith ( 'export type {' ) ) {
631
+ return declaration
653
632
}
654
633
655
- // Remove leading comments and whitespace when checking its type
656
- const declarationWithoutComments = removeLeadingComments ( declaration ) . trimStart ( )
634
+ // Only modify the first line
635
+ const prefix = isExported ? 'export declare' : 'declare'
636
+ const modifiedFirstLine = lines [ 0 ] . replace (
637
+ / ^ ( \s * ) (?: e x p o r t \s + ) ? t y p e (? ! \s * \{ ) / ,
638
+ `$1${ prefix } type` ,
639
+ )
657
640
658
- // Process the declaration as before
659
- processSpecificDeclaration ( declarationWithoutComments , declaration , state )
641
+ // Return original declaration with only the first line modified
642
+ return [ modifiedFirstLine , ... lines . slice ( 1 ) ] . join ( '\n' )
660
643
}
661
644
662
645
function processSpecificDeclaration (
@@ -905,45 +888,70 @@ function processFunctionDeclaration(
905
888
* Process interface declarations
906
889
*/
907
890
function processInterfaceDeclaration ( declaration : string , isExported = true ) : string {
891
+ // Split into lines while preserving all formatting and comments
908
892
const lines = declaration . split ( '\n' )
909
893
910
- // Only modify the first line to add 'declare' if needed
894
+ // Only modify the first line to add necessary keywords
911
895
const firstLine = lines [ 0 ]
912
896
const prefix = isExported ? 'export declare' : 'declare'
913
897
914
- // Replace 'export interface' or 'interface' with the correct prefix
898
+ // Replace only the ' interface' or 'export interface' part
915
899
const modifiedFirstLine = firstLine . replace (
916
900
/ ^ ( \s * ) (?: e x p o r t \s + ) ? i n t e r f a c e / ,
917
901
`$1${ prefix } interface` ,
918
902
)
919
903
920
- // Return the modified first line with all other lines unchanged
904
+ // Return original declaration with only the first line modified
921
905
return [ modifiedFirstLine , ...lines . slice ( 1 ) ] . join ( '\n' )
922
906
}
923
907
924
908
/**
925
909
* Process type declarations
926
910
*/
927
- function processTypeDeclaration ( declaration : string , isExported = true ) : string {
928
- const lines = declaration . split ( '\n' )
929
- const firstLine = lines [ 0 ] . trim ( )
911
+ function processDeclarationBlock (
912
+ lines : string [ ] ,
913
+ comments : string [ ] ,
914
+ state : ProcessingState ,
915
+ ) : void {
916
+ const declarationText = lines . join ( '\n' )
917
+ const cleanedDeclaration = removeLeadingComments ( declarationText ) . trimStart ( )
930
918
931
- // Preserve direct type exports (export type { X })
932
- if ( firstLine . startsWith ( 'export type {' ) ) {
933
- return declaration
919
+ if (
920
+ cleanedDeclaration . startsWith ( 'interface' )
921
+ || cleanedDeclaration . startsWith ( 'export interface' )
922
+ || cleanedDeclaration . startsWith ( 'type' )
923
+ || cleanedDeclaration . startsWith ( 'export type' )
924
+ ) {
925
+ // Process the declaration while preserving all formatting and comments
926
+ const isInterface = cleanedDeclaration . startsWith ( 'interface' ) || cleanedDeclaration . startsWith ( 'export interface' )
927
+ const isExported = declarationText . trimStart ( ) . startsWith ( 'export' )
928
+
929
+ const processed = isInterface
930
+ ? processInterfaceDeclaration ( declarationText , isExported )
931
+ : processTypeDeclaration ( declarationText , isExported )
932
+
933
+ state . dtsLines . push ( processed )
934
+ return
934
935
}
935
936
936
- // Only modify the first line to add 'declare' if needed
937
- const prefix = isExported ? 'export declare' : 'declare'
937
+ // Handle other declarations as before...
938
+ const jsdocComments = comments . filter ( isJSDocComment )
939
+ if ( jsdocComments . length > 0 ) {
940
+ state . dtsLines . push ( ...jsdocComments . map ( comment => comment . trimEnd ( ) ) )
941
+ }
938
942
939
- // Replace 'export type' or 'type' with the correct prefix
940
- const modifiedFirstLine = lines [ 0 ] . replace (
941
- / ^ ( \s * ) (?: e x p o r t \s + ) ? t y p e (? ! \s * \{ ) / ,
942
- `$1${ prefix } type` ,
943
- )
943
+ const cleanedLines = lines . map ( ( line ) => {
944
+ const commentIndex = line . indexOf ( '//' )
945
+ return commentIndex !== - 1 ? line . substring ( 0 , commentIndex ) . trim ( ) : line
946
+ } ) . filter ( Boolean )
944
947
945
- // Return the modified first line with all other lines unchanged
946
- return [ modifiedFirstLine , ...lines . slice ( 1 ) ] . join ( '\n' )
948
+ const declaration = cleanedLines . join ( '\n' ) . trim ( )
949
+ if ( ! declaration ) {
950
+ return
951
+ }
952
+
953
+ const declarationWithoutComments = removeLeadingComments ( declaration ) . trimStart ( )
954
+ processSpecificDeclaration ( declarationWithoutComments , declaration , state )
947
955
}
948
956
949
957
function processSourceFile ( content : string , state : ProcessingState ) : void {
0 commit comments