Skip to content

Commit e06753d

Browse files
committed
chore: wip
1 parent 32188de commit e06753d

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

src/extract.ts

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ export function isFunction(value: string): boolean {
368368
/**
369369
* Infer array type from array literal
370370
*/
371-
function inferArrayType(value: string): string {
371+
export function inferArrayType(value: string): string {
372372
const content = extractNestedContent(value, '[', ']')
373373
if (!content)
374374
return 'never[]'
@@ -377,9 +377,27 @@ function inferArrayType(value: string): string {
377377
if (elements.length === 0)
378378
return 'never[]'
379379

380-
// Use processNestedArray to handle the array structure
381-
const processedType = processNestedArray(elements)
382-
return `Array<${processedType}>`
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) => {
384+
const trimmed = element.trim()
385+
if (trimmed.startsWith('[')) {
386+
const innerTypes = inferArrayType(trimmed)
387+
return innerTypes
388+
}
389+
return inferElementType(trimmed)
390+
})
391+
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(' | ')}>`
395+
}
396+
397+
// For simple arrays, process elements normally
398+
const elementTypes = elements.map(element => inferElementType(element.trim()))
399+
const uniqueTypes = [...new Set(elementTypes)]
400+
return `Array<${uniqueTypes.join(' | ')}>`
383401
}
384402

385403
/**
@@ -404,27 +422,21 @@ export function inferElementType(element: string): string {
404422
return formatObjectType(parseObjectLiteral(trimmed))
405423
}
406424

407-
// Handle known function references
408-
if (trimmed === 'console.log' || trimmed.endsWith('.log')) {
425+
// Handle function expressions and references
426+
if (
427+
trimmed === 'console.log'
428+
|| trimmed.endsWith('.log')
429+
|| trimmed.includes('=>')
430+
|| trimmed.includes('function')
431+
) {
409432
return '(...args: any[]) => void'
410433
}
411434

412-
// Handle arrow functions
413-
if (trimmed.includes('=>')) {
414-
return '(...args: any[]) => void'
415-
}
416-
417-
// Handle function calls
418-
if (trimmed.includes('(') && trimmed.includes(')')) {
419-
return 'unknown'
420-
}
421-
422-
// Handle references to global objects
423-
if (trimmed.includes('.')) {
435+
// Handle global references and unknown identifiers
436+
if (trimmed.includes('.') || /^[a-z_]\w*$/i.test(trimmed)) {
424437
return 'unknown'
425438
}
426439

427-
// Default case
428440
return 'unknown'
429441
}
430442

@@ -440,16 +452,17 @@ export function processNestedArray(elements: string[]): string {
440452
const nestedContent = extractNestedContent(trimmed, '[', ']')
441453
if (nestedContent) {
442454
const nestedElements = splitArrayElements(nestedContent)
443-
return `Array<${processNestedArray(nestedElements)}>`
455+
// Ensure nested arrays are properly typed
456+
const nestedTypes = nestedElements.map(e => inferElementType(e.trim()))
457+
return `Array<${[...new Set(nestedTypes)].join(' | ')}>`
444458
}
445459
return 'never'
446460
}
447461

448462
return inferElementType(trimmed)
449-
})
463+
}).filter(type => type !== 'never' && type !== '')
450464

451-
const uniqueTypes = [...new Set(processedTypes.filter(type => type !== 'never'))]
452-
return uniqueTypes.join(' | ')
465+
return [...new Set(processedTypes)].join(' | ')
453466
}
454467

455468
/**
@@ -498,6 +511,7 @@ export function splitArrayElements(content: string): string[] {
498511
let stringChar = ''
499512

500513
for (const char of content) {
514+
// Handle string boundaries
501515
if ((char === '"' || char === '\'') && !inString) {
502516
inString = true
503517
stringChar = char
@@ -506,22 +520,26 @@ export function splitArrayElements(content: string): string[] {
506520
inString = false
507521
}
508522

523+
// Track nesting depth
509524
if (!inString) {
510525
if (char === '[' || char === '{')
511526
depth++
512527
else if (char === ']' || char === '}')
513528
depth--
514529
}
515530

531+
// Split elements only at top level
516532
if (char === ',' && depth === 0 && !inString) {
517-
elements.push(current.trim())
533+
if (current.trim())
534+
elements.push(current.trim())
518535
current = ''
519536
}
520537
else {
521538
current += char
522539
}
523540
}
524541

542+
// Add the last element
525543
if (current.trim())
526544
elements.push(current.trim())
527545

0 commit comments

Comments
 (0)