@@ -559,6 +559,20 @@ function indentMultilineType(type: string, baseIndent: string, isLast: boolean):
559559 // Calculate proper indentation based on nesting level
560560 const stackDepth = bracketStack . reduce ( ( depth , info ) => depth + info . depth , 0 )
561561 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+ }
562576 }
563577
564578 // Handle opening brackets with Array context
@@ -584,8 +598,15 @@ function indentMultilineType(type: string, baseIndent: string, isLast: boolean):
584598 }
585599
586600 // 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 ? ' |' : '' } `
589610 } ) . filter ( Boolean )
590611
591612 return formattedLines . join ( '\n' )
@@ -625,10 +646,8 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
625646 debugLog ( state , 'infer-array-value' , `Input value:\n${ value } ` )
626647
627648 const content = value . slice ( 1 , - 1 ) . trim ( )
628- if ( ! content ) {
629- debugLog ( state , 'infer-array-empty' , 'Empty array content' )
649+ if ( ! content )
630650 return 'unknown[]'
631- }
632651
633652 const baseIndent = ' ' . repeat ( indentLevel )
634653 debugLog ( state , 'infer-array-indent' , `Base indent="${ baseIndent } "` )
@@ -684,8 +703,6 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
684703
685704 if ( needsMultiline ) {
686705 debugLog ( state , 'multiline-start' , `Starting multiline formatting with ${ types . length } types` )
687-
688- // Instead of manual formatting, use indentMultilineType
689706 const formattedContent = types . map ( ( type , i ) => {
690707 const isLast = i === types . length - 1
691708 return indentMultilineType ( type , `${ baseIndent } ` , isLast )
@@ -708,15 +725,17 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe
708725 return 'Record<string, unknown>'
709726
710727 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 } ` )
712731
713732 const props = processObjectProperties ( content , state , indentLevel )
714733 if ( ! props . length )
715734 return '{}'
716735
717736 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 } `
720739 } )
721740
722741 return `{\n${ propertyStrings . join ( ';\n' ) } \n${ baseIndent } }`
0 commit comments