Skip to content

Commit b3f3882

Browse files
committed
chore: wip
1 parent 98f08ef commit b3f3882

File tree

7 files changed

+45
-24
lines changed

7 files changed

+45
-24
lines changed

fixtures/output/example-1.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export declare const config: { [key: string]: string }
66
/**
77
* Example of interface declaration
88
*/
9-
export interface User {
9+
export interface User {
1010
id: number
1111
name: string
1212
email: string
@@ -15,7 +15,7 @@ export interface User {
1515
/**
1616
* Example of type declaration
1717
*/
18-
export interface ResponseData {
18+
export interface ResponseData {
1919
success: boolean
2020
data: User[]
2121
}

fixtures/output/example-2.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
*/
44
export declare const settings: { [key: string]: any }
55

6-
export interface Product {
6+
export interface Product {
77
id: number
88
name: string
99
price: number
1010
}
1111

12-
export interface ApiResponse<T> {
12+
export interface ApiResponse<T> {
1313
status: number
1414
message: string
1515
data: T

fixtures/output/example-3.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
export declare const endpoints
22

3-
export interface Order {
3+
export interface Order {
44
orderId: number
55
userId: number
66
productIds: number[]
77
}
88

9-
export interface OrderResponse {
9+
export interface OrderResponse {
1010
success: boolean
1111
order: Order
1212
}

fixtures/output/example-4.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export declare const apiKeys
22

3-
export interface AuthResponse {
3+
export interface AuthResponse {
44
token: string
55
expiresIn: number
66
}

fixtures/output/example-5.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
export declare const defaultHeaders
22

3-
export interface Comment {
3+
export interface Comment {
44
id: number
55
postId: number
66
body: string
77
}
88

9-
export interface CommentsResponse {
9+
export interface CommentsResponse {
1010
comments: Comment[]
1111
}
1212

src/extract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
8787
}
8888

8989
// Apply final formatting
90-
return formatDeclarations(declarations, false)
90+
return formatDeclarations(declarations)
9191
}

src/utils.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,42 @@ export async function checkIsolatedDeclarations(options: DtsGenerationConfig): P
3232
}
3333
}
3434

35-
export function formatDeclarations(declarations: string, isConfigFile: boolean): string {
36-
if (isConfigFile) {
37-
return declarations.trim() + '\n'
38-
}
35+
export function formatDeclarations(declarations: string): string {
36+
const lines = declarations.split('\n')
37+
const formattedLines = lines.map(line => {
38+
// Trim trailing spaces
39+
line = line.trimEnd()
3940

40-
return declarations
41-
.replace(/\n{3,}/g, '\n\n')
42-
.replace(/;\n/g, '\n')
43-
.replace(/export (interface|type) ([^\{]+)\s*\{\s*\n/g, 'export $1 $2 {\n')
44-
.replace(/\n\s*\}/g, '\n}')
45-
.replace(/\/\*\*\n([^*]*)(\n \*\/)/g, (match, content) => {
46-
const formattedContent = content.split('\n').map((line: string) => ` *${line.trim() ? ' ' + line.trim() : ''}`).join('\n')
47-
return `/**\n${formattedContent}\n */`
48-
})
49-
.trim() + '\n'
41+
// Handle interface and type declarations
42+
if (line.startsWith('export interface') || line.startsWith('export type')) {
43+
const parts = line.split('{')
44+
if (parts.length > 1) {
45+
return `${parts[0].trim()} {${parts[1]}`
46+
}
47+
}
48+
49+
// Remove semicolons from the end of lines
50+
if (line.endsWith(';')) {
51+
line = line.slice(0, -1)
52+
}
53+
54+
return line
55+
})
56+
57+
// Join lines and ensure only one blank line between declarations
58+
let result = formattedLines.join('\n')
59+
result = result.replace(/\n{3,}/g, '\n\n')
60+
61+
// Format comments
62+
result = result.replace(/\/\*\*\n([^*]*)(\n \*\/)/g, (match, content) => {
63+
const formattedContent = content
64+
.split('\n')
65+
.map(line => ` *${line.trim() ? ' ' + line.trim() : ''}`)
66+
.join('\n')
67+
return `/**\n${formattedContent}\n */`
68+
})
69+
70+
return result.trim() + '\n'
5071
}
5172

5273
export function formatComment(comment: string): string {

0 commit comments

Comments
 (0)