Skip to content

Commit

Permalink
chore: improve trackTypeUsage
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Nov 8, 2024
1 parent fdbb721 commit 0691934
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
48 changes: 48 additions & 0 deletions fixtures/input/example/0003.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { PluginBuilder } from 'bun'
import type { UnimportOptions } from 'unimport'

function getLoader(path: string): string {
return path.endsWith('ts')
? 'ts'
: path.endsWith('js')
? 'js'
: path.endsWith('tsx')
? 'tsx'
: 'jsx'
}

interface AutoImportsPlugin {
name: string
setup: (builder: PluginBuilder) => Promise<void>
}

export function autoImports(options: Partial<UnimportOptions & { dts: string }>): AutoImportsPlugin {
return {
name: 'bun-plugin-auto-imports',

async setup(builder: PluginBuilder): Promise<void> {
const { createUnimport } = await import('unimport')
const { injectImports, generateTypeDeclarations } = createUnimport({
...options,
dts: undefined,
} as UnimportOptions)

const dtsContent = await generateTypeDeclarations()
Bun.write(options.dts ?? './auto-import.d.ts', dtsContent)

builder.onLoad({ filter: /.*/ }, async (args) => {
const fileContent = await Bun.file(args.path).text()
const transformedFileContent = await injectImports(fileContent)

return {
contents: transformedFileContent.code,
loader: getLoader(args.path),
}
})
},
}
}

export type AutoImportsOptions = UnimportOptions

export default autoImports
12 changes: 12 additions & 0 deletions fixtures/output/0003.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { PluginBuilder } from 'bun';
import type { UnimportOptions } from 'unimport';

declare function getLoader(path: string): string;
declare interface AutoImportsPlugin {
name: string
setup: (builder: PluginBuilder) => Promise<void>
}
export declare function autoImports(options: Partial<UnimportOptions & { dts: string }>): AutoImportsPlugin;
export declare type AutoImportsOptions = UnimportOptions

export default autoImports;
22 changes: 20 additions & 2 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2458,14 +2458,32 @@ function processPropertyValue(value: string, indentLevel: number, state?: Proces
* Track type usage in declarations
*/
function trackTypeUsage(content: string, state: ImportTrackingState): void {
// Only look for capitalized type references that are actually used in declarations
// Existing pattern for types in declarations
const typePattern = /(?:extends|implements|:|<)\s*([A-Z][a-zA-Z0-9]*(?:<[^>]+>)?)/g
let match

// Pattern for parameterized types like Partial<T>
const parameterizedTypePattern = /(?:^|[\s<,])\s*([A-Z][a-zA-Z0-9]*)(?:[<>,\s]|$)/g

// Track both patterns
let match
while ((match = typePattern.exec(content)) !== null) {
const typeName = match[1].split('<')[0] // Handle generic types
state.usedTypes.add(typeName)
}

while ((match = parameterizedTypePattern.exec(content)) !== null) {
const typeName = match[1]
state.usedTypes.add(typeName)
}

// special handling for types used in Partial<T> and similar constructs
const partialPattern = /Partial<([^>]+)>/g
while ((match = partialPattern.exec(content)) !== null) {
const innerType = match[1].trim()
if (/^[A-Z]/.test(innerType)) { // Only track if it starts with capital letter
state.usedTypes.add(innerType)
}
}
}

/**
Expand Down

0 comments on commit 0691934

Please sign in to comment.