@@ -61,6 +61,7 @@ interface TypeAnnotation {
61
61
}
62
62
63
63
function getTypeAnnotation ( declaration : string ) : TypeAnnotation {
64
+ // eslint-disable-next-line regexp/no-super-linear-backtracking
64
65
const match = declaration . match ( / : \s * ( \{ [ ^ = ] + \} | \[ [ ^ \] ] + \] | [ ^ = ] + ?) \s * = / )
65
66
return {
66
67
raw : match ?. [ 1 ] ?. trim ( ) ?? null ,
@@ -376,46 +377,79 @@ function inferArrayType(value: string): string {
376
377
if ( elements . length === 0 )
377
378
return 'never[]'
378
379
379
- const elementTypes = elements . map ( element => inferElementType ( element . trim ( ) ) )
380
-
381
- // Check if we have nested arrays
382
- if ( elements . some ( el => el . trim ( ) . startsWith ( '[' ) ) ) {
383
- // Preserve array nesting structure
384
- return `Array<${ elementTypes . join ( ' | ' ) . replace ( / ' + $ / , '' ) } >`
385
- }
386
-
387
- const uniqueTypes = [ ...new Set ( elementTypes ) ]
388
- return `Array<${ uniqueTypes . join ( ' | ' ) } >`
380
+ // Use processNestedArray to handle the array structure
381
+ const processedType = processNestedArray ( elements )
382
+ return `Array<${ processedType } >`
389
383
}
390
384
391
385
/**
392
386
* Infer element type from a single array element
393
387
*/
394
388
export function inferElementType ( element : string ) : string {
395
- if ( element . trim ( ) . startsWith ( '[' ) ) {
396
- const nested = inferArrayType ( element )
397
- return nested
398
- }
389
+ const trimmed = element . trim ( )
399
390
400
- if ( element . trim ( ) . startsWith ( '{' ) )
401
- return formatObjectType ( parseObjectLiteral ( element ) )
391
+ // Handle string literals
392
+ if ( trimmed . startsWith ( '\'' ) || trimmed . startsWith ( '"' ) ) {
393
+ const cleanValue = trimmed . slice ( 1 , - 1 ) . replace ( / ' + $ / , '' )
394
+ return `'${ cleanValue } '`
395
+ }
402
396
403
- if ( element . trim ( ) . startsWith ( '\'' ) || element . trim ( ) . startsWith ( '"' ) )
404
- return `'${ element . slice ( 1 , - 1 ) . replace ( / ' + $ / , '' ) } '`
397
+ // Handle numbers
398
+ if ( ! Number . isNaN ( Number ( trimmed ) ) ) {
399
+ return trimmed
400
+ }
405
401
406
- if ( ! Number . isNaN ( Number ( element . trim ( ) ) ) )
407
- return element . trim ( )
402
+ // Handle objects
403
+ if ( trimmed . startsWith ( '{' ) ) {
404
+ return formatObjectType ( parseObjectLiteral ( trimmed ) )
405
+ }
408
406
409
- if ( element . trim ( ) === 'console.log' )
407
+ // Handle known function references
408
+ if ( trimmed === 'console.log' || trimmed . endsWith ( '.log' ) ) {
410
409
return '(...args: any[]) => void'
410
+ }
411
411
412
- if ( element . includes ( '=>' ) )
412
+ // Handle arrow functions
413
+ if ( trimmed . includes ( '=>' ) ) {
413
414
return '(...args: any[]) => void'
415
+ }
416
+
417
+ // Handle function calls
418
+ if ( trimmed . includes ( '(' ) && trimmed . includes ( ')' ) ) {
419
+ return 'unknown'
420
+ }
414
421
415
- if ( element . includes ( '.' ) )
422
+ // Handle references to global objects
423
+ if ( trimmed . includes ( '.' ) ) {
416
424
return 'unknown'
425
+ }
426
+
427
+ // Default case
428
+ return 'unknown'
429
+ }
430
+
431
+ /**
432
+ * Process nested array structures
433
+ */
434
+ export function processNestedArray ( elements : string [ ] ) : string {
435
+ const processedTypes = elements . map ( ( element ) => {
436
+ const trimmed = element . trim ( )
437
+
438
+ // Handle nested arrays
439
+ if ( trimmed . startsWith ( '[' ) ) {
440
+ const nestedContent = extractNestedContent ( trimmed , '[' , ']' )
441
+ if ( nestedContent ) {
442
+ const nestedElements = splitArrayElements ( nestedContent )
443
+ return `Array<${ processNestedArray ( nestedElements ) } >`
444
+ }
445
+ return 'never'
446
+ }
447
+
448
+ return inferElementType ( trimmed )
449
+ } )
417
450
418
- return 'any'
451
+ const uniqueTypes = [ ...new Set ( processedTypes . filter ( type => type !== 'never' ) ) ]
452
+ return uniqueTypes . join ( ' | ' )
419
453
}
420
454
421
455
/**
@@ -491,7 +525,7 @@ export function splitArrayElements(content: string): string[] {
491
525
if ( current . trim ( ) )
492
526
elements . push ( current . trim ( ) )
493
527
494
- return elements
528
+ return elements . filter ( Boolean )
495
529
}
496
530
497
531
/**
@@ -654,10 +688,10 @@ export function isCommentLine(line: string): boolean {
654
688
655
689
function processCommentLine ( line : string , state : ProcessingState ) : void {
656
690
const indentedLine = line . startsWith ( '*' )
657
- ? ` ${ line } ` // Add indentation for content lines
691
+ ? ` ${ line } ` // Add indentation for content lines
658
692
: line . startsWith ( '/**' ) || line . startsWith ( '*/' )
659
693
? line // Keep delimiters at original indentation
660
- : ` ${ line } ` // Add indentation for other lines
694
+ : ` ${ line } ` // Add indentation for other lines
661
695
662
696
if ( line . startsWith ( '/**' ) )
663
697
state . lastCommentBlock = ''
0 commit comments