Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
chore: wip
  • Loading branch information
chrisbbreuer committed Oct 28, 2024
1 parent 04012b7 commit e17362a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 49 deletions.
25 changes: 12 additions & 13 deletions fixtures/input/example-0001.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,19 +352,18 @@ export const complexArrays = {
// }
// }

// Complex Async Patterns
// due to isolatedDeclarations, we can assume the return type here
// export async function* complexAsyncGenerator(): any {
// const results = await Promise.all([
// fetchUsers(),
// getProduct(1),
// authenticate('user', 'pass'),
// ])

// for (const result of results) {
// yield result
// }
// }
// Complex Async Patterns -> due to isolatedDeclarations, we can assume the return type here
export async function* complexAsyncGenerator(): any {
const results = await Promise.all([
fetchUsers(),
getProduct(1),
authenticate('user', 'pass'),
])

for (const result of results) {
yield result
}
}

// Type Assertions and Guards
export function isUser(value: unknown): value is User {
Expand Down
1 change: 1 addition & 0 deletions fixtures/output/example-0001.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export declare const complexArrays: {
matrix: Array<Array<1 | 2 | Array<3 | 4 | Array<5 | 6>>> | Array<'a' | 'b' | Array<'c' | 'd'>> | Array<true | Array<false | Array<true>>>>;
tuples: Array<Array<1 | 'string' | true> | Array<'literal' | 42 | false>>;
};
export declare function complexAsyncGenerator(): any;
export declare function isUser(value: unknown): value is User;
export declare type UserId = string & { readonly __brand: unique symbol };
export declare type ProductId = number & {
Expand Down
45 changes: 9 additions & 36 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,13 @@ function extractFunctionName(declaration: string): { name: string, rest: string
* Extract complete function signature using regex
*/
function extractFunctionSignature(declaration: string): FunctionSignature {
// Remove comments from the declaration
// Remove comments and clean up the declaration
const cleanDeclaration = removeLeadingComments(declaration).trim()

// Remove leading 'export', 'async', 'function' keywords
const withoutKeywords = cleanDeclaration.replace(/^\s*(export\s+)?(async\s+)?function\s+/, '').trim()
const functionPattern = /^\s*(export\s+)?(async\s+)?function\s*(\*)?\s*([^(<\s]+)/
const functionMatch = cleanDeclaration.match(functionPattern)

// Extract function name
const { name, rest: afterName } = extractFunctionName(withoutKeywords)
if (!name) {
if (!functionMatch) {
console.error('Function name could not be extracted from declaration:', declaration)
return {
name: '',
Expand All @@ -363,7 +361,8 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
}
}

let rest = afterName
const name = functionMatch[4]
let rest = cleanDeclaration.slice(cleanDeclaration.indexOf(name) + name.length).trim()

// Extract generics
let generics = ''
Expand All @@ -373,40 +372,19 @@ function extractFunctionSignature(declaration: string): FunctionSignature {
generics = genericsResult.content
rest = genericsResult.rest.trim()
}
else {
console.error('Generics could not be extracted from declaration:', declaration)
}
}

// Extract parameters
let params = ''
if (rest.startsWith('(')) {
const paramsResult = extractBalancedSymbols(rest, '(', ')')
if (paramsResult) {
params = paramsResult.content.slice(1, -1).trim() // Remove the surrounding parentheses
params = paramsResult.content.slice(1, -1).trim()
rest = paramsResult.rest.trim()
}
else {
console.error('Parameters could not be extracted from declaration:', declaration)
return {
name,
params: '',
returnType: 'void',
generics,
}
}
}
else {
console.error('Parameters could not be extracted from declaration:', declaration)
return {
name,
params: '',
returnType: 'void',
generics,
}
}

// Extract return type
// Extract return type - keep it exactly as specified
let returnType = 'void'
if (rest.startsWith(':')) {
const match = rest.match(/^:\s*([^{]+)/)
Expand Down Expand Up @@ -895,17 +873,12 @@ function processFunctionDeclaration(
generics,
} = extractFunctionSignature(cleanDeclaration)

// Add logs to verify extracted components
console.log('Function Name:', name)
console.log('Generics:', generics)
console.log('Parameters:', params)
console.log('Return Type:', returnType)

// Track used types if provided
if (usedTypes) {
trackUsedTypes(`${generics} ${params} ${returnType}`, usedTypes)
}

// Build the declaration string
const parts = [
isExported ? 'export' : '',
'declare',
Expand Down

0 comments on commit e17362a

Please sign in to comment.