Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 17, 2024
1 parent 98f08ef commit b3f3882
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 24 deletions.
4 changes: 2 additions & 2 deletions fixtures/output/example-1.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export declare const config: { [key: string]: string }
/**
* Example of interface declaration
*/
export interface User {
export interface User {
id: number
name: string
email: string
Expand All @@ -15,7 +15,7 @@ export interface User {
/**
* Example of type declaration
*/
export interface ResponseData {
export interface ResponseData {
success: boolean
data: User[]
}
Expand Down
4 changes: 2 additions & 2 deletions fixtures/output/example-2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
*/
export declare const settings: { [key: string]: any }

export interface Product {
export interface Product {
id: number
name: string
price: number
}

export interface ApiResponse<T> {
export interface ApiResponse<T> {
status: number
message: string
data: T
Expand Down
4 changes: 2 additions & 2 deletions fixtures/output/example-3.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export declare const endpoints

export interface Order {
export interface Order {
orderId: number
userId: number
productIds: number[]
}

export interface OrderResponse {
export interface OrderResponse {
success: boolean
order: Order
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/output/example-4.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export declare const apiKeys

export interface AuthResponse {
export interface AuthResponse {
token: string
expiresIn: number
}
Expand Down
4 changes: 2 additions & 2 deletions fixtures/output/example-5.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export declare const defaultHeaders

export interface Comment {
export interface Comment {
id: number
postId: number
body: string
}

export interface CommentsResponse {
export interface CommentsResponse {
comments: Comment[]
}

Expand Down
2 changes: 1 addition & 1 deletion src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ export async function extractTypeFromSource(filePath: string): Promise<string> {
}

// Apply final formatting
return formatDeclarations(declarations, false)
return formatDeclarations(declarations)
}
49 changes: 35 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,42 @@ export async function checkIsolatedDeclarations(options: DtsGenerationConfig): P
}
}

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

return declarations
.replace(/\n{3,}/g, '\n\n')
.replace(/;\n/g, '\n')
.replace(/export (interface|type) ([^\{]+)\s*\{\s*\n/g, 'export $1 $2 {\n')
.replace(/\n\s*\}/g, '\n}')
.replace(/\/\*\*\n([^*]*)(\n \*\/)/g, (match, content) => {
const formattedContent = content.split('\n').map((line: string) => ` *${line.trim() ? ' ' + line.trim() : ''}`).join('\n')
return `/**\n${formattedContent}\n */`
})
.trim() + '\n'
// Handle interface and type declarations
if (line.startsWith('export interface') || line.startsWith('export type')) {
const parts = line.split('{')
if (parts.length > 1) {
return `${parts[0].trim()} {${parts[1]}`
}
}

// Remove semicolons from the end of lines
if (line.endsWith(';')) {
line = line.slice(0, -1)
}

return line
})

// Join lines and ensure only one blank line between declarations
let result = formattedLines.join('\n')
result = result.replace(/\n{3,}/g, '\n\n')

// Format comments
result = result.replace(/\/\*\*\n([^*]*)(\n \*\/)/g, (match, content) => {
const formattedContent = content
.split('\n')
.map(line => ` *${line.trim() ? ' ' + line.trim() : ''}`)
.join('\n')
return `/**\n${formattedContent}\n */`
})

return result.trim() + '\n'
}

export function formatComment(comment: string): string {
Expand Down

0 comments on commit b3f3882

Please sign in to comment.