Skip to content

Commit d2ab838

Browse files
committed
chore: wip
1 parent 0552414 commit d2ab838

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

src/extract.ts

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
/* eslint-disable no-console */
12
const DEBUG = true // Set to false to disable debug logs
23

3-
export async function extract(filePath: string): Promise<string> {
4+
export async function extract(filePath: string, debug: boolean): Promise<string> {
45
try {
6+
debug = debug || DEBUG
57
const sourceCode = await Bun.file(filePath).text()
6-
return generateDtsTypes(sourceCode)
8+
return generateDtsTypes(sourceCode, debug)
79
}
810
catch (error) {
911
console.error(error)
1012
throw new Error(`Failed to extract and generate .d.ts file`)
1113
}
1214
}
1315

14-
function generateDtsTypes(sourceCode: string): string {
15-
if (DEBUG)
16+
function generateDtsTypes(sourceCode: string, debug: boolean): string {
17+
debug = debug || DEBUG
18+
if (debug)
1619
console.log('Starting generateDtsTypes')
1720
const lines = sourceCode.split('\n')
1821
const dtsLines: string[] = []
@@ -42,7 +45,7 @@ function generateDtsTypes(sourceCode: string): string {
4245
if (line.trim().startsWith('import')) {
4346
const processedImport = processImport(line)
4447
imports.push(processedImport)
45-
if (DEBUG)
48+
if (debug)
4649
console.log(`Processed import: ${processedImport}`)
4750
continue
4851
}
@@ -133,15 +136,42 @@ function processDeclaration(declaration: string): string {
133136
function processConstDeclaration(declaration: string): string {
134137
if (DEBUG)
135138
console.log(`Processing const declaration: ${declaration}`)
139+
136140
const lines = declaration.split('\n')
137141
const firstLine = lines[0]
138142
const name = firstLine.split('export const')[1].split('=')[0].trim().split(':')[0].trim()
139143

140144
const properties = lines.slice(1, -1).map((line) => {
141-
const commentIndex = line.indexOf('//')
145+
let inString = false
146+
let stringChar = ''
147+
let commentIndex = -1
148+
149+
for (let i = 0; i < line.length; i++) {
150+
const char = line[i]
151+
if (inString) {
152+
if (char === stringChar && line[i - 1] !== '\\') {
153+
inString = false
154+
}
155+
}
156+
else {
157+
if (char === '"' || char === '\'' || char === '`') {
158+
inString = true
159+
stringChar = char
160+
}
161+
else if (char === '/' && line[i + 1] === '/') {
162+
commentIndex = i
163+
break
164+
}
165+
}
166+
}
167+
142168
const hasComment = commentIndex !== -1
143169
const mainPart = hasComment ? line.slice(0, commentIndex) : line
144-
const comment = hasComment ? line.slice(commentIndex) : ''
170+
let comment = hasComment ? line.slice(commentIndex) : ''
171+
172+
if (hasComment && !comment.startsWith(' //')) {
173+
comment = ` //${comment.slice(2)}`
174+
}
145175

146176
const [key, ...valueParts] = mainPart.split(':')
147177
let value = valueParts.join(':').trim()
@@ -189,6 +219,7 @@ function processFunctionDeclaration(declaration: string): string {
189219
function cleanOutput(output: string): string {
190220
if (DEBUG)
191221
console.log('Cleaning output')
222+
192223
const result = output
193224
.replace(/\{\s*\}/g, '{}')
194225
.replace(/\s*;\s*(?=\}|$)/g, ';')
@@ -201,8 +232,15 @@ function cleanOutput(output: string): string {
201232
.replace(/;\n(\s*)\}/g, ';\n$1\n$1}')
202233
.replace(/,\n\s*;/g, ';') // Remove unnecessary commas before semicolons
203234
.replace(/;\s*\/\/\s*/g, '; // ') // Ensure comments are properly formatted
235+
.replace(/,\s*;/g, ';') // Remove trailing commas before semicolons
236+
.replace(/;[\t\v\f\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*\n\s*\}/g, ';\n}') // Ensure closing braces are on their own lines
237+
.replace(/;\s*\/\/\s*/g, '; // ') // Ensure comments are properly formatted
238+
.replace(/;\s*\}/g, ';\n}') // Ensure closing braces are on their own lines
239+
.replace(/;\s*\/\/\s*/g, '; // ') // Ensure comments are properly formatted
204240
.trim()
241+
205242
if (DEBUG)
206243
console.log('Cleaned output:', result)
244+
207245
return result
208246
}

0 commit comments

Comments
 (0)