Skip to content

Commit a0502af

Browse files
feat: process arrow functions as function instead of variable kind in ApiModelGenerator
1 parent fe1cbf4 commit a0502af

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

apps/api-extractor/src/generators/ApiModelGenerator.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,28 @@ export class ApiModelGenerator {
250250
break;
251251

252252
case ts.SyntaxKind.VariableDeclaration:
253-
this._processApiVariable(astDeclaration, context);
253+
// check for arrow functions in variable declaration
254+
const functionDeclaration: ts.FunctionDeclaration | undefined =
255+
this._hasFunctionDeclaration(astDeclaration);
256+
if (functionDeclaration) {
257+
this._processApiFunction(astDeclaration, context, functionDeclaration);
258+
} else {
259+
this._processApiVariable(astDeclaration, context);
260+
}
254261
break;
255262

256263
default:
257264
// ignore unknown types
258265
}
259266
}
260267

268+
private _hasFunctionDeclaration(astDeclaration: AstDeclaration): ts.FunctionDeclaration | undefined {
269+
const children: ts.Node[] = astDeclaration.declaration.getChildren(
270+
astDeclaration.declaration.getSourceFile()
271+
);
272+
return children.find(ts.isFunctionTypeNode) as ts.FunctionDeclaration | undefined;
273+
}
274+
261275
private _processChildDeclarations(astDeclaration: AstDeclaration, context: IProcessAstEntityContext): void {
262276
for (const childDeclaration of astDeclaration.children) {
263277
this._processDeclaration(childDeclaration, {
@@ -544,7 +558,11 @@ export class ApiModelGenerator {
544558
}
545559
}
546560

547-
private _processApiFunction(astDeclaration: AstDeclaration, context: IProcessAstEntityContext): void {
561+
private _processApiFunction(
562+
astDeclaration: AstDeclaration,
563+
context: IProcessAstEntityContext,
564+
altFunctionDeclaration?: ts.FunctionDeclaration
565+
): void {
548566
const { name, isExported, parentApiItem } = context;
549567

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

555573
if (apiFunction === undefined) {
556574
const functionDeclaration: ts.FunctionDeclaration =
557-
astDeclaration.declaration as ts.FunctionDeclaration;
575+
altFunctionDeclaration ?? (astDeclaration.declaration as ts.FunctionDeclaration);
558576

559577
const nodesToCapture: IExcerptBuilderNodeToCapture[] = [];
560578

0 commit comments

Comments
 (0)