@@ -377,24 +377,24 @@ export function inferArrayType(value: string): string {
377
377
if ( elements . length === 0 )
378
378
return 'never[]'
379
379
380
- // For deeply nested arrays, handle each level properly
381
- const isNestedArray = elements . some ( el => el . trim ( ) . startsWith ( '[' ) )
382
- if ( isNestedArray ) {
383
- const processedTypes = elements . map ( ( element ) => {
380
+ // Handle case where elements themselves are arrays
381
+ if ( elements . some ( el => el . trim ( ) . startsWith ( '[' ) ) ) {
382
+ const nestedTypes = elements . map ( ( element ) => {
384
383
const trimmed = element . trim ( )
385
384
if ( trimmed . startsWith ( '[' ) ) {
386
- const innerTypes = inferArrayType ( trimmed )
387
- return innerTypes
385
+ const nestedContent = extractNestedContent ( trimmed , '[' , ']' )
386
+ if ( nestedContent ) {
387
+ const nestedElements = splitArrayElements ( nestedContent )
388
+ return `Array<${ nestedElements . map ( ne => inferElementType ( ne . trim ( ) ) ) . join ( ' | ' ) } >`
389
+ }
388
390
}
389
391
return inferElementType ( trimmed )
390
392
} )
391
393
392
- // Filter out any invalid types and create union
393
- const validTypes = processedTypes . filter ( type => type !== 'never' && type !== '' )
394
- return `Array<${ [ ...new Set ( validTypes ) ] . join ( ' | ' ) } >`
394
+ return `Array<${ nestedTypes . join ( ' | ' ) } >`
395
395
}
396
396
397
- // For simple arrays, process elements normally
397
+ // Handle simple array case
398
398
const elementTypes = elements . map ( element => inferElementType ( element . trim ( ) ) )
399
399
const uniqueTypes = [ ...new Set ( elementTypes ) ]
400
400
return `Array<${ uniqueTypes . join ( ' | ' ) } >`
@@ -422,18 +422,29 @@ export function inferElementType(element: string): string {
422
422
return formatObjectType ( parseObjectLiteral ( trimmed ) )
423
423
}
424
424
425
- // Handle function expressions and references
426
- if (
427
- trimmed === 'console.log'
428
- || trimmed . endsWith ( '.log' )
429
- || trimmed . includes ( '=>' )
430
- || trimmed . includes ( 'function' )
431
- ) {
425
+ // Handle function references and calls
426
+ if ( trimmed === 'console.log' || trimmed . endsWith ( '.log' ) ) {
432
427
return '(...args: any[]) => void'
433
428
}
434
429
435
- // Handle global references and unknown identifiers
436
- if ( trimmed . includes ( '.' ) || / ^ [ a - z _ ] \w * $ / i. test ( trimmed ) ) {
430
+ // Handle arrow functions
431
+ if ( trimmed . includes ( '=>' ) ) {
432
+ const arrowFn = trimmed . match ( / \( .* \) \s * = > \s * ( .* ) / )
433
+ return '(...args: any[]) => void'
434
+ }
435
+
436
+ // Handle function calls
437
+ if ( trimmed . endsWith ( '()' ) ) {
438
+ return 'unknown'
439
+ }
440
+
441
+ // Handle object references
442
+ if ( trimmed . includes ( '.' ) ) {
443
+ return 'unknown'
444
+ }
445
+
446
+ // Handle identifiers that might be undefined
447
+ if ( / ^ [ a - z _ ] \w * $ / i. test ( trimmed ) ) {
437
448
return 'unknown'
438
449
}
439
450
@@ -452,17 +463,17 @@ export function processNestedArray(elements: string[]): string {
452
463
const nestedContent = extractNestedContent ( trimmed , '[' , ']' )
453
464
if ( nestedContent ) {
454
465
const nestedElements = splitArrayElements ( nestedContent )
455
- // Ensure nested arrays are properly typed
456
- const nestedTypes = nestedElements . map ( e => inferElementType ( e . trim ( ) ) )
457
- return `Array<${ [ ... new Set ( nestedTypes ) ] . join ( ' | ' ) } >`
466
+ // Process each nested element to create a proper union type
467
+ const nestedTypes = nestedElements . map ( ne => inferElementType ( ne . trim ( ) ) )
468
+ return `Array<${ nestedTypes . join ( ' | ' ) } >`
458
469
}
459
470
return 'never'
460
471
}
461
472
462
473
return inferElementType ( trimmed )
463
- } ) . filter ( type => type !== 'never' && type !== '' )
474
+ } ) . filter ( type => type !== 'never' )
464
475
465
- return [ ... new Set ( processedTypes ) ] . join ( ' | ' )
476
+ return processedTypes . join ( ' | ' )
466
477
}
467
478
468
479
/**
@@ -510,17 +521,21 @@ export function splitArrayElements(content: string): string[] {
510
521
let inString = false
511
522
let stringChar = ''
512
523
513
- for ( const char of content ) {
524
+ for ( let i = 0 ; i < content . length ; i ++ ) {
525
+ const char = content [ i ]
526
+
514
527
// Handle string boundaries
515
- if ( ( char === '"' || char === '\'' ) && ! inString ) {
516
- inString = true
517
- stringChar = char
518
- }
519
- else if ( char === stringChar && ! inString ) {
520
- inString = false
528
+ if ( ( char === '"' || char === '\'' ) && ( i === 0 || content [ i - 1 ] !== '\\' ) ) {
529
+ if ( ! inString ) {
530
+ inString = true
531
+ stringChar = char
532
+ }
533
+ else if ( char === stringChar ) {
534
+ inString = false
535
+ }
521
536
}
522
537
523
- // Track nesting depth
538
+ // Track nested structures
524
539
if ( ! inString ) {
525
540
if ( char === '[' || char === '{' )
526
541
depth ++
@@ -530,8 +545,9 @@ export function splitArrayElements(content: string): string[] {
530
545
531
546
// Split elements only at top level
532
547
if ( char === ',' && depth === 0 && ! inString ) {
533
- if ( current . trim ( ) )
548
+ if ( current . trim ( ) ) {
534
549
elements . push ( current . trim ( ) )
550
+ }
535
551
current = ''
536
552
}
537
553
else {
@@ -540,8 +556,9 @@ export function splitArrayElements(content: string): string[] {
540
556
}
541
557
542
558
// Add the last element
543
- if ( current . trim ( ) )
559
+ if ( current . trim ( ) ) {
544
560
elements . push ( current . trim ( ) )
561
+ }
545
562
546
563
return elements . filter ( Boolean )
547
564
}
0 commit comments