From 73d453e76a69d241eb6d5aae429f851da61aefac Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 22 Oct 2024 23:05:02 +0200 Subject: [PATCH] chore: wip --- fixtures/output/example-0001.d.ts | 6 +----- src/extract.ts | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/fixtures/output/example-0001.d.ts b/fixtures/output/example-0001.d.ts index 1652d29..13192e9 100644 --- a/fixtures/output/example-0001.d.ts +++ b/fixtures/output/example-0001.d.ts @@ -1,5 +1,3 @@ -import type { BunPlugin } from 'bun' -import type { DtsGenerationConfig, DtsGenerationOption } from '@stacksjs/dtsx' import type { BunPlugin } from 'bun'; /** * Example of const declaration @@ -103,6 +101,4 @@ export * from './extract' export * from './generate' export * from './types' export * from './utils' - -export default -export default dts; \ No newline at end of file +export default dts; diff --git a/src/extract.ts b/src/extract.ts index 72cb66c..8d53f71 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -796,7 +796,7 @@ export function processDeclarationLine(line: string, state: ProcessingState): vo } } -function formatOutput(state: ProcessingState): string { +export function formatOutput(state: ProcessingState): string { const uniqueImports = processImports(state.imports, state.usedTypes) const dynamicImports = Array.from(state.usedTypes) .map((type) => { @@ -816,16 +816,28 @@ function formatOutput(state: ProcessingState): string { line.startsWith('*') ? ` ${line}` : line, ) - const result = [ + let result = [ ...allImports, '', '', // Extra newline after imports ...declarations, ].filter(Boolean).join('\n') - return state.defaultExport - ? `${result}\n\nexport default ${state.defaultExport.trim()};\n` - : `${result}\n` + // Clean up default export - extract just the identifier + if (state.defaultExport) { + const exportIdentifier = state.defaultExport + .replace(/^export\s+default\s+/, '') // Remove leading export default + .replace(/export\s+default\s+/, '') // Remove any additional export default + .replace(/;+$/, '') // Remove trailing semicolons + .trim() + + result += `\nexport default ${exportIdentifier};\n` + } + else { + result += '\n' + } + + return result } /**