Skip to content

Commit 6456687

Browse files
committed
Move getOverloadIndex() into Collector.ts
1 parent 742e977 commit 6456687

2 files changed

Lines changed: 46 additions & 39 deletions

File tree

apps/api-extractor/src/collector/Collector.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ export class Collector {
8282
private readonly _dtsTypeReferenceDirectives: Set<string> = new Set<string>();
8383
private readonly _dtsLibReferenceDirectives: Set<string> = new Set<string>();
8484

85+
// Used by getOverloadIndex()
86+
private readonly _cachedOverloadIndexesByDeclaration: Map<AstDeclaration, number>;
87+
8588
public constructor(options: ICollectorOptions) {
8689
this.packageJsonLookup = new PackageJsonLookup();
8790

@@ -119,6 +122,8 @@ export class Collector {
119122
this.astSymbolTable = new AstSymbolTable(this.program, this.typeChecker, this.packageJsonLookup,
120123
bundledPackageNames, this.messageRouter);
121124
this.astReferenceResolver = new AstReferenceResolver(this);
125+
126+
this._cachedOverloadIndexesByDeclaration = new Map<AstDeclaration, number>();
122127
}
123128

124129
/**
@@ -306,6 +311,40 @@ export class Collector {
306311
return parts.join('');
307312
}
308313

314+
/**
315+
* For function-like signatures, this returns the TSDoc "overload index" which can be used to identify
316+
* a specific overload.
317+
*/
318+
public getOverloadIndex(astDeclaration: AstDeclaration): number {
319+
const allDeclarations: ReadonlyArray<AstDeclaration> = astDeclaration.astSymbol.astDeclarations;
320+
if (allDeclarations.length === 1) {
321+
return 1; // trivial case
322+
}
323+
324+
let overloadIndex: number | undefined = this._cachedOverloadIndexesByDeclaration.get(astDeclaration);
325+
326+
if (overloadIndex === undefined) {
327+
// TSDoc index selectors are positive integers counting from 1
328+
let nextIndex: number = 1;
329+
for (const other of allDeclarations) {
330+
// Filter out other declarations that are not overloads. For example, an overloaded function can also
331+
// be a namespace.
332+
if (other.declaration.kind === astDeclaration.declaration.kind) {
333+
this._cachedOverloadIndexesByDeclaration.set(other, nextIndex);
334+
++nextIndex;
335+
}
336+
}
337+
overloadIndex = this._cachedOverloadIndexesByDeclaration.get(astDeclaration);
338+
}
339+
340+
if (overloadIndex === undefined) {
341+
// This should never happen
342+
throw new Error('Error calculating overload index for declaration');
343+
}
344+
345+
return overloadIndex;
346+
}
347+
309348
private _createCollectorEntity(astEntity: AstEntity, exportedName: string | undefined): void {
310349
let entity: CollectorEntity | undefined = this._entitiesByAstEntity.get(astEntity);
311350

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

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,11 @@ import { DeclarationMetadata } from '../collector/DeclarationMetadata';
4242

4343
export class ApiModelGenerator {
4444
private readonly _collector: Collector;
45-
private readonly _cachedOverloadIndexesByDeclaration: Map<AstDeclaration, number>;
4645
private readonly _apiModel: ApiModel;
4746
private readonly _referenceGenerator: DeclarationReferenceGenerator;
4847

4948
public constructor(collector: Collector) {
5049
this._collector = collector;
51-
this._cachedOverloadIndexesByDeclaration = new Map<AstDeclaration, number>();
5250
this._apiModel = new ApiModel();
5351
this._referenceGenerator = new DeclarationReferenceGenerator(
5452
collector.packageJsonLookup,
@@ -193,7 +191,7 @@ export class ApiModelGenerator {
193191
private _processApiCallSignature(astDeclaration: AstDeclaration, exportedName: string | undefined,
194192
parentApiItem: ApiItemContainerMixin): void {
195193

196-
const overloadIndex: number = this._getOverloadIndex(astDeclaration);
194+
const overloadIndex: number = this._collector.getOverloadIndex(astDeclaration);
197195
const containerKey: string = ApiCallSignature.getContainerKey(overloadIndex);
198196

199197
let apiCallSignature: ApiCallSignature | undefined = parentApiItem.tryGetMemberByKey(containerKey) as
@@ -234,7 +232,7 @@ export class ApiModelGenerator {
234232
private _processApiConstructor(astDeclaration: AstDeclaration, exportedName: string | undefined,
235233
parentApiItem: ApiItemContainerMixin): void {
236234

237-
const overloadIndex: number = this._getOverloadIndex(astDeclaration);
235+
const overloadIndex: number = this._collector.getOverloadIndex(astDeclaration);
238236
const containerKey: string = ApiConstructor.getContainerKey(overloadIndex);
239237

240238
let apiConstructor: ApiConstructor | undefined = parentApiItem.tryGetMemberByKey(containerKey) as ApiConstructor;
@@ -322,7 +320,7 @@ export class ApiModelGenerator {
322320
private _processApiConstructSignature(astDeclaration: AstDeclaration, exportedName: string | undefined,
323321
parentApiItem: ApiItemContainerMixin): void {
324322

325-
const overloadIndex: number = this._getOverloadIndex(astDeclaration);
323+
const overloadIndex: number = this._collector.getOverloadIndex(astDeclaration);
326324
const containerKey: string = ApiConstructSignature.getContainerKey(overloadIndex);
327325

328326
let apiConstructSignature: ApiConstructSignature | undefined = parentApiItem.tryGetMemberByKey(containerKey) as
@@ -420,7 +418,7 @@ export class ApiModelGenerator {
420418

421419
const name: string = exportedName ? exportedName : astDeclaration.astSymbol.localName;
422420

423-
const overloadIndex: number = this._getOverloadIndex(astDeclaration);
421+
const overloadIndex: number = this._collector.getOverloadIndex(astDeclaration);
424422
const containerKey: string = ApiFunction.getContainerKey(name, overloadIndex);
425423

426424
let apiFunction: ApiFunction | undefined = parentApiItem.tryGetMemberByKey(containerKey) as
@@ -466,7 +464,7 @@ export class ApiModelGenerator {
466464
private _processApiIndexSignature(astDeclaration: AstDeclaration, exportedName: string | undefined,
467465
parentApiItem: ApiItemContainerMixin): void {
468466

469-
const overloadIndex: number = this._getOverloadIndex(astDeclaration);
467+
const overloadIndex: number = this._collector.getOverloadIndex(astDeclaration);
470468
const containerKey: string = ApiIndexSignature.getContainerKey(overloadIndex);
471469

472470
let apiIndexSignature: ApiIndexSignature | undefined = parentApiItem.tryGetMemberByKey(containerKey) as
@@ -554,7 +552,7 @@ export class ApiModelGenerator {
554552
const name: string = exportedName ? exportedName : astDeclaration.astSymbol.localName;
555553

556554
const isStatic: boolean = (astDeclaration.modifierFlags & ts.ModifierFlags.Static) !== 0;
557-
const overloadIndex: number = this._getOverloadIndex(astDeclaration);
555+
const overloadIndex: number = this._collector.getOverloadIndex(astDeclaration);
558556
const containerKey: string = ApiMethod.getContainerKey(name, isStatic, overloadIndex);
559557

560558
let apiMethod: ApiMethod | undefined = parentApiItem.tryGetMemberByKey(containerKey) as ApiMethod;
@@ -601,7 +599,7 @@ export class ApiModelGenerator {
601599

602600
const name: string = exportedName ? exportedName : astDeclaration.astSymbol.localName;
603601

604-
const overloadIndex: number = this._getOverloadIndex(astDeclaration);
602+
const overloadIndex: number = this._collector.getOverloadIndex(astDeclaration);
605603
const containerKey: string = ApiMethodSignature.getContainerKey(name, overloadIndex);
606604

607605
let apiMethodSignature: ApiMethodSignature | undefined = parentApiItem.tryGetMemberByKey(containerKey) as
@@ -857,34 +855,4 @@ export class ApiModelGenerator {
857855
}
858856
return parameters;
859857
}
860-
861-
private _getOverloadIndex(astDeclaration: AstDeclaration): number {
862-
const allDeclarations: ReadonlyArray<AstDeclaration> = astDeclaration.astSymbol.astDeclarations;
863-
if (allDeclarations.length === 1) {
864-
return 1; // trivial case
865-
}
866-
867-
let overloadIndex: number | undefined = this._cachedOverloadIndexesByDeclaration.get(astDeclaration);
868-
869-
if (overloadIndex === undefined) {
870-
// TSDoc index selectors are positive integers counting from 1
871-
let nextIndex: number = 1;
872-
for (const other of allDeclarations) {
873-
// Filter out other declarations that are not overloads. For example, an overloaded function can also
874-
// be a namespace.
875-
if (other.declaration.kind === astDeclaration.declaration.kind) {
876-
this._cachedOverloadIndexesByDeclaration.set(other, nextIndex);
877-
++nextIndex;
878-
}
879-
}
880-
overloadIndex = this._cachedOverloadIndexesByDeclaration.get(astDeclaration);
881-
}
882-
883-
if (overloadIndex === undefined) {
884-
// This should never happen
885-
throw new Error('Error calculating overload index for declaration');
886-
}
887-
888-
return overloadIndex;
889-
}
890858
}

0 commit comments

Comments
 (0)