Skip to content

Commit

Permalink
fix(api): check for optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 10, 2022
1 parent ad3113d commit 6aa937e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions packages/api/src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from "assert";
import ts, { createProgram, TypeChecker } from "typescript";
import { APIConfig } from "./config";
import { IndexInfo, SignatureInfo, SymbolInfo, TypeId, TypeInfo, TypeInfoNoId, TypeParameterInfo } from "./types";
import { getIndexInfos, getIntersectionTypesFlat, getSignaturesOfType, getSymbolType, getTypeId, TSIndexInfoMerged, isPureObject, wrapSafe, isArrayType, getTypeArguments, isTupleType, SignatureInternal, isFunctionParameterOptional } from "./util";
import { getIndexInfos, getIntersectionTypesFlat, getSignaturesOfType, getSymbolType, getTypeId, TSIndexInfoMerged, isPureObject, wrapSafe, isArrayType, getTypeArguments, isTupleType, SignatureInternal, isParameterOptional } from "./util";

const maxDepthExceeded: TypeInfo = {kind: 'max_depth', id: -1}

Expand Down Expand Up @@ -200,7 +200,7 @@ function _generateTypeTree({ symbol, type }: SymbolOrType, ctx: TypeTreeContext,

function getFunctionParameterInfo(parameter: ts.Symbol, signature: ts.Signature, index: number): TypeInfo {
return parseSymbol(parameter, {
optional: isFunctionParameterOptional(typeChecker, parameter, signature)
optional: isParameterOptional(typeChecker, parameter, signature)
})
}

Expand All @@ -215,12 +215,14 @@ function _generateTypeTree({ symbol, type }: SymbolOrType, ctx: TypeTreeContext,
}
}

function getSymbolInfo(symbol: ts.Symbol, isAnonymous: boolean = false, options?: TypeTreeOptions): SymbolInfo {
function getSymbolInfo(symbol: ts.Symbol, isAnonymous: boolean = false, options: TypeTreeOptions = {}): SymbolInfo {
let optional = options.optional ?? isParameterOptional(typeChecker, symbol)

return {
name: symbol.getName(),
flags: symbol.getFlags(),
...isAnonymous && { anonymous: true },
...options?.optional && { optional: true },
...optional && { optional: true },
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ export function getObjectFlags(type: ts.Type): number {
return (type.flags & ts.TypeFlags.Object) && (type as ObjectType).objectFlags
}

export function isFunctionParameterOptional(typeChecker: ts.TypeChecker, parameter: ts.Symbol, signature: ts.Signature) {
const parameterDeclaration = typeChecker.symbolToParameterDeclaration(parameter, signature.getDeclaration(), undefined)
export function isParameterOptional(typeChecker: ts.TypeChecker, parameter: ts.Symbol, signature?: ts.Signature) {
const parameterDeclaration = typeChecker.symbolToParameterDeclaration(parameter, signature?.getDeclaration(), undefined)
const baseParameterDeclaration = parameter.getDeclarations()?.find((x) => x.kind && ts.SyntaxKind.Parameter) as ts.ParameterDeclaration|undefined

if(parameterDeclaration) {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/consoleLog.tree

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/baselines/reference/function.tree
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ function func(a: string, b?: number) {
> a: string
> a --- {"kind":"primitive","primitive":"string","symbolMeta":{"name":"a","flags":1},"id":15}
> b?: number
> b --- {"kind":"primitive","primitive":"number","symbolMeta":{"name":"b","flags":1},"id":16}
> b --- {"kind":"primitive","primitive":"number","symbolMeta":{"name":"b","flags":1,"optional":true},"id":16}

0 comments on commit 6aa937e

Please sign in to comment.