Skip to content

Commit be4fe59

Browse files
committed
chore: wip
chore: wip
1 parent 62f0bc9 commit be4fe59

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

fixtures/output/variable.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ export declare const someObject: {
3030
Array<
3131
{
3232
key: 'value'
33-
}
34-
> |
33+
} |
34+
> |
3535
Array<
3636
{
3737
key2: 'value2'
3838
} |
3939
'test' |
4040
1000
41-
> |
41+
> |
4242
Array<'some string' | unknown | unknown>
4343
>;
4444
someObject: {
@@ -56,10 +56,10 @@ export declare const someObject: {
5656
someNestedObjectArray: Array<
5757
{
5858
key: 'value'
59-
} |
59+
} |
6060
{
6161
key2: 'value2'
62-
}
62+
}
6363
>;
6464
someOtherObject: unknown;
6565
someInlineCall2: unknown;

src/extract.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,20 @@ function indentMultilineType(type: string, baseIndent: string, isLast: boolean):
559559
// Calculate proper indentation based on nesting level
560560
const stackDepth = bracketStack.reduce((depth, info) => depth + info.depth, 0)
561561
currentIndent = baseIndent + ' '.repeat(stackDepth)
562+
563+
// Dedent closing tokens
564+
if ((trimmed === '}' || trimmed === '>' || trimmed.startsWith('> |')) && bracketStack.length > 0) {
565+
// For array closings (>), also dedent if it's part of a closing sequence
566+
if (trimmed === '>' || trimmed.startsWith('> |')) {
567+
const lastBracket = bracketStack[bracketStack.length - 1]
568+
if (lastBracket?.isArray) {
569+
currentIndent = baseIndent + ' '.repeat(Math.max(0, stackDepth - 1))
570+
}
571+
}
572+
else {
573+
currentIndent = baseIndent + ' '.repeat(Math.max(0, stackDepth - 1))
574+
}
575+
}
562576
}
563577

564578
// Handle opening brackets with Array context
@@ -584,8 +598,15 @@ function indentMultilineType(type: string, baseIndent: string, isLast: boolean):
584598
}
585599

586600
// Add union operator for non-last lines
587-
const needsUnion = !isLast && i === lines.length - 1 && !trimmed.endsWith(' |') && !trimmed.endsWith(';')
588-
return `${currentIndent}${trimmed}${needsUnion ? ' |' : ''}`
601+
let needsUnion = !isLast && i === lines.length - 1 && !trimmed.endsWith(' |') && !trimmed.endsWith(';')
602+
const hasUnion = (trimmed === '}' || trimmed === '>') && !isLast && i !== lines.length - 1
603+
604+
// Don't add union if it's already part of the trimmed content (like '> |')
605+
if (trimmed.endsWith(' |')) {
606+
needsUnion = false
607+
}
608+
609+
return `${currentIndent}${trimmed}${needsUnion ? ' |' : hasUnion ? ' |' : ''}`
589610
}).filter(Boolean)
590611

591612
return formattedLines.join('\n')
@@ -625,10 +646,8 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
625646
debugLog(state, 'infer-array-value', `Input value:\n${value}`)
626647

627648
const content = value.slice(1, -1).trim()
628-
if (!content) {
629-
debugLog(state, 'infer-array-empty', 'Empty array content')
649+
if (!content)
630650
return 'unknown[]'
631-
}
632651

633652
const baseIndent = ' '.repeat(indentLevel)
634653
debugLog(state, 'infer-array-indent', `Base indent="${baseIndent}"`)
@@ -684,8 +703,6 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
684703

685704
if (needsMultiline) {
686705
debugLog(state, 'multiline-start', `Starting multiline formatting with ${types.length} types`)
687-
688-
// Instead of manual formatting, use indentMultilineType
689706
const formattedContent = types.map((type, i) => {
690707
const isLast = i === types.length - 1
691708
return indentMultilineType(type, `${baseIndent} `, isLast)
@@ -708,15 +725,17 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe
708725
return 'Record<string, unknown>'
709726

710727
const baseIndent = ' '.repeat(indentLevel)
711-
debugLog(state, 'infer-complex-content', `Processing content: ${content.substring(0, 100)}...`)
728+
const propIndent = ' '.repeat(indentLevel + 1)
729+
730+
debugLog(state, 'infer-complex-content', `Processing content with indent level ${indentLevel}`)
712731

713732
const props = processObjectProperties(content, state, indentLevel)
714733
if (!props.length)
715734
return '{}'
716735

717736
const propertyStrings = props.map(({ key, value }) => {
718-
debugLog(state, 'infer-complex-prop', `Processing property ${key}: ${value.substring(0, 50)}...`)
719-
return `${baseIndent} ${key}: ${value}`
737+
debugLog(state, 'infer-complex-prop', `Processing property ${key}`)
738+
return `${propIndent}${key}: ${value}`
720739
})
721740

722741
return `{\n${propertyStrings.join(';\n')}\n${baseIndent}}`

0 commit comments

Comments
 (0)