Skip to content

Commit 5326f73

Browse files
committed
chore: wip
1 parent 63be9e3 commit 5326f73

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

fixtures/output/example-0001.d.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export declare const someObject: {
2727
nestedKey: 'value';
2828
};
2929
otherKey: {
30-
nestedKey: unknown;
30+
nestedKey: () => void;
3131
nestedKey2: (...args: any[]) => unknown;
3232
};
3333
};
@@ -121,10 +121,11 @@ export declare function processData(data: boolean): boolean;
121121
export declare function processData<T extends object>(data: T): T;
122122
export declare function processData(data: unknown): unknown;
123123
export declare const complexObject: {
124-
handlers: {
125-
'async onSuccess<T>(data': unknown;
126-
'onError(error': unknown;
127-
};
124+
handlers: <T>(data: T) => Promise<void> {
125+
console.log(data)
126+
},
127+
onError(error: Error & { code?: number }): never {
128+
throw error;
128129
};
129130

130131
export { generate, dtsConfig }

src/extract.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ interface ImportTrackingState {
5959
usedValues: Set<string> // All used value names
6060
}
6161

62+
interface MethodSignature {
63+
name: string
64+
async: boolean
65+
generics: string
66+
params: string
67+
returnType: string
68+
}
69+
6270
/**
6371
* Regular expression patterns used throughout the module
6472
* @remarks These patterns are optimized for performance and reliability
@@ -116,6 +124,7 @@ export interface PropertyInfo {
116124
type: string
117125
/** Nested property definitions */
118126
nested?: PropertyInfo[]
127+
method?: MethodSignature
119128
}
120129

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

405-
function processValue(value: string): { type: string, nested?: PropertyInfo[] } {
414+
function processValue(value: string): { type: string, nested?: PropertyInfo[], method?: MethodSignature } {
406415
const trimmed = value.trim()
407416

417+
// Handle method declarations
418+
if (trimmed.includes('(') && !trimmed.startsWith('(')) {
419+
const methodSig = parseMethodSignature(trimmed)
420+
if (methodSig) {
421+
const { async, generics, params, returnType } = methodSig
422+
const genericPart = generics ? `<${generics}>` : ''
423+
const returnTypePart = returnType || 'void'
424+
const type = `${async ? 'async ' : ''}${genericPart}(${params}) => ${returnTypePart}`
425+
return { type, method: methodSig }
426+
}
427+
}
428+
429+
// Rest of the existing processValue logic...
408430
if (trimmed.startsWith('{')) {
409431
const nestedProperties = extractObjectProperties(trimmed)
410432
return {
@@ -1554,6 +1576,36 @@ export function parseObjectLiteral(objStr: string): PropertyInfo[] {
15541576
return extractObjectProperties(content)
15551577
}
15561578

1579+
function parseMethodSignature(value: string): MethodSignature | null {
1580+
// Match async methods
1581+
const asyncMatch = value.match(/^async\s+([^<(]+)(?:<([^>]+)>)?\s*\(([\s\S]*?)\)(?:\s*:\s*([\s\S]+))?$/)
1582+
if (asyncMatch) {
1583+
const [, name, generics, params, returnType] = asyncMatch
1584+
return {
1585+
name,
1586+
async: true,
1587+
generics: generics || '',
1588+
params,
1589+
returnType: returnType || 'Promise<void>',
1590+
}
1591+
}
1592+
1593+
// Match regular methods
1594+
const methodMatch = value.match(/^([^<(]+)(?:<([^>]+)>)?\s*\(([\s\S]*?)\)(?:\s*:\s*([\s\S]+))?$/)
1595+
if (methodMatch) {
1596+
const [, name, generics, params, returnType] = methodMatch
1597+
return {
1598+
name,
1599+
async: false,
1600+
generics: generics || '',
1601+
params,
1602+
returnType: returnType || 'void',
1603+
}
1604+
}
1605+
1606+
return null
1607+
}
1608+
15571609
/**
15581610
* Process object type literals
15591611
*/
@@ -2105,6 +2157,12 @@ export function formatObjectType(properties: PropertyInfo[]): string {
21052157

21062158
const formattedProps = properties
21072159
.map((prop) => {
2160+
if (prop.method) {
2161+
const { async, generics, params, returnType } = prop.method
2162+
const genericPart = generics ? `<${generics}>` : ''
2163+
return `${prop.key}${genericPart}(${params})${returnType ? `: ${returnType}` : ''}`
2164+
}
2165+
21082166
const type = prop.nested ? formatNestedType(prop.nested) : prop.type
21092167
return `${prop.key}: ${type}`
21102168
})

0 commit comments

Comments
 (0)