Skip to content

Commit f1a8c70

Browse files
committed
chore: wip
1 parent cfdf372 commit f1a8c70

File tree

2 files changed

+101
-85
lines changed

2 files changed

+101
-85
lines changed

fixtures/output/variable.d.ts

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,55 @@ export declare const someObject: {
1212
someArray: Array<1 | 2 | 3>;
1313
someNestedArray: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10>>;
1414
someNestedArray2: Array<
15-
Array<1 | 2 | 3> |
16-
Array<4 | 5 | 6 | 7 | 8 | 9 | 10> |
17-
'dummy value'
18-
>;
15+
Array<1 | 2 | 3> |
16+
Array<4 | 5 | 6 | 7 | 8 | 9 | 10> |
17+
'dummy value'
18+
>;
1919
someNestedArray3: Array<
20-
Array<1 | 2 | 3> |
21-
Array<4 | 5 | 6 | 7 | 8 | 9 | 10> |
22-
'dummy value' |
23-
Array<11 | 12 | 13>
24-
>;
20+
Array<1 | 2 | 3> |
21+
Array<4 | 5 | 6 | 7 | 8 | 9 | 10> |
22+
'dummy value' |
23+
Array<11 | 12 | 13>
24+
>;
2525
someOtherNestedArray: Array<
26-
Array<'some text' | 2 | unknown | (() => void) | unknown> |
27-
Array<4 | 5 | 6 | 7 | 8 | 9 | 10>
28-
>;
26+
Array<'some text' | 2 | unknown | (() => void) | unknown> |
27+
Array<4 | 5 | 6 | 7 | 8 | 9 | 10>
28+
>;
2929
someComplexArray: Array<
30-
Array<
31-
{
32-
key: 'value'
33-
}
34-
> |
35-
Array<
36-
{
37-
key2: 'value2'
38-
} |
39-
'test' |
40-
1000
41-
> |
42-
Array<'some string' | unknown | unknown>
43-
>;
30+
Array<
31+
{
32+
key: 'value'
33+
}
34+
> |
35+
Array<
36+
{
37+
key2: 'value2'
38+
} |
39+
'test' |
40+
1000
41+
> |
42+
Array<'some string' | unknown | unknown>
43+
>;
4444
someObject: {
45-
key: 'value'
46-
};
45+
key: 'value'
46+
};
4747
someNestedObject: {
48-
key: {
49-
nestedKey: 'value'
50-
};
51-
otherKey: {
52-
nestedKey: unknown;
53-
nestedKey2: () => void
54-
}
55-
};
48+
key: {
49+
nestedKey: 'value'
50+
};
51+
otherKey: {
52+
nestedKey: unknown;
53+
nestedKey2: () => void
54+
}
55+
};
5656
someNestedObjectArray: Array<
57-
{
58-
key: 'value'
59-
} |
60-
{
61-
key2: 'value2'
62-
}
63-
>;
57+
{
58+
key: 'value'
59+
} |
60+
{
61+
key2: 'value2'
62+
}
63+
>;
6464
someOtherObject: unknown;
6565
someInlineCall2: unknown;
6666
someInlineCall3: unknown
@@ -72,27 +72,27 @@ export declare const defaultHeaders: {
7272
declare const dtsConfig: DtsGenerationConfig;
7373
export declare const complexArrays: {
7474
matrix: Array<
75-
Array<1 | 2 | Array<3 | 4 | Array<5 | 6>>> |
76-
Array<'a' | 'b' | Array<'c' | 'd'>> |
77-
Array<true | Array<false | Array<true>>>
78-
>;
75+
Array<1 | 2 | Array<3 | 4 | Array<5 | 6>>> |
76+
Array<'a' | 'b' | Array<'c' | 'd'>> |
77+
Array<true | Array<false | Array<true>>>
78+
>;
7979
tuples: Array<readonly [1, 'string', true] | readonly ['literal', 42, false]>;
8080
mixedArrays: Array<
81-
unknown |
82-
unknown |
83-
((...args: any[]) => unknown) |
84-
((...args: any[]) => unknown)
85-
>
81+
unknown |
82+
unknown |
83+
((...args: any[]) => unknown) |
84+
((...args: any[]) => unknown)
85+
>
8686
};
8787
export declare const complexObject: {
8888
handlers: {
89-
onSuccess: <T> (data: T) => Promise<void>;
90-
onError: (error: Error & { code?: number }) => never
91-
};
89+
onSuccess: <T> (data: T) => Promise<void>;
90+
onError: (error: Error & { code?: number }) => never
91+
};
9292
utils: {
93-
formatters: {
94-
date: (input: Date) => string;
95-
currency: (amount: number, currency: string) => string
96-
}
97-
}
93+
formatters: {
94+
date: (input: Date) => string;
95+
currency: (amount: number, currency: string) => string
96+
}
97+
}
9898
};

src/extract.ts

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,21 @@ function createImportTrackingState(): ImportTrackingState {
531531
}
532532
}
533533

534+
function indentMultilineType(type: string, baseIndent: string, isLast: boolean): string {
535+
const lines = type.split('\n')
536+
return lines
537+
.map((line, i) => {
538+
if (i === 0)
539+
return `${baseIndent}${line}`
540+
const trimmed = line.trim()
541+
if (!trimmed)
542+
return ''
543+
return `${baseIndent} ${trimmed}`
544+
})
545+
.filter(Boolean)
546+
.join('\n') + (isLast ? '' : ' |')
547+
}
548+
534549
function inferValueType(value: string): string {
535550
value = value.trim()
536551

@@ -582,7 +597,7 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
582597
}
583598

584599
// Process each element
585-
const elementTypes = elements.map((element, index) => {
600+
const elementTypes = elements.map((element) => {
586601
const trimmed = element.trim()
587602

588603
// Handle nested arrays
@@ -626,23 +641,14 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
626641
if (needsMultiline) {
627642
const formattedTypes = types.map((type, index) => {
628643
const isLast = index === types.length - 1
629-
// For types that contain newlines, ensure proper indentation
644+
// For types that contain newlines
630645
if (type.includes('\n')) {
631-
const lines = type.split('\n')
632-
const formattedLines = lines.map((line, i) => {
633-
// First line gets element indentation
634-
if (i === 0)
635-
return line
636-
// Other lines get additional indentation
637-
return `${elementIndent}${line.trimLeft()}`
638-
}).join('\n')
639-
return `${elementIndent}${formattedLines}${isLast ? '' : ' |'}`
646+
return indentMultilineType(type, elementIndent, isLast)
640647
}
641648
// For single-line types
642649
return `${elementIndent}${type}${isLast ? '' : ' |'}`
643650
})
644651

645-
// Join lines and ensure closing bracket aligns with parent
646652
return `Array<\n${formattedTypes.join('\n')}\n${baseIndent}>`
647653
}
648654

@@ -661,6 +667,7 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe
661667

662668
const baseIndent = ' '.repeat(indentLevel)
663669
const propIndent = ' '.repeat(indentLevel + 1)
670+
const innerIndent = ' '.repeat(indentLevel + 2)
664671

665672
const props = processObjectProperties(content, state)
666673
if (!props.length)
@@ -669,20 +676,26 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe
669676
const propertyStrings = props.map(({ key, value }) => {
670677
const formattedKey = /^\w+$/.test(key) ? key : `'${key}'`
671678

672-
// Handle multiline values (like objects and arrays)
673679
if (value.includes('\n')) {
674-
// Add one level of indentation to each line after the first
680+
// Indent nested multiline values
675681
const indentedValue = value
676682
.split('\n')
677-
.map((line, i) => i === 0 ? line : `${propIndent}${line.trim()}`)
683+
.map((line, i) => {
684+
if (i === 0)
685+
return line
686+
const trimmed = line.trim()
687+
if (!trimmed)
688+
return ''
689+
return `${innerIndent}${trimmed}`
690+
})
691+
.filter(Boolean)
678692
.join('\n')
679693
return `${propIndent}${formattedKey}: ${indentedValue}`
680694
}
681695

682696
return `${propIndent}${formattedKey}: ${value}`
683697
})
684698

685-
// Ensure closing brace aligns with parent
686699
return `{\n${propertyStrings.join(';\n')}\n${baseIndent}}`
687700
}
688701

@@ -1348,7 +1361,13 @@ function processObjectMethod(declaration: string, value: string, state?: Process
13481361
else if (isAsync && !effectiveReturnType.includes('Promise')) {
13491362
effectiveReturnType = `Promise<${effectiveReturnType}>`
13501363
}
1351-
else if (value.includes('toISOString()') || (value.includes('Intl.NumberFormat') && value.includes('format'))) {
1364+
else if (value.includes('toISOString()') || value.includes('toString()')) {
1365+
effectiveReturnType = 'string'
1366+
}
1367+
else if (value.includes('console.log') || value.match(/void\s*[;{]/)) {
1368+
effectiveReturnType = 'void'
1369+
}
1370+
else if (value.includes('Intl.NumberFormat') && value.includes('format')) {
13521371
effectiveReturnType = 'string'
13531372
}
13541373

@@ -1452,8 +1471,6 @@ function processProperty(key: string, value: string, state?: ProcessingState, in
14521471
const cleanKey = key.trim().replace(/^['"](.*)['"]$/, '$1')
14531472
const cleanValue = value.trim()
14541473

1455-
debugLog(state, 'process-property', `Processing property "${cleanKey}" with value: ${cleanValue}`)
1456-
14571474
// Handle method declarations
14581475
if (cleanKey.includes('(')) {
14591476
const { name, signature } = processObjectMethod(cleanKey, cleanValue, state)
@@ -1462,13 +1479,14 @@ function processProperty(key: string, value: string, state?: ProcessingState, in
14621479

14631480
// Handle arrays with proper indentation
14641481
if (cleanValue.startsWith('[')) {
1465-
debugLog(state, 'process-array', `Processing array in property "${cleanKey}"`)
1466-
return { key: cleanKey, value: inferArrayType(cleanValue, state, indentLevel) }
1482+
return {
1483+
key: cleanKey,
1484+
value: inferArrayType(cleanValue, state, indentLevel),
1485+
}
14671486
}
14681487

14691488
// Handle object literals with proper indentation
14701489
if (cleanValue.startsWith('{')) {
1471-
debugLog(state, 'process-object', `Processing nested object in property "${cleanKey}"`)
14721490
return {
14731491
key: cleanKey,
14741492
value: inferComplexObjectType(cleanValue, state, indentLevel),
@@ -1477,7 +1495,6 @@ function processProperty(key: string, value: string, state?: ProcessingState, in
14771495

14781496
// Handle function expressions
14791497
if (cleanValue.includes('=>') || cleanValue.includes('function')) {
1480-
debugLog(state, 'process-property', 'Processing function expression')
14811498
const funcType = extractFunctionType(cleanValue, state)
14821499
return {
14831500
key: cleanKey,
@@ -1491,12 +1508,11 @@ function processProperty(key: string, value: string, state?: ProcessingState, in
14911508
return { key: cleanKey, value: cleanValue }
14921509
}
14931510

1494-
// Handle references to global objects or function calls
1511+
// Handle references and function calls
14951512
if (cleanValue.includes('.') || cleanValue.includes('(')) {
14961513
return { key: cleanKey, value: 'unknown' }
14971514
}
14981515

1499-
// Default case
15001516
return { key: cleanKey, value: 'unknown' }
15011517
}
15021518

0 commit comments

Comments
 (0)