diff --git a/apps/api-extractor/src/generators/ApiModelGenerator.ts b/apps/api-extractor/src/generators/ApiModelGenerator.ts index 06eb2828fbe..9dec797310b 100644 --- a/apps/api-extractor/src/generators/ApiModelGenerator.ts +++ b/apps/api-extractor/src/generators/ApiModelGenerator.ts @@ -250,7 +250,14 @@ 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._tryFindFunctionDeclaration(astDeclaration); + if (functionDeclaration) { + this._processApiFunction(astDeclaration, context, functionDeclaration); + } else { + this._processApiVariable(astDeclaration, context); + } break; default: @@ -258,6 +265,13 @@ export class ApiModelGenerator { } } + private _tryFindFunctionDeclaration(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, { @@ -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); @@ -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[] = []; diff --git a/build-tests/api-extractor-scenarios/etc/defaultExportOfEntryPoint2/api-extractor-scenarios.api.json b/build-tests/api-extractor-scenarios/etc/defaultExportOfEntryPoint2/api-extractor-scenarios.api.json index d5b2da9ff7c..b09660a4142 100644 --- a/build-tests/api-extractor-scenarios/etc/defaultExportOfEntryPoint2/api-extractor-scenarios.api.json +++ b/build-tests/api-extractor-scenarios/etc/defaultExportOfEntryPoint2/api-extractor-scenarios.api.json @@ -173,27 +173,28 @@ "preserveMemberOrder": false, "members": [ { - "kind": "Variable", - "canonicalReference": "api-extractor-scenarios!defaultFunctionStatement:var", + "kind": "Function", + "canonicalReference": "api-extractor-scenarios!defaultFunctionStatement:function(1)", "docComment": "/**\n * @public\n */\n", "excerptTokens": [ { "kind": "Content", - "text": "defaultFunctionStatement: " + "text": "defaultFunctionStatement: () => " }, { "kind": "Content", - "text": "() => void" + "text": "void" } ], "fileUrlPath": "src/defaultExportOfEntryPoint2/index.ts", - "isReadonly": true, - "releaseTag": "Public", - "name": "defaultFunctionStatement", - "variableTypeTokenRange": { + "returnTypeTokenRange": { "startIndex": 1, "endIndex": 2 - } + }, + "releaseTag": "Public", + "overloadIndex": 1, + "parameters": [], + "name": "defaultFunctionStatement" } ] } diff --git a/build-tests/api-extractor-scenarios/etc/spanSorting/api-extractor-scenarios.api.json b/build-tests/api-extractor-scenarios/etc/spanSorting/api-extractor-scenarios.api.json index 5315cb39238..6b0f70e844d 100644 --- a/build-tests/api-extractor-scenarios/etc/spanSorting/api-extractor-scenarios.api.json +++ b/build-tests/api-extractor-scenarios/etc/spanSorting/api-extractor-scenarios.api.json @@ -375,27 +375,45 @@ "implementsTokenRanges": [] }, { - "kind": "Variable", - "canonicalReference": "api-extractor-scenarios!exampleD:var", + "kind": "Function", + "canonicalReference": "api-extractor-scenarios!exampleD:function(1)", "docComment": "/**\n * Outer description\n *\n * @public\n */\n", "excerptTokens": [ { "kind": "Content", - "text": "exampleD: " + "text": "exampleD: (o: " }, { "kind": "Content", - "text": "(o: {\n a: number;\n b(): string;\n}) => void" + "text": "{\n a: number;\n b(): string;\n}" + }, + { + "kind": "Content", + "text": ") => " + }, + { + "kind": "Content", + "text": "void" } ], "fileUrlPath": "src/spanSorting/index.ts", - "isReadonly": true, + "returnTypeTokenRange": { + "startIndex": 3, + "endIndex": 4 + }, "releaseTag": "Public", - "name": "exampleD", - "variableTypeTokenRange": { - "startIndex": 1, - "endIndex": 2 - } + "overloadIndex": 1, + "parameters": [ + { + "parameterName": "o", + "parameterTypeTokenRange": { + "startIndex": 1, + "endIndex": 2 + }, + "isOptional": false + } + ], + "name": "exampleD" } ] } diff --git a/common/changes/@microsoft/api-extractor/bartvandenende-wm-api-extractor-arrow-func_2024-02-06-14-12.json b/common/changes/@microsoft/api-extractor/bartvandenende-wm-api-extractor-arrow-func_2024-02-06-14-12.json new file mode 100644 index 00000000000..dfd9d41b279 --- /dev/null +++ b/common/changes/@microsoft/api-extractor/bartvandenende-wm-api-extractor-arrow-func_2024-02-06-14-12.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@microsoft/api-extractor", + "comment": "Classify arrow functions as `function` kind in the doc model export.", + "type": "minor" + } + ], + "packageName": "@microsoft/api-extractor" +} \ No newline at end of file