Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 26, 2024
1 parent db92cdd commit 1ccd221
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion fixtures/output/example-0001.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export declare const someObject: {
someNestedArray: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10>>;
someNestedArray2: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10> | 'dummy value'>;
someNestedArray3: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10> | 'dummy value' | Array<11 | 12 | 13>>;
someOtherNestedArray: Array<Array<'some text' | 2 | unknown | (...args: any[]) => unknown> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10>>;
someOtherNestedArray: Array<Array<'some text' | 2 | unknown | ((...args: any[]) => unknown)> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10>>;
someComplexArray: Array<Array<{ key: 'value' }> | Array<{ key2: 'value2' } | 'test' | 1000> | Array<'some string' | unknown>>;
someObject: {
key: 'value';
Expand Down
41 changes: 26 additions & 15 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,11 +909,10 @@ export function processCompleteProperty({ key, content }: { key?: string, conten
function processComplexTypeDeclaration(declaration: string): string {
// Handle union and intersection types
if (declaration.includes('|') || declaration.includes('&')) {
const match = declaration.match(REGEX.unionIntersection)
if (match) {
const types = declaration.split(/\s*[|&]\s*/)
return types.join(declaration.includes('|') ? ' | ' : ' & ')
}
const operator = declaration.includes('|') ? '|' : '&'
const types = declaration.split(new RegExp(`\\s*\\${operator}\\s*`)).map(type => type.trim())
const combinedTypes = combineTypes(types, operator as '|' | '&')
return combinedTypes
}

// Handle mapped types
Expand Down Expand Up @@ -1018,6 +1017,23 @@ export function isFunction(value: string): boolean {
)
}

/**
* Check if a given type string represents a function type
*/
function isFunctionType(type: string): boolean {
const functionTypeRegex = /^\s*\(.*\)\s*=>\s*(?:\S.*|[\t\v\f \xA0\u1680\u2000-\u200A\u202F\u205F\u3000\uFEFF])$/
return functionTypeRegex.test(type.trim())
}

/**
* Combine types into a union or intersection, wrapping function types in parentheses
*/
function combineTypes(types: string[], operator: '|' | '&' = '|'): string {
const uniqueTypes = [...new Set(types)]
const normalizedTypes = uniqueTypes.map(type => isFunctionType(type) ? `(${type})` : type)
return normalizedTypes.join(` ${operator} `)
}

/**
* Determines if a line is a comment
* @param line - Source code line to check
Expand Down Expand Up @@ -1079,14 +1095,8 @@ function inferArrayType(value: string): string {
}

const elementTypes = elements.map(element => inferElementType(element.trim()))
const uniqueTypes = [...new Set(elementTypes)]

// Handle nested arrays
if (uniqueTypes.every(type => type.startsWith('Array<'))) {
return `Array<${uniqueTypes.join(' | ')}>`
}

return `Array<${uniqueTypes.join(' | ')}>`
const combinedTypes = combineTypes(elementTypes)
return `Array<${combinedTypes}>`
}

/**
Expand Down Expand Up @@ -1172,15 +1182,16 @@ export function processNestedArray(elements: string[]): string {
if (nestedContent) {
const nestedElements = splitArrayElements(nestedContent)
const nestedTypes = nestedElements.map(ne => inferElementType(ne.trim()))
return `Array<${nestedTypes.join(' | ')}>`
const combinedNestedTypes = combineTypes(nestedTypes)
return `Array<${combinedNestedTypes}>`
}
return 'never'
}

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

return processedTypes.join(' | ')
return combineTypes(processedTypes)
}

/**
Expand Down

0 comments on commit 1ccd221

Please sign in to comment.