Skip to content

Commit

Permalink
added optional function params
Browse files Browse the repository at this point in the history
  • Loading branch information
Myles Murphy authored and Myles Murphy committed Jun 30, 2024
1 parent 6145c61 commit d705120
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions packages/vscode-extension/src/stringify-type-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,21 @@ export function stringifyTypeTree (typeTree: TypeTree, anonymousFunction = true)

const signatures = typeTree.signatures.map(s => {
const { parameters, returnType } = s
return `(${parameters.map(p => `${p.isRestParameter ? '...' : ''}${p.name}: ${stringifyTypeTree(p.type)}`).join(', ')})${returnTypeChar} ${stringifyTypeTree(returnType)}`
const parametersString = parameters.map(p => {
let optional = ''
if (p.type.kind === 'union') {
const hasUndefined = p.type.types.some(t => t.kind === 'basic' && t.typeName === 'undefined')
if (hasUndefined) {
optional = '?'
}
// Remove undefined from union
p.type.types = p.type.types.filter(t => t.kind !== 'basic' || t.typeName !== 'undefined')
}

return `${p.isRestParameter ? '...' : ''}${p.name}${optional}: ${stringifyTypeTree(p.type)}`
}).join(', ')

return `(${parametersString})${returnTypeChar} ${stringifyTypeTree(returnType)}`
})

// If there are multiple signatures, wrap them in braces with semi-colons at the end of each line
Expand Down Expand Up @@ -162,9 +176,9 @@ export function prettyPrintTypeString (typeStringInput: string, indentation = 2)
line = line.trim()

// Replace : with ?: if line contains undefined union
if (line.includes(':') && (line.includes(' | undefined') || line.includes('undefined | '))) {
line = line.replace(':', '?:').replace(' | undefined', '').replace('undefined | ', '').replace('??', '?')
}
// if (line.includes(':') && (line.includes(' | undefined') || line.includes('undefined | '))) {
// line = line.replace(':', '?:').replace(' | undefined', '').replace('undefined | ', '').replace('??', '?')
// }

// Replace true/false with boolean
line = line.replace('false | true', 'boolean')
Expand Down

0 comments on commit d705120

Please sign in to comment.