Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 24, 2024
1 parent 3a19f8b commit 7833930
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
40 changes: 34 additions & 6 deletions fixtures/output/example-0001.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { BunPlugin } from 'bun'
import process from 'node:process'
import { generate, deepMerge } from '@stacksjs/dtsx'
import type { DtsGenerationConfig, DtsGenerationOption } from '@stacksjs/dtsx'
import { existsSync } from 'node:fs'
import { generate, deepMerge } from '@stacksjs/dtsx'
import { resolve } from 'node:path'
import process from 'node:process'

/**
* Example of const declaration
*/
export declare const conf: { [key: string]: string };

export declare const someObject: {
someString: 'Stacks';
someNumber: 1000;
Expand Down Expand Up @@ -38,6 +39,7 @@ export declare const someObject: {
someInlineCall2: (...args: any[]) => void;
someInlineCall3: (...args: any[]) => void;
};

/**
* Example of interface declaration
* with another comment in an extra line
Expand All @@ -47,6 +49,7 @@ export declare interface User {
name: string
email: string
}

/**
* Example of type declaration
*
Expand All @@ -56,69 +59,94 @@ export declare interface ResponseData {
success: boolean
data: User[]
}

/**
* Example of function declaration
*
*
* with multiple empty lines, including an empty lines
*/
export declare function fetchUsers(): Promise<ResponseData>;

export declare interface ApiResponse<T> {
status: number
message: string
data: T
}

/**
* Example of another const declaration
*
* with multiple empty lines, including being poorly formatted
*/
declare const settings: { [key: string]: any };

export declare interface Product {
id: number
name: string
price: number
}

/**
* Example of function declaration
*/
export declare function getProduct(id: number): Promise<ApiResponse<Product>>;

export declare interface AuthResponse {
token: string
expiresIn: number
}

export declare type AuthStatus = 'authenticated' | 'unauthenticated';

export declare function authenticate(user: string, password: string): Promise<AuthResponse>;

export declare const defaultHeaders: {
'Content-Type': 'application/json';
};

export declare function dts(options?: DtsGenerationOption): BunPlugin;

declare interface Options<T> {
name: string
cwd?: string
defaultConfig: T
}

export declare async function loadConfig<T extends Record<string, unknown>>(options: Options<T>): Promise<T>;


declare const dtsConfig: DtsGenerationConfig;

export { generate, dtsConfig }

export type { DtsGenerationOption }

export { config } from './config'
export * from './extract'
export * from './generate'
export * from './types'
export * from './utils'


export declare interface ComplexGeneric<T extends Record<string, unknown>, K extends keyof T> {
data: T
key: K
value: T[K]
transform: (input: T[K]) => string
nested: Array<Partial<T>>
}


export declare type ComplexUnionIntersection =

| (User & { role: 'admin' })

| (Product & { category: string })

& {
metadata: Record<string, unknown>
}

export * from './extract'
export * from './generate'
export * from './types'
export * from './utils'

export default dts
51 changes: 35 additions & 16 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,6 @@ function processConstDeclaration(declaration: string, isExported = true): string
console.log('Processing const declaration:', { declaration })
const lines = declaration.split('\n')
const firstLine = lines[0]
const indentMatch = firstLine.match(/^(\s*)/)
const indent = indentMatch ? indentMatch[1] : ''

// Check for type annotation
const typeMatch = firstLine.match(/const\s+([^:]+):\s*([^=]+)\s*=/)
Expand Down Expand Up @@ -1234,11 +1232,12 @@ function processSourceFile(content: string, state: ProcessingState): void {
const lines = content.split('\n')
const importLines: string[] = []
const exportDefaultLines: string[] = []
const exportStarLines: string[] = []
let currentBlock: string[] = []
let currentComments: string[] = []
let isInMultilineDeclaration = false

// First pass: collect imports and export defaults
// First pass: collect imports and exports
for (const line of lines) {
const trimmedLine = line.trim()
if (trimmedLine.startsWith('import')) {
Expand All @@ -1249,20 +1248,26 @@ function processSourceFile(content: string, state: ProcessingState): void {
exportDefaultLines.push(line)
continue
}
if (trimmedLine.startsWith('export *')) {
exportStarLines.push(line)
continue
}
}

// Process imports
if (importLines.length > 0) {
state.dtsLines.push(...importLines)
state.dtsLines.push(...cleanImports(importLines))
state.dtsLines.push('') // Add single line break after imports
}

// Process rest of the content
// Process main content
for (const line of lines) {
const trimmedLine = line.trim()

// Skip imports and export defaults as they're handled separately
if (trimmedLine.startsWith('import') || trimmedLine.startsWith('export default')) {
// Skip already processed lines
if (trimmedLine.startsWith('import')
|| trimmedLine.startsWith('export default')
|| trimmedLine.startsWith('export *')) {
continue
}

Expand All @@ -1272,6 +1277,7 @@ function processSourceFile(content: string, state: ProcessingState): void {
processDeclarationBlock(currentBlock, currentComments, state)
currentBlock = []
currentComments = []
state.dtsLines.push('') // Add line break after each declaration
}
continue
}
Expand Down Expand Up @@ -1307,24 +1313,35 @@ function processSourceFile(content: string, state: ProcessingState): void {
processDeclarationBlock(currentBlock, currentComments, state)
currentBlock = []
currentComments = []
state.dtsLines.push('') // Add line break after each declaration
}
}
else if (!trimmedLine.endsWith(',')) {
processDeclarationBlock(currentBlock, currentComments, state)
currentBlock = []
currentComments = []
state.dtsLines.push('') // Add line break after each declaration
}
}

// Process any remaining block
if (currentBlock.length > 0) {
processDeclarationBlock(currentBlock, currentComments, state)
state.dtsLines.push('')
}

// Add export * statements with proper spacing
if (exportStarLines.length > 0) {
if (state.dtsLines[state.dtsLines.length - 1] !== '') {
state.dtsLines.push('')
}
state.dtsLines.push(...exportStarLines)
}

// Add export default at the end with proper spacing
if (exportDefaultLines.length > 0) {
if (state.dtsLines[state.dtsLines.length - 1] !== '') {
state.dtsLines.push('') // Add line break before export default
state.dtsLines.push('')
}
state.dtsLines.push(...exportDefaultLines)
}
Expand Down Expand Up @@ -1684,16 +1701,18 @@ function getDeclarationType(line: string): 'interface' | 'type' | 'const' | 'fun
* Format the final output with proper spacing and organization
*/
function formatOutput(state: ProcessingState): string {
let output = state.dtsLines.join('\n')
const output = state.dtsLines
// Remove more than two consecutive empty lines
.reduce((acc, line, index, arr) => {
if (line === '' && arr[index - 1] === '' && arr[index - 2] === '') {
return acc
}
return [...acc, line]
}, [] as string[])
.join('\n')

// Ensure proper formatting
output = `${output
// Remove multiple consecutive empty lines
.replace(/\n{3,}/g, '\n\n')
// Ensure file ends with single newline
.trim()}\n`

return output
return `${output.trim()}\n`
}

function getIndentation(line: string): string {
Expand Down

0 comments on commit 7833930

Please sign in to comment.