@@ -531,6 +531,21 @@ function createImportTrackingState(): ImportTrackingState {
531
531
}
532
532
}
533
533
534
+ function indentMultilineType ( type : string , baseIndent : string , isLast : boolean ) : string {
535
+ const lines = type . split ( '\n' )
536
+ return lines
537
+ . map ( ( line , i ) => {
538
+ if ( i === 0 )
539
+ return `${ baseIndent } ${ line } `
540
+ const trimmed = line . trim ( )
541
+ if ( ! trimmed )
542
+ return ''
543
+ return `${ baseIndent } ${ trimmed } `
544
+ } )
545
+ . filter ( Boolean )
546
+ . join ( '\n' ) + ( isLast ? '' : ' |' )
547
+ }
548
+
534
549
function inferValueType ( value : string ) : string {
535
550
value = value . trim ( )
536
551
@@ -582,7 +597,7 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
582
597
}
583
598
584
599
// Process each element
585
- const elementTypes = elements . map ( ( element , index ) => {
600
+ const elementTypes = elements . map ( ( element ) => {
586
601
const trimmed = element . trim ( )
587
602
588
603
// Handle nested arrays
@@ -626,23 +641,14 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
626
641
if ( needsMultiline ) {
627
642
const formattedTypes = types . map ( ( type , index ) => {
628
643
const isLast = index === types . length - 1
629
- // For types that contain newlines, ensure proper indentation
644
+ // For types that contain newlines
630
645
if ( type . includes ( '\n' ) ) {
631
- const lines = type . split ( '\n' )
632
- const formattedLines = lines . map ( ( line , i ) => {
633
- // First line gets element indentation
634
- if ( i === 0 )
635
- return line
636
- // Other lines get additional indentation
637
- return `${ elementIndent } ${ line . trimLeft ( ) } `
638
- } ) . join ( '\n' )
639
- return `${ elementIndent } ${ formattedLines } ${ isLast ? '' : ' |' } `
646
+ return indentMultilineType ( type , elementIndent , isLast )
640
647
}
641
648
// For single-line types
642
649
return `${ elementIndent } ${ type } ${ isLast ? '' : ' |' } `
643
650
} )
644
651
645
- // Join lines and ensure closing bracket aligns with parent
646
652
return `Array<\n${ formattedTypes . join ( '\n' ) } \n${ baseIndent } >`
647
653
}
648
654
@@ -661,6 +667,7 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe
661
667
662
668
const baseIndent = ' ' . repeat ( indentLevel )
663
669
const propIndent = ' ' . repeat ( indentLevel + 1 )
670
+ const innerIndent = ' ' . repeat ( indentLevel + 2 )
664
671
665
672
const props = processObjectProperties ( content , state )
666
673
if ( ! props . length )
@@ -669,20 +676,26 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe
669
676
const propertyStrings = props . map ( ( { key, value } ) => {
670
677
const formattedKey = / ^ \w + $ / . test ( key ) ? key : `'${ key } '`
671
678
672
- // Handle multiline values (like objects and arrays)
673
679
if ( value . includes ( '\n' ) ) {
674
- // Add one level of indentation to each line after the first
680
+ // Indent nested multiline values
675
681
const indentedValue = value
676
682
. split ( '\n' )
677
- . map ( ( line , i ) => i === 0 ? line : `${ propIndent } ${ line . trim ( ) } ` )
683
+ . map ( ( line , i ) => {
684
+ if ( i === 0 )
685
+ return line
686
+ const trimmed = line . trim ( )
687
+ if ( ! trimmed )
688
+ return ''
689
+ return `${ innerIndent } ${ trimmed } `
690
+ } )
691
+ . filter ( Boolean )
678
692
. join ( '\n' )
679
693
return `${ propIndent } ${ formattedKey } : ${ indentedValue } `
680
694
}
681
695
682
696
return `${ propIndent } ${ formattedKey } : ${ value } `
683
697
} )
684
698
685
- // Ensure closing brace aligns with parent
686
699
return `{\n${ propertyStrings . join ( ';\n' ) } \n${ baseIndent } }`
687
700
}
688
701
@@ -1348,7 +1361,13 @@ function processObjectMethod(declaration: string, value: string, state?: Process
1348
1361
else if ( isAsync && ! effectiveReturnType . includes ( 'Promise' ) ) {
1349
1362
effectiveReturnType = `Promise<${ effectiveReturnType } >`
1350
1363
}
1351
- else if ( value . includes ( 'toISOString()' ) || ( value . includes ( 'Intl.NumberFormat' ) && value . includes ( 'format' ) ) ) {
1364
+ else if ( value . includes ( 'toISOString()' ) || value . includes ( 'toString()' ) ) {
1365
+ effectiveReturnType = 'string'
1366
+ }
1367
+ else if ( value . includes ( 'console.log' ) || value . match ( / v o i d \s * [ ; { ] / ) ) {
1368
+ effectiveReturnType = 'void'
1369
+ }
1370
+ else if ( value . includes ( 'Intl.NumberFormat' ) && value . includes ( 'format' ) ) {
1352
1371
effectiveReturnType = 'string'
1353
1372
}
1354
1373
@@ -1452,8 +1471,6 @@ function processProperty(key: string, value: string, state?: ProcessingState, in
1452
1471
const cleanKey = key . trim ( ) . replace ( / ^ [ ' " ] ( .* ) [ ' " ] $ / , '$1' )
1453
1472
const cleanValue = value . trim ( )
1454
1473
1455
- debugLog ( state , 'process-property' , `Processing property "${ cleanKey } " with value: ${ cleanValue } ` )
1456
-
1457
1474
// Handle method declarations
1458
1475
if ( cleanKey . includes ( '(' ) ) {
1459
1476
const { name, signature } = processObjectMethod ( cleanKey , cleanValue , state )
@@ -1462,13 +1479,14 @@ function processProperty(key: string, value: string, state?: ProcessingState, in
1462
1479
1463
1480
// Handle arrays with proper indentation
1464
1481
if ( cleanValue . startsWith ( '[' ) ) {
1465
- debugLog ( state , 'process-array' , `Processing array in property "${ cleanKey } "` )
1466
- return { key : cleanKey , value : inferArrayType ( cleanValue , state , indentLevel ) }
1482
+ return {
1483
+ key : cleanKey ,
1484
+ value : inferArrayType ( cleanValue , state , indentLevel ) ,
1485
+ }
1467
1486
}
1468
1487
1469
1488
// Handle object literals with proper indentation
1470
1489
if ( cleanValue . startsWith ( '{' ) ) {
1471
- debugLog ( state , 'process-object' , `Processing nested object in property "${ cleanKey } "` )
1472
1490
return {
1473
1491
key : cleanKey ,
1474
1492
value : inferComplexObjectType ( cleanValue , state , indentLevel ) ,
@@ -1477,7 +1495,6 @@ function processProperty(key: string, value: string, state?: ProcessingState, in
1477
1495
1478
1496
// Handle function expressions
1479
1497
if ( cleanValue . includes ( '=>' ) || cleanValue . includes ( 'function' ) ) {
1480
- debugLog ( state , 'process-property' , 'Processing function expression' )
1481
1498
const funcType = extractFunctionType ( cleanValue , state )
1482
1499
return {
1483
1500
key : cleanKey ,
@@ -1491,12 +1508,11 @@ function processProperty(key: string, value: string, state?: ProcessingState, in
1491
1508
return { key : cleanKey , value : cleanValue }
1492
1509
}
1493
1510
1494
- // Handle references to global objects or function calls
1511
+ // Handle references and function calls
1495
1512
if ( cleanValue . includes ( '.' ) || cleanValue . includes ( '(' ) ) {
1496
1513
return { key : cleanKey , value : 'unknown' }
1497
1514
}
1498
1515
1499
- // Default case
1500
1516
return { key : cleanKey , value : 'unknown' }
1501
1517
}
1502
1518
0 commit comments