Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
chore: wip

chore: wip
  • Loading branch information
chrisbbreuer committed Oct 29, 2024
1 parent d74fcee commit 9497d6f
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 127 deletions.
67 changes: 67 additions & 0 deletions fixtures/input/example-0001.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,71 @@ interface RegexPatterns {
readonly moduleAugmentation: RegExp
}

/**
* Extract complete function signature using regex
*/
export function extractFunctionSignature(declaration: string): FunctionSignature {
// Remove comments and clean up the declaration
const cleanDeclaration = removeLeadingComments(declaration).trim()

const functionPattern = /^\s*(export\s+)?(async\s+)?function\s*(\*)?\s*([^(<\s]+)/
const functionMatch = cleanDeclaration.match(functionPattern)

if (!functionMatch) {
console.error('Function name could not be extracted from declaration:', declaration)
return {
name: '',
params: '',
returnType: 'void',
generics: '',
}
}

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

// Extract generics
let generics = ''
if (rest.startsWith('<')) {
const genericsResult = extractBalancedSymbols(rest, '<', '>')
if (genericsResult) {
generics = genericsResult.content
rest = genericsResult.rest.trim()
}
}

// Extract parameters
let params = ''
if (rest.startsWith('(')) {
const paramsResult = extractBalancedSymbols(rest, '(', ')')
if (paramsResult) {
params = paramsResult.content.slice(1, -1).trim()
rest = paramsResult.rest.trim()
}
}

// Extract return type - keep it exactly as specified
let returnType = 'void'
if (rest.startsWith(':')) {
const match = rest.match(/^:\s*([^{]+)/)
if (match) {
returnType = match[1].trim()
}
}

return {
name,
params,
returnType: normalizeType(returnType),
generics,
}
}

// export interface ImportTrackingState {
// typeImports: Map<string, Set<string>>
// valueImports: Map<string, Set<string>>
// usedTypes: Set<string>
// usedValues: Set<string>
// }

export default dts
4 changes: 4 additions & 0 deletions fixtures/output/example-0001.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ declare interface RegexPatterns {
readonly moduleDeclaration: RegExp
readonly moduleAugmentation: RegExp
}
/**
* Extract complete function signature using regex
*/
export declare function extractFunctionSignature(declaration: string): FunctionSignature;

export { generate, dtsConfig }
export type { DtsGenerationOption }
Expand Down
Loading

0 comments on commit 9497d6f

Please sign in to comment.