Skip to content

Commit

Permalink
feat: process arrow functions as function instead of variable kin…
Browse files Browse the repository at this point in the history
…d in ApiModelGenerator
  • Loading branch information
bartvandenende-wm committed Feb 6, 2024
1 parent fe1cbf4 commit a0502af
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions apps/api-extractor/src/generators/ApiModelGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,28 @@ export class ApiModelGenerator {
break;

case ts.SyntaxKind.VariableDeclaration:
this._processApiVariable(astDeclaration, context);
// check for arrow functions in variable declaration
const functionDeclaration: ts.FunctionDeclaration | undefined =
this._hasFunctionDeclaration(astDeclaration);
if (functionDeclaration) {
this._processApiFunction(astDeclaration, context, functionDeclaration);
} else {
this._processApiVariable(astDeclaration, context);
}
break;

default:
// ignore unknown types
}
}

private _hasFunctionDeclaration(astDeclaration: AstDeclaration): ts.FunctionDeclaration | undefined {
const children: ts.Node[] = astDeclaration.declaration.getChildren(
astDeclaration.declaration.getSourceFile()
);
return children.find(ts.isFunctionTypeNode) as ts.FunctionDeclaration | undefined;
}

private _processChildDeclarations(astDeclaration: AstDeclaration, context: IProcessAstEntityContext): void {
for (const childDeclaration of astDeclaration.children) {
this._processDeclaration(childDeclaration, {
Expand Down Expand Up @@ -544,7 +558,11 @@ export class ApiModelGenerator {
}
}

private _processApiFunction(astDeclaration: AstDeclaration, context: IProcessAstEntityContext): void {
private _processApiFunction(
astDeclaration: AstDeclaration,
context: IProcessAstEntityContext,
altFunctionDeclaration?: ts.FunctionDeclaration
): void {
const { name, isExported, parentApiItem } = context;

const overloadIndex: number = this._collector.getOverloadIndex(astDeclaration);
Expand All @@ -554,7 +572,7 @@ export class ApiModelGenerator {

if (apiFunction === undefined) {
const functionDeclaration: ts.FunctionDeclaration =
astDeclaration.declaration as ts.FunctionDeclaration;
altFunctionDeclaration ?? (astDeclaration.declaration as ts.FunctionDeclaration);

const nodesToCapture: IExcerptBuilderNodeToCapture[] = [];

Expand Down

0 comments on commit a0502af

Please sign in to comment.