@@ -327,14 +327,6 @@ function generateOptimizedImports(state: ImportTrackingState, dtsLines: string[]
327
327
return imports . sort ( )
328
328
}
329
329
330
- function getDeclarationType ( declaration : string ) : string {
331
- if ( declaration . includes ( 'const ' ) )
332
- return 'const'
333
- if ( declaration . includes ( 'let ' ) )
334
- return 'let'
335
- return 'var'
336
- }
337
-
338
330
function extractCompleteObjectContent ( value : string ) : string | null {
339
331
// debugLog(state, 'extract-object', `Processing object of length ${value.length}`)
340
332
const fullContent = value . trim ( )
@@ -471,12 +463,9 @@ function createImportTrackingState(): ImportTrackingState {
471
463
}
472
464
473
465
function indentMultilineType ( type : string , baseIndent : string , isLast : boolean ) : string {
474
- // debugLog(undefined, 'indent-multiline', `Processing multiline type with baseIndent="${baseIndent}", isLast=${isLast}`)
475
-
476
466
const lines = type . split ( '\n' )
477
- if ( lines . length === 1 ) {
467
+ if ( lines . length === 1 )
478
468
return `${ baseIndent } ${ type } ${ isLast ? '' : ' |' } `
479
- }
480
469
481
470
interface BracketInfo {
482
471
char : string
@@ -508,45 +497,49 @@ function indentMultilineType(type: string, baseIndent: string, isLast: boolean):
508
497
if ( ! trimmed )
509
498
return ''
510
499
511
- // Track Array type specifically
512
- const openBrackets = trimmed . match ( / [ { < [ ] / g) || [ ]
513
- const closeBrackets = trimmed . match ( / [ } \] > ] / g) || [ ]
514
-
500
+ // Calculate base indentation for this line
515
501
let currentIndent = baseIndent
516
502
if ( i > 0 ) {
503
+ // Add additional indentation for nested structures
517
504
const stackDepth = bracketStack . reduce ( ( depth , info ) => depth + info . depth , 0 )
518
505
currentIndent = baseIndent + ' ' . repeat ( stackDepth )
519
506
520
- // Dedent closing tokens
507
+ // Handle object property indentation
508
+ if ( trimmed . match ( / ^ [ ' " ] / ) ) { // Property starts with a quote
509
+ currentIndent += ' '
510
+ }
511
+
512
+ // Adjust closing brace/bracket indentation
521
513
if ( ( trimmed . startsWith ( '}' ) || trimmed . startsWith ( '>' ) || trimmed . startsWith ( '> |' ) ) && bracketStack . length > 0 ) {
522
514
currentIndent = baseIndent + ' ' . repeat ( Math . max ( 0 , stackDepth - 1 ) )
523
515
}
524
516
}
525
517
526
- // Handle opening brackets with Array context
527
- if ( openBrackets . length > 0 ) {
528
- openBrackets . forEach ( ( bracket ) => {
529
- const isArrayBracket = trimmed . startsWith ( 'Array' ) && bracket === '<'
530
- bracketStack . push ( {
531
- char : bracket ,
532
- indent : currentIndent ,
533
- isArray : isArrayBracket ,
534
- depth : 1 ,
535
- isSingleElement : isInSingleElementArray ,
536
- } )
518
+ // Track brackets for nested structures
519
+ const openBrackets = trimmed . match ( / [ { < [ ] / g) || [ ]
520
+ const closeBrackets = trimmed . match ( / [ } \] > ] / g) || [ ]
521
+
522
+ // Handle opening brackets
523
+ openBrackets . forEach ( ( bracket ) => {
524
+ const isArrayBracket = trimmed . startsWith ( 'Array' ) && bracket === '<'
525
+ bracketStack . push ( {
526
+ char : bracket ,
527
+ indent : currentIndent ,
528
+ isArray : isArrayBracket ,
529
+ depth : 1 ,
530
+ isSingleElement : isInSingleElementArray ,
537
531
} )
538
- }
532
+ } )
539
533
540
534
// Handle closing brackets
541
535
if ( closeBrackets . length > 0 ) {
542
536
for ( let j = 0 ; j < closeBrackets . length ; j ++ ) {
543
- if ( bracketStack . length > 0 ) {
537
+ if ( bracketStack . length > 0 )
544
538
bracketStack . pop ( )
545
- }
546
539
}
547
540
}
548
541
549
- // Add union operator only when appropriate
542
+ // Add union operator when needed
550
543
let needsUnion = false
551
544
if ( ! isLast && i === lines . length - 1 && ! trimmed . endsWith ( ' |' ) && ! trimmed . endsWith ( ';' ) ) {
552
545
needsUnion = true
@@ -555,15 +548,13 @@ function indentMultilineType(type: string, baseIndent: string, isLast: boolean):
555
548
// Handle special cases for objects in arrays
556
549
if ( trimmed === '}' ) {
557
550
const lastArray = [ ...bracketStack ] . reverse ( ) . find ( info => info . isArray )
558
- if ( lastArray ?. isSingleElement ) {
551
+ if ( lastArray ?. isSingleElement )
559
552
needsUnion = false
560
- }
561
553
}
562
554
563
- // Don't add union if it's already part of the trimmed content
564
- if ( trimmed . endsWith ( ' |' ) ) {
555
+ // Don't add union if it's already there
556
+ if ( trimmed . endsWith ( ' |' ) )
565
557
needsUnion = false
566
- }
567
558
568
559
return `${ currentIndent } ${ trimmed } ${ needsUnion ? ' |' : '' } `
569
560
} ) . filter ( Boolean )
@@ -623,10 +614,10 @@ function inferArrayType(value: string, state?: ProcessingState, preserveLineBrea
623
614
} )
624
615
625
616
if ( needsMultilineFormat ( tuples ) ) {
626
- const formattedContent = tuples . map ( ( type , i ) => {
627
- const isLast = i === tuples . length - 1
628
- return ` ${ type } ${ isLast ? '' : ' |' } `
629
- } ) . join ( '\n' )
617
+ // Use indentMultilineType for tuple formatting
618
+ const formattedContent = tuples . map ( ( type , i ) =>
619
+ indentMultilineType ( type , ' ' , i === tuples . length - 1 ) ,
620
+ ) . join ( '\n' )
630
621
return `readonly [\n${ formattedContent } \n ]`
631
622
}
632
623
@@ -651,13 +642,18 @@ function inferArrayType(value: string, state?: ProcessingState, preserveLineBrea
651
642
} )
652
643
653
644
const types = elementTypes . filter ( Boolean )
654
- const needsMultiline = types . some ( type => type . includes ( '\n' ) || type . includes ( '{' ) || type . length > 40 )
645
+ const needsMultiline = types . some ( type =>
646
+ type . includes ( '\n' )
647
+ || type . includes ( '{' )
648
+ || type . length > 40
649
+ || types . join ( ' | ' ) . length > 60 ,
650
+ )
655
651
656
652
if ( needsMultiline && preserveLineBreaks ) {
657
- const formattedContent = types . map ( ( type , i ) => {
658
- const isLast = i === types . length - 1
659
- return ` ${ type } ${ isLast ? '' : ' |' } `
660
- } ) . join ( '\n' )
653
+ // Use indentMultilineType for array type formatting
654
+ const formattedContent = types . map ( ( type , i ) =>
655
+ indentMultilineType ( type , ' ' , i === types . length - 1 ) ,
656
+ ) . join ( '\n' )
661
657
return `Array<\n${ formattedContent } \n >`
662
658
}
663
659
@@ -668,27 +664,25 @@ function inferArrayType(value: string, state?: ProcessingState, preserveLineBrea
668
664
* Process object properties with improved formatting
669
665
*/
670
666
function inferComplexObjectType ( value : string , state ?: ProcessingState , indentLevel = 0 ) : string {
671
- // debugLog(state, 'infer-complex', `Inferring type for object of length ${value.length}`)
672
-
673
667
const content = extractCompleteObjectContent ( value )
674
668
if ( ! content )
675
669
return 'Record<string, unknown>'
676
670
671
+ // Calculate indentation based on nesting level
677
672
const baseIndent = ' ' . repeat ( indentLevel )
678
673
const propIndent = ' ' . repeat ( indentLevel + 1 )
679
-
680
- // debugLog(state, 'infer-complex-content', `Processing content with indent level ${indentLevel}`)
674
+ const closingIndent = baseIndent // Keep closing brace aligned with opening
681
675
682
676
const props = processObjectProperties ( content , state , indentLevel )
683
677
if ( ! props . length )
684
678
return '{}'
685
679
686
680
const propertyStrings = props . map ( ( { key, value } ) => {
687
- // debugLog(state, 'infer-complex-prop', `Processing property ${key}`)
688
681
return `${ propIndent } ${ key } : ${ value } `
689
682
} )
690
683
691
- return `{\n${ propertyStrings . join ( ';\n' ) } \n${ baseIndent } }`
684
+ // Format the object with consistent indentation
685
+ return `{\n${ propertyStrings . join ( ';\n' ) } \n${ closingIndent } }`
692
686
}
693
687
694
688
function inferConstArrayType ( value : string , state ?: ProcessingState ) : string {
0 commit comments