Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 27, 2024
1 parent 63be9e3 commit 5326f73
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
11 changes: 6 additions & 5 deletions fixtures/output/example-0001.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export declare const someObject: {
nestedKey: 'value';
};
otherKey: {
nestedKey: unknown;
nestedKey: () => void;
nestedKey2: (...args: any[]) => unknown;
};
};
Expand Down Expand Up @@ -121,10 +121,11 @@ export declare function processData(data: boolean): boolean;
export declare function processData<T extends object>(data: T): T;
export declare function processData(data: unknown): unknown;
export declare const complexObject: {
handlers: {
'async onSuccess<T>(data': unknown;
'onError(error': unknown;
};
handlers: <T>(data: T) => Promise<void> {
console.log(data)
},
onError(error: Error & { code?: number }): never {
throw error;
};

export { generate, dtsConfig }
Expand Down
60 changes: 59 additions & 1 deletion src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ interface ImportTrackingState {
usedValues: Set<string> // All used value names
}

interface MethodSignature {
name: string
async: boolean
generics: string
params: string
returnType: string
}

/**
* Regular expression patterns used throughout the module
* @remarks These patterns are optimized for performance and reliability
Expand Down Expand Up @@ -116,6 +124,7 @@ export interface PropertyInfo {
type: string
/** Nested property definitions */
nested?: PropertyInfo[]
method?: MethodSignature
}

/**
Expand Down Expand Up @@ -402,9 +411,22 @@ export function processLine(line: string, state: ProcessingState): void {
console.log('Unprocessed line:', trimmedLine)
}

function processValue(value: string): { type: string, nested?: PropertyInfo[] } {
function processValue(value: string): { type: string, nested?: PropertyInfo[], method?: MethodSignature } {
const trimmed = value.trim()

// Handle method declarations
if (trimmed.includes('(') && !trimmed.startsWith('(')) {
const methodSig = parseMethodSignature(trimmed)
if (methodSig) {
const { async, generics, params, returnType } = methodSig
const genericPart = generics ? `<${generics}>` : ''
const returnTypePart = returnType || 'void'
const type = `${async ? 'async ' : ''}${genericPart}(${params}) => ${returnTypePart}`
return { type, method: methodSig }
}
}

// Rest of the existing processValue logic...
if (trimmed.startsWith('{')) {
const nestedProperties = extractObjectProperties(trimmed)
return {
Expand Down Expand Up @@ -1554,6 +1576,36 @@ export function parseObjectLiteral(objStr: string): PropertyInfo[] {
return extractObjectProperties(content)
}

function parseMethodSignature(value: string): MethodSignature | null {
// Match async methods
const asyncMatch = value.match(/^async\s+([^<(]+)(?:<([^>]+)>)?\s*\(([\s\S]*?)\)(?:\s*:\s*([\s\S]+))?$/)
if (asyncMatch) {
const [, name, generics, params, returnType] = asyncMatch
return {
name,
async: true,
generics: generics || '',
params,
returnType: returnType || 'Promise<void>',
}
}

// Match regular methods
const methodMatch = value.match(/^([^<(]+)(?:<([^>]+)>)?\s*\(([\s\S]*?)\)(?:\s*:\s*([\s\S]+))?$/)
if (methodMatch) {
const [, name, generics, params, returnType] = methodMatch
return {
name,
async: false,
generics: generics || '',
params,
returnType: returnType || 'void',
}
}

return null
}

/**
* Process object type literals
*/
Expand Down Expand Up @@ -2105,6 +2157,12 @@ export function formatObjectType(properties: PropertyInfo[]): string {

const formattedProps = properties
.map((prop) => {
if (prop.method) {
const { async, generics, params, returnType } = prop.method
const genericPart = generics ? `<${generics}>` : ''
return `${prop.key}${genericPart}(${params})${returnType ? `: ${returnType}` : ''}`
}

const type = prop.nested ? formatNestedType(prop.nested) : prop.type
return `${prop.key}: ${type}`
})
Expand Down

0 comments on commit 5326f73

Please sign in to comment.