Skip to content

Commit 16f1500

Browse files
committed
chore: wip
1 parent db27a06 commit 16f1500

File tree

5 files changed

+88
-68
lines changed

5 files changed

+88
-68
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ storage
1515
fixtures/generated
1616
bin/dtsx*
1717
/test/temp-output
18-
test/debug
18+
test/debug
19+
/test/fixtures/generated

src/extractor.ts

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -198,37 +198,37 @@ function extractFunctionDeclaration(node: ts.FunctionDeclaration, sourceCode: st
198198
* Build clean function signature for DTS output
199199
*/
200200
function buildFunctionSignature(node: ts.FunctionDeclaration): string {
201-
const parts: string[] = []
201+
let result = ''
202202

203203
// Add modifiers
204-
if (hasExportModifier(node)) parts.push('export')
205-
parts.push('declare')
206-
if (node.asteriskToken) parts.push('function*')
207-
else parts.push('function')
204+
if (hasExportModifier(node)) result += 'export '
205+
result += 'declare '
206+
if (node.asteriskToken) result += 'function* '
207+
else result += 'function '
208208

209-
// Add name
210-
if (node.name) parts.push(node.name.getText())
209+
// Add name (no space before)
210+
if (node.name) result += node.name.getText()
211211

212-
// Add generics
212+
// Add generics (no space before)
213213
if (node.typeParameters) {
214214
const generics = node.typeParameters.map(tp => tp.getText()).join(', ')
215-
parts.push(`<${generics}>`)
215+
result += `<${generics}>`
216216
}
217217

218-
// Add parameters
218+
// Add parameters (no space before)
219219
const params = node.parameters.map(param => {
220220
const name = param.name.getText()
221221
const type = param.type?.getText() || 'any'
222222
const optional = param.questionToken ? '?' : ''
223223
return `${name}${optional}: ${type}`
224224
}).join(', ')
225-
parts.push(`(${params})`)
225+
result += `(${params})`
226226

227-
// Add return type
227+
// Add return type (no space before colon)
228228
const returnType = node.type?.getText() || 'void'
229-
parts.push(`: ${returnType}`)
229+
result += `: ${returnType}`
230230

231-
return parts.join(' ') + ';'
231+
return result + ';'
232232
}
233233

234234
/**
@@ -271,18 +271,18 @@ function extractVariableStatement(node: ts.VariableStatement, sourceCode: string
271271
* Build clean variable declaration for DTS
272272
*/
273273
function buildVariableDeclaration(name: string, type: string | undefined, kind: string, isExported: boolean): string {
274-
const parts: string[] = []
274+
let result = ''
275275

276-
if (isExported) parts.push('export')
277-
parts.push('declare')
278-
parts.push(kind)
279-
parts.push(name)
276+
if (isExported) result += 'export '
277+
result += 'declare '
278+
result += kind + ' '
279+
result += name
280280

281281
if (type) {
282-
parts.push(`: ${type}`)
282+
result += `: ${type}`
283283
}
284284

285-
return parts.join(' ') + ';'
285+
return result + ';'
286286
}
287287

288288
/**
@@ -319,17 +319,17 @@ function extractInterfaceDeclaration(node: ts.InterfaceDeclaration, sourceCode:
319319
* Build clean interface declaration for DTS
320320
*/
321321
function buildInterfaceDeclaration(node: ts.InterfaceDeclaration, isExported: boolean): string {
322-
const parts: string[] = []
322+
let result = ''
323323

324-
if (isExported) parts.push('export')
325-
parts.push('declare')
326-
parts.push('interface')
327-
parts.push(node.name.getText())
324+
if (isExported) result += 'export '
325+
result += 'declare '
326+
result += 'interface '
327+
result += node.name.getText()
328328

329-
// Add generics
329+
// Add generics (no space before)
330330
if (node.typeParameters) {
331331
const generics = node.typeParameters.map(tp => tp.getText()).join(', ')
332-
parts.push(`<${generics}>`)
332+
result += `<${generics}>`
333333
}
334334

335335
// Add extends
@@ -339,15 +339,15 @@ function buildInterfaceDeclaration(node: ts.InterfaceDeclaration, isExported: bo
339339
)
340340
if (extendsClause) {
341341
const types = extendsClause.types.map(type => type.getText()).join(', ')
342-
parts.push(`extends ${types}`)
342+
result += ` extends ${types}`
343343
}
344344
}
345345

346346
// Add body (simplified)
347347
const body = getInterfaceBody(node)
348-
parts.push(body)
348+
result += ' ' + body
349349

350-
return parts.join(' ')
350+
return result
351351
}
352352

353353
/**
@@ -406,22 +406,22 @@ function extractTypeAliasDeclaration(node: ts.TypeAliasDeclaration, sourceCode:
406406
* Build clean type declaration for DTS
407407
*/
408408
function buildTypeDeclaration(node: ts.TypeAliasDeclaration, isExported: boolean): string {
409-
const parts: string[] = []
409+
let result = ''
410410

411-
if (isExported) parts.push('export')
412-
parts.push('type')
413-
parts.push(node.name.getText())
411+
if (isExported) result += 'export '
412+
result += 'type '
413+
result += node.name.getText()
414414

415-
// Add generics
415+
// Add generics (no space before)
416416
if (node.typeParameters) {
417417
const generics = node.typeParameters.map(tp => tp.getText()).join(', ')
418-
parts.push(`<${generics}>`)
418+
result += `<${generics}>`
419419
}
420420

421-
parts.push('=')
422-
parts.push(node.type.getText())
421+
result += ' = '
422+
result += node.type.getText()
423423

424-
return parts.join(' ')
424+
return result
425425
}
426426

427427
/**

src/processor.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,22 +214,22 @@ export function processFunctionDeclaration(decl: Declaration): string {
214214
* Process variable declaration to DTS format
215215
*/
216216
export function processVariableDeclaration(decl: Declaration): string {
217-
const parts: string[] = []
217+
let result = ''
218218

219219
// Add export if needed
220220
if (decl.isExported) {
221-
parts.push('export')
221+
result += 'export '
222222
}
223223

224224
// Add declare keyword
225-
parts.push('declare')
225+
result += 'declare '
226226

227227
// Add variable kind (const, let, var)
228228
const kind = decl.modifiers?.[0] || 'const'
229-
parts.push(kind)
229+
result += kind + ' '
230230

231231
// Add variable name
232-
parts.push(decl.name)
232+
result += decl.name
233233

234234
// Add type annotation
235235
let typeAnnotation = decl.typeAnnotation
@@ -245,15 +245,7 @@ export function processVariableDeclaration(decl: Declaration): string {
245245
typeAnnotation = 'any'
246246
}
247247

248-
parts.push(':')
249-
parts.push(typeAnnotation)
250-
251-
// Combine parts
252-
let result = parts.join(' ')
253-
result = result.replace(' : ', ': ')
254-
255-
// Add semicolon
256-
result += ';'
248+
result += `: ${typeAnnotation};`
257249

258250
return result
259251
}
@@ -275,7 +267,7 @@ export function processInterfaceDeclaration(decl: Declaration): string {
275267
// Add interface name
276268
result += decl.name
277269

278-
// Add generics if present
270+
// Add generics if present (no space before)
279271
if (decl.generics) {
280272
result += decl.generics
281273
}
@@ -327,7 +319,7 @@ export function processTypeDeclaration(decl: Declaration): string {
327319
// Fallback to simple format
328320
result += 'type ' + decl.name
329321
if (decl.generics) {
330-
result += decl.generics
322+
result += decl.generics // No space before generics
331323
}
332324
result += ' = any'
333325
}

test-generate.ts

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
11
import { generate } from './src/generator'
22
import { join } from 'node:path'
33

4-
const config = {
5-
entrypoints: [join(__dirname, 'test/fixtures/input/example/0003.ts')],
6-
outdir: join(__dirname, 'test/fixtures/generated'),
7-
clean: false,
8-
tsconfigPath: join(__dirname, 'tsconfig.json'),
9-
outputStructure: 'flat' as const,
10-
}
4+
// Test all examples
5+
const examples = ['0001', '0002', '0003', '0004', '0005', '0006', '0007', '0008', '0009', '0010', '0011']
6+
7+
for (const example of examples) {
8+
console.log(`\n🔍 Testing example ${example}...`)
9+
10+
const config = {
11+
entrypoints: [join(__dirname, `test/fixtures/input/example/${example}.ts`)],
12+
outdir: join(__dirname, 'test/fixtures/generated'),
13+
clean: false,
14+
tsconfigPath: join(__dirname, 'tsconfig.json'),
15+
outputStructure: 'flat' as const,
16+
}
1117

12-
console.log('Generating with config:', config)
18+
await generate(config)
1319

14-
await generate(config)
20+
// Read generated and expected content
21+
const generatedPath = join(__dirname, 'test/fixtures/generated', `${example}.d.ts`)
22+
const expectedPath = join(__dirname, 'test/fixtures/output/example', `${example}.d.ts`)
23+
24+
try {
25+
const generatedContent = await Bun.file(generatedPath).text()
26+
const expectedContent = await Bun.file(expectedPath).text()
27+
28+
if (generatedContent === expectedContent) {
29+
console.log(`✅ ${example}: MATCH`)
30+
} else {
31+
console.log(`❌ ${example}: MISMATCH`)
32+
console.log(`Generated (${generatedContent.length} chars):`)
33+
console.log(generatedContent)
34+
console.log(`\nExpected (${expectedContent.length} chars):`)
35+
console.log(expectedContent)
36+
console.log('\n' + '='.repeat(80))
37+
}
38+
} catch (error) {
39+
console.log(`❌ ${example}: ERROR - ${error instanceof Error ? error.message : String(error)}`)
40+
}
41+
}
1542

16-
console.log('Generation complete')
43+
console.log('All examples tested!')

test/fixtures/output/example/0003.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { PluginBuilder } from 'bun';
22
import type { UnimportOptions } from 'unimport';
3-
export declare function autoImports (options: Partial<UnimportOptions & { dts: string }>) : AutoImportsPlugin;
3+
export declare function autoImports(options: Partial<UnimportOptions & { dts: string }>): AutoImportsPlugin;
44
declare interface AutoImportsPlugin {
55
name: string
66
setup: (builder: PluginBuilder) => Promise<void>

0 commit comments

Comments
 (0)