Skip to content

Commit d44ecab

Browse files
committed
First pass
1 parent 8031cd4 commit d44ecab

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed

.changeset/popular-beans-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/api-documenter': minor
3+
---
4+
5+
Add an option to sort functions by first param. (--sort-functions)

repo-scripts/api-documenter/src/cli/MarkdownAction.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import { ApiDocumenterCommandLine } from './ApiDocumenterCommandLine';
2222
import { BaseAction } from './BaseAction';
2323
import { MarkdownDocumenter } from '../documenters/MarkdownDocumenter';
24+
import { CommandLineFlagParameter } from '@rushstack/ts-command-line';
2425

2526
export class MarkdownAction extends BaseAction {
27+
private _shouldSortFunctions!: CommandLineFlagParameter;
2628
public constructor(parser: ApiDocumenterCommandLine) {
2729
super({
2830
actionName: 'markdown',
@@ -33,10 +35,21 @@ export class MarkdownAction extends BaseAction {
3335
});
3436
}
3537

38+
protected onDefineParameters(): void {
39+
super.onDefineParameters();
40+
41+
this._shouldSortFunctions = this.defineFlagParameter({
42+
parameterLongName: '--sort-functions',
43+
description:
44+
`Sorts functions tables and listings by first parameter.`
45+
});
46+
}
47+
3648
protected async onExecute(): Promise<void> {
3749
// override
3850
const { apiModel, outputFolder, addFileNameSuffix, projectName } =
3951
this.buildApiModel();
52+
const shouldSortFunctions: boolean = this._shouldSortFunctions.value;
4053

4154
if (!projectName) {
4255
throw new Error('No project name provided. Use --project.');
@@ -47,7 +60,8 @@ export class MarkdownAction extends BaseAction {
4760
documenterConfig: undefined,
4861
outputFolder,
4962
addFileNameSuffix,
50-
projectName
63+
projectName,
64+
shouldSortFunctions
5165
});
5266
markdownDocumenter.generateFiles();
5367
}

repo-scripts/api-documenter/src/documenters/MarkdownDocumenter.ts

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export interface IMarkdownDocumenterOptions {
9393
outputFolder: string;
9494
addFileNameSuffix: boolean;
9595
projectName: string;
96+
shouldSortFunctions: boolean;
9697
}
9798

9899
/**
@@ -108,13 +109,15 @@ export class MarkdownDocumenter {
108109
private readonly _pluginLoader: PluginLoader;
109110
private readonly _addFileNameSuffix: boolean;
110111
private readonly _projectName: string;
112+
private readonly _shouldSortFunctions: boolean;
111113

112114
public constructor(options: IMarkdownDocumenterOptions) {
113115
this._apiModel = options.apiModel;
114116
this._documenterConfig = options.documenterConfig;
115117
this._outputFolder = options.outputFolder;
116118
this._addFileNameSuffix = options.addFileNameSuffix;
117119
this._projectName = options.projectName;
120+
this._shouldSortFunctions = options.shouldSortFunctions;
118121
this._tsdocConfiguration = CustomDocNodes.configuration;
119122
this._markdownEmitter = new CustomMarkdownEmitter(this._apiModel);
120123

@@ -834,11 +837,13 @@ page_type: reference
834837
headerTitles: ['Enumeration', 'Description']
835838
});
836839

837-
const functionsTable: DocTable = new DocTable({
840+
const finalFunctionsTable: DocTable = new DocTable({
838841
configuration,
839842
headerTitles: ['Function', 'Description']
840843
});
841844

845+
const functionsRowGroup: Record<string, DocTableRow[]> = {};
846+
842847
const interfacesTable: DocTable = new DocTable({
843848
configuration,
844849
headerTitles: ['Interface', 'Description']
@@ -859,7 +864,8 @@ page_type: reference
859864
headerTitles: ['Type Alias', 'Description']
860865
});
861866

862-
const functionsDefinitions: DocNode[] = [];
867+
const functionsDefinitionsGroup: Record<string, DocNode[]> = {};
868+
const finalFunctionsDefinitions: DocNode[] = [];
863869
const variablesDefinitions: DocNode[] = [];
864870
const typeAliasDefinitions: DocNode[] = [];
865871
const enumsDefinitions: DocNode[] = [];
@@ -899,10 +905,24 @@ page_type: reference
899905
break;
900906

901907
case ApiItemKind.Function:
902-
functionsTable.addRow(row);
903-
functionsDefinitions.push(
904-
...this._createCompleteOutputForApiItem(apiMember)
905-
);
908+
if (this._shouldSortFunctions) {
909+
const firstParam = (apiMember as ApiParameterListMixin).parameters[0] || { name : '' };
910+
if (!functionsRowGroup[firstParam.name]) {
911+
functionsRowGroup[firstParam.name] = [];
912+
}
913+
if (!functionsDefinitionsGroup[firstParam.name]) {
914+
functionsDefinitionsGroup[firstParam.name] = [];
915+
}
916+
functionsRowGroup[firstParam.name].push(row);
917+
functionsDefinitionsGroup[firstParam.name].push(
918+
...this._createCompleteOutputForApiItem(apiMember)
919+
);
920+
} else {
921+
finalFunctionsTable.addRow(row);
922+
finalFunctionsDefinitions.push(
923+
...this._createCompleteOutputForApiItem(apiMember)
924+
);
925+
}
906926
break;
907927

908928
case ApiItemKind.TypeAlias:
@@ -921,9 +941,30 @@ page_type: reference
921941
}
922942
}
923943

924-
if (functionsTable.rows.length > 0) {
944+
if (this._shouldSortFunctions) {
945+
const sortedFunctionsFirstParamKeys = Object.keys(functionsRowGroup).sort((a, b) => {
946+
if (a === 'app') {
947+
return -1;
948+
}
949+
return (a.localeCompare(b));
950+
});
951+
952+
for (const paramKey of sortedFunctionsFirstParamKeys) {
953+
if (finalFunctionsTable.rows.length > 0) {
954+
finalFunctionsTable.createAndAddRow();
955+
}
956+
for (const functionsRow of functionsRowGroup[paramKey]) {
957+
finalFunctionsTable.addRow(functionsRow);
958+
}
959+
for (const functionDefinition of functionsDefinitionsGroup[paramKey]) {
960+
finalFunctionsDefinitions.push(functionDefinition);
961+
}
962+
}
963+
}
964+
965+
if (finalFunctionsTable.rows.length > 0) {
925966
output.push(new DocHeading({ configuration, title: 'Functions' }));
926-
output.push(functionsTable);
967+
output.push(finalFunctionsTable);
927968
}
928969

929970
if (classesTable.rows.length > 0) {
@@ -956,8 +997,8 @@ page_type: reference
956997
output.push(typeAliasesTable);
957998
}
958999

959-
if (functionsDefinitions.length > 0) {
960-
output.push(...functionsDefinitions);
1000+
if (finalFunctionsDefinitions.length > 0) {
1001+
output.push(...finalFunctionsDefinitions);
9611002
}
9621003

9631004
if (variablesDefinitions.length > 0) {

scripts/docgen/docgen.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ async function generateDocs(
181181
'--output',
182182
outputFolder,
183183
'--project',
184-
'js'
184+
'js',
185+
'--sort-functions'
185186
],
186187
{ stdio: 'inherit' }
187188
);

0 commit comments

Comments
 (0)