From a0502af016550fe3774a4f38095237e24dd48d88 Mon Sep 17 00:00:00 2001 From: Bart van den Ende Date: Tue, 6 Feb 2024 14:36:11 +0100 Subject: [PATCH 1/4] feat: process arrow functions as `function` instead of `variable` kind in ApiModelGenerator --- .../src/generators/ApiModelGenerator.ts | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/apps/api-extractor/src/generators/ApiModelGenerator.ts b/apps/api-extractor/src/generators/ApiModelGenerator.ts index 06eb2828fbe..c7acc09410f 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._hasFunctionDeclaration(astDeclaration); + if (functionDeclaration) { + this._processApiFunction(astDeclaration, context, functionDeclaration); + } else { + this._processApiVariable(astDeclaration, context); + } break; default: @@ -258,6 +265,13 @@ export class ApiModelGenerator { } } + 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, { @@ -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[] = []; From 371ada87abd0bb4e9e3de75aa164e2077e591f27 Mon Sep 17 00:00:00 2001 From: Bart van den Ende Date: Tue, 6 Feb 2024 15:13:43 +0100 Subject: [PATCH 2/4] chore add rush change --- ...e-wm-api-extractor-arrow-func_2024-02-06-14-12.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 common/changes/@microsoft/api-extractor/bartvandenende-wm-api-extractor-arrow-func_2024-02-06-14-12.json 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..d10a4adaedd --- /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 From af3bfcf7d28c3bb69de5ff1fff4c6561c91fdac6 Mon Sep 17 00:00:00 2001 From: Bart van den Ende Date: Tue, 6 Feb 2024 16:48:03 +0100 Subject: [PATCH 3/4] chore: update test snapshots --- .../api-extractor-scenarios.api.json | 19 +++++----- .../api-extractor-scenarios.api.json | 38 ++++++++++++++----- 2 files changed, 38 insertions(+), 19 deletions(-) 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" } ] } From 80bc088757a310adb3fd0281e7da9c9d2db37fbc Mon Sep 17 00:00:00 2001 From: Bart van den Ende Date: Tue, 6 Feb 2024 19:52:21 +0100 Subject: [PATCH 4/4] chore: PR feedback --- apps/api-extractor/src/generators/ApiModelGenerator.ts | 4 ++-- ...ndenende-wm-api-extractor-arrow-func_2024-02-06-14-12.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/api-extractor/src/generators/ApiModelGenerator.ts b/apps/api-extractor/src/generators/ApiModelGenerator.ts index c7acc09410f..9dec797310b 100644 --- a/apps/api-extractor/src/generators/ApiModelGenerator.ts +++ b/apps/api-extractor/src/generators/ApiModelGenerator.ts @@ -252,7 +252,7 @@ export class ApiModelGenerator { case ts.SyntaxKind.VariableDeclaration: // check for arrow functions in variable declaration const functionDeclaration: ts.FunctionDeclaration | undefined = - this._hasFunctionDeclaration(astDeclaration); + this._tryFindFunctionDeclaration(astDeclaration); if (functionDeclaration) { this._processApiFunction(astDeclaration, context, functionDeclaration); } else { @@ -265,7 +265,7 @@ export class ApiModelGenerator { } } - private _hasFunctionDeclaration(astDeclaration: AstDeclaration): ts.FunctionDeclaration | undefined { + private _tryFindFunctionDeclaration(astDeclaration: AstDeclaration): ts.FunctionDeclaration | undefined { const children: ts.Node[] = astDeclaration.declaration.getChildren( astDeclaration.declaration.getSourceFile() ); 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 index d10a4adaedd..dfd9d41b279 100644 --- 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 @@ -2,7 +2,7 @@ "changes": [ { "packageName": "@microsoft/api-extractor", - "comment": "\"Classify arrow functions as 'function' kind in the doc model export\"", + "comment": "Classify arrow functions as `function` kind in the doc model export.", "type": "minor" } ],