Skip to content

Commit 1a8a76e

Browse files
committed
chore: wip
1 parent f51f28f commit 1a8a76e

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/extract.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export function processImport(importLine: string, typeSources: Map<string, strin
157157
/**
158158
* Filter out unused imports and only keep type imports
159159
*/
160-
function processImports(imports: string[]): string[] {
160+
export function processImports(imports: string[], usedTypes: Set<string>): string[] {
161161
const importMap = new Map<string, Set<string>>()
162162

163163
// Process each import line to extract module and types
@@ -170,16 +170,27 @@ function processImports(imports: string[]): string[] {
170170
const types = match[1].split(',').map(t => t.trim())
171171
const module = match[2]
172172

173-
// Add types to the set for this module
174-
if (!importMap.has(module)) {
175-
importMap.set(module, new Set())
173+
// Only track types that are actually used
174+
const usedImports = types.filter((type) => {
175+
const baseName = type.split(' as ')[0].trim()
176+
return usedTypes.has(baseName)
177+
})
178+
179+
if (usedImports.length > 0) {
180+
if (!importMap.has(module)) {
181+
importMap.set(module, new Set())
182+
}
183+
usedImports.forEach(type => importMap.get(module)!.add(type))
176184
}
177-
types.forEach(type => importMap.get(module)!.add(type))
178185
}
179186
}
180187

181-
// Format imports
188+
// These are the only modules we want to keep imports from
189+
const allowedModules = ['bun', '@stacksjs/dtsx']
190+
191+
// Format imports, filtering to allowed modules only
182192
return Array.from(importMap.entries())
193+
.filter(([module]) => allowedModules.includes(module))
183194
.map(([module, types]) => {
184195
const sortedTypes = Array.from(types).sort()
185196
return `import type { ${sortedTypes.join(', ')} } from '${module}';`
@@ -785,12 +796,15 @@ export function processDeclarationLine(line: string, state: ProcessingState): vo
785796
}
786797
}
787798

788-
export function formatOutput(state: ProcessingState): string {
789-
const uniqueImports = processImports(state.imports)
799+
function formatOutput(state: ProcessingState): string {
800+
const uniqueImports = processImports(state.imports, state.usedTypes)
790801
const dynamicImports = Array.from(state.usedTypes)
791802
.map((type) => {
792803
const source = state.typeSources.get(type)
793-
return source ? `import type { ${type} } from '${source}';` : ''
804+
if (source && ['bun', '@stacksjs/dtsx'].includes(source)) {
805+
return `import type { ${type} } from '${source}';`
806+
}
807+
return ''
794808
})
795809
.filter(Boolean)
796810
.sort()
@@ -805,10 +819,13 @@ export function formatOutput(state: ProcessingState): string {
805819
const result = [
806820
...allImports,
807821
'',
822+
'', // Extra newline after imports
808823
...declarations,
809824
].filter(Boolean).join('\n')
810825

811-
return state.defaultExport ? `${result}\n\nexport default ${state.defaultExport.trim()};` : result
826+
return state.defaultExport
827+
? `${result}\n\nexport default ${state.defaultExport.trim()};\n`
828+
: `${result}\n`
812829
}
813830

814831
/**

0 commit comments

Comments
 (0)