Skip to content

Commit 14dada6

Browse files
committed
chore: wip
1 parent d2ab838 commit 14dada6

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

src/extract.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
/* eslint-disable no-console */
22
const DEBUG = true // Set to false to disable debug logs
33

4-
export async function extract(filePath: string, debug: boolean): Promise<string> {
4+
export async function extract(filePath: string): Promise<string> {
55
try {
6-
debug = debug || DEBUG
76
const sourceCode = await Bun.file(filePath).text()
8-
return generateDtsTypes(sourceCode, debug)
7+
return generateDtsTypes(sourceCode)
98
}
109
catch (error) {
1110
console.error(error)
1211
throw new Error(`Failed to extract and generate .d.ts file`)
1312
}
1413
}
1514

16-
function generateDtsTypes(sourceCode: string, debug: boolean): string {
17-
debug = debug || DEBUG
18-
if (debug)
15+
function generateDtsTypes(sourceCode: string): string {
16+
if (DEBUG)
1917
console.log('Starting generateDtsTypes')
2018
const lines = sourceCode.split('\n')
2119
const dtsLines: string[] = []
@@ -45,19 +43,19 @@ function generateDtsTypes(sourceCode: string, debug: boolean): string {
4543
if (line.trim().startsWith('import')) {
4644
const processedImport = processImport(line)
4745
imports.push(processedImport)
48-
if (debug)
46+
if (DEBUG)
4947
console.log(`Processed import: ${processedImport}`)
5048
continue
5149
}
5250

5351
if (line.trim().startsWith('export default')) {
54-
defaultExport = `${line.trim()};`
52+
defaultExport = `\n${line.trim()};`
5553
if (DEBUG)
5654
console.log(`Default export found: ${defaultExport}`)
5755
continue
5856
}
5957

60-
if (line.trim().startsWith('export const') || isMultiLineDeclaration) {
58+
if (line.trim().startsWith('export') || isMultiLineDeclaration) {
6159
currentDeclaration += `${line}\n`
6260
bracketCount += (line.match(/\{/g) || []).length - (line.match(/\}/g) || []).length
6361
isMultiLineDeclaration = bracketCount > 0
@@ -69,30 +67,16 @@ function generateDtsTypes(sourceCode: string, debug: boolean): string {
6967
console.log(`Comment block added to dtsLines: ${lastCommentBlock.trimEnd()}`)
7068
lastCommentBlock = ''
7169
}
72-
const processed = processConstDeclaration(currentDeclaration.trim())
70+
const processed = processDeclaration(currentDeclaration.trim())
7371
if (processed) {
7472
dtsLines.push(processed)
7573
if (DEBUG)
76-
console.log(`Processed const declaration added to dtsLines: ${processed}`)
74+
console.log(`Processed declaration added to dtsLines: ${processed}`)
7775
}
7876
currentDeclaration = ''
7977
bracketCount = 0
8078
}
8179
}
82-
else if (line.trim().startsWith('export')) {
83-
if (lastCommentBlock) {
84-
dtsLines.push(lastCommentBlock.trimEnd())
85-
if (DEBUG)
86-
console.log(`Comment block added to dtsLines: ${lastCommentBlock.trimEnd()}`)
87-
lastCommentBlock = ''
88-
}
89-
const processed = processDeclaration(line)
90-
if (processed) {
91-
dtsLines.push(processed)
92-
if (DEBUG)
93-
console.log(`Processed declaration added to dtsLines: ${processed}`)
94-
}
95-
}
9680
}
9781

9882
const result = cleanOutput([...imports, '', ...dtsLines, '', ...exports, defaultExport].filter(Boolean).join('\n'))
@@ -136,7 +120,6 @@ function processDeclaration(declaration: string): string {
136120
function processConstDeclaration(declaration: string): string {
137121
if (DEBUG)
138122
console.log(`Processing const declaration: ${declaration}`)
139-
140123
const lines = declaration.split('\n')
141124
const firstLine = lines[0]
142125
const name = firstLine.split('export const')[1].split('=')[0].trim().split(':')[0].trim()
@@ -158,7 +141,7 @@ function processConstDeclaration(declaration: string): string {
158141
inString = true
159142
stringChar = char
160143
}
161-
else if (char === '/' && line[i + 1] === '/') {
144+
else if (char === '/' && line[i + 1] === '//') {
162145
commentIndex = i
163146
break
164147
}
@@ -200,7 +183,10 @@ function processInterfaceDeclaration(declaration: string): string {
200183
function processTypeDeclaration(declaration: string): string {
201184
if (DEBUG)
202185
console.log(`Processing type declaration: ${declaration}`)
203-
const result = declaration.replace('export type', 'export declare type')
186+
const lines = declaration.split('\n')
187+
const typeName = lines[0].split('type')[1].split('=')[0].trim()
188+
const typeBody = lines.slice(1).map(line => ` ${line.trim()}`).join('\n')
189+
const result = `export declare type ${typeName} = ${typeBody}`
204190
if (DEBUG)
205191
console.log(`Processed type declaration: ${result}`)
206192
return result

0 commit comments

Comments
 (0)