Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 26, 2024
1 parent a0347c5 commit 9cd4e4e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
19 changes: 18 additions & 1 deletion fixtures/output/example-0001.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { BunPlugin } from 'bun'
import type { DtsGenerationConfig, DtsGenerationOption } from '@stacksjs/dtsx'
import { generate } from '@stacksjs/dtsx'

export declare const conf: { [key: string]: string };

export declare const someObject: {
someString: 'Stacks';
someNumber: 1000;
Expand All @@ -29,45 +29,61 @@ export declare const someObject: {
};
};
};

export declare interface User {
id: number
name: string
email: string
}

export declare interface ResponseData {
success: boolean
data: User[]
}

export declare function fetchUsers(): Promise<ResponseData>;

export declare interface ApiResponse<T> {
status: number
message: string
data: T
}

declare const settings: { [key: string]: any };

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

export declare function getProduct(id: number): Promise<ApiResponse<Product>>;

export declare interface AuthResponse {
token: string
expiresIn: number
}

export declare type AuthStatus = 'authenticated' | 'unauthenticated';

export declare function authenticate(user: string, password: string): Promise<AuthResponse>;

export declare const defaultHeaders: {
'Content-Type': 'application/json';
};

export declare function dts(options?: DtsGenerationOption): BunPlugin;

declare interface Options<T> {
name: string
cwd?: string
defaultConfig: T
}

export declare function loadConfig<T extends Record<string, unknown>>({ name, cwd, defaultConfig }: Options<T>): Promise<T>;

declare const dtsConfig: DtsGenerationConfig;

export { generate, dtsConfig }
export type { DtsGenerationOption }
export declare interface ComplexGeneric<T extends Record<string, unknown>, K extends keyof T> {
Expand All @@ -77,6 +93,7 @@ export declare interface ComplexGeneric<T extends Record<string, unknown>, K ext
transform: (input: T[K]) => string
nested: Array<Partial<T>>
}

export declare type ComplexUnionIntersection =
| (User & { role: 'admin' })
| (Product & { category: string })
Expand Down
23 changes: 20 additions & 3 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2133,14 +2133,31 @@ function formatOutput(state: ProcessingState): string {
// Process imports
const imports = state.imports.join('\n').trim()
if (imports) {
sections.push(imports)
sections.push(`${imports}\n`) // Add extra newline after imports
}

// Process declarations (excluding exports)
const declarations = state.dtsLines
// Process declarations with proper spacing
const declarationLines = state.dtsLines
.filter(line => !line.startsWith('export *') && !line.startsWith('export { config }'))

// Group declarations and add spacing
const declarations = declarationLines
.reduce((acc: string[], line: string) => {
if (line.trim()) {
acc.push(line)
// Add newline after declaration statements
if (
line.match(/^export\s+declare\s+(const|interface|function|type|class|enum|namespace|module|global|abstract\s+class)/)
|| line.match(/^declare\s+(const|interface|function|type|class|enum|namespace|module|global|abstract\s+class)/)
) {
acc.push('')
}
}
return acc
}, [])
.join('\n')
.trim()

if (declarations) {
sections.push(declarations)
}
Expand Down

0 comments on commit 9cd4e4e

Please # to comment.