1
+ /* eslint-disable no-console */
1
2
const DEBUG = true // Set to false to disable debug logs
2
3
3
- export async function extract ( filePath : string ) : Promise < string > {
4
+ export async function extract ( filePath : string , debug : boolean ) : Promise < string > {
4
5
try {
6
+ debug = debug || DEBUG
5
7
const sourceCode = await Bun . file ( filePath ) . text ( )
6
- return generateDtsTypes ( sourceCode )
8
+ return generateDtsTypes ( sourceCode , debug )
7
9
}
8
10
catch ( error ) {
9
11
console . error ( error )
10
12
throw new Error ( `Failed to extract and generate .d.ts file` )
11
13
}
12
14
}
13
15
14
- function generateDtsTypes ( sourceCode : string ) : string {
15
- if ( DEBUG )
16
+ function generateDtsTypes ( sourceCode : string , debug : boolean ) : string {
17
+ debug = debug || DEBUG
18
+ if ( debug )
16
19
console . log ( 'Starting generateDtsTypes' )
17
20
const lines = sourceCode . split ( '\n' )
18
21
const dtsLines : string [ ] = [ ]
@@ -42,7 +45,7 @@ function generateDtsTypes(sourceCode: string): string {
42
45
if ( line . trim ( ) . startsWith ( 'import' ) ) {
43
46
const processedImport = processImport ( line )
44
47
imports . push ( processedImport )
45
- if ( DEBUG )
48
+ if ( debug )
46
49
console . log ( `Processed import: ${ processedImport } ` )
47
50
continue
48
51
}
@@ -133,15 +136,42 @@ function processDeclaration(declaration: string): string {
133
136
function processConstDeclaration ( declaration : string ) : string {
134
137
if ( DEBUG )
135
138
console . log ( `Processing const declaration: ${ declaration } ` )
139
+
136
140
const lines = declaration . split ( '\n' )
137
141
const firstLine = lines [ 0 ]
138
142
const name = firstLine . split ( 'export const' ) [ 1 ] . split ( '=' ) [ 0 ] . trim ( ) . split ( ':' ) [ 0 ] . trim ( )
139
143
140
144
const properties = lines . slice ( 1 , - 1 ) . map ( ( line ) => {
141
- const commentIndex = line . indexOf ( '//' )
145
+ let inString = false
146
+ let stringChar = ''
147
+ let commentIndex = - 1
148
+
149
+ for ( let i = 0 ; i < line . length ; i ++ ) {
150
+ const char = line [ i ]
151
+ if ( inString ) {
152
+ if ( char === stringChar && line [ i - 1 ] !== '\\' ) {
153
+ inString = false
154
+ }
155
+ }
156
+ else {
157
+ if ( char === '"' || char === '\'' || char === '`' ) {
158
+ inString = true
159
+ stringChar = char
160
+ }
161
+ else if ( char === '/' && line [ i + 1 ] === '/' ) {
162
+ commentIndex = i
163
+ break
164
+ }
165
+ }
166
+ }
167
+
142
168
const hasComment = commentIndex !== - 1
143
169
const mainPart = hasComment ? line . slice ( 0 , commentIndex ) : line
144
- const comment = hasComment ? line . slice ( commentIndex ) : ''
170
+ let comment = hasComment ? line . slice ( commentIndex ) : ''
171
+
172
+ if ( hasComment && ! comment . startsWith ( ' //' ) ) {
173
+ comment = ` //${ comment . slice ( 2 ) } `
174
+ }
145
175
146
176
const [ key , ...valueParts ] = mainPart . split ( ':' )
147
177
let value = valueParts . join ( ':' ) . trim ( )
@@ -189,6 +219,7 @@ function processFunctionDeclaration(declaration: string): string {
189
219
function cleanOutput ( output : string ) : string {
190
220
if ( DEBUG )
191
221
console . log ( 'Cleaning output' )
222
+
192
223
const result = output
193
224
. replace ( / \{ \s * \} / g, '{}' )
194
225
. replace ( / \s * ; \s * (? = \} | $ ) / g, ';' )
@@ -201,8 +232,15 @@ function cleanOutput(output: string): string {
201
232
. replace ( / ; \n ( \s * ) \} / g, ';\n$1\n$1}' )
202
233
. replace ( / , \n \s * ; / g, ';' ) // Remove unnecessary commas before semicolons
203
234
. replace ( / ; \s * \/ \/ \s * / g, '; // ' ) // Ensure comments are properly formatted
235
+ . replace ( / , \s * ; / g, ';' ) // Remove trailing commas before semicolons
236
+ . replace ( / ; [ \t \v \f \r \xA0 \u1680 \u2000 - \u200A \u2028 \u2029 \u202F \u205F \u3000 \uFEFF ] * \n \s * \} / g, ';\n}' ) // Ensure closing braces are on their own lines
237
+ . replace ( / ; \s * \/ \/ \s * / g, '; // ' ) // Ensure comments are properly formatted
238
+ . replace ( / ; \s * \} / g, ';\n}' ) // Ensure closing braces are on their own lines
239
+ . replace ( / ; \s * \/ \/ \s * / g, '; // ' ) // Ensure comments are properly formatted
204
240
. trim ( )
241
+
205
242
if ( DEBUG )
206
243
console . log ( 'Cleaned output:' , result )
244
+
207
245
return result
208
246
}
0 commit comments