Skip to content

Commit

Permalink
Merge pull request #4513 from bartvandenende-wm/bartvandenende-wm/api…
Browse files Browse the repository at this point in the history
…-extractor-arrow-func

[api-extractor] Classify arrow functions as function kind in the doc model exports.
  • Loading branch information
iclanton authored Feb 6, 2024
2 parents 8752399 + 80bc088 commit c88b970
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 22 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._tryFindFunctionDeclaration(astDeclaration);
if (functionDeclaration) {
this._processApiFunction(astDeclaration, context, functionDeclaration);
} else {
this._processApiVariable(astDeclaration, context);
}
break;

default:
// ignore unknown types
}
}

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, {
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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit c88b970

Please sign in to comment.