Skip to content

Commit 65638e7

Browse files
committed
chore: wip
1 parent 414908d commit 65638e7

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

fixtures/output/example-0001.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/**
22
* Example of const declaration
33
*/
4-
export declare const config: { [key: string]: string }
5-
4+
export declare const config: {
5+
apiUrl: 'https://api.example.com',
6+
timeout: '5000'
7+
}
68
/**
79
* Example of interface declaration
810
*/
@@ -11,15 +13,13 @@ export interface User {
1113
name: string
1214
email: string
1315
}
14-
1516
/**
1617
* Example of type declaration
1718
*/
1819
export interface ResponseData {
1920
success: boolean
2021
data: User[]
2122
}
22-
2323
/**
2424
* Example of function declaration
2525
*/

fixtures/output/example-0005.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
export declare const defaultHeaders: {
22
'Content-Type': 'application/json',
33
}
4-
54
export interface Comment {
65
id: number
76
postId: number
87
body: string
98
}
10-
119
export interface CommentsResponse {
1210
comments: Comment[]
1311
}
14-
1512
export declare function fetchComments(postId: number): Promise<CommentsResponse>

src/extract.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,14 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
5050
// Function to parse object literal
5151
function parseObjectLiteral(str: string) {
5252
const obj: Record<string, string> = {}
53-
str.split(',').forEach((pair) => {
54-
const trimmedPair = pair.trim()
55-
if (trimmedPair) {
56-
const colonIndex = trimmedPair.indexOf(':')
57-
if (colonIndex !== -1) {
58-
const key = trimmedPair.slice(0, colonIndex).trim().replace(/['"]/g, '')
59-
const value = trimmedPair.slice(colonIndex + 1).trim()
60-
obj[key] = value
61-
}
62-
}
63-
})
53+
const regex = /(['"]?)([^\s'":]+)\1\s*:\s*(['"]?)([^\s'"]+)\3/g
54+
let match
55+
56+
while ((match = regex.exec(str)) !== null) {
57+
const [, , key, , value] = match
58+
obj[key] = value
59+
}
60+
6461
return obj
6562
}
6663

@@ -79,8 +76,8 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
7976
// Parse the object literal
8077
const parsedValue = parseObjectLiteral(constValue.slice(1, -1))
8178
const formattedValue = Object.entries(parsedValue)
82-
.map(([key, value]) => ` ${key}: ${value.includes('/') || value.includes('\'') ? value : `'${value}'`}`)
83-
.join('\n')
79+
.map(([key, value]) => ` ${key}: ${value.includes('/') || value.includes('\'') ? `'${value}'` : value}`)
80+
.join(',\n')
8481

8582
if (pendingComment) {
8683
declarations += `${pendingComment}\n`

0 commit comments

Comments
 (0)