X Tutup
Skip to content

Commit 8df88d6

Browse files
committed
Add AstDeclaration.findChildrenWithName()
1 parent e54a8b8 commit 8df88d6

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

apps/api-extractor/src/analyzer/AstDeclaration.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ export class AstDeclaration {
5353
*/
5454
public metadata: unknown;
5555

56+
// NOTE: This array becomes immutable after astSymbol.analyze() sets astSymbol.analyzed=true
5657
private readonly _analyzedChildren: AstDeclaration[] = [];
5758

5859
private readonly _analyzedReferencedAstEntitiesSet: Set<AstEntity> = new Set<AstEntity>();
5960

61+
// Reverse lookup used by findChildrenWithName()
62+
private _childrenByName: Map<string, AstDeclaration[]> | undefined = undefined;
63+
6064
public constructor(options: IAstDeclarationOptions) {
6165
this.declaration = options.declaration;
6266
this.astSymbol = options.astSymbol;
@@ -182,6 +186,40 @@ export class AstDeclaration {
182186
}
183187
}
184188

189+
/**
190+
* Returns the list of child declarations whose `AstSymbol.localName` matches the provided `name`.
191+
*
192+
* @remarks
193+
* This is an efficient O(1) lookup.
194+
*/
195+
public findChildrenWithName(name: string): ReadonlyArray<AstDeclaration> {
196+
// The children property returns:
197+
//
198+
// return this.astSymbol.analyzed ? this._analyzedChildren : [];
199+
//
200+
if (!this.astSymbol.analyzed || this._analyzedChildren.length === 0) {
201+
return [];
202+
}
203+
204+
if (this._childrenByName === undefined) {
205+
// Build the lookup table
206+
const childrenByName: Map<string, AstDeclaration[]> = new Map<string, AstDeclaration[]>();
207+
208+
for (const child of this._analyzedChildren) {
209+
const childName: string = child.astSymbol.localName;
210+
let array: AstDeclaration[] | undefined = childrenByName.get(childName);
211+
if (array === undefined) {
212+
array = [];
213+
childrenByName.set(childName, array);
214+
}
215+
array.push(child);
216+
}
217+
this._childrenByName = childrenByName;
218+
}
219+
220+
return this._childrenByName.get(name) || [];
221+
}
222+
185223
/**
186224
* This function determines which ts.Node kinds will generate an AstDeclaration.
187225
* These correspond to the definitions that we can add AEDoc to.

0 commit comments

Comments
 (0)
X Tutup