@@ -624,19 +624,26 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
624
624
)
625
625
626
626
if ( needsMultiline ) {
627
- const formattedTypes = types . map ( ( type ) => {
628
- // Indent nested types that have newlines
627
+ const formattedTypes = types . map ( ( type , index ) => {
628
+ const isLast = index === types . length - 1
629
+ // For types that contain newlines, maintain their internal formatting
629
630
if ( type . includes ( '\n' ) ) {
630
- return type . replace ( / \n / g, `\n${ elementIndent } ` )
631
+ const lines = type . split ( '\n' )
632
+ const formattedLines = lines . map ( ( line , i ) => {
633
+ if ( i === 0 )
634
+ return line
635
+ // Increment indentation for nested lines
636
+ const currentIndent = line . match ( / ^ \s * / ) [ 0 ]
637
+ const additionalIndent = ' ' . repeat ( indentLevel + 1 )
638
+ return `${ additionalIndent } ${ line . trimLeft ( ) } `
639
+ } ) . join ( '\n' )
640
+ return `${ elementIndent } ${ formattedLines } ${ isLast ? '' : ' |' } `
631
641
}
632
- return type
642
+ // For single-line types
643
+ return `${ elementIndent } ${ type } ${ isLast ? '' : ' |' } `
633
644
} )
634
645
635
- return [
636
- 'Array<' ,
637
- ...formattedTypes . map ( type => `${ elementIndent } ${ type } ` ) ,
638
- `${ baseIndent } >` ,
639
- ] . join ( '\n' )
646
+ return `Array<\n${ formattedTypes . join ( '\n' ) } \n${ baseIndent } >`
640
647
}
641
648
642
649
return `Array<${ types . join ( ' | ' ) } >`
@@ -654,56 +661,28 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe
654
661
655
662
const baseIndent = ' ' . repeat ( indentLevel )
656
663
const propIndent = ' ' . repeat ( indentLevel + 1 )
664
+ const innerIndent = ' ' . repeat ( indentLevel + 2 )
657
665
658
666
const props = processObjectProperties ( content , state )
659
667
if ( ! props . length )
660
668
return '{}'
661
669
662
- // Group methods and regular properties
663
- const methods : Array < { key : string , value : string } > = [ ]
664
- const regularProps : Array < { key : string , value : string } > = [ ]
670
+ const propertyStrings = props . map ( ( { key, value } ) => {
671
+ const formattedKey = / ^ \w + $ / . test ( key ) ? key : `'${ key } '`
665
672
666
- props . forEach ( ( prop ) => {
667
- if ( prop . key . includes ( '(' ) )
668
- methods . push ( prop )
669
- else
670
- regularProps . push ( prop )
671
- } )
672
-
673
- const parts : string [ ] = [ ]
674
-
675
- if ( methods . length > 0 ) {
676
- const methodsStr = processObjectMethods ( methods , state , indentLevel + 1 )
677
- if ( methodsStr !== '{}' ) {
678
- parts . push ( methodsStr . slice ( 1 , - 1 ) . trim ( ) ) // Remove outer braces
673
+ // Handle multiline values (like objects and arrays)
674
+ if ( value . includes ( '\n' ) ) {
675
+ const indentedValue = value
676
+ . split ( '\n' )
677
+ . map ( ( line , i ) => i === 0 ? line : `${ innerIndent } ${ line . trimLeft ( ) } ` )
678
+ . join ( '\n' )
679
+ return `${ propIndent } ${ formattedKey } : ${ indentedValue } `
679
680
}
680
- }
681
-
682
- if ( regularProps . length > 0 ) {
683
- const propsStr = regularProps . map ( ( { key, value } ) => {
684
- const formattedKey = / ^ \w + $ / . test ( key ) ? key : `'${ key } '`
685
- let formattedValue = value
686
-
687
- // Format nested objects with proper indentation
688
- if ( value . startsWith ( '{' ) ) {
689
- formattedValue = inferComplexObjectType ( value , state , indentLevel + 1 )
690
- }
691
- // Format array types with proper indentation
692
- else if ( value . startsWith ( 'Array<' ) ) {
693
- // Extract the array content and re-indent it
694
- const arrayContent = value . slice ( 6 , - 1 )
695
- formattedValue = `Array<${ arrayContent } >`
696
- }
697
-
698
- return `${ propIndent } ${ formattedKey } : ${ formattedValue } `
699
- } ) . join ( ';\n' )
700
- parts . push ( propsStr )
701
- }
702
681
703
- if ( parts . length === 0 )
704
- return '{}'
682
+ return ` ${ propIndent } ${ formattedKey } : ${ value } `
683
+ } )
705
684
706
- return `{\n${ parts . join ( ';\n' ) } \n${ baseIndent } }`
685
+ return `{\n${ propertyStrings . join ( ';\n' ) } \n${ baseIndent } }`
707
686
}
708
687
709
688
function inferConstArrayType ( value : string , state ?: ProcessingState ) : string {
0 commit comments