Skip to content

Commit a516408

Browse files
committed
chore: wip
chore: wip
1 parent 6ecbe9e commit a516408

File tree

2 files changed

+71
-63
lines changed

2 files changed

+71
-63
lines changed

fixtures/output/variable.d.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,28 @@ export declare const someObject: {
1111
anotherOne: () => unknown;
1212
someArray: Array<1 | 2 | 3>;
1313
someNestedArray: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10>>;
14-
someNestedArray2: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10> | 'dummy value'>;
15-
someNestedArray3: Array<Array<1 | 2 | 3> | Array<4 | 5 | 6 | 7 | 8 | 9 | 10> | 'dummy value' | Array<11 | 12 | 13>>;
14+
someNestedArray2: Array<
15+
Array<1 | 2 | 3> |
16+
Array<4 | 5 | 6 | 7 | 8 | 9 | 10> |
17+
'dummy value'
18+
>;
19+
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+
>;
1625
someOtherNestedArray: Array<
1726
Array<'some text' | 2 | unknown | (() => void) | unknown> |
1827
Array<4 | 5 | 6 | 7 | 8 | 9 | 10>
1928
>;
2029
someComplexArray: Array<
2130
Array<{
22-
key: 'value'
23-
}> |
31+
key: 'value'
32+
}> |
2433
Array<{
25-
key2: 'value2'
26-
} | 'test' | 1000> |
34+
key2: 'value2'
35+
} | 'test' | 1000> |
2736
Array<'some string' | unknown | unknown>
2837
>;
2938
someObject: {
@@ -40,11 +49,11 @@ export declare const someObject: {
4049
};
4150
someNestedObjectArray: Array<
4251
{
43-
key: 'value'
44-
} |
52+
key: 'value'
53+
} |
4554
{
46-
key2: 'value2'
47-
}
55+
key2: 'value2'
56+
}
4857
>;
4958
someOtherObject: unknown;
5059
someInlineCall2: unknown;
@@ -65,7 +74,12 @@ export declare const complexArrays: {
6574
readonly [1, 'string', true] |
6675
readonly ['literal', 42, false]
6776
];
68-
mixedArrays: Array<unknown | unknown | ((...args: any[]) => unknown) | ((...args: any[]) => unknown)>
77+
mixedArrays: Array<
78+
unknown |
79+
unknown |
80+
((...args: any[]) => unknown) |
81+
((...args: any[]) => unknown)
82+
>
6983
};
7084
export declare const complexObject: {
7185
handlers: {

src/extract.ts

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,6 @@ function generateOptimizedImports(state: ImportTrackingState, dtsLines: string[]
327327
return imports.sort()
328328
}
329329

330-
function getDeclarationType(declaration: string): string {
331-
if (declaration.includes('const '))
332-
return 'const'
333-
if (declaration.includes('let '))
334-
return 'let'
335-
return 'var'
336-
}
337-
338330
function extractCompleteObjectContent(value: string): string | null {
339331
// debugLog(state, 'extract-object', `Processing object of length ${value.length}`)
340332
const fullContent = value.trim()
@@ -471,12 +463,9 @@ function createImportTrackingState(): ImportTrackingState {
471463
}
472464

473465
function indentMultilineType(type: string, baseIndent: string, isLast: boolean): string {
474-
// debugLog(undefined, 'indent-multiline', `Processing multiline type with baseIndent="${baseIndent}", isLast=${isLast}`)
475-
476466
const lines = type.split('\n')
477-
if (lines.length === 1) {
467+
if (lines.length === 1)
478468
return `${baseIndent}${type}${isLast ? '' : ' |'}`
479-
}
480469

481470
interface BracketInfo {
482471
char: string
@@ -508,45 +497,49 @@ function indentMultilineType(type: string, baseIndent: string, isLast: boolean):
508497
if (!trimmed)
509498
return ''
510499

511-
// Track Array type specifically
512-
const openBrackets = trimmed.match(/[{<[]/g) || []
513-
const closeBrackets = trimmed.match(/[}\]>]/g) || []
514-
500+
// Calculate base indentation for this line
515501
let currentIndent = baseIndent
516502
if (i > 0) {
503+
// Add additional indentation for nested structures
517504
const stackDepth = bracketStack.reduce((depth, info) => depth + info.depth, 0)
518505
currentIndent = baseIndent + ' '.repeat(stackDepth)
519506

520-
// Dedent closing tokens
507+
// Handle object property indentation
508+
if (trimmed.match(/^['"]/)) { // Property starts with a quote
509+
currentIndent += ' '
510+
}
511+
512+
// Adjust closing brace/bracket indentation
521513
if ((trimmed.startsWith('}') || trimmed.startsWith('>') || trimmed.startsWith('> |')) && bracketStack.length > 0) {
522514
currentIndent = baseIndent + ' '.repeat(Math.max(0, stackDepth - 1))
523515
}
524516
}
525517

526-
// Handle opening brackets with Array context
527-
if (openBrackets.length > 0) {
528-
openBrackets.forEach((bracket) => {
529-
const isArrayBracket = trimmed.startsWith('Array') && bracket === '<'
530-
bracketStack.push({
531-
char: bracket,
532-
indent: currentIndent,
533-
isArray: isArrayBracket,
534-
depth: 1,
535-
isSingleElement: isInSingleElementArray,
536-
})
518+
// Track brackets for nested structures
519+
const openBrackets = trimmed.match(/[{<[]/g) || []
520+
const closeBrackets = trimmed.match(/[}\]>]/g) || []
521+
522+
// Handle opening brackets
523+
openBrackets.forEach((bracket) => {
524+
const isArrayBracket = trimmed.startsWith('Array') && bracket === '<'
525+
bracketStack.push({
526+
char: bracket,
527+
indent: currentIndent,
528+
isArray: isArrayBracket,
529+
depth: 1,
530+
isSingleElement: isInSingleElementArray,
537531
})
538-
}
532+
})
539533

540534
// Handle closing brackets
541535
if (closeBrackets.length > 0) {
542536
for (let j = 0; j < closeBrackets.length; j++) {
543-
if (bracketStack.length > 0) {
537+
if (bracketStack.length > 0)
544538
bracketStack.pop()
545-
}
546539
}
547540
}
548541

549-
// Add union operator only when appropriate
542+
// Add union operator when needed
550543
let needsUnion = false
551544
if (!isLast && i === lines.length - 1 && !trimmed.endsWith(' |') && !trimmed.endsWith(';')) {
552545
needsUnion = true
@@ -555,15 +548,13 @@ function indentMultilineType(type: string, baseIndent: string, isLast: boolean):
555548
// Handle special cases for objects in arrays
556549
if (trimmed === '}') {
557550
const lastArray = [...bracketStack].reverse().find(info => info.isArray)
558-
if (lastArray?.isSingleElement) {
551+
if (lastArray?.isSingleElement)
559552
needsUnion = false
560-
}
561553
}
562554

563-
// Don't add union if it's already part of the trimmed content
564-
if (trimmed.endsWith(' |')) {
555+
// Don't add union if it's already there
556+
if (trimmed.endsWith(' |'))
565557
needsUnion = false
566-
}
567558

568559
return `${currentIndent}${trimmed}${needsUnion ? ' |' : ''}`
569560
}).filter(Boolean)
@@ -623,10 +614,10 @@ function inferArrayType(value: string, state?: ProcessingState, preserveLineBrea
623614
})
624615

625616
if (needsMultilineFormat(tuples)) {
626-
const formattedContent = tuples.map((type, i) => {
627-
const isLast = i === tuples.length - 1
628-
return ` ${type}${isLast ? '' : ' |'}`
629-
}).join('\n')
617+
// Use indentMultilineType for tuple formatting
618+
const formattedContent = tuples.map((type, i) =>
619+
indentMultilineType(type, ' ', i === tuples.length - 1),
620+
).join('\n')
630621
return `readonly [\n${formattedContent}\n ]`
631622
}
632623

@@ -651,13 +642,18 @@ function inferArrayType(value: string, state?: ProcessingState, preserveLineBrea
651642
})
652643

653644
const types = elementTypes.filter(Boolean)
654-
const needsMultiline = types.some(type => type.includes('\n') || type.includes('{') || type.length > 40)
645+
const needsMultiline = types.some(type =>
646+
type.includes('\n')
647+
|| type.includes('{')
648+
|| type.length > 40
649+
|| types.join(' | ').length > 60,
650+
)
655651

656652
if (needsMultiline && preserveLineBreaks) {
657-
const formattedContent = types.map((type, i) => {
658-
const isLast = i === types.length - 1
659-
return ` ${type}${isLast ? '' : ' |'}`
660-
}).join('\n')
653+
// Use indentMultilineType for array type formatting
654+
const formattedContent = types.map((type, i) =>
655+
indentMultilineType(type, ' ', i === types.length - 1),
656+
).join('\n')
661657
return `Array<\n${formattedContent}\n >`
662658
}
663659

@@ -668,27 +664,25 @@ function inferArrayType(value: string, state?: ProcessingState, preserveLineBrea
668664
* Process object properties with improved formatting
669665
*/
670666
function inferComplexObjectType(value: string, state?: ProcessingState, indentLevel = 0): string {
671-
// debugLog(state, 'infer-complex', `Inferring type for object of length ${value.length}`)
672-
673667
const content = extractCompleteObjectContent(value)
674668
if (!content)
675669
return 'Record<string, unknown>'
676670

671+
// Calculate indentation based on nesting level
677672
const baseIndent = ' '.repeat(indentLevel)
678673
const propIndent = ' '.repeat(indentLevel + 1)
679-
680-
// debugLog(state, 'infer-complex-content', `Processing content with indent level ${indentLevel}`)
674+
const closingIndent = baseIndent // Keep closing brace aligned with opening
681675

682676
const props = processObjectProperties(content, state, indentLevel)
683677
if (!props.length)
684678
return '{}'
685679

686680
const propertyStrings = props.map(({ key, value }) => {
687-
// debugLog(state, 'infer-complex-prop', `Processing property ${key}`)
688681
return `${propIndent}${key}: ${value}`
689682
})
690683

691-
return `{\n${propertyStrings.join(';\n')}\n${baseIndent}}`
684+
// Format the object with consistent indentation
685+
return `{\n${propertyStrings.join(';\n')}\n${closingIndent}}`
692686
}
693687

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

0 commit comments

Comments
 (0)