@@ -6,6 +6,15 @@ interface ProcessedMethod {
6
6
signature : string
7
7
}
8
8
9
+ function cleanDeclaration ( declaration : string ) : string {
10
+ return declaration
11
+ . replace ( / \r \n / g, '\n' ) // Normalize line endings
12
+ . replace ( / \/ \* [ \s \S ] * ?\* \/ / g, '' ) // Remove multi-line comments
13
+ . replace ( / \/ \/ .* / g, '' ) // Remove single-line comments
14
+ . replace ( / \s + / g, ' ' ) // Normalize whitespace
15
+ . trim ( )
16
+ }
17
+
9
18
function cleanParameterTypes ( params : string ) : string {
10
19
if ( ! params . trim ( ) )
11
20
return ''
@@ -573,25 +582,31 @@ function indentMultilineType(type: string, baseIndent: string, isLast: boolean):
573
582
}
574
583
575
584
function inferValueType ( value : string ) : string {
576
- value = value . trim ( )
585
+ const normalizedValue = value . split ( '\n' ) . map ( line => line . trim ( ) ) . join ( ' ' )
577
586
578
- // For string literals, return the literal itself as the type
579
- if ( / ^ [ ' " ` ] .* [ ' " ` ] $ / . test ( value ) ) {
580
- return value
587
+ // For string literals
588
+ if ( / ^ [ ' " ` ] .* [ ' " ` ] $ / . test ( normalizedValue ) ) {
589
+ return normalizedValue
581
590
}
582
591
583
592
// For numeric literals
584
- if ( ! Number . isNaN ( Number ( value ) ) ) {
585
- return value
593
+ if ( ! Number . isNaN ( Number ( normalizedValue ) ) ) {
594
+ return normalizedValue
586
595
}
587
596
588
597
// For boolean literals
589
- if ( value === 'true' || value === 'false' ) {
590
- return value
598
+ if ( normalizedValue === 'true' || normalizedValue === 'false' ) {
599
+ return normalizedValue
600
+ }
601
+
602
+ // Check for explicit return type annotations with better multiline handling
603
+ const returnTypeMatch = normalizedValue . match ( / \( [ ^ ) ] * \) \s * : \s * ( [ ^ = > { ] + ) / )
604
+ if ( returnTypeMatch ) {
605
+ return returnTypeMatch [ 1 ] . trim ( )
591
606
}
592
607
593
608
// For function expressions
594
- if ( value . includes ( '=>' ) ) {
609
+ if ( normalizedValue . includes ( '=>' ) ) {
595
610
return '(...args: any[]) => unknown'
596
611
}
597
612
@@ -869,7 +884,7 @@ export function isDefaultExport(line: string): boolean {
869
884
return line . trim ( ) . startsWith ( 'export default' )
870
885
}
871
886
872
- export function isDeclarationStart ( line : string ) : boolean {
887
+ function isDeclarationStart ( line : string ) : boolean {
873
888
return (
874
889
line . startsWith ( 'export ' )
875
890
|| line . startsWith ( 'interface ' )
@@ -953,10 +968,10 @@ export function processBlock(lines: string[], comments: string[], state: Process
953
968
954
969
// Keep track of declaration for debugging
955
970
state . debug . currentProcessing = cleanDeclaration
956
- debugLog ( state , 'processing' , `Processing block: ${ cleanDeclaration . substring ( 0 , 100 ) } ... ` )
971
+ debugLog ( state , 'block- processing' , `Full block content:\n ${ cleanDeclaration } ` )
957
972
958
973
if ( ! cleanDeclaration ) {
959
- debugLog ( state , 'processing' , 'Empty declaration block' )
974
+ debugLog ( state , 'block- processing' , 'Empty declaration block' )
960
975
return
961
976
}
962
977
@@ -973,7 +988,11 @@ export function processBlock(lines: string[], comments: string[], state: Process
973
988
if ( cleanDeclaration . startsWith ( 'const' ) || cleanDeclaration . startsWith ( 'let' ) || cleanDeclaration . startsWith ( 'var' )
974
989
|| cleanDeclaration . startsWith ( 'export const' ) || cleanDeclaration . startsWith ( 'export let' ) || cleanDeclaration . startsWith ( 'export var' ) ) {
975
990
const isExported = cleanDeclaration . startsWith ( 'export' )
976
- state . dtsLines . push ( processVariable ( declarationText , isExported , state ) )
991
+
992
+ // For variable declarations, ensure we have the complete declaration
993
+ const fullDeclaration = lines . join ( '\n' )
994
+ debugLog ( state , 'block-processing' , `Processing variable declaration:\n${ fullDeclaration } ` )
995
+ state . dtsLines . push ( processVariable ( fullDeclaration , isExported , state ) )
977
996
return
978
997
}
979
998
@@ -998,6 +1017,24 @@ export function processBlock(lines: string[], comments: string[], state: Process
998
1017
debugLog ( state , 'processing' , `Unhandled declaration type: ${ cleanDeclaration . split ( '\n' ) [ 0 ] } ` )
999
1018
}
1000
1019
1020
+ function processDeclaration ( declaration : string ) : boolean {
1021
+ let bracketDepth = 0
1022
+ let parenDepth = 0
1023
+
1024
+ for ( const char of declaration ) {
1025
+ if ( char === '(' )
1026
+ parenDepth ++
1027
+ if ( char === ')' )
1028
+ parenDepth --
1029
+ if ( char === '{' )
1030
+ bracketDepth ++
1031
+ if ( char === '}' )
1032
+ bracketDepth --
1033
+ }
1034
+
1035
+ return bracketDepth === 0 && parenDepth === 0
1036
+ }
1037
+
1001
1038
export function processSpecificDeclaration ( declarationWithoutComments : string , fullDeclaration : string , state : ProcessingState ) : void {
1002
1039
state . debug . currentProcessing = declarationWithoutComments
1003
1040
debugLog ( state , 'processing' , `Processing declaration: ${ declarationWithoutComments . substring ( 0 , 100 ) } ...` )
@@ -1148,13 +1185,13 @@ export function processSpecificDeclaration(declarationWithoutComments: string, f
1148
1185
console . warn ( 'Unhandled declaration type:' , declarationWithoutComments . split ( '\n' ) [ 0 ] )
1149
1186
}
1150
1187
1151
- export function processSourceFile ( content : string , state : ProcessingState ) : void {
1188
+ function processSourceFile ( content : string , state : ProcessingState ) : void {
1152
1189
const lines = content . split ( '\n' )
1153
1190
let currentBlock : string [ ] = [ ]
1154
1191
let currentComments : string [ ] = [ ]
1155
1192
let bracketDepth = 0
1193
+ let parenDepth = 0
1156
1194
let inDeclaration = false
1157
- // let declarationStart = -1
1158
1195
1159
1196
for ( let i = 0 ; i < lines . length ; i ++ ) {
1160
1197
const line = lines [ i ]
@@ -1170,43 +1207,57 @@ export function processSourceFile(content: string, state: ProcessingState): void
1170
1207
continue
1171
1208
}
1172
1209
1173
- // Track brackets for nesting depth
1174
- bracketDepth += ( line . match ( / \{ / g) || [ ] ) . length
1175
- bracketDepth -= ( line . match ( / \} / g) || [ ] ) . length
1176
-
1177
- // Handle declaration starts
1210
+ // Track brackets and parentheses for nesting depth
1178
1211
if ( isDeclarationStart ( trimmedLine ) ) {
1179
1212
if ( inDeclaration && currentBlock . length > 0 ) {
1180
1213
processBlock ( currentBlock , currentComments , state )
1181
1214
currentBlock = [ ]
1182
1215
currentComments = [ ]
1216
+ bracketDepth = 0
1217
+ parenDepth = 0
1183
1218
}
1184
1219
inDeclaration = true
1185
- // declarationStart = i
1186
1220
currentBlock = [ line ]
1221
+
1222
+ // Initialize depths for the first line
1223
+ parenDepth += ( line . match ( / \( / g) || [ ] ) . length
1224
+ parenDepth -= ( line . match ( / \) / g) || [ ] ) . length
1225
+ bracketDepth += ( line . match ( / \{ / g) || [ ] ) . length
1226
+ bracketDepth -= ( line . match ( / \} / g) || [ ] ) . length
1227
+
1187
1228
continue
1188
1229
}
1189
1230
1190
- // Add line to current block if in declaration
1231
+ // If we're in a declaration, track the nesting
1191
1232
if ( inDeclaration ) {
1192
1233
currentBlock . push ( line )
1193
1234
1194
- // Check for declaration end
1235
+ // Update depths
1236
+ parenDepth += ( line . match ( / \( / g) || [ ] ) . length
1237
+ parenDepth -= ( line . match ( / \) / g) || [ ] ) . length
1238
+ bracketDepth += ( line . match ( / \{ / g) || [ ] ) . length
1239
+ bracketDepth -= ( line . match ( / \} / g) || [ ] ) . length
1240
+
1241
+ // Check if the declaration is complete
1195
1242
const isComplete = (
1196
- bracketDepth === 0 && (
1243
+ parenDepth === 0
1244
+ && bracketDepth === 0 && (
1197
1245
trimmedLine . endsWith ( ';' )
1198
1246
|| trimmedLine . endsWith ( '}' )
1199
1247
|| trimmedLine . endsWith ( ',' )
1200
1248
|| trimmedLine . match ( / \b a s \s + c o n s t [ , ; ] ? $ / )
1201
1249
)
1202
1250
)
1203
1251
1252
+ debugLog ( state , 'source-processing' , `Line "${ trimmedLine } ": parenDepth=${ parenDepth } , bracketDepth=${ bracketDepth } , complete=${ isComplete } ` )
1253
+
1204
1254
if ( isComplete ) {
1205
1255
processBlock ( currentBlock , currentComments , state )
1206
1256
currentBlock = [ ]
1207
1257
currentComments = [ ]
1208
1258
inDeclaration = false
1209
- // declarationStart = -1
1259
+ bracketDepth = 0
1260
+ parenDepth = 0
1210
1261
}
1211
1262
}
1212
1263
}
@@ -1273,7 +1324,17 @@ function processType(declaration: string, isExported = true): string {
1273
1324
* Process variable (const, let, var) declarations with type inference
1274
1325
*/
1275
1326
function processVariable ( declaration : string , isExported : boolean , state : ProcessingState ) : string {
1276
- // Handle explicit type annotations first
1327
+ debugLog ( state , 'process-variable' , `Processing complete declaration:\n${ declaration } ` )
1328
+
1329
+ // First, try to match explicit return type for function declarations
1330
+ const functionTypeMatch = declaration . match ( / (?: e x p o r t \s + ) ? (?: c o n s t | l e t | v a r ) \s + ( [ ^ = \s ] + ) \s * = \s * \( [ ^ ) ] * \) \s * : \s * ( [ ^ = > { ] + ) / )
1331
+ if ( functionTypeMatch ) {
1332
+ const [ , name , returnType ] = functionTypeMatch
1333
+ debugLog ( state , 'process-variable' , `Found explicit return type for ${ name } : ${ returnType } ` )
1334
+ return `${ isExported ? 'export ' : '' } declare const ${ name } : ${ returnType . trim ( ) } ;`
1335
+ }
1336
+
1337
+ // Handle explicit type annotations in the declaration
1277
1338
const explicitTypeMatch = declaration . match ( / (?: e x p o r t \s + ) ? (?: c o n s t | l e t | v a r ) \s + ( [ ^ : \s ] + ) \s * : \s * ( [ ^ = ] + ) = / )
1278
1339
if ( explicitTypeMatch ) {
1279
1340
const [ , name , type ] = explicitTypeMatch
@@ -1282,7 +1343,7 @@ function processVariable(declaration: string, isExported: boolean, state: Proces
1282
1343
}
1283
1344
1284
1345
// Handle value assignments
1285
- const valueMatch = declaration . match ( / (?: e x p o r t \s + ) ? (?: c o n s t | l e t | v a r ) \s + ( [ ^ = \s ] + ) \s * = \s * ( . + ) $ / s )
1346
+ const valueMatch = declaration . match ( / (?: e x p o r t \s + ) ? (?: c o n s t | l e t | v a r ) \s + ( [ ^ = \s ] + ) \s * = \s * ( [ \s \S ] + ) $ / )
1286
1347
if ( ! valueMatch ) {
1287
1348
debugLog ( state , 'process-variable' , 'Failed to match variable declaration' )
1288
1349
return declaration
@@ -1292,11 +1353,10 @@ function processVariable(declaration: string, isExported: boolean, state: Proces
1292
1353
const declarationType = getDeclarationType ( declaration )
1293
1354
const trimmedValue = rawValue . trim ( )
1294
1355
1295
- debugLog ( state , 'process-variable' , `Processing ${ name } with value length ${ trimmedValue . length } ` )
1356
+ debugLog ( state , 'process-variable' , `Processing ${ name } with value:\n ${ trimmedValue } ` )
1296
1357
1297
1358
// Handle string literals
1298
- if ( / ^ ( [ ' " ` ] ) .* \1$ / . test ( trimmedValue ) ) {
1299
- // For string literals, use type annotation instead of value assignment
1359
+ if ( / ^ [ ' " ` ] .* [ ' " ` ] $ / . test ( trimmedValue ) ) {
1300
1360
return `${ isExported ? 'export ' : '' } declare ${ declarationType } ${ name } : ${ trimmedValue } ;`
1301
1361
}
1302
1362
0 commit comments