Skip to content

Commit 503d404

Browse files
committed
Add support for matching dotted names such as "Office.MailboxEnums.ItemType"
1 parent ba3cff8 commit 503d404

File tree

1 file changed

+65
-28
lines changed

1 file changed

+65
-28
lines changed

apps/api-documenter/src/yaml/YamlDocumenter.ts

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,34 +54,7 @@ export class YamlDocumenter {
5454
this._docItemSet = docItemSet;
5555
this._docItemsByTypeName = new Map<string, DocItem>();
5656

57-
// Collect the _docItemsByTypeName table
58-
const ambiguousNames: Set<string> = new Set<string>();
59-
60-
this._docItemSet.forEach((docItem: DocItem) => {
61-
switch (docItem.kind) {
62-
case DocItemKind.Class:
63-
case DocItemKind.Enum:
64-
case DocItemKind.Interface:
65-
const typeName: string = docItem.name;
66-
if (ambiguousNames.has(typeName)) {
67-
break;
68-
}
69-
70-
if (this._docItemsByTypeName.has(typeName)) {
71-
// We saw this name before, so it's an ambiguous match
72-
ambiguousNames.add(typeName);
73-
break;
74-
}
75-
76-
this._docItemsByTypeName.set(typeName, docItem);
77-
break;
78-
}
79-
});
80-
81-
// Remove the ambiguous matches
82-
for (const ambiguousName of ambiguousNames) {
83-
this._docItemsByTypeName.delete(ambiguousName);
84-
}
57+
this._initDocItemsByTypeName();
8558
}
8659

8760
public generateFiles(outputFolder: string): void { // virtual
@@ -488,6 +461,53 @@ export class YamlDocumenter {
488461
return result;
489462
}
490463

464+
/**
465+
* Initialize the _docItemsByTypeName() data structure.
466+
*/
467+
private _initDocItemsByTypeName(): void {
468+
// Collect the _docItemsByTypeName table
469+
const ambiguousNames: Set<string> = new Set<string>();
470+
471+
this._docItemSet.forEach((docItem: DocItem) => {
472+
switch (docItem.kind) {
473+
case DocItemKind.Class:
474+
case DocItemKind.Enum:
475+
case DocItemKind.Interface:
476+
// Attempt to register both the fully qualified name and the short name
477+
const namesForType: string[] = [docItem.name];
478+
479+
// Note that nameWithDot cannot conflict with docItem.name (because docItem.name
480+
// cannot contain a dot)
481+
const nameWithDot: string | undefined = this._getTypeNameWithDot(docItem);
482+
if (nameWithDot) {
483+
namesForType.push(nameWithDot);
484+
}
485+
486+
// Register all names
487+
for (const typeName of namesForType) {
488+
if (ambiguousNames.has(typeName)) {
489+
break;
490+
}
491+
492+
if (this._docItemsByTypeName.has(typeName)) {
493+
// We saw this name before, so it's an ambiguous match
494+
ambiguousNames.add(typeName);
495+
break;
496+
}
497+
498+
this._docItemsByTypeName.set(typeName, docItem);
499+
}
500+
501+
break;
502+
}
503+
});
504+
505+
// Remove the ambiguous matches
506+
for (const ambiguousName of ambiguousNames) {
507+
this._docItemsByTypeName.delete(ambiguousName);
508+
}
509+
}
510+
491511
/**
492512
* This is a temporary workaround to enable limited autolinking of API item types
493513
* until the YAML file format is enhanced to support general hyperlinks.
@@ -499,6 +519,7 @@ export class YamlDocumenter {
499519
* it is given #2 but substitutes #1 if the name can be matched to a DocItem.
500520
*/
501521
private _linkToUidIfPossible(typeName: string): string {
522+
// Note that typeName might be a _getTypeNameWithDot() name or it might be a simple class name
502523
const docItem: DocItem | undefined = this._docItemsByTypeName.get(typeName.trim());
503524
if (docItem) {
504525
// Substitute the UID
@@ -507,6 +528,22 @@ export class YamlDocumenter {
507528
return typeName;
508529
}
509530

531+
/**
532+
* If the docItem represents a scoped name such as "my-library:MyNamespace.MyClass",
533+
* this returns a string such as "MyNamespace.MyClass". If the result would not
534+
* have at least one dot in it, then undefined is returned.
535+
*/
536+
private _getTypeNameWithDot(docItem: DocItem): string | undefined {
537+
const hierarchy: DocItem[] = docItem.getHierarchy();
538+
if (hierarchy.length > 0 && hierarchy[0].kind === DocItemKind.Package) {
539+
hierarchy.shift(); // ignore the package qualifier
540+
}
541+
if (hierarchy.length < 1) {
542+
return undefined;
543+
}
544+
return hierarchy.map(x => x.name).join('.');
545+
}
546+
510547
private _getYamlItemName(docItem: DocItem): string {
511548
if (docItem.parent && docItem.parent.kind === DocItemKind.Namespace) {
512549
// For members a namespace, show the full name excluding the package part:

0 commit comments

Comments
 (0)