Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 31, 2024
1 parent 3420eb0 commit dfc8cb2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
54 changes: 34 additions & 20 deletions fixtures/output/variable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,34 @@ export declare const someObject: {
anotherOne: () => unknown;
someArray: Array<1 | 2 | 3>;
someNestedArray: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10>>;
someNestedArray2: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10> | 'dummy value'>;
someNestedArray3: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10> | 'dummy value' | Array<11 | 12 | 13>>;
someNestedArray2: Array<
Array<1 | 2 | 3>
Array<4 | 5 | 6 | 7 | 8 | 9 | 10>
'dummy value'
>;
someNestedArray3: Array<
Array<1 | 2 | 3>
Array<4 | 5 | 6 | 7 | 8 | 9 | 10>
'dummy value'
Array<11 | 12 | 13>
>;
someOtherNestedArray: Array<
Array<'some text' | 2 | unknown | (() => void) | unknown> |
Array<'some text' | 2 | unknown | (() => void) | unknown>
Array<4 | 5 | 6 | 7 | 8 | 9 | 10>
>;
someComplexArray: Array<
Array<
{
key: 'value'
}
> |
{
key: 'value'
}
>
Array<
{
key2: 'value2'
} |
'test' |
1000
> |
{
key2: 'value2'
}
'test'
1000
>
Array<'some string' | unknown | unknown>
>;
someObject: {
Expand All @@ -46,11 +55,11 @@ export declare const someObject: {
};
someNestedObjectArray: Array<
{
key: 'value'
} |
key: 'value'
}
{
key2: 'value2'
}
key2: 'value2'
}
>;
someOtherObject: unknown;
someInlineCall2: unknown;
Expand All @@ -63,12 +72,17 @@ export declare const defaultHeaders: {
declare const dtsConfig: DtsGenerationConfig;
export declare const complexArrays: {
matrix: Array<
Array<1 | 2 | Array<3 | 4 | Array<5 | 6>>> |
Array<'a' | 'b' | Array<'c' | 'd'>> |
Array<1 | 2 | Array<3 | 4 | Array<5 | 6>>>
Array<'a' | 'b' | Array<'c' | 'd'>>
Array<true | Array<false | Array<true>>>
>;
tuples: Array<readonly [1, 'string', true] | readonly ['literal', 42, false]>;
mixedArrays: Array<unknown | unknown | ((...args: any[]) => unknown) | ((...args: any[]) => unknown)>
mixedArrays: Array<
unknown
unknown
((...args: any[]) => unknown)
((...args: any[]) => unknown)
>
};
export declare const complexObject: {
handlers: {
Expand Down
17 changes: 15 additions & 2 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,24 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
const needsMultiline = types.some(type =>
type.includes('\n')
|| type.includes('{')
|| type.length > 40,
|| type.length > 40
|| types.join(' | ').length > 60,
)

if (needsMultiline) {
return `Array<\n${elementIndent}${types.join(` |\n${elementIndent}`)}\n${baseIndent}>`
const formattedTypes = types.map((type) => {
// Indent nested types that have newlines
if (type.includes('\n')) {
return type.replace(/\n/g, `\n${elementIndent}`)
}
return type
})

return [
'Array<',
...formattedTypes.map(type => `${elementIndent}${type}`),
`${baseIndent}>`,
].join('\n')
}

return `Array<${types.join(' | ')}>`
Expand Down

0 comments on commit dfc8cb2

Please sign in to comment.