@@ -11,6 +11,7 @@ export async function extract(filePath: string): Promise<string> {
11
11
}
12
12
13
13
export function generateDtsTypes ( sourceCode : string ) : string {
14
+ console . log ( 'Starting generateDtsTypes' )
14
15
const lines = sourceCode . split ( '\n' )
15
16
const dtsLines : string [ ] = [ ]
16
17
const imports : string [ ] = [ ]
@@ -23,37 +24,46 @@ export function generateDtsTypes(sourceCode: string): string {
23
24
24
25
for ( let i = 0 ; i < lines . length ; i ++ ) {
25
26
const line = lines [ i ]
27
+ console . log ( `Processing line ${ i + 1 } : ${ line } ` )
26
28
27
29
// Handle comments
28
30
if ( line . trim ( ) . startsWith ( '/**' ) || line . trim ( ) . startsWith ( '*' ) || line . trim ( ) . startsWith ( '*/' ) ) {
29
31
if ( line . trim ( ) . startsWith ( '/**' ) )
30
32
lastCommentBlock = ''
31
33
lastCommentBlock += `${ line } \n`
34
+ console . log ( 'Comment line added to lastCommentBlock' )
32
35
continue
33
36
}
34
37
35
38
if ( line . trim ( ) . startsWith ( 'import' ) ) {
36
- imports . push ( line )
39
+ const processedImport = processImport ( line )
40
+ imports . push ( processedImport )
41
+ console . log ( `Processed import: ${ processedImport } ` )
37
42
continue
38
43
}
39
44
40
45
if ( line . trim ( ) . startsWith ( 'export' ) && ( line . includes ( '{' ) || line . includes ( '*' ) || line . includes ( 'from' ) ) ) {
41
46
exports . push ( line )
47
+ console . log ( `Export line added: ${ line } ` )
42
48
continue
43
49
}
44
50
45
51
if ( isMultiLineDeclaration || line . trim ( ) . startsWith ( 'export' ) ) {
46
52
currentDeclaration += `${ line } \n`
47
53
bracketCount += ( line . match ( / \{ / g) || [ ] ) . length - ( line . match ( / \} / g) || [ ] ) . length
54
+ console . log ( `Current declaration: ${ currentDeclaration . trim ( ) } , Bracket count: ${ bracketCount } ` )
48
55
49
56
if ( bracketCount === 0 || ( i === lines . length - 1 ) ) {
50
57
if ( lastCommentBlock ) {
51
58
dtsLines . push ( lastCommentBlock . trimEnd ( ) )
59
+ console . log ( `Comment block added to dtsLines: ${ lastCommentBlock . trimEnd ( ) } ` )
52
60
lastCommentBlock = ''
53
61
}
54
62
const processed = processDeclaration ( currentDeclaration . trim ( ) )
55
- if ( processed )
63
+ if ( processed ) {
56
64
dtsLines . push ( processed )
65
+ console . log ( `Processed declaration added to dtsLines: ${ processed } ` )
66
+ }
57
67
isMultiLineDeclaration = false
58
68
currentDeclaration = ''
59
69
bracketCount = 0
@@ -73,121 +83,119 @@ export function generateDtsTypes(sourceCode: string): string {
73
83
...exports ,
74
84
] . filter ( Boolean ) . join ( '\n' ) )
75
85
86
+ console . log ( 'Final result:' , result )
76
87
return result
77
88
}
78
89
79
- function processDeclaration ( declaration : string ) : string {
80
- // Remove comments
81
- const declWithoutComments = declaration . replace ( / \/ \/ .* $ / gm, '' ) . trim ( )
82
- const trimmed = declWithoutComments
83
-
84
- if ( trimmed . startsWith ( 'export const' ) ) {
85
- return processConstDeclaration ( trimmed )
90
+ function processImport ( importLine : string ) : string {
91
+ console . log ( `Processing import: ${ importLine } ` )
92
+ 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
86
96
}
87
- else if ( trimmed . startsWith ( 'export interface' ) ) {
88
- return processInterfaceDeclaration ( trimmed )
97
+ return importLine
98
+ }
99
+
100
+ function processDeclaration ( declaration : string ) : string {
101
+ console . log ( `Processing declaration: ${ declaration } ` )
102
+ if ( declaration . startsWith ( 'export const' ) ) {
103
+ return processConstDeclaration ( declaration )
89
104
}
90
- else if ( trimmed . startsWith ( 'export type ' ) ) {
91
- return processTypeDeclaration ( trimmed )
105
+ else if ( declaration . startsWith ( 'export interface ' ) ) {
106
+ return processInterfaceDeclaration ( declaration )
92
107
}
93
- else if ( trimmed . startsWith ( 'export function' ) || trimmed . startsWith ( 'export async function ') ) {
94
- return processFunctionDeclaration ( trimmed )
108
+ else if ( declaration . startsWith ( 'export type ' ) ) {
109
+ return processTypeDeclaration ( declaration )
95
110
}
96
- else if ( trimmed . startsWith ( 'export default ' ) ) {
97
- return ` ${ trimmed } ;`
111
+ else if ( declaration . startsWith ( 'export function' ) || declaration . startsWith ( 'export async function ') ) {
112
+ return processFunctionDeclaration ( declaration )
98
113
}
99
- else if ( trimmed . startsWith ( 'export' ) ) {
100
- return trimmed . endsWith ( ';' ) ? trimmed : `${ trimmed } ;`
114
+ else if ( declaration . startsWith ( 'export default ' ) ) {
115
+ return `${ declaration } ;`
101
116
}
102
-
103
- return ''
117
+ console . log ( `Declaration not processed: ${ declaration } ` )
118
+ return declaration
104
119
}
105
120
106
121
function processConstDeclaration ( declaration : string ) : string {
122
+ console . log ( `Processing const declaration: ${ declaration } ` )
107
123
const equalIndex = declaration . indexOf ( '=' )
108
124
if ( equalIndex === - 1 )
109
- return declaration // No value assigned
125
+ return declaration
110
126
111
127
const name = declaration . slice ( 0 , equalIndex ) . trim ( ) . replace ( 'export const' , '' ) . trim ( )
112
128
const value = declaration . slice ( equalIndex + 1 ) . trim ( ) . replace ( / ; $ / , '' )
113
129
114
- // Handle object literals
115
130
if ( value . startsWith ( '{' ) ) {
116
131
const objectType = parseObjectLiteral ( value )
117
- return `export declare const ${ name } : ${ objectType } ;`
132
+ const result = `export declare const ${ name } : ${ objectType } ;`
133
+ console . log ( `Processed const declaration: ${ result } ` )
134
+ return result
118
135
}
119
136
else {
120
- const valueType = preserveValueType ( value )
121
- return `export declare const ${ name } : ${ valueType } ;`
137
+ const valueType = inferValueType ( value )
138
+ const result = `export declare const ${ name } : ${ valueType } ;`
139
+ console . log ( `Processed const declaration: ${ result } ` )
140
+ return result
122
141
}
123
142
}
124
143
125
144
function processInterfaceDeclaration ( declaration : string ) : string {
126
- const lines = declaration . split ( '\n' )
127
- const interfaceName = lines [ 0 ] . split ( ' interface') [ 1 ] . split ( '{' ) [ 0 ] . trim ( )
128
- const interfaceBody = lines . slice ( 1 , - 1 ) . join ( '\n' )
129
- return `export declare interface ${ interfaceName } {\n ${ interfaceBody } \n}`
145
+ console . log ( `Processing interface declaration: ${ declaration } ` )
146
+ const result = declaration . replace ( 'export interface', 'export declare interface' )
147
+ console . log ( `Processed interface declaration: ${ result } ` )
148
+ return result
130
149
}
131
150
132
151
function processTypeDeclaration ( declaration : string ) : string {
133
- return declaration . replace ( 'export type' , 'export declare type' )
152
+ console . log ( `Processing type declaration: ${ declaration } ` )
153
+ const result = declaration . replace ( 'export type' , 'export declare type' )
154
+ console . log ( `Processed type declaration: ${ result } ` )
155
+ return result
134
156
}
135
157
136
158
function processFunctionDeclaration ( declaration : string ) : string {
137
- // Remove the function body
138
- const functionSignature = declaration . split ( '{' ) [ 0 ] . trim ( )
139
- return `export declare ${ functionSignature . replace ( 'export ' , '' ) } ;`
159
+ console . log ( `Processing function declaration: ${ declaration } ` )
160
+ const functionBody = declaration . match ( / \{ [ \s \S ] * \} / ) ?. [ 0 ] || ''
161
+ const result = `export declare ${ declaration . replace ( functionBody , '' ) . trim ( ) } ;`
162
+ console . log ( `Processed function declaration: ${ result } ` )
163
+ return result
140
164
}
141
165
142
166
function parseObjectLiteral ( objectLiteral : string ) : string {
143
- // Remove the opening and closing braces and newlines
144
- const content = objectLiteral . replace ( / ^ \{ | \} $ / g, '' ) . replace ( / \n / g, ' ' ) . trim ( )
145
-
146
- const pairs = content . split ( ',' ) . map ( pair => pair . trim ( ) ) . filter ( Boolean )
147
-
148
- const parsedProperties = pairs . map ( ( pair ) => {
149
- const [ key , ...valueParts ] = pair . split ( ':' )
150
- const value = valueParts . join ( ':' ) . trim ( )
151
-
152
- if ( value . startsWith ( '\'' ) || value . startsWith ( '"' ) ) {
153
- // For string literals, keep as is
154
- return ` ${ key . trim ( ) } : ${ value } ;`
155
- }
156
- else {
157
- // For other types, use preserveValueType
158
- const preservedValue = preserveValueType ( value )
159
- return ` ${ key . trim ( ) } : ${ preservedValue } ;`
160
- }
167
+ console . log ( `Parsing object literal: ${ objectLiteral } ` )
168
+ const content = objectLiteral . replace ( / ^ \{ | \} $ / g, '' ) . split ( ',' ) . map ( pair => pair . trim ( ) )
169
+ const parsedProperties = content . map ( ( pair ) => {
170
+ const [ key , value ] = pair . split ( ':' ) . map ( p => p . trim ( ) )
171
+ return ` ${ key } : ${ inferValueType ( value ) } ;`
161
172
} )
162
-
163
- return `{\n${ parsedProperties . join ( '\n' ) } \n}`
173
+ const result = `{\n${ parsedProperties . join ( '\n' ) } \n}`
174
+ console . log ( `Parsed object literal: ${ result } ` )
175
+ return result
164
176
}
165
177
166
- function preserveValueType ( value : string ) : string {
167
- value = value . trim ( )
168
- if ( value === 'true' || value === 'false' ) {
169
- return 'boolean' // Use boolean type for true and false
170
- }
171
- else if ( ! Number . isNaN ( Number ( value ) ) ) {
172
- return 'number' // Use number type for numeric values
173
- }
174
- else if ( value . startsWith ( '[' ) && value . endsWith ( ']' ) ) {
175
- return 'any[]' // Generic array type
176
- }
177
- else if ( ( value . startsWith ( '\'' ) && value . endsWith ( '\'' ) ) || ( value . startsWith ( '"' ) && value . endsWith ( '"' ) ) ) {
178
- return 'string' // Use string type for string literals
179
- }
180
- else {
181
- return 'any' // Default to any for other cases
182
- }
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
183
188
}
184
189
185
190
function cleanOutput ( output : string ) : string {
186
- return output
187
- . replace ( / \{ \s * \} / g, '{}' ) // Replace empty objects with {}
188
- . replace ( / \s * ; \s * (? = \} | $ ) / g, ';' ) // Clean up semicolons before closing braces or end of string
189
- . replace ( / \n + / g, '\n' ) // Remove multiple consecutive newlines
190
- . replace ( / ; \n \} / g, ';\n }' ) // Add indentation to closing brace of object literals
191
- . replace ( / \{ ; / g, '{' ) // Remove unnecessary semicolons after opening braces
191
+ console . log ( 'Cleaning output' )
192
+ const result = output
193
+ . replace ( / \{ \s * \} / g, '{}' )
194
+ . replace ( / \s * ; \s * (? = \} | $ ) / g, ';' )
195
+ . replace ( / \n + / g, '\n' )
196
+ . replace ( / ; \n \} / g, ';\n }' )
197
+ . replace ( / \{ ; / g, '{' )
192
198
. trim ( )
199
+ console . log ( 'Cleaned output:' , result )
200
+ return result
193
201
}
0 commit comments