From 9cd4e4eebb5b7ba4b04b04036e80b7e0f520cf55 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 26 Oct 2024 21:09:16 +0200 Subject: [PATCH] chore: wip --- fixtures/output/example-0001.d.ts | 19 ++++++++++++++++++- src/extract.ts | 23 ++++++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/fixtures/output/example-0001.d.ts b/fixtures/output/example-0001.d.ts index 6e24a15..dd22d8c 100644 --- a/fixtures/output/example-0001.d.ts +++ b/fixtures/output/example-0001.d.ts @@ -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; @@ -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; + export declare interface ApiResponse { 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>; + export declare interface AuthResponse { token: string expiresIn: number } + export declare type AuthStatus = 'authenticated' | 'unauthenticated'; + export declare function authenticate(user: string, password: string): Promise; + export declare const defaultHeaders: { 'Content-Type': 'application/json'; }; + export declare function dts(options?: DtsGenerationOption): BunPlugin; + declare interface Options { name: string cwd?: string defaultConfig: T } + export declare function loadConfig>({ name, cwd, defaultConfig }: Options): Promise; + declare const dtsConfig: DtsGenerationConfig; + export { generate, dtsConfig } export type { DtsGenerationOption } export declare interface ComplexGeneric, K extends keyof T> { @@ -77,6 +93,7 @@ export declare interface ComplexGeneric, K ext transform: (input: T[K]) => string nested: Array> } + export declare type ComplexUnionIntersection = | (User & { role: 'admin' }) | (Product & { category: string }) diff --git a/src/extract.ts b/src/extract.ts index a77f07f..1be0d77 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -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) }