Skip to content

Commit 5ca36da

Browse files
committed
chore: wip
1 parent dfc8cb2 commit 5ca36da

File tree

2 files changed

+81
-102
lines changed

2 files changed

+81
-102
lines changed

fixtures/output/variable.d.ts

Lines changed: 52 additions & 52 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: {
4545
key: 'value'
46-
};
46+
};
4747
someNestedObject: {
4848
key: {
49-
nestedKey: 'value'
49+
nestedKey: 'value'
5050
};
5151
otherKey: {
52-
nestedKey: unknown;
53-
nestedKey2: () => unknown
52+
nestedKey: unknown;
53+
nestedKey2: () => void
5454
}
55-
};
55+
};
5656
someNestedObjectArray: Array<
57-
{
58-
key: 'value'
59-
}
60-
{
61-
key2: 'value2'
57+
{
58+
key: 'value'
59+
} |
60+
{
61+
key2: 'value2'
6262
}
63-
>;
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: (...args: any[]) => unknown;
90-
onError: (error: Error & { code?: number }) => unknown
91-
};
89+
onSuccess: <T> (data: T) => Promise<void>;
90+
onError: (error: Error & { code?: number }) => never
91+
};
9292
utils: {
9393
formatters: {
94-
date: (input: Date) => unknown;
95-
currency: (amount: number, currency: string) => unknown
94+
date: (input: Date) => string;
95+
currency: (amount: number, currency: string) => string
96+
}
9697
}
97-
}
9898
};

src/extract.ts

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -624,19 +624,26 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
624624
)
625625

626626
if (needsMultiline) {
627-
const formattedTypes = types.map((type) => {
628-
// Indent nested types that have newlines
627+
const formattedTypes = types.map((type, index) => {
628+
const isLast = index === types.length - 1
629+
// For types that contain newlines, maintain their internal formatting
629630
if (type.includes('\n')) {
630-
return type.replace(/\n/g, `\n${elementIndent}`)
631+
const lines = type.split('\n')
632+
const formattedLines = lines.map((line, i) => {
633+
if (i === 0)
634+
return line
635+
// Increment indentation for nested lines
636+
const currentIndent = line.match(/^\s*/)[0]
637+
const additionalIndent = ' '.repeat(indentLevel + 1)
638+
return `${additionalIndent}${line.trimLeft()}`
639+
}).join('\n')
640+
return `${elementIndent}${formattedLines}${isLast ? '' : ' |'}`
631641
}
632-
return type
642+
// For single-line types
643+
return `${elementIndent}${type}${isLast ? '' : ' |'}`
633644
})
634645

635-
return [
636-
'Array<',
637-
...formattedTypes.map(type => `${elementIndent}${type}`),
638-
`${baseIndent}>`,
639-
].join('\n')
646+
return `Array<\n${formattedTypes.join('\n')}\n${baseIndent}>`
640647
}
641648

642649
return `Array<${types.join(' | ')}>`
@@ -654,56 +661,28 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe
654661

655662
const baseIndent = ' '.repeat(indentLevel)
656663
const propIndent = ' '.repeat(indentLevel + 1)
664+
const innerIndent = ' '.repeat(indentLevel + 2)
657665

658666
const props = processObjectProperties(content, state)
659667
if (!props.length)
660668
return '{}'
661669

662-
// Group methods and regular properties
663-
const methods: Array<{ key: string, value: string }> = []
664-
const regularProps: Array<{ key: string, value: string }> = []
670+
const propertyStrings = props.map(({ key, value }) => {
671+
const formattedKey = /^\w+$/.test(key) ? key : `'${key}'`
665672

666-
props.forEach((prop) => {
667-
if (prop.key.includes('('))
668-
methods.push(prop)
669-
else
670-
regularProps.push(prop)
671-
})
672-
673-
const parts: string[] = []
674-
675-
if (methods.length > 0) {
676-
const methodsStr = processObjectMethods(methods, state, indentLevel + 1)
677-
if (methodsStr !== '{}') {
678-
parts.push(methodsStr.slice(1, -1).trim()) // Remove outer braces
673+
// Handle multiline values (like objects and arrays)
674+
if (value.includes('\n')) {
675+
const indentedValue = value
676+
.split('\n')
677+
.map((line, i) => i === 0 ? line : `${innerIndent}${line.trimLeft()}`)
678+
.join('\n')
679+
return `${propIndent}${formattedKey}: ${indentedValue}`
679680
}
680-
}
681-
682-
if (regularProps.length > 0) {
683-
const propsStr = regularProps.map(({ key, value }) => {
684-
const formattedKey = /^\w+$/.test(key) ? key : `'${key}'`
685-
let formattedValue = value
686-
687-
// Format nested objects with proper indentation
688-
if (value.startsWith('{')) {
689-
formattedValue = inferComplexObjectType(value, state, indentLevel + 1)
690-
}
691-
// Format array types with proper indentation
692-
else if (value.startsWith('Array<')) {
693-
// Extract the array content and re-indent it
694-
const arrayContent = value.slice(6, -1)
695-
formattedValue = `Array<${arrayContent}>`
696-
}
697-
698-
return `${propIndent}${formattedKey}: ${formattedValue}`
699-
}).join(';\n')
700-
parts.push(propsStr)
701-
}
702681

703-
if (parts.length === 0)
704-
return '{}'
682+
return `${propIndent}${formattedKey}: ${value}`
683+
})
705684

706-
return `{\n${parts.join(';\n')}\n${baseIndent}}`
685+
return `{\n${propertyStrings.join(';\n')}\n${baseIndent}}`
707686
}
708687

709688
function inferConstArrayType(value: string, state?: ProcessingState): string {

0 commit comments

Comments
 (0)