Skip to content

Commit 6e980e4

Browse files
committed
Set up the new ApiItem.canonicalReference property
1 parent 83dfeda commit 6e980e4

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

apps/api-extractor-model/src/items/ApiItem.ts

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4+
import { DeclarationReference } from '@microsoft/tsdoc/lib/beta/DeclarationReference';
45
import { Constructor, PropertiesOf } from '../mixins/Mixin';
56
import { ApiPackage } from '../model/ApiPackage';
67
import { ApiParameterListMixin } from '../mixins/ApiParameterListMixin';
78
import { DeserializerContext } from '../model/DeserializerContext';
9+
import { InternalError } from '@microsoft/node-core-library';
810

911
/**
1012
* The type returned by the {@link ApiItem.kind} property, which can be used to easily distinguish subclasses of
@@ -46,12 +48,10 @@ export interface IApiItemJson {
4648
kind: ApiItemKind;
4749
}
4850

49-
/**
50-
* PRIVATE
51-
* Allows ApiItemContainerMixin to assign the parent.
52-
*/
51+
// PRIVATE - Allows ApiItemContainerMixin to assign the parent.
52+
//
5353
// tslint:disable-next-line:variable-name
54-
export const ApiItem_parent: unique symbol = Symbol('ApiItem._parent');
54+
export const ApiItem_setParent: unique symbol = Symbol('ApiItem._setParent');
5555

5656
/**
5757
* The abstract base class for all members of an `ApiModel` object.
@@ -62,7 +62,8 @@ export const ApiItem_parent: unique symbol = Symbol('ApiItem._parent');
6262
* @public
6363
*/
6464
export class ApiItem {
65-
public [ApiItem_parent]: ApiItem | undefined;
65+
private _canonicalReference: DeclarationReference | undefined;
66+
private _parent: ApiItem | undefined;
6667

6768
public static deserialize(jsonObject: IApiItemJson, context: DeserializerContext): ApiItem {
6869
// The Deserializer class is coupled with a ton of other classes, so we delay loading it
@@ -94,6 +95,22 @@ export class ApiItem {
9495
throw new Error('ApiItem.kind was not implemented by the child class');
9596
}
9697

98+
/**
99+
* Warning: This API is used internally by API extractor but is not yet ready for general usage.
100+
*
101+
* @remarks
102+
*
103+
* Returns a `DeclarationReference` object using the experimental new declaration reference notation.
104+
*
105+
* @beta
106+
*/
107+
public get canonicalReference(): DeclarationReference {
108+
if (!this._canonicalReference) {
109+
this._canonicalReference = this.buildCanonicalReference();
110+
}
111+
return this._canonicalReference;
112+
}
113+
97114
/**
98115
* Returns a string key that can be used to efficiently retrieve an `ApiItem` from an `ApiItemContainerMixin`.
99116
* The key is unique within the container. Its format is undocumented and may change at any time.
@@ -105,7 +122,7 @@ export class ApiItem {
105122
* @virtual
106123
*/
107124
public get containerKey(): string {
108-
throw new Error('ApiItem.containerKey was not implemented by the child class');
125+
throw new InternalError('ApiItem.containerKey was not implemented by the child class');
109126
}
110127

111128
/**
@@ -135,7 +152,7 @@ export class ApiItem {
135152
* @virtual
136153
*/
137154
public get parent(): ApiItem | undefined {
138-
return this[ApiItem_parent];
155+
return this._parent;
139156
}
140157

141158
/**
@@ -215,6 +232,27 @@ export class ApiItem {
215232
public getSortKey(): string {
216233
return this.containerKey;
217234
}
235+
236+
/**
237+
* PRIVATE
238+
*
239+
* @privateRemarks
240+
* Allows ApiItemContainerMixin to assign the parent.
241+
*
242+
* @internal
243+
*/
244+
public [ApiItem_setParent](parent: ApiItem | undefined): void {
245+
this._parent = parent;
246+
this._canonicalReference = undefined;
247+
}
248+
249+
/**
250+
* Builds the cached object used by the `canonicalReference` property.
251+
* @virtual
252+
*/
253+
protected buildCanonicalReference(): DeclarationReference {
254+
throw new InternalError('ApiItem.canonicalReference was not implemented by the child class');
255+
}
218256
}
219257

220258
/**

apps/api-extractor-model/src/mixins/ApiItemContainerMixin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.s
33

4-
import { ApiItem, ApiItem_parent, IApiItemJson, IApiItemOptions, IApiItemConstructor } from '../items/ApiItem';
4+
import { ApiItem, ApiItem_setParent, IApiItemJson, IApiItemOptions, IApiItemConstructor } from '../items/ApiItem';
55
import { ApiNameMixin } from './ApiNameMixin';
66
import { DeserializerContext } from '../model/DeserializerContext';
77

@@ -135,7 +135,7 @@ export function ApiItemContainerMixin<TBaseClass extends IApiItemConstructor>(ba
135135
throw new Error('Another member has already been added with the same name and containerKey');
136136
}
137137

138-
const existingParent: ApiItem | undefined = member[ApiItem_parent];
138+
const existingParent: ApiItem | undefined = member.parent;
139139
if (existingParent !== undefined) {
140140
throw new Error(`This item has already been added to another container: "${existingParent.displayName}"`);
141141
}
@@ -145,7 +145,7 @@ export function ApiItemContainerMixin<TBaseClass extends IApiItemConstructor>(ba
145145
this[_membersSorted] = false;
146146
this[_membersByContainerKey].set(member.containerKey, member);
147147

148-
member[ApiItem_parent] = this;
148+
member[ApiItem_setParent](this);
149149
}
150150

151151
public tryGetMemberByKey(containerKey: string): ApiItem | undefined {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
```ts
66

7+
import { DeclarationReference } from '@microsoft/tsdoc/lib/beta/DeclarationReference';
78
import { DocDeclarationReference } from '@microsoft/tsdoc';
89
import { IJsonFileSaveOptions } from '@microsoft/node-core-library';
910
import * as tsdoc from '@microsoft/tsdoc';
@@ -211,10 +212,14 @@ export class ApiInterface extends ApiInterface_base {
211212

212213
// @public
213214
export class ApiItem {
214-
// (undocumented)
215-
[ApiItem_parent]: ApiItem | undefined;
215+
// @internal
216+
[ApiItem_setParent](parent: ApiItem | undefined): void;
216217
constructor(options: IApiItemOptions);
217218
// @virtual
219+
protected buildCanonicalReference(): DeclarationReference;
220+
// @beta
221+
readonly canonicalReference: DeclarationReference;
222+
// @virtual
218223
readonly containerKey: string;
219224
// (undocumented)
220225
static deserialize(jsonObject: IApiItemJson, context: DeserializerContext): ApiItem;

0 commit comments

Comments
 (0)