1
1
/* eslint-disable no-console */
2
2
const DEBUG = true // Set to false to disable debug logs
3
3
4
- export async function extract ( filePath : string , debug : boolean ) : Promise < string > {
4
+ export async function extract ( filePath : string ) : Promise < string > {
5
5
try {
6
- debug = debug || DEBUG
7
6
const sourceCode = await Bun . file ( filePath ) . text ( )
8
- return generateDtsTypes ( sourceCode , debug )
7
+ return generateDtsTypes ( sourceCode )
9
8
}
10
9
catch ( error ) {
11
10
console . error ( error )
12
11
throw new Error ( `Failed to extract and generate .d.ts file` )
13
12
}
14
13
}
15
14
16
- function generateDtsTypes ( sourceCode : string , debug : boolean ) : string {
17
- debug = debug || DEBUG
18
- if ( debug )
15
+ function generateDtsTypes ( sourceCode : string ) : string {
16
+ if ( DEBUG )
19
17
console . log ( 'Starting generateDtsTypes' )
20
18
const lines = sourceCode . split ( '\n' )
21
19
const dtsLines : string [ ] = [ ]
@@ -45,19 +43,19 @@ function generateDtsTypes(sourceCode: string, debug: boolean): string {
45
43
if ( line . trim ( ) . startsWith ( 'import' ) ) {
46
44
const processedImport = processImport ( line )
47
45
imports . push ( processedImport )
48
- if ( debug )
46
+ if ( DEBUG )
49
47
console . log ( `Processed import: ${ processedImport } ` )
50
48
continue
51
49
}
52
50
53
51
if ( line . trim ( ) . startsWith ( 'export default' ) ) {
54
- defaultExport = `${ line . trim ( ) } ;`
52
+ defaultExport = `\n ${ line . trim ( ) } ;`
55
53
if ( DEBUG )
56
54
console . log ( `Default export found: ${ defaultExport } ` )
57
55
continue
58
56
}
59
57
60
- if ( line . trim ( ) . startsWith ( 'export const ' ) || isMultiLineDeclaration ) {
58
+ if ( line . trim ( ) . startsWith ( 'export' ) || isMultiLineDeclaration ) {
61
59
currentDeclaration += `${ line } \n`
62
60
bracketCount += ( line . match ( / \{ / g) || [ ] ) . length - ( line . match ( / \} / g) || [ ] ) . length
63
61
isMultiLineDeclaration = bracketCount > 0
@@ -69,30 +67,16 @@ function generateDtsTypes(sourceCode: string, debug: boolean): string {
69
67
console . log ( `Comment block added to dtsLines: ${ lastCommentBlock . trimEnd ( ) } ` )
70
68
lastCommentBlock = ''
71
69
}
72
- const processed = processConstDeclaration ( currentDeclaration . trim ( ) )
70
+ const processed = processDeclaration ( currentDeclaration . trim ( ) )
73
71
if ( processed ) {
74
72
dtsLines . push ( processed )
75
73
if ( DEBUG )
76
- console . log ( `Processed const declaration added to dtsLines: ${ processed } ` )
74
+ console . log ( `Processed declaration added to dtsLines: ${ processed } ` )
77
75
}
78
76
currentDeclaration = ''
79
77
bracketCount = 0
80
78
}
81
79
}
82
- else if ( line . trim ( ) . startsWith ( 'export' ) ) {
83
- if ( lastCommentBlock ) {
84
- dtsLines . push ( lastCommentBlock . trimEnd ( ) )
85
- if ( DEBUG )
86
- console . log ( `Comment block added to dtsLines: ${ lastCommentBlock . trimEnd ( ) } ` )
87
- lastCommentBlock = ''
88
- }
89
- const processed = processDeclaration ( line )
90
- if ( processed ) {
91
- dtsLines . push ( processed )
92
- if ( DEBUG )
93
- console . log ( `Processed declaration added to dtsLines: ${ processed } ` )
94
- }
95
- }
96
80
}
97
81
98
82
const result = cleanOutput ( [ ...imports , '' , ...dtsLines , '' , ...exports , defaultExport ] . filter ( Boolean ) . join ( '\n' ) )
@@ -136,7 +120,6 @@ function processDeclaration(declaration: string): string {
136
120
function processConstDeclaration ( declaration : string ) : string {
137
121
if ( DEBUG )
138
122
console . log ( `Processing const declaration: ${ declaration } ` )
139
-
140
123
const lines = declaration . split ( '\n' )
141
124
const firstLine = lines [ 0 ]
142
125
const name = firstLine . split ( 'export const' ) [ 1 ] . split ( '=' ) [ 0 ] . trim ( ) . split ( ':' ) [ 0 ] . trim ( )
@@ -158,7 +141,7 @@ function processConstDeclaration(declaration: string): string {
158
141
inString = true
159
142
stringChar = char
160
143
}
161
- else if ( char === '/' && line [ i + 1 ] === '/' ) {
144
+ else if ( char === '/' && line [ i + 1 ] === '// ' ) {
162
145
commentIndex = i
163
146
break
164
147
}
@@ -200,7 +183,10 @@ function processInterfaceDeclaration(declaration: string): string {
200
183
function processTypeDeclaration ( declaration : string ) : string {
201
184
if ( DEBUG )
202
185
console . log ( `Processing type declaration: ${ declaration } ` )
203
- const result = declaration . replace ( 'export type' , 'export declare type' )
186
+ const lines = declaration . split ( '\n' )
187
+ const typeName = lines [ 0 ] . split ( 'type' ) [ 1 ] . split ( '=' ) [ 0 ] . trim ( )
188
+ const typeBody = lines . slice ( 1 ) . map ( line => ` ${ line . trim ( ) } ` ) . join ( '\n' )
189
+ const result = `export declare type ${ typeName } = ${ typeBody } `
204
190
if ( DEBUG )
205
191
console . log ( `Processed type declaration: ${ result } ` )
206
192
return result
0 commit comments