@@ -560,12 +560,15 @@ function inferValueType(value: string): string {
560
560
/**
561
561
* Infer array type from array literal with support for nested arrays and mixed elements
562
562
*/
563
- function inferArrayType ( value : string , state ?: ProcessingState ) : string {
563
+ function inferArrayType ( value : string , state ?: ProcessingState , indentLevel = 0 ) : string {
564
564
debugLog ( state , 'infer-array' , `Inferring array type for: ${ value } ` )
565
565
const content = value . slice ( 1 , - 1 ) . trim ( )
566
566
if ( ! content )
567
567
return 'unknown[]'
568
568
569
+ const baseIndent = ' ' . repeat ( indentLevel )
570
+ const elementIndent = ' ' . repeat ( indentLevel + 1 )
571
+
569
572
// Handle const assertions first
570
573
const elements = splitArrayElements ( content , state )
571
574
const allConstTuples = elements . every ( el => el . trim ( ) . endsWith ( 'as const' ) )
@@ -584,12 +587,12 @@ function inferArrayType(value: string, state?: ProcessingState): string {
584
587
585
588
// Handle nested arrays
586
589
if ( trimmed . startsWith ( '[' ) ) {
587
- return inferArrayType ( trimmed , state )
590
+ return inferArrayType ( trimmed , state , indentLevel + 1 )
588
591
}
589
592
590
- // Handle objects
593
+ // Handle objects with proper indentation
591
594
if ( trimmed . startsWith ( '{' ) ) {
592
- return inferComplexObjectType ( trimmed , state )
595
+ return inferComplexObjectType ( trimmed , state , indentLevel + 1 )
593
596
}
594
597
595
598
// Handle function expressions - always parenthesize
@@ -607,7 +610,23 @@ function inferArrayType(value: string, state?: ProcessingState): string {
607
610
return normalizeTypeReference ( trimmed )
608
611
} )
609
612
610
- return `Array<${ elementTypes . join ( ' | ' ) } >`
613
+ // Format the array type with proper indentation
614
+ const types = elementTypes . filter ( Boolean )
615
+ if ( types . length === 0 )
616
+ return 'unknown[]'
617
+
618
+ // Check if we need multiline formatting
619
+ const needsMultiline = types . some ( type =>
620
+ type . includes ( '\n' )
621
+ || type . includes ( '{' )
622
+ || type . length > 40 ,
623
+ )
624
+
625
+ if ( needsMultiline ) {
626
+ return `Array<\n${ elementIndent } ${ types . join ( ` |\n${ elementIndent } ` ) } \n${ baseIndent } >`
627
+ }
628
+
629
+ return `Array<${ types . join ( ' | ' ) } >`
611
630
}
612
631
613
632
/**
@@ -656,6 +675,12 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe
656
675
if ( value . startsWith ( '{' ) ) {
657
676
formattedValue = inferComplexObjectType ( value , state , indentLevel + 1 )
658
677
}
678
+ // Format array types with proper indentation
679
+ else if ( value . startsWith ( 'Array<' ) ) {
680
+ // Extract the array content and re-indent it
681
+ const arrayContent = value . slice ( 6 , - 1 )
682
+ formattedValue = `Array<${ arrayContent } >`
683
+ }
659
684
660
685
return `${ propIndent } ${ formattedKey } : ${ formattedValue } `
661
686
} ) . join ( ';\n' )
@@ -1476,7 +1501,7 @@ function processObjectProperties(content: string, state?: ProcessingState): Arra
1476
1501
return properties
1477
1502
}
1478
1503
1479
- function processProperty ( key : string , value : string , state ?: ProcessingState ) : { key : string , value : string } {
1504
+ function processProperty ( key : string , value : string , state ?: ProcessingState , indentLevel = 0 ) : { key : string , value : string } {
1480
1505
const cleanKey = key . trim ( ) . replace ( / ^ [ ' " ] ( .* ) [ ' " ] $ / , '$1' )
1481
1506
const cleanValue = value . trim ( )
1482
1507
@@ -1488,18 +1513,18 @@ function processProperty(key: string, value: string, state?: ProcessingState): {
1488
1513
return { key : name , value : signature }
1489
1514
}
1490
1515
1491
- // Handle arrays
1516
+ // Handle arrays with proper indentation
1492
1517
if ( cleanValue . startsWith ( '[' ) ) {
1493
1518
debugLog ( state , 'process-array' , `Processing array in property "${ cleanKey } "` )
1494
- return { key : cleanKey , value : inferArrayType ( cleanValue , state ) }
1519
+ return { key : cleanKey , value : inferArrayType ( cleanValue , state , indentLevel ) }
1495
1520
}
1496
1521
1497
1522
// Handle object literals with proper indentation
1498
1523
if ( cleanValue . startsWith ( '{' ) ) {
1499
1524
debugLog ( state , 'process-object' , `Processing nested object in property "${ cleanKey } "` )
1500
1525
return {
1501
1526
key : cleanKey ,
1502
- value : inferComplexObjectType ( cleanValue , state , 0 ) ,
1527
+ value : inferComplexObjectType ( cleanValue , state , indentLevel ) ,
1503
1528
}
1504
1529
}
1505
1530
@@ -1524,6 +1549,7 @@ function processProperty(key: string, value: string, state?: ProcessingState): {
1524
1549
return { key : cleanKey , value : 'unknown' }
1525
1550
}
1526
1551
1552
+ // Default case
1527
1553
return { key : cleanKey , value : 'unknown' }
1528
1554
}
1529
1555
0 commit comments