@@ -24,41 +24,36 @@ function generateDtsTypes(sourceCode: string): string {
2424 let lastCommentBlock = ''
2525
2626 for ( let i = 0 ; i < lines . length ; i ++ ) {
27- const line = lines [ i ] . trim ( )
27+ const line = lines [ i ]
2828 console . log ( `Processing line ${ i + 1 } : ${ line } ` )
2929
30- if ( line . startsWith ( '/**' ) || line . startsWith ( '*' ) || line . startsWith ( '*/' ) ) {
31- if ( line . startsWith ( '/**' ) )
30+ if ( line . trim ( ) . startsWith ( '/**' ) || line . trim ( ) . startsWith ( '*' ) || line . trim ( ) . startsWith ( '*/' ) ) {
31+ if ( line . trim ( ) . startsWith ( '/**' ) )
3232 lastCommentBlock = ''
3333 lastCommentBlock += `${ line } \n`
3434 console . log ( 'Comment line added to lastCommentBlock' )
3535 continue
3636 }
3737
38- if ( line . startsWith ( 'import' ) ) {
38+ if ( line . trim ( ) . startsWith ( 'import' ) ) {
3939 const processedImport = processImport ( line )
4040 imports . push ( processedImport )
4141 console . log ( `Processed import: ${ processedImport } ` )
4242 continue
4343 }
4444
45- if ( line . startsWith ( 'export default' ) ) {
46- defaultExport = `${ line } ;`
45+ if ( line . trim ( ) . startsWith ( 'export default' ) ) {
46+ defaultExport = `${ line . trim ( ) } ;`
4747 console . log ( `Default export found: ${ defaultExport } ` )
4848 continue
4949 }
5050
51- if ( line . startsWith ( 'export const' ) ) {
52- isMultiLineDeclaration = true
53- currentDeclaration = ''
54- bracketCount = 0
55- }
56-
57- if ( isMultiLineDeclaration ) {
51+ if ( line . trim ( ) . startsWith ( 'export const' ) || isMultiLineDeclaration ) {
5852 currentDeclaration += `${ line } \n`
5953 bracketCount += ( line . match ( / \{ / g) || [ ] ) . length - ( line . match ( / \} / g) || [ ] ) . length
54+ isMultiLineDeclaration = bracketCount > 0
6055
61- if ( bracketCount === 0 || i === lines . length - 1 ) {
56+ if ( ! isMultiLineDeclaration ) {
6257 if ( lastCommentBlock ) {
6358 dtsLines . push ( lastCommentBlock . trimEnd ( ) )
6459 console . log ( `Comment block added to dtsLines: ${ lastCommentBlock . trimEnd ( ) } ` )
@@ -69,11 +64,11 @@ function generateDtsTypes(sourceCode: string): string {
6964 dtsLines . push ( processed )
7065 console . log ( `Processed const declaration added to dtsLines: ${ processed } ` )
7166 }
72- isMultiLineDeclaration = false
7367 currentDeclaration = ''
68+ bracketCount = 0
7469 }
7570 }
76- else if ( line . startsWith ( 'export' ) ) {
71+ else if ( line . trim ( ) . startsWith ( 'export' ) ) {
7772 if ( lastCommentBlock ) {
7873 dtsLines . push ( lastCommentBlock . trimEnd ( ) )
7974 console . log ( `Comment block added to dtsLines: ${ lastCommentBlock . trimEnd ( ) } ` )
@@ -125,15 +120,15 @@ function processConstDeclaration(declaration: string): string {
125120 console . log ( `Processing const declaration: ${ declaration } ` )
126121 const lines = declaration . split ( '\n' )
127122 const firstLine = lines [ 0 ]
128- const name = firstLine . split ( 'export const' ) [ 1 ] . split ( '=' ) [ 0 ] . trim ( )
129- const type = firstLine . includes ( ':' ) ? firstLine . split ( ':' ) [ 1 ] . split ( '=' ) [ 0 ] . trim ( ) : inferType ( lines . slice ( 1 , - 1 ) )
123+ const name = firstLine . split ( 'export const' ) [ 1 ] . split ( '=' ) [ 0 ] . trim ( ) . split ( ':' ) [ 0 ] . trim ( )
130124
131125 const properties = lines . slice ( 1 , - 1 ) . map ( ( line ) => {
132- const [ key , value ] = line . split ( ':' ) . map ( part => part . trim ( ) )
133- return ` ${ key } : ${ value . replace ( ',' , ';' ) } `
126+ const [ key , ...valueParts ] = line . split ( ':' )
127+ const value = valueParts . join ( ':' ) . trim ( ) . replace ( ',' , '' )
128+ return ` ${ key . trim ( ) } : ${ value } ;`
134129 } ) . join ( '\n' )
135130
136- return `export declare const ${ name } : ${ type } {\n${ properties } \n};`
131+ return `export declare const ${ name } : {\n${ properties } \n};`
137132}
138133
139134function processInterfaceDeclaration ( declaration : string ) : string {
@@ -195,8 +190,9 @@ function cleanOutput(output: string): string {
195190 . replace ( / \{ \s * \} / g, '{}' )
196191 . replace ( / \s * ; \s * (? = \} | $ ) / g, ';' )
197192 . replace ( / \n + / g, '\n' )
198- . replace ( / ; \n \} / g, ';\n }' )
193+ . replace ( / ; \n \} / g, ';\n}' )
199194 . replace ( / \{ ; / g, '{' )
195+ . replace ( / \} ; \n / g, '}\n\n' ) // Add an extra line break after each declaration
200196 . trim ( )
201197 console . log ( 'Cleaned output:' , result )
202198 return result
0 commit comments