Skip to content

Commit

Permalink
parse arrow functions with explicit types correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
luvkapur committed Nov 3, 2022
1 parent 5989588 commit 08fc162
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,34 @@ export class VariableDeclaration implements SchemaTransformer {
const displaySig = info?.body?.displayString || '';
const location = context.getLocation(varDec);
const doc = await context.jsDocToDocSchema(varDec);
const modifiers = varDec.modifiers?.map((modifier) => modifier.getText()) || [];
if (varDec.initializer?.kind === ts.SyntaxKind.ArrowFunction) {
const args = await getParams((varDec.initializer as ArrowFunction).parameters, context);
// example => export const useLanesContext: () => LanesContextModel | undefined = () => {
if (varDec.type) {
const funcType = await context.resolveType(varDec, '');
if (isFunctionLike(funcType)) {
return new FunctionLikeSchema(
location,
name,
funcType.params,
funcType.returnType,
displaySig,
modifiers as Modifier[],
doc
);
}
}
const typeStr = parseReturnTypeFromQuickInfo(info);
const returnType = await context.resolveType(varDec, typeStr);
const modifiers = varDec.modifiers?.map((modifier) => modifier.getText()) || [];
return new FunctionLikeSchema(location, name, args, returnType, displaySig, modifiers as Modifier[], doc);
}
const typeStr = parseTypeFromQuickInfo(info);
const type = await context.resolveType(varDec, typeStr);
return new VariableLikeSchema(location, name, displaySig, type, false, doc);
}
}

function isFunctionLike(node: SchemaNode): node is FunctionLikeSchema {
return node instanceof FunctionLikeSchema;
}

0 comments on commit 08fc162

Please sign in to comment.