@@ -559,6 +559,20 @@ function indentMultilineType(type: string, baseIndent: string, isLast: boolean):
559
559
// Calculate proper indentation based on nesting level
560
560
const stackDepth = bracketStack . reduce ( ( depth , info ) => depth + info . depth , 0 )
561
561
currentIndent = baseIndent + ' ' . repeat ( stackDepth )
562
+
563
+ // Dedent closing tokens
564
+ if ( ( trimmed === '}' || trimmed === '>' || trimmed . startsWith ( '> |' ) ) && bracketStack . length > 0 ) {
565
+ // For array closings (>), also dedent if it's part of a closing sequence
566
+ if ( trimmed === '>' || trimmed . startsWith ( '> |' ) ) {
567
+ const lastBracket = bracketStack [ bracketStack . length - 1 ]
568
+ if ( lastBracket ?. isArray ) {
569
+ currentIndent = baseIndent + ' ' . repeat ( Math . max ( 0 , stackDepth - 1 ) )
570
+ }
571
+ }
572
+ else {
573
+ currentIndent = baseIndent + ' ' . repeat ( Math . max ( 0 , stackDepth - 1 ) )
574
+ }
575
+ }
562
576
}
563
577
564
578
// Handle opening brackets with Array context
@@ -584,8 +598,15 @@ function indentMultilineType(type: string, baseIndent: string, isLast: boolean):
584
598
}
585
599
586
600
// Add union operator for non-last lines
587
- const needsUnion = ! isLast && i === lines . length - 1 && ! trimmed . endsWith ( ' |' ) && ! trimmed . endsWith ( ';' )
588
- return `${ currentIndent } ${ trimmed } ${ needsUnion ? ' |' : '' } `
601
+ let needsUnion = ! isLast && i === lines . length - 1 && ! trimmed . endsWith ( ' |' ) && ! trimmed . endsWith ( ';' )
602
+ const hasUnion = ( trimmed === '}' || trimmed === '>' ) && ! isLast && i !== lines . length - 1
603
+
604
+ // Don't add union if it's already part of the trimmed content (like '> |')
605
+ if ( trimmed . endsWith ( ' |' ) ) {
606
+ needsUnion = false
607
+ }
608
+
609
+ return `${ currentIndent } ${ trimmed } ${ needsUnion ? ' |' : hasUnion ? ' |' : '' } `
589
610
} ) . filter ( Boolean )
590
611
591
612
return formattedLines . join ( '\n' )
@@ -625,10 +646,8 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
625
646
debugLog ( state , 'infer-array-value' , `Input value:\n${ value } ` )
626
647
627
648
const content = value . slice ( 1 , - 1 ) . trim ( )
628
- if ( ! content ) {
629
- debugLog ( state , 'infer-array-empty' , 'Empty array content' )
649
+ if ( ! content )
630
650
return 'unknown[]'
631
- }
632
651
633
652
const baseIndent = ' ' . repeat ( indentLevel )
634
653
debugLog ( state , 'infer-array-indent' , `Base indent="${ baseIndent } "` )
@@ -684,8 +703,6 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
684
703
685
704
if ( needsMultiline ) {
686
705
debugLog ( state , 'multiline-start' , `Starting multiline formatting with ${ types . length } types` )
687
-
688
- // Instead of manual formatting, use indentMultilineType
689
706
const formattedContent = types . map ( ( type , i ) => {
690
707
const isLast = i === types . length - 1
691
708
return indentMultilineType ( type , `${ baseIndent } ` , isLast )
@@ -708,15 +725,17 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe
708
725
return 'Record<string, unknown>'
709
726
710
727
const baseIndent = ' ' . repeat ( indentLevel )
711
- debugLog ( state , 'infer-complex-content' , `Processing content: ${ content . substring ( 0 , 100 ) } ...` )
728
+ const propIndent = ' ' . repeat ( indentLevel + 1 )
729
+
730
+ debugLog ( state , 'infer-complex-content' , `Processing content with indent level ${ indentLevel } ` )
712
731
713
732
const props = processObjectProperties ( content , state , indentLevel )
714
733
if ( ! props . length )
715
734
return '{}'
716
735
717
736
const propertyStrings = props . map ( ( { key, value } ) => {
718
- debugLog ( state , 'infer-complex-prop' , `Processing property ${ key } : ${ value . substring ( 0 , 50 ) } ... ` )
719
- return `${ baseIndent } ${ key } : ${ value } `
737
+ debugLog ( state , 'infer-complex-prop' , `Processing property ${ key } ` )
738
+ return `${ propIndent } ${ key } : ${ value } `
720
739
} )
721
740
722
741
return `{\n${ propertyStrings . join ( ';\n' ) } \n${ baseIndent } }`
0 commit comments