Skip to content

Commit 86674a2

Browse files
committed
Remove yaml references for type params and toJSON/fromJSON from ExcerptToken
1 parent 33f8d0d commit 86674a2

File tree

3 files changed

+58
-85
lines changed

3 files changed

+58
-85
lines changed

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

Lines changed: 57 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const yamlApiSchema: JsonSchema = JsonSchema.fromFile(path.join(__dirname, '..',
6363
interface IYamlReferences {
6464
references: IYamlReference[];
6565
typeNameToUid: Map<string, string>;
66+
uidTypeReferenceCounters: Map<string, number>;
6667
}
6768

6869
/**
@@ -73,17 +74,14 @@ export class YamlDocumenter {
7374
private readonly _markdownEmitter: CustomMarkdownEmitter;
7475

7576
private _apiItemsByCanonicalReference: Map<string, ApiItem>;
76-
private _knownTypeParameters: Set<string> | undefined;
7777
private _yamlReferences: IYamlReferences | undefined;
78-
private _uidTypeReferenceCounters: Map<string, number>;
7978

8079
private _outputFolder: string;
8180

8281
public constructor(apiModel: ApiModel) {
8382
this._apiModel = apiModel;
8483
this._markdownEmitter = new CustomMarkdownEmitter(this._apiModel);
8584
this._apiItemsByCanonicalReference = new Map<string, ApiItem>();
86-
this._uidTypeReferenceCounters = new Map<string, number>();
8785

8886
this._initApiItems();
8987
}
@@ -118,94 +116,78 @@ export class YamlDocumenter {
118116
}
119117

120118
private _visitApiItems(apiItem: ApiDocumentedItem, parentYamlFile: IYamlApiFile | undefined): boolean {
121-
const savedKnownTypeParameters: Set<string> | undefined = this._knownTypeParameters;
122-
try {
123-
// Track type parameters declared by a declaration so that we do not resolve them
124-
// when looking up types in _linkToUidIfPossible()
125-
if (ApiTypeParameterListMixin.isBaseClassOf(apiItem)) {
126-
this._knownTypeParameters = savedKnownTypeParameters
127-
? new Set(savedKnownTypeParameters)
128-
: new Set();
129-
for (const typeParameter of apiItem.typeParameters) {
130-
this._knownTypeParameters.add(typeParameter.name);
131-
}
132-
}
119+
const yamlItem: IYamlItem | undefined = this._generateYamlItem(apiItem);
120+
if (!yamlItem) {
121+
return false;
122+
}
133123

134-
const yamlItem: IYamlItem | undefined = this._generateYamlItem(apiItem);
135-
if (!yamlItem) {
136-
return false;
137-
}
124+
this.onCustomizeYamlItem(yamlItem);
138125

139-
this.onCustomizeYamlItem(yamlItem);
126+
if (this._shouldEmbed(apiItem.kind)) {
127+
if (!parentYamlFile) {
128+
throw new InternalError('Missing file context');
129+
}
130+
parentYamlFile.items.push(yamlItem);
131+
} else {
132+
const newYamlFile: IYamlApiFile = {
133+
items: []
134+
};
135+
newYamlFile.items.push(yamlItem);
140136

141-
if (this._shouldEmbed(apiItem.kind)) {
142-
if (!parentYamlFile) {
143-
throw new InternalError('Missing file context');
144-
}
145-
parentYamlFile.items.push(yamlItem);
137+
let children: ReadonlyArray<ApiItem>;
138+
if (apiItem.kind === ApiItemKind.Package) {
139+
// Skip over the entry point, since it's not part of the documentation hierarchy
140+
children = apiItem.members[0].members;
146141
} else {
147-
const newYamlFile: IYamlApiFile = {
148-
items: []
149-
};
150-
newYamlFile.items.push(yamlItem);
151-
152-
let children: ReadonlyArray<ApiItem>;
153-
if (apiItem.kind === ApiItemKind.Package) {
154-
// Skip over the entry point, since it's not part of the documentation hierarchy
155-
children = apiItem.members[0].members;
156-
} else {
157-
children = apiItem.members;
158-
}
142+
children = apiItem.members;
143+
}
159144

160-
const flattenedChildren: ApiItem[] = this._flattenNamespaces(children);
145+
const flattenedChildren: ApiItem[] = this._flattenNamespaces(children);
161146

162-
for (const child of flattenedChildren) {
163-
if (child instanceof ApiDocumentedItem) {
164-
if (this._visitApiItems(child, newYamlFile)) {
165-
if (!yamlItem.children) {
166-
yamlItem.children = [];
167-
}
168-
yamlItem.children.push(this._getUid(child));
147+
for (const child of flattenedChildren) {
148+
if (child instanceof ApiDocumentedItem) {
149+
if (this._visitApiItems(child, newYamlFile)) {
150+
if (!yamlItem.children) {
151+
yamlItem.children = [];
169152
}
153+
yamlItem.children.push(this._getUid(child));
170154
}
171155
}
156+
}
172157

173-
if (this._yamlReferences) {
174-
if (this._yamlReferences.references.length > 0) {
175-
if (newYamlFile.references) {
176-
newYamlFile.references = [...newYamlFile.references, ...this._yamlReferences.references];
177-
} else {
178-
newYamlFile.references = this._yamlReferences.references;
179-
}
158+
if (this._yamlReferences) {
159+
if (this._yamlReferences.references.length > 0) {
160+
if (newYamlFile.references) {
161+
newYamlFile.references = [...newYamlFile.references, ...this._yamlReferences.references];
162+
} else {
163+
newYamlFile.references = this._yamlReferences.references;
180164
}
181-
this._yamlReferences = undefined;
182165
}
166+
this._yamlReferences = undefined;
167+
}
183168

184-
const yamlFilePath: string = this._getYamlFilePath(apiItem);
169+
const yamlFilePath: string = this._getYamlFilePath(apiItem);
185170

186-
if (apiItem.kind === ApiItemKind.Package) {
187-
console.log('Writing ' + yamlFilePath);
188-
}
171+
if (apiItem.kind === ApiItemKind.Package) {
172+
console.log('Writing ' + yamlFilePath);
173+
}
189174

190-
this._writeYamlFile(newYamlFile, yamlFilePath, 'UniversalReference', yamlApiSchema);
175+
this._writeYamlFile(newYamlFile, yamlFilePath, 'UniversalReference', yamlApiSchema);
191176

192-
if (parentYamlFile) {
193-
if (!parentYamlFile.references) {
194-
parentYamlFile.references = [];
195-
}
177+
if (parentYamlFile) {
178+
if (!parentYamlFile.references) {
179+
parentYamlFile.references = [];
180+
}
196181

197-
parentYamlFile.references.push({
198-
uid: this._getUid(apiItem),
199-
name: this._getYamlItemName(apiItem)
200-
});
182+
parentYamlFile.references.push({
183+
uid: this._getUid(apiItem),
184+
name: this._getYamlItemName(apiItem)
185+
});
201186

202-
}
203187
}
204-
205-
return true;
206-
} finally {
207-
this._knownTypeParameters = savedKnownTypeParameters;
208188
}
189+
190+
return true;
209191
}
210192

211193
// Since the YAML schema does not yet support nested namespaces, we simply omit them from
@@ -636,7 +618,8 @@ export class YamlDocumenter {
636618
if (!this._yamlReferences) {
637619
this._yamlReferences = {
638620
references: [],
639-
typeNameToUid: new Map()
621+
typeNameToUid: new Map(),
622+
uidTypeReferenceCounters: new Map()
640623
};
641624
}
642625
return this._yamlReferences;
@@ -662,11 +645,6 @@ export class YamlDocumenter {
662645

663646
const typeName: string = typeExcerpt.text.trim();
664647

665-
// Record a reference to a type parameter as its name, so as not to resolve to a conflicting name
666-
if (this._knownTypeParameters && this._knownTypeParameters.has(typeName)) {
667-
return this._recordYamlReference(this._ensureYamlReferences(), typeName, typeName);
668-
}
669-
670648
// If there are no references to be used for a complex type, return the type name.
671649
if (!excerptTokens.some(tok => tok.kind === ExcerptTokenKind.Reference && !!tok.canonicalReference)) {
672650
return typeName;
@@ -700,8 +678,8 @@ export class YamlDocumenter {
700678

701679
// Keep track of the count for the base uid (without meaning or overload index) to ensure
702680
// that each complex type reference is unique.
703-
const counter: number = this._uidTypeReferenceCounters.get(baseUid) || 0;
704-
this._uidTypeReferenceCounters.set(baseUid, counter + 1);
681+
const counter: number = yamlReferences.uidTypeReferenceCounters.get(baseUid) || 0;
682+
yamlReferences.uidTypeReferenceCounters.set(baseUid, counter + 1);
705683

706684
const uid: string = contextUid
707685
.addNavigationStep(Navigation.Locals, `${counter}`)

build-tests/api-documenter-test/etc/yaml/api-documenter-test/idocinterface6.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ references:
112112
fullName: IDocInterface1
113113
- name: '[]'
114114
fullName: '[]'
115-
- uid: T
116115
- uid: 'api-documenter-test!IDocInterface6#intersectionProperty~0:complex'
117116
name: IDocInterface1 & IDocInterface2
118117
fullName: IDocInterface1 & IDocInterface2

common/reviews/api/api-extractor-model.api.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,14 +617,10 @@ export class ExcerptToken {
617617
// (undocumented)
618618
readonly canonicalReference: DeclarationReference | undefined;
619619
// (undocumented)
620-
static fromJSON(object: IExcerptToken): ExcerptToken;
621-
// (undocumented)
622620
readonly kind: ExcerptTokenKind;
623621
// (undocumented)
624622
readonly text: string;
625-
// (undocumented)
626-
toJSON(): IExcerptToken;
627-
}
623+
}
628624

629625
// @public (undocumented)
630626
export const enum ExcerptTokenKind {

0 commit comments

Comments
 (0)