From be4fe590f5dee72f0b63cada957e2223ee164d37 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 1 Nov 2024 16:56:46 +0100 Subject: [PATCH] chore: wip chore: wip --- fixtures/output/variable.d.ts | 10 ++++----- src/extract.ts | 39 ++++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/fixtures/output/variable.d.ts b/fixtures/output/variable.d.ts index 6d0cf0b..9cbc82c 100644 --- a/fixtures/output/variable.d.ts +++ b/fixtures/output/variable.d.ts @@ -30,15 +30,15 @@ export declare const someObject: { Array< { key: 'value' - } - > | + } | + > | Array< { key2: 'value2' } | 'test' | 1000 - > | + > | Array<'some string' | unknown | unknown> >; someObject: { @@ -56,10 +56,10 @@ export declare const someObject: { someNestedObjectArray: Array< { key: 'value' - } | + } | { key2: 'value2' - } + } >; someOtherObject: unknown; someInlineCall2: unknown; diff --git a/src/extract.ts b/src/extract.ts index 509a642..6ed05e4 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -559,6 +559,20 @@ function indentMultilineType(type: string, baseIndent: string, isLast: boolean): // Calculate proper indentation based on nesting level const stackDepth = bracketStack.reduce((depth, info) => depth + info.depth, 0) currentIndent = baseIndent + ' '.repeat(stackDepth) + + // Dedent closing tokens + if ((trimmed === '}' || trimmed === '>' || trimmed.startsWith('> |')) && bracketStack.length > 0) { + // For array closings (>), also dedent if it's part of a closing sequence + if (trimmed === '>' || trimmed.startsWith('> |')) { + const lastBracket = bracketStack[bracketStack.length - 1] + if (lastBracket?.isArray) { + currentIndent = baseIndent + ' '.repeat(Math.max(0, stackDepth - 1)) + } + } + else { + currentIndent = baseIndent + ' '.repeat(Math.max(0, stackDepth - 1)) + } + } } // Handle opening brackets with Array context @@ -584,8 +598,15 @@ function indentMultilineType(type: string, baseIndent: string, isLast: boolean): } // Add union operator for non-last lines - const needsUnion = !isLast && i === lines.length - 1 && !trimmed.endsWith(' |') && !trimmed.endsWith(';') - return `${currentIndent}${trimmed}${needsUnion ? ' |' : ''}` + let needsUnion = !isLast && i === lines.length - 1 && !trimmed.endsWith(' |') && !trimmed.endsWith(';') + const hasUnion = (trimmed === '}' || trimmed === '>') && !isLast && i !== lines.length - 1 + + // Don't add union if it's already part of the trimmed content (like '> |') + if (trimmed.endsWith(' |')) { + needsUnion = false + } + + return `${currentIndent}${trimmed}${needsUnion ? ' |' : hasUnion ? ' |' : ''}` }).filter(Boolean) return formattedLines.join('\n') @@ -625,10 +646,8 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0) debugLog(state, 'infer-array-value', `Input value:\n${value}`) const content = value.slice(1, -1).trim() - if (!content) { - debugLog(state, 'infer-array-empty', 'Empty array content') + if (!content) return 'unknown[]' - } const baseIndent = ' '.repeat(indentLevel) debugLog(state, 'infer-array-indent', `Base indent="${baseIndent}"`) @@ -684,8 +703,6 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0) if (needsMultiline) { debugLog(state, 'multiline-start', `Starting multiline formatting with ${types.length} types`) - - // Instead of manual formatting, use indentMultilineType const formattedContent = types.map((type, i) => { const isLast = i === types.length - 1 return indentMultilineType(type, `${baseIndent} `, isLast) @@ -708,15 +725,17 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe return 'Record' const baseIndent = ' '.repeat(indentLevel) - debugLog(state, 'infer-complex-content', `Processing content: ${content.substring(0, 100)}...`) + const propIndent = ' '.repeat(indentLevel + 1) + + debugLog(state, 'infer-complex-content', `Processing content with indent level ${indentLevel}`) const props = processObjectProperties(content, state, indentLevel) if (!props.length) return '{}' const propertyStrings = props.map(({ key, value }) => { - debugLog(state, 'infer-complex-prop', `Processing property ${key}: ${value.substring(0, 50)}...`) - return `${baseIndent} ${key}: ${value}` + debugLog(state, 'infer-complex-prop', `Processing property ${key}`) + return `${propIndent}${key}: ${value}` }) return `{\n${propertyStrings.join(';\n')}\n${baseIndent}}`