@@ -10,89 +10,92 @@ export async function extract(filePath: string): Promise<string> {
1010 }
1111}
1212
13- export function generateDtsTypes ( sourceCode : string ) : string {
13+ function generateDtsTypes ( sourceCode : string ) : string {
1414 console . log ( 'Starting generateDtsTypes' )
1515 const lines = sourceCode . split ( '\n' )
1616 const dtsLines : string [ ] = [ ]
1717 const imports : string [ ] = [ ]
1818 const exports : string [ ] = [ ]
19+ let defaultExport = ''
1920
2021 let isMultiLineDeclaration = false
2122 let currentDeclaration = ''
2223 let bracketCount = 0
2324 let lastCommentBlock = ''
2425
2526 for ( let i = 0 ; i < lines . length ; i ++ ) {
26- const line = lines [ i ]
27+ const line = lines [ i ] . trim ( )
2728 console . log ( `Processing line ${ i + 1 } : ${ line } ` )
2829
29- // Handle comments
30- if ( line . trim ( ) . startsWith ( '/**' ) || line . trim ( ) . startsWith ( '*' ) || line . trim ( ) . startsWith ( '*/' ) ) {
31- if ( line . trim ( ) . startsWith ( '/**' ) )
30+ if ( line . startsWith ( '/**' ) || line . startsWith ( '*' ) || line . startsWith ( '*/' ) ) {
31+ if ( line . startsWith ( '/**' ) )
3232 lastCommentBlock = ''
3333 lastCommentBlock += `${ line } \n`
3434 console . log ( 'Comment line added to lastCommentBlock' )
3535 continue
3636 }
3737
38- if ( line . trim ( ) . startsWith ( 'import' ) ) {
38+ if ( line . startsWith ( 'import' ) ) {
3939 const processedImport = processImport ( line )
4040 imports . push ( processedImport )
4141 console . log ( `Processed import: ${ processedImport } ` )
4242 continue
4343 }
4444
45- if ( line . trim ( ) . startsWith ( 'export' ) && ( line . includes ( '{' ) || line . includes ( '*' ) || line . includes ( 'from' ) ) ) {
46- exports . push ( line )
47- console . log ( `Export line added : ${ line } ` )
45+ if ( line . startsWith ( 'export default' ) ) {
46+ defaultExport = ` ${ line } ;`
47+ console . log ( `Default export found : ${ defaultExport } ` )
4848 continue
4949 }
5050
51- if ( isMultiLineDeclaration || line . trim ( ) . startsWith ( 'export' ) ) {
51+ if ( line . startsWith ( 'export const' ) ) {
52+ isMultiLineDeclaration = true
53+ currentDeclaration = ''
54+ bracketCount = 0
55+ }
56+
57+ if ( isMultiLineDeclaration ) {
5258 currentDeclaration += `${ line } \n`
5359 bracketCount += ( line . match ( / \{ / g) || [ ] ) . length - ( line . match ( / \} / g) || [ ] ) . length
54- console . log ( `Current declaration: ${ currentDeclaration . trim ( ) } , Bracket count: ${ bracketCount } ` )
5560
56- if ( bracketCount === 0 || ( i === lines . length - 1 ) ) {
61+ if ( bracketCount === 0 || i === lines . length - 1 ) {
5762 if ( lastCommentBlock ) {
5863 dtsLines . push ( lastCommentBlock . trimEnd ( ) )
5964 console . log ( `Comment block added to dtsLines: ${ lastCommentBlock . trimEnd ( ) } ` )
6065 lastCommentBlock = ''
6166 }
62- const processed = processDeclaration ( currentDeclaration . trim ( ) )
67+ const processed = processConstDeclaration ( currentDeclaration . trim ( ) )
6368 if ( processed ) {
6469 dtsLines . push ( processed )
65- console . log ( `Processed declaration added to dtsLines: ${ processed } ` )
70+ console . log ( `Processed const declaration added to dtsLines: ${ processed } ` )
6671 }
6772 isMultiLineDeclaration = false
6873 currentDeclaration = ''
69- bracketCount = 0
7074 }
71- else {
72- isMultiLineDeclaration = true
75+ }
76+ else if ( line . startsWith ( 'export' ) ) {
77+ if ( lastCommentBlock ) {
78+ dtsLines . push ( lastCommentBlock . trimEnd ( ) )
79+ console . log ( `Comment block added to dtsLines: ${ lastCommentBlock . trimEnd ( ) } ` )
80+ lastCommentBlock = ''
81+ }
82+ const processed = processDeclaration ( line )
83+ if ( processed ) {
84+ dtsLines . push ( processed )
85+ console . log ( `Processed declaration added to dtsLines: ${ processed } ` )
7386 }
7487 }
7588 }
7689
77- // Combine imports, declarations, and exports
78- const result = cleanOutput ( [
79- ...imports ,
80- '' ,
81- ...dtsLines ,
82- '' ,
83- ...exports ,
84- ] . filter ( Boolean ) . join ( '\n' ) )
85-
90+ const result = cleanOutput ( [ ...imports , '' , ...dtsLines , '' , ...exports , defaultExport ] . filter ( Boolean ) . join ( '\n' ) )
8691 console . log ( 'Final result:' , result )
8792 return result
8893}
8994
9095function processImport ( importLine : string ) : string {
9196 console . log ( `Processing import: ${ importLine } ` )
9297 if ( importLine . includes ( 'type' ) ) {
93- const processed = importLine . replace ( 'import' , 'import type' ) . replace ( 'type type' , 'type' )
94- console . log ( `Processed import: ${ processed } ` )
95- return processed
98+ return importLine . replace ( 'import' , 'import type' ) . replace ( 'type type' , 'type' )
9699 }
97100 return importLine
98101}
@@ -120,30 +123,25 @@ function processDeclaration(declaration: string): string {
120123
121124function processConstDeclaration ( declaration : string ) : string {
122125 console . log ( `Processing const declaration: ${ declaration } ` )
123- const equalIndex = declaration . indexOf ( '=' )
124- if ( equalIndex === - 1 )
125- return declaration
126-
127- const name = declaration . slice ( 0 , equalIndex ) . trim ( ) . replace ( 'export const' , '' ) . trim ( )
128- const value = declaration . slice ( equalIndex + 1 ) . trim ( ) . replace ( / ; $ / , '' )
129-
130- if ( value . startsWith ( '{' ) ) {
131- const objectType = parseObjectLiteral ( value )
132- const result = `export declare const ${ name } : ${ objectType } ;`
133- console . log ( `Processed const declaration: ${ result } ` )
134- return result
135- }
136- else {
137- const valueType = inferValueType ( value )
138- const result = `export declare const ${ name } : ${ valueType } ;`
139- console . log ( `Processed const declaration: ${ result } ` )
140- return result
141- }
126+ const lines = declaration . split ( '\n' )
127+ 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 ) )
130+
131+ const properties = lines . slice ( 1 , - 1 ) . map ( ( line ) => {
132+ const [ key , value ] = line . split ( ':' ) . map ( part => part . trim ( ) )
133+ return ` ${ key } : ${ value . replace ( ',' , ';' ) } `
134+ } ) . join ( '\n' )
135+
136+ return `export declare const ${ name } : ${ type } {\n${ properties } \n};`
142137}
143138
144139function processInterfaceDeclaration ( declaration : string ) : string {
145140 console . log ( `Processing interface declaration: ${ declaration } ` )
146- const result = declaration . replace ( 'export interface' , 'export declare interface' )
141+ const lines = declaration . split ( '\n' )
142+ const interfaceName = lines [ 0 ] . split ( 'interface' ) [ 1 ] . split ( '{' ) [ 0 ] . trim ( )
143+ const interfaceBody = lines . slice ( 1 , - 1 ) . map ( line => ` ${ line . trim ( ) } ` ) . join ( '\n' )
144+ const result = `export declare interface ${ interfaceName } {\n${ interfaceBody } \n}`
147145 console . log ( `Processed interface declaration: ${ result } ` )
148146 return result
149147}
@@ -157,8 +155,8 @@ function processTypeDeclaration(declaration: string): string {
157155
158156function processFunctionDeclaration ( declaration : string ) : string {
159157 console . log ( `Processing function declaration: ${ declaration } ` )
160- const functionBody = declaration . match ( / \{ [ \s \S ] * \} / ) ?. [ 0 ] || ''
161- const result = `export declare ${ declaration . replace ( functionBody , '' ) . trim ( ) } ;`
158+ const functionSignature = declaration . split ( '{' ) [ 0 ] . trim ( )
159+ const result = `export declare ${ functionSignature . replace ( 'export ' , '' ) } ;`
162160 console . log ( `Processed function declaration: ${ result } ` )
163161 return result
164162}
@@ -168,23 +166,27 @@ function parseObjectLiteral(objectLiteral: string): string {
168166 const content = objectLiteral . replace ( / ^ \{ | \} $ / g, '' ) . split ( ',' ) . map ( pair => pair . trim ( ) )
169167 const parsedProperties = content . map ( ( pair ) => {
170168 const [ key , value ] = pair . split ( ':' ) . map ( p => p . trim ( ) )
171- return ` ${ key } : ${ inferValueType ( value ) } ;`
169+ console . log ( `Parsing property: key=${ key } , value=${ value } ` )
170+ return ` ${ key } : ${ value } ;`
172171 } )
173172 const result = `{\n${ parsedProperties . join ( '\n' ) } \n}`
174173 console . log ( `Parsed object literal: ${ result } ` )
175174 return result
176175}
177176
178- function inferValueType ( value : string ) : string {
179- console . log ( `Inferring value type for: ${ value } ` )
180- if ( value === 'true' || value === 'false' )
181- return value
182- if ( ! Number . isNaN ( Number ( value ) ) )
183- return value
184- if ( value . startsWith ( '\'' ) || value . startsWith ( '"' ) )
185- return value
186- console . log ( `Defaulting to string for: ${ value } ` )
187- return 'string' // Default to string for other cases
177+ function inferType ( properties : string [ ] ) : string {
178+ const types = properties . map ( ( prop ) => {
179+ const value = prop . split ( ':' ) [ 1 ] . trim ( )
180+ if ( value . startsWith ( '\'' ) || value . startsWith ( '"' ) )
181+ return 'string'
182+ if ( value === 'true' || value === 'false' )
183+ return 'boolean'
184+ if ( ! isNaN ( Number ( value ) ) )
185+ return 'number'
186+ return 'any'
187+ } )
188+ const uniqueTypes = [ ...new Set ( types ) ]
189+ return uniqueTypes . length === 1 ? `{ [key: string]: ${ uniqueTypes [ 0 ] } }` : '{ [key: string]: any }'
188190}
189191
190192function cleanOutput ( output : string ) : string {
0 commit comments