Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 26, 2024
1 parent 1ccd221 commit a7e6e7f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions fixtures/output/example-0001.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,5 @@ export declare type ComplexUnionIntersection =
& {
metadata: Record<string, unknown>
}

export default dts;
26 changes: 21 additions & 5 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export function processLine(line: string, state: ProcessingState): void {
}

if (trimmedLine.startsWith('export default')) {
state.defaultExport = `\n${trimmedLine};`
state.defaultExport = trimmedLine.endsWith(';') ? trimmedLine : `${trimmedLine};`
return
}

Expand Down Expand Up @@ -590,6 +590,16 @@ export function processDeclarationBlock(
return
}

if (
declarationWithoutComments.startsWith('export default')
) {
// Handle export default statements
state.defaultExport = declarationWithoutComments.endsWith(';')
? declarationWithoutComments
: `${declarationWithoutComments};`
return
}

if (
declarationWithoutComments.startsWith('export const')
|| declarationWithoutComments.startsWith('const')
Expand Down Expand Up @@ -1964,18 +1974,24 @@ function getDeclarationType(line: string): 'interface' | 'type' | 'const' | 'fun
* Format the final output with proper spacing and organization
*/
function formatOutput(state: ProcessingState): string {
const output = state.dtsLines
const outputLines = 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 file ends with single newline
return `${output.trim()}\n`
// Ensure file ends with a single newline
let output = outputLines.join('\n').trim()

// Append default export at the end if it exists
if (state.defaultExport) {
output += `\n\n${state.defaultExport.trim()}`
}

return `${output}\n`
}

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

0 comments on commit a7e6e7f

Please sign in to comment.