From 1511b1a2361aed121858dfe24543e07a3a796671 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 4 Nov 2024 22:10:58 +0100 Subject: [PATCH] chore: wip chore: wip --- fixtures/input/exports.ts | 1 + fixtures/output/exports.d.ts | 18 ++++++++++-------- fixtures/output/function.d.ts | 2 ++ fixtures/output/variable.d.ts | 2 ++ src/extract.ts | 36 +++++++++++++++++++++++++++-------- 5 files changed, 43 insertions(+), 16 deletions(-) diff --git a/fixtures/input/exports.ts b/fixtures/input/exports.ts index 58af81d..a83ca2e 100644 --- a/fixtures/input/exports.ts +++ b/fixtures/input/exports.ts @@ -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 diff --git a/fixtures/output/exports.d.ts b/fixtures/output/exports.d.ts index 28af9f4..40d7c3e 100644 --- a/fixtures/output/exports.d.ts +++ b/fixtures/output/exports.d.ts @@ -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; diff --git a/fixtures/output/function.d.ts b/fixtures/output/function.d.ts index 12927d4..fdca73c 100644 --- a/fixtures/output/function.d.ts +++ b/fixtures/output/function.d.ts @@ -1,5 +1,6 @@ import type { BunPlugin } from 'bun'; import type { DtsGenerationOption } from '@stacksjs/dtsx'; + export declare function fetchUsers(): Promise; export declare function getProduct(id: number): Promise>; export declare function authenticate(user: string, password: string): Promise; @@ -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 any>>(endpoints: T): { [K in keyof T]: ReturnType extends Promise ? R : ReturnType }; + diff --git a/fixtures/output/variable.d.ts b/fixtures/output/variable.d.ts index 1e47449..46225c7 100644 --- a/fixtures/output/variable.d.ts +++ b/fixtures/output/variable.d.ts @@ -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'; @@ -119,3 +120,4 @@ export declare const CONFIG_MAP: { } } }; + diff --git a/src/extract.ts b/src/extract.ts index 67baa84..58b70d9 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -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` }