Skip to content

Commit 1511b1a

Browse files
committed
chore: wip
chore: wip
1 parent 91a5a8e commit 1511b1a

File tree

5 files changed

+43
-16
lines changed

5 files changed

+43
-16
lines changed

fixtures/input/exports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { BunPlugin } from 'bun';
55

66
export { generate, dtsConfig, type BunPlugin }
77
export type { SomeOtherType }
8+
export type { BunRegisterPlugin } from 'bun'
89

910
export default dts
1011

fixtures/output/exports.d.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { dtsConfig } from './config';
22
import { generate, something as dts } from './generate';
3-
export { generate, dtsConfig, type BunPlugin };
4-
export type { SomeOtherType }
5-
;
6-
export { config } from './config';
7-
export * from './extract';
8-
export * from './generate';
9-
export * from './types';
10-
export * from './utils';
3+
4+
export { generate, dtsConfig, type BunPlugin }
5+
export type { SomeOtherType };
6+
export type { BunRegisterPlugin } from 'bun';
7+
export { config } from './config'
8+
export * from './extract'
9+
export * from './generate'
10+
export * from './types'
11+
export * from './utils'
12+
1113
export default dts;

fixtures/output/function.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { BunPlugin } from 'bun';
22
import type { DtsGenerationOption } from '@stacksjs/dtsx';
3+
34
export declare function fetchUsers(): Promise<ResponseData>;
45
export declare function getProduct(id: number): Promise<ApiResponse<Product>>;
56
export declare function authenticate(user: string, password: string): Promise<AuthResponse>;
@@ -14,3 +15,4 @@ export declare function complexAsyncGenerator(): any;
1415
export declare function isUser(value: unknown): value is User;
1516
export declare function extractFunctionSignature(declaration: string): FunctionSignature;
1617
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]> };
18+

fixtures/output/variable.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { DtsGenerationConfig } from '@stacksjs/dtsx';
2+
23
export declare const conf: { [key: string]: string };
34
export declare let test: 'test';
45
export declare var helloWorld: 'Hello World';
@@ -119,3 +120,4 @@ export declare const CONFIG_MAP: {
119120
}
120121
}
121122
};
123+

src/extract.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -601,28 +601,48 @@ function formatOutput(state: ProcessingState): string {
601601
.filter(line => line.startsWith('import'))
602602
.forEach(imp => imports.add(imp.replace(/;+$/, ''))) // Remove any existing semicolons
603603

604-
// Get all non-import lines
604+
// Get all non-import lines and clean up semicolons
605605
const declarations = state.dtsLines
606606
.filter(line => !line.startsWith('import'))
607-
.map(line => line.replace(/;+$/, '')) // Clean up any multiple semicolons
607+
.map((line) => {
608+
// Clean up any multiple semicolons and ensure all declarations end with one
609+
const trimmed = line.trim()
610+
if (!trimmed)
611+
return ''
612+
613+
// Don't add semicolons to export * statements or when one already exists
614+
if (trimmed.startsWith('export *') || trimmed.endsWith(';')) {
615+
return trimmed
616+
}
617+
618+
// Add semicolon to type exports that don't have one
619+
if (trimmed.startsWith('export type')) {
620+
return `${trimmed};`
621+
}
622+
623+
return trimmed.replace(/;+$/, ';')
624+
})
608625

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

613-
// Reconstruct the output with single semicolons where needed
630+
// Reconstruct the output with proper line breaks and semicolons
614631
const output = [
632+
// Add semicolons to imports
615633
...Array.from(imports).map(imp => `${imp};`),
616634
'',
617-
...declarations.map(decl => decl.trim() !== '' ? `${decl};` : ''),
635+
// Filter empty lines and join declarations
636+
...declarations.filter(Boolean),
618637
'',
619-
...defaultExports.map(exp => `${exp};`),
638+
// Add default export
639+
...defaultExports,
620640
]
621641

622-
// Remove comments and normalize whitespace
642+
// Remove comments, normalize whitespace, and ensure single trailing newline
623643
return `${output
624644
.map(line => line.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ''))
625-
.filter(Boolean)
645+
.filter(line => line.trim() || line === '') // Keep empty lines for spacing
626646
.join('\n')
627647
}\n`
628648
}

0 commit comments

Comments
 (0)