Skip to content

Commit 3420eb0

Browse files
committed
chore: wip
1 parent e892723 commit 3420eb0

File tree

2 files changed

+67
-21
lines changed

2 files changed

+67
-21
lines changed

fixtures/output/variable.d.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,25 @@ export declare const someObject: {
1313
someNestedArray: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10>>;
1414
someNestedArray2: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10> | 'dummy value'>;
1515
someNestedArray3: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10> | 'dummy value' | Array<11 | 12 | 13>>;
16-
someOtherNestedArray: Array<Array<'some text' | 2 | unknown | (() => void) | unknown> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10>>;
17-
someComplexArray: Array<Array<{
18-
key: 'value'
19-
}> | Array<{
20-
key2: 'value2'
21-
} | 'test' | 1000> | Array<'some string' | unknown | unknown>>;
16+
someOtherNestedArray: Array<
17+
Array<'some text' | 2 | unknown | (() => void) | unknown> |
18+
Array<4 | 5 | 6 | 7 | 8 | 9 | 10>
19+
>;
20+
someComplexArray: Array<
21+
Array<
22+
{
23+
key: 'value'
24+
}
25+
> |
26+
Array<
27+
{
28+
key2: 'value2'
29+
} |
30+
'test' |
31+
1000
32+
> |
33+
Array<'some string' | unknown | unknown>
34+
>;
2235
someObject: {
2336
key: 'value'
2437
};
@@ -31,11 +44,14 @@ export declare const someObject: {
3144
nestedKey2: () => unknown
3245
}
3346
};
34-
someNestedObjectArray: Array<{
35-
key: 'value'
36-
} | {
37-
key2: 'value2'
38-
}>;
47+
someNestedObjectArray: Array<
48+
{
49+
key: 'value'
50+
} |
51+
{
52+
key2: 'value2'
53+
}
54+
>;
3955
someOtherObject: unknown;
4056
someInlineCall2: unknown;
4157
someInlineCall3: unknown
@@ -46,7 +62,11 @@ export declare const defaultHeaders: {
4662
};
4763
declare const dtsConfig: DtsGenerationConfig;
4864
export declare const complexArrays: {
49-
matrix: Array<Array<1 | 2 | Array<3 | 4 | Array<5 | 6>>> | Array<'a' | 'b' | Array<'c' | 'd'>> | Array<true | Array<false | Array<true>>>>;
65+
matrix: Array<
66+
Array<1 | 2 | Array<3 | 4 | Array<5 | 6>>> |
67+
Array<'a' | 'b' | Array<'c' | 'd'>> |
68+
Array<true | Array<false | Array<true>>>
69+
>;
5070
tuples: Array<readonly [1, 'string', true] | readonly ['literal', 42, false]>;
5171
mixedArrays: Array<unknown | unknown | ((...args: any[]) => unknown) | ((...args: any[]) => unknown)>
5272
};

src/extract.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -560,12 +560,15 @@ function inferValueType(value: string): string {
560560
/**
561561
* Infer array type from array literal with support for nested arrays and mixed elements
562562
*/
563-
function inferArrayType(value: string, state?: ProcessingState): string {
563+
function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0): string {
564564
debugLog(state, 'infer-array', `Inferring array type for: ${value}`)
565565
const content = value.slice(1, -1).trim()
566566
if (!content)
567567
return 'unknown[]'
568568

569+
const baseIndent = ' '.repeat(indentLevel)
570+
const elementIndent = ' '.repeat(indentLevel + 1)
571+
569572
// Handle const assertions first
570573
const elements = splitArrayElements(content, state)
571574
const allConstTuples = elements.every(el => el.trim().endsWith('as const'))
@@ -584,12 +587,12 @@ function inferArrayType(value: string, state?: ProcessingState): string {
584587

585588
// Handle nested arrays
586589
if (trimmed.startsWith('[')) {
587-
return inferArrayType(trimmed, state)
590+
return inferArrayType(trimmed, state, indentLevel + 1)
588591
}
589592

590-
// Handle objects
593+
// Handle objects with proper indentation
591594
if (trimmed.startsWith('{')) {
592-
return inferComplexObjectType(trimmed, state)
595+
return inferComplexObjectType(trimmed, state, indentLevel + 1)
593596
}
594597

595598
// Handle function expressions - always parenthesize
@@ -607,7 +610,23 @@ function inferArrayType(value: string, state?: ProcessingState): string {
607610
return normalizeTypeReference(trimmed)
608611
})
609612

610-
return `Array<${elementTypes.join(' | ')}>`
613+
// Format the array type with proper indentation
614+
const types = elementTypes.filter(Boolean)
615+
if (types.length === 0)
616+
return 'unknown[]'
617+
618+
// Check if we need multiline formatting
619+
const needsMultiline = types.some(type =>
620+
type.includes('\n')
621+
|| type.includes('{')
622+
|| type.length > 40,
623+
)
624+
625+
if (needsMultiline) {
626+
return `Array<\n${elementIndent}${types.join(` |\n${elementIndent}`)}\n${baseIndent}>`
627+
}
628+
629+
return `Array<${types.join(' | ')}>`
611630
}
612631

613632
/**
@@ -656,6 +675,12 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe
656675
if (value.startsWith('{')) {
657676
formattedValue = inferComplexObjectType(value, state, indentLevel + 1)
658677
}
678+
// Format array types with proper indentation
679+
else if (value.startsWith('Array<')) {
680+
// Extract the array content and re-indent it
681+
const arrayContent = value.slice(6, -1)
682+
formattedValue = `Array<${arrayContent}>`
683+
}
659684

660685
return `${propIndent}${formattedKey}: ${formattedValue}`
661686
}).join(';\n')
@@ -1476,7 +1501,7 @@ function processObjectProperties(content: string, state?: ProcessingState): Arra
14761501
return properties
14771502
}
14781503

1479-
function processProperty(key: string, value: string, state?: ProcessingState): { key: string, value: string } {
1504+
function processProperty(key: string, value: string, state?: ProcessingState, indentLevel = 0): { key: string, value: string } {
14801505
const cleanKey = key.trim().replace(/^['"](.*)['"]$/, '$1')
14811506
const cleanValue = value.trim()
14821507

@@ -1488,18 +1513,18 @@ function processProperty(key: string, value: string, state?: ProcessingState): {
14881513
return { key: name, value: signature }
14891514
}
14901515

1491-
// Handle arrays
1516+
// Handle arrays with proper indentation
14921517
if (cleanValue.startsWith('[')) {
14931518
debugLog(state, 'process-array', `Processing array in property "${cleanKey}"`)
1494-
return { key: cleanKey, value: inferArrayType(cleanValue, state) }
1519+
return { key: cleanKey, value: inferArrayType(cleanValue, state, indentLevel) }
14951520
}
14961521

14971522
// Handle object literals with proper indentation
14981523
if (cleanValue.startsWith('{')) {
14991524
debugLog(state, 'process-object', `Processing nested object in property "${cleanKey}"`)
15001525
return {
15011526
key: cleanKey,
1502-
value: inferComplexObjectType(cleanValue, state, 0),
1527+
value: inferComplexObjectType(cleanValue, state, indentLevel),
15031528
}
15041529
}
15051530

@@ -1524,6 +1549,7 @@ function processProperty(key: string, value: string, state?: ProcessingState): {
15241549
return { key: cleanKey, value: 'unknown' }
15251550
}
15261551

1552+
// Default case
15271553
return { key: cleanKey, value: 'unknown' }
15281554
}
15291555

0 commit comments

Comments
 (0)