Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
chore: wip
  • Loading branch information
chrisbbreuer committed Nov 4, 2024
1 parent 91a5a8e commit 1511b1a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
1 change: 1 addition & 0 deletions fixtures/input/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { BunPlugin } from 'bun';

export { generate, dtsConfig, type BunPlugin }
export type { SomeOtherType }
export type { BunRegisterPlugin } from 'bun'

export default dts

Expand Down
18 changes: 10 additions & 8 deletions fixtures/output/exports.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { dtsConfig } from './config';
import { generate, something as dts } from './generate';
export { generate, dtsConfig, type BunPlugin };
export type { SomeOtherType }
;
export { config } from './config';
export * from './extract';
export * from './generate';
export * from './types';
export * from './utils';

export { generate, dtsConfig, type BunPlugin }
export type { SomeOtherType };
export type { BunRegisterPlugin } from 'bun';
export { config } from './config'
export * from './extract'
export * from './generate'
export * from './types'
export * from './utils'

export default dts;
2 changes: 2 additions & 0 deletions fixtures/output/function.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { BunPlugin } from 'bun';
import type { DtsGenerationOption } from '@stacksjs/dtsx';

export declare function fetchUsers(): Promise<ResponseData>;
export declare function getProduct(id: number): Promise<ApiResponse<Product>>;
export declare function authenticate(user: string, password: string): Promise<AuthResponse>;
Expand All @@ -14,3 +15,4 @@ export declare function complexAsyncGenerator(): any;
export declare function isUser(value: unknown): value is User;
export declare function extractFunctionSignature(declaration: string): FunctionSignature;
export declare function createApi<T extends Record<string, (...args: any[]) => any>>(endpoints: T): { [K in keyof T]: ReturnType<T[K]> extends Promise<infer R> ? R : ReturnType<T[K]> };

2 changes: 2 additions & 0 deletions fixtures/output/variable.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DtsGenerationConfig } from '@stacksjs/dtsx';

export declare const conf: { [key: string]: string };
export declare let test: 'test';
export declare var helloWorld: 'Hello World';
Expand Down Expand Up @@ -119,3 +120,4 @@ export declare const CONFIG_MAP: {
}
}
};

36 changes: 28 additions & 8 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,28 +601,48 @@ function formatOutput(state: ProcessingState): string {
.filter(line => line.startsWith('import'))
.forEach(imp => imports.add(imp.replace(/;+$/, ''))) // Remove any existing semicolons

// Get all non-import lines
// Get all non-import lines and clean up semicolons
const declarations = state.dtsLines
.filter(line => !line.startsWith('import'))
.map(line => line.replace(/;+$/, '')) // Clean up any multiple semicolons
.map((line) => {
// Clean up any multiple semicolons and ensure all declarations end with one
const trimmed = line.trim()
if (!trimmed)
return ''

// Don't add semicolons to export * statements or when one already exists
if (trimmed.startsWith('export *') || trimmed.endsWith(';')) {
return trimmed
}

// Add semicolon to type exports that don't have one
if (trimmed.startsWith('export type')) {
return `${trimmed};`
}

return trimmed.replace(/;+$/, ';')
})

// Add default exports from state.defaultExports
const defaultExports = Array.from(state.defaultExports)
.map(exp => exp.replace(/;+$/, '')) // Clean up any multiple semicolons
.map(exp => exp.trim().replace(/;+$/, ';')) // Ensure single semicolon

// Reconstruct the output with single semicolons where needed
// Reconstruct the output with proper line breaks and semicolons
const output = [
// Add semicolons to imports
...Array.from(imports).map(imp => `${imp};`),
'',
...declarations.map(decl => decl.trim() !== '' ? `${decl};` : ''),
// Filter empty lines and join declarations
...declarations.filter(Boolean),
'',
...defaultExports.map(exp => `${exp};`),
// Add default export
...defaultExports,
]

// Remove comments and normalize whitespace
// Remove comments, normalize whitespace, and ensure single trailing newline
return `${output
.map(line => line.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ''))
.filter(Boolean)
.filter(line => line.trim() || line === '') // Keep empty lines for spacing
.join('\n')
}\n`
}
Expand Down

0 comments on commit 1511b1a

Please sign in to comment.