Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 22, 2024
1 parent 32188de commit e06753d
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export function isFunction(value: string): boolean {
/**
* Infer array type from array literal
*/
function inferArrayType(value: string): string {
export function inferArrayType(value: string): string {
const content = extractNestedContent(value, '[', ']')
if (!content)
return 'never[]'
Expand All @@ -377,9 +377,27 @@ function inferArrayType(value: string): string {
if (elements.length === 0)
return 'never[]'

// Use processNestedArray to handle the array structure
const processedType = processNestedArray(elements)
return `Array<${processedType}>`
// For deeply nested arrays, handle each level properly
const isNestedArray = elements.some(el => el.trim().startsWith('['))
if (isNestedArray) {
const processedTypes = elements.map((element) => {
const trimmed = element.trim()
if (trimmed.startsWith('[')) {
const innerTypes = inferArrayType(trimmed)
return innerTypes
}
return inferElementType(trimmed)
})

// Filter out any invalid types and create union
const validTypes = processedTypes.filter(type => type !== 'never' && type !== '')
return `Array<${[...new Set(validTypes)].join(' | ')}>`
}

// For simple arrays, process elements normally
const elementTypes = elements.map(element => inferElementType(element.trim()))
const uniqueTypes = [...new Set(elementTypes)]
return `Array<${uniqueTypes.join(' | ')}>`
}

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

// Handle known function references
if (trimmed === 'console.log' || trimmed.endsWith('.log')) {
// Handle function expressions and references
if (
trimmed === 'console.log'
|| trimmed.endsWith('.log')
|| trimmed.includes('=>')
|| trimmed.includes('function')
) {
return '(...args: any[]) => void'
}

// Handle arrow functions
if (trimmed.includes('=>')) {
return '(...args: any[]) => void'
}

// Handle function calls
if (trimmed.includes('(') && trimmed.includes(')')) {
return 'unknown'
}

// Handle references to global objects
if (trimmed.includes('.')) {
// Handle global references and unknown identifiers
if (trimmed.includes('.') || /^[a-z_]\w*$/i.test(trimmed)) {
return 'unknown'
}

// Default case
return 'unknown'
}

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

return inferElementType(trimmed)
})
}).filter(type => type !== 'never' && type !== '')

const uniqueTypes = [...new Set(processedTypes.filter(type => type !== 'never'))]
return uniqueTypes.join(' | ')
return [...new Set(processedTypes)].join(' | ')
}

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

for (const char of content) {
// Handle string boundaries
if ((char === '"' || char === '\'') && !inString) {
inString = true
stringChar = char
Expand All @@ -506,22 +520,26 @@ export function splitArrayElements(content: string): string[] {
inString = false
}

// Track nesting depth
if (!inString) {
if (char === '[' || char === '{')
depth++
else if (char === ']' || char === '}')
depth--
}

// Split elements only at top level
if (char === ',' && depth === 0 && !inString) {
elements.push(current.trim())
if (current.trim())
elements.push(current.trim())
current = ''
}
else {
current += char
}
}

// Add the last element
if (current.trim())
elements.push(current.trim())

Expand Down

0 comments on commit e06753d

Please sign in to comment.