@@ -531,6 +531,21 @@ function createImportTrackingState(): ImportTrackingState {
531531 }
532532}
533533
534+ function indentMultilineType ( type : string , baseIndent : string , isLast : boolean ) : string {
535+ const lines = type . split ( '\n' )
536+ return lines
537+ . map ( ( line , i ) => {
538+ if ( i === 0 )
539+ return `${ baseIndent } ${ line } `
540+ const trimmed = line . trim ( )
541+ if ( ! trimmed )
542+ return ''
543+ return `${ baseIndent } ${ trimmed } `
544+ } )
545+ . filter ( Boolean )
546+ . join ( '\n' ) + ( isLast ? '' : ' |' )
547+ }
548+
534549function inferValueType ( value : string ) : string {
535550 value = value . trim ( )
536551
@@ -582,7 +597,7 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
582597 }
583598
584599 // Process each element
585- const elementTypes = elements . map ( ( element , index ) => {
600+ const elementTypes = elements . map ( ( element ) => {
586601 const trimmed = element . trim ( )
587602
588603 // Handle nested arrays
@@ -626,23 +641,14 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
626641 if ( needsMultiline ) {
627642 const formattedTypes = types . map ( ( type , index ) => {
628643 const isLast = index === types . length - 1
629- // For types that contain newlines, ensure proper indentation
644+ // For types that contain newlines
630645 if ( type . includes ( '\n' ) ) {
631- const lines = type . split ( '\n' )
632- const formattedLines = lines . map ( ( line , i ) => {
633- // First line gets element indentation
634- if ( i === 0 )
635- return line
636- // Other lines get additional indentation
637- return `${ elementIndent } ${ line . trimLeft ( ) } `
638- } ) . join ( '\n' )
639- return `${ elementIndent } ${ formattedLines } ${ isLast ? '' : ' |' } `
646+ return indentMultilineType ( type , elementIndent , isLast )
640647 }
641648 // For single-line types
642649 return `${ elementIndent } ${ type } ${ isLast ? '' : ' |' } `
643650 } )
644651
645- // Join lines and ensure closing bracket aligns with parent
646652 return `Array<\n${ formattedTypes . join ( '\n' ) } \n${ baseIndent } >`
647653 }
648654
@@ -661,6 +667,7 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe
661667
662668 const baseIndent = ' ' . repeat ( indentLevel )
663669 const propIndent = ' ' . repeat ( indentLevel + 1 )
670+ const innerIndent = ' ' . repeat ( indentLevel + 2 )
664671
665672 const props = processObjectProperties ( content , state )
666673 if ( ! props . length )
@@ -669,20 +676,26 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe
669676 const propertyStrings = props . map ( ( { key, value } ) => {
670677 const formattedKey = / ^ \w + $ / . test ( key ) ? key : `'${ key } '`
671678
672- // Handle multiline values (like objects and arrays)
673679 if ( value . includes ( '\n' ) ) {
674- // Add one level of indentation to each line after the first
680+ // Indent nested multiline values
675681 const indentedValue = value
676682 . split ( '\n' )
677- . map ( ( line , i ) => i === 0 ? line : `${ propIndent } ${ line . trim ( ) } ` )
683+ . map ( ( line , i ) => {
684+ if ( i === 0 )
685+ return line
686+ const trimmed = line . trim ( )
687+ if ( ! trimmed )
688+ return ''
689+ return `${ innerIndent } ${ trimmed } `
690+ } )
691+ . filter ( Boolean )
678692 . join ( '\n' )
679693 return `${ propIndent } ${ formattedKey } : ${ indentedValue } `
680694 }
681695
682696 return `${ propIndent } ${ formattedKey } : ${ value } `
683697 } )
684698
685- // Ensure closing brace aligns with parent
686699 return `{\n${ propertyStrings . join ( ';\n' ) } \n${ baseIndent } }`
687700}
688701
@@ -1348,7 +1361,13 @@ function processObjectMethod(declaration: string, value: string, state?: Process
13481361 else if ( isAsync && ! effectiveReturnType . includes ( 'Promise' ) ) {
13491362 effectiveReturnType = `Promise<${ effectiveReturnType } >`
13501363 }
1351- else if ( value . includes ( 'toISOString()' ) || ( value . includes ( 'Intl.NumberFormat' ) && value . includes ( 'format' ) ) ) {
1364+ else if ( value . includes ( 'toISOString()' ) || value . includes ( 'toString()' ) ) {
1365+ effectiveReturnType = 'string'
1366+ }
1367+ else if ( value . includes ( 'console.log' ) || value . match ( / v o i d \s * [ ; { ] / ) ) {
1368+ effectiveReturnType = 'void'
1369+ }
1370+ else if ( value . includes ( 'Intl.NumberFormat' ) && value . includes ( 'format' ) ) {
13521371 effectiveReturnType = 'string'
13531372 }
13541373
@@ -1452,8 +1471,6 @@ function processProperty(key: string, value: string, state?: ProcessingState, in
14521471 const cleanKey = key . trim ( ) . replace ( / ^ [ ' " ] ( .* ) [ ' " ] $ / , '$1' )
14531472 const cleanValue = value . trim ( )
14541473
1455- debugLog ( state , 'process-property' , `Processing property "${ cleanKey } " with value: ${ cleanValue } ` )
1456-
14571474 // Handle method declarations
14581475 if ( cleanKey . includes ( '(' ) ) {
14591476 const { name, signature } = processObjectMethod ( cleanKey , cleanValue , state )
@@ -1462,13 +1479,14 @@ function processProperty(key: string, value: string, state?: ProcessingState, in
14621479
14631480 // Handle arrays with proper indentation
14641481 if ( cleanValue . startsWith ( '[' ) ) {
1465- debugLog ( state , 'process-array' , `Processing array in property "${ cleanKey } "` )
1466- return { key : cleanKey , value : inferArrayType ( cleanValue , state , indentLevel ) }
1482+ return {
1483+ key : cleanKey ,
1484+ value : inferArrayType ( cleanValue , state , indentLevel ) ,
1485+ }
14671486 }
14681487
14691488 // Handle object literals with proper indentation
14701489 if ( cleanValue . startsWith ( '{' ) ) {
1471- debugLog ( state , 'process-object' , `Processing nested object in property "${ cleanKey } "` )
14721490 return {
14731491 key : cleanKey ,
14741492 value : inferComplexObjectType ( cleanValue , state , indentLevel ) ,
@@ -1477,7 +1495,6 @@ function processProperty(key: string, value: string, state?: ProcessingState, in
14771495
14781496 // Handle function expressions
14791497 if ( cleanValue . includes ( '=>' ) || cleanValue . includes ( 'function' ) ) {
1480- debugLog ( state , 'process-property' , 'Processing function expression' )
14811498 const funcType = extractFunctionType ( cleanValue , state )
14821499 return {
14831500 key : cleanKey ,
@@ -1491,12 +1508,11 @@ function processProperty(key: string, value: string, state?: ProcessingState, in
14911508 return { key : cleanKey , value : cleanValue }
14921509 }
14931510
1494- // Handle references to global objects or function calls
1511+ // Handle references and function calls
14951512 if ( cleanValue . includes ( '.' ) || cleanValue . includes ( '(' ) ) {
14961513 return { key : cleanKey , value : 'unknown' }
14971514 }
14981515
1499- // Default case
15001516 return { key : cleanKey , value : 'unknown' }
15011517}
15021518
0 commit comments