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 5ca36da commit cfdf372
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 114 deletions.
120 changes: 60 additions & 60 deletions fixtures/output/variable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,55 @@ export declare const someObject: {
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'
>;
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>
>;
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<4 | 5 | 6 | 7 | 8 | 9 | 10>
>;
Array<'some text' | 2 | unknown | (() => void) | unknown> |
Array<4 | 5 | 6 | 7 | 8 | 9 | 10>
>;
someComplexArray: Array<
Array<
{
key: 'value'
}
> |
Array<
{
key2: 'value2'
} |
'test' |
1000
> |
Array<'some string' | unknown | unknown>
>;
Array<
{
key: 'value'
}
> |
Array<
{
key2: 'value2'
} |
'test' |
1000
> |
Array<'some string' | unknown | unknown>
>;
someObject: {
key: 'value'
};
key: 'value'
};
someNestedObject: {
key: {
nestedKey: 'value'
};
otherKey: {
nestedKey: unknown;
nestedKey2: () => void
}
};
key: {
nestedKey: 'value'
};
otherKey: {
nestedKey: unknown;
nestedKey2: () => void
}
};
someNestedObjectArray: Array<
{
key: 'value'
} |
{
key2: 'value2'
}
>;
{
key: 'value'
} |
{
key2: 'value2'
}
>;
someOtherObject: unknown;
someInlineCall2: unknown;
someInlineCall3: unknown
Expand All @@ -72,27 +72,27 @@ 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<true | Array<false | Array<true>>>
>;
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)
>
unknown |
unknown |
((...args: any[]) => unknown) |
((...args: any[]) => unknown)
>
};
export declare const complexObject: {
handlers: {
onSuccess: <T> (data: T) => Promise<void>;
onError: (error: Error & { code?: number }) => never
};
onSuccess: <T> (data: T) => Promise<void>;
onError: (error: Error & { code?: number }) => never
};
utils: {
formatters: {
date: (input: Date) => string;
currency: (amount: number, currency: string) => string
}
}
formatters: {
date: (input: Date) => string;
currency: (amount: number, currency: string) => string
}
}
};
63 changes: 9 additions & 54 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
}

// Process each element
const elementTypes = elements.map((element) => {
const elementTypes = elements.map((element, index) => {
const trimmed = element.trim()

// Handle nested arrays
Expand Down Expand Up @@ -626,23 +626,23 @@ function inferArrayType(value: string, state?: ProcessingState, indentLevel = 0)
if (needsMultiline) {
const formattedTypes = types.map((type, index) => {
const isLast = index === types.length - 1
// For types that contain newlines, maintain their internal formatting
// For types that contain newlines, ensure proper indentation
if (type.includes('\n')) {
const lines = type.split('\n')
const formattedLines = lines.map((line, i) => {
// First line gets element indentation
if (i === 0)
return line
// Increment indentation for nested lines
const currentIndent = line.match(/^\s*/)[0]
const additionalIndent = ' '.repeat(indentLevel + 1)
return `${additionalIndent}${line.trimLeft()}`
// Other lines get additional indentation
return `${elementIndent}${line.trimLeft()}`
}).join('\n')
return `${elementIndent}${formattedLines}${isLast ? '' : ' |'}`
}
// For single-line types
return `${elementIndent}${type}${isLast ? '' : ' |'}`
})

// Join lines and ensure closing bracket aligns with parent
return `Array<\n${formattedTypes.join('\n')}\n${baseIndent}>`
}

Expand All @@ -661,7 +661,6 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe

const baseIndent = ' '.repeat(indentLevel)
const propIndent = ' '.repeat(indentLevel + 1)
const innerIndent = ' '.repeat(indentLevel + 2)

const props = processObjectProperties(content, state)
if (!props.length)
Expand All @@ -672,16 +671,18 @@ function inferComplexObjectType(value: string, state?: ProcessingState, indentLe

// Handle multiline values (like objects and arrays)
if (value.includes('\n')) {
// Add one level of indentation to each line after the first
const indentedValue = value
.split('\n')
.map((line, i) => i === 0 ? line : `${innerIndent}${line.trimLeft()}`)
.map((line, i) => i === 0 ? line : `${propIndent}${line.trim()}`)
.join('\n')
return `${propIndent}${formattedKey}: ${indentedValue}`
}

return `${propIndent}${formattedKey}: ${value}`
})

// Ensure closing brace aligns with parent
return `{\n${propertyStrings.join(';\n')}\n${baseIndent}}`
}

Expand Down Expand Up @@ -723,34 +724,6 @@ function inferConstArrayType(value: string, state?: ProcessingState): string {
return 'unknown'
}

function inferReturnType(value: string): string {
// Remove comments and whitespace
const cleanValue = value.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '').trim()

// Handle Promise returns
if (cleanValue.includes('Promise.') || cleanValue.includes('async'))
return 'Promise<unknown>'

// Handle void returns
if (cleanValue.includes('console.') || cleanValue.includes('void'))
return 'void'

// Handle never returns
if (cleanValue.includes('throw '))
return 'never'

// Handle string returns
if (cleanValue.includes('.toString') || cleanValue.includes('.toISOString'))
return 'string'

// Handle specific known return types
if (cleanValue.includes('new Intl.NumberFormat'))
return 'string'

// Default to unknown
return 'unknown'
}

function inferTypeFromDefaultValue(defaultValue: string): string {
// Handle string literals
if (/^['"`].*['"`]$/.test(defaultValue)) {
Expand Down Expand Up @@ -1395,24 +1368,6 @@ function processObjectMethod(declaration: string, value: string, state?: Process
return { name, signature }
}

/**
* Process a collection of object methods
*/
function processObjectMethods(methods: Array<{ key: string, value: string }>, state?: ProcessingState, indentLevel = 0): string {
debugLog(state, 'process-methods', `Processing ${methods.length} methods`)

if (methods.length === 0)
return '{}'

const indent = ' '.repeat(indentLevel)
const processedMethods = methods.map(({ key, value }) => {
const { name, signature } = processObjectMethod(key, value, state)
return `${indent}${name}: ${signature}`
})

return `{\n${processedMethods.join(';\n')}\n${indent}}`
}

function processObjectProperties(content: string, state?: ProcessingState): Array<{ key: string, value: string }> {
debugLog(state, 'process-props', `Processing properties from content length ${content.length}`)
const properties: Array<{ key: string, value: string }> = []
Expand Down

0 comments on commit cfdf372

Please sign in to comment.