diff --git a/fixtures/output/example-0001.d.ts b/fixtures/output/example-0001.d.ts index 7c54255..c6cab0b 100644 --- a/fixtures/output/example-0001.d.ts +++ b/fixtures/output/example-0001.d.ts @@ -1,8 +1,10 @@ /** * Example of const declaration */ -export declare const config: { [key: string]: string } - +export declare const config: { + apiUrl: 'https://api.example.com', + timeout: '5000' +} /** * Example of interface declaration */ @@ -11,7 +13,6 @@ export interface User { name: string email: string } - /** * Example of type declaration */ @@ -19,7 +20,6 @@ export interface ResponseData { success: boolean data: User[] } - /** * Example of function declaration */ diff --git a/fixtures/output/example-0005.d.ts b/fixtures/output/example-0005.d.ts index f08f8c4..644e540 100644 --- a/fixtures/output/example-0005.d.ts +++ b/fixtures/output/example-0005.d.ts @@ -1,15 +1,12 @@ export declare const defaultHeaders: { 'Content-Type': 'application/json', } - export interface Comment { id: number postId: number body: string } - export interface CommentsResponse { comments: Comment[] } - export declare function fetchComments(postId: number): Promise diff --git a/src/extract.ts b/src/extract.ts index f8d54b8..07f84ed 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -50,17 +50,14 @@ export async function extractTypeFromSource(filePath: string): Promise { // Function to parse object literal function parseObjectLiteral(str: string) { const obj: Record = {} - str.split(',').forEach((pair) => { - const trimmedPair = pair.trim() - if (trimmedPair) { - const colonIndex = trimmedPair.indexOf(':') - if (colonIndex !== -1) { - const key = trimmedPair.slice(0, colonIndex).trim().replace(/['"]/g, '') - const value = trimmedPair.slice(colonIndex + 1).trim() - obj[key] = value - } - } - }) + const regex = /(['"]?)([^\s'":]+)\1\s*:\s*(['"]?)([^\s'"]+)\3/g + let match + + while ((match = regex.exec(str)) !== null) { + const [, , key, , value] = match + obj[key] = value + } + return obj } @@ -79,8 +76,8 @@ export async function extractTypeFromSource(filePath: string): Promise { // Parse the object literal const parsedValue = parseObjectLiteral(constValue.slice(1, -1)) const formattedValue = Object.entries(parsedValue) - .map(([key, value]) => ` ${key}: ${value.includes('/') || value.includes('\'') ? value : `'${value}'`}`) - .join('\n') + .map(([key, value]) => ` ${key}: ${value.includes('/') || value.includes('\'') ? `'${value}'` : value}`) + .join(',\n') if (pendingComment) { declarations += `${pendingComment}\n`