Skip to content

Commit efaf045

Browse files
committed
Implement buildCanonicalReference() for each ApiItem subclass
1 parent 6e980e4 commit efaf045

21 files changed

Lines changed: 223 additions & 0 deletions

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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 { StringChecks } from '@microsoft/tsdoc/lib/parser/StringChecks';
45
import { ApiItem, IApiItemJson, IApiItemConstructor, IApiItemOptions } from '../items/ApiItem';
56
import { DeserializerContext } from '../model/DeserializerContext';
67

@@ -46,6 +47,9 @@ export interface ApiNameMixin extends ApiItem {
4647

4748
/** @override */
4849
serializeInto(jsonObject: Partial<IApiItemJson>): void;
50+
51+
/** @internal */
52+
_getCanonicalReferenceName(): string;
4953
}
5054

5155
/**
@@ -94,6 +98,17 @@ export function ApiNameMixin<TBaseClass extends IApiItemConstructor>(baseClass:
9498

9599
jsonObject.name = this.name;
96100
}
101+
102+
/** @internal */
103+
public _getCanonicalReferenceName(): string {
104+
// TODO: This is a temporary workaround for a limitation of the experimental DeclarationReference API.
105+
// We will remove this when the final implementation is in place.
106+
if (StringChecks.explainIfInvalidUnquotedIdentifier(this.name)) {
107+
return JSON.stringify(this.name);
108+
} else {
109+
return this.name;
110+
}
111+
}
97112
}
98113

99114
return MixedClass;

apps/api-extractor-model/src/model/ApiCallSignature.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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.
33

4+
import { DeclarationReference, Meaning, Navigation } from '@microsoft/tsdoc/lib/beta/DeclarationReference';
45
import { ApiItemKind } from '../items/ApiItem';
56
import { IApiDeclaredItemOptions, ApiDeclaredItem } from '../items/ApiDeclaredItem';
67
import { IApiParameterListMixinOptions, ApiParameterListMixin } from '../mixins/ApiParameterListMixin';
@@ -69,4 +70,12 @@ export class ApiCallSignature extends ApiTypeParameterListMixin(ApiParameterList
6970
public get containerKey(): string {
7071
return ApiCallSignature.getContainerKey(this.overloadIndex);
7172
}
73+
74+
/** @beta @override */
75+
public buildCanonicalReference(): DeclarationReference {
76+
return (this.parent ? this.parent.canonicalReference : DeclarationReference.empty())
77+
.addNavigationStep(Navigation.Members, '')
78+
.withMeaning(Meaning.Signature)
79+
.withOverloadIndex(this.overloadIndex);
80+
}
7281
}

apps/api-extractor-model/src/model/ApiClass.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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.
33

4+
import { DeclarationReference, Meaning, Navigation } from '@microsoft/tsdoc/lib/beta/DeclarationReference';
45
import { ApiItemKind } from '../items/ApiItem';
56
import { ApiDeclaredItem, IApiDeclaredItemOptions, IApiDeclaredItemJson } from '../items/ApiDeclaredItem';
67
import { ApiItemContainerMixin, IApiItemContainerMixinOptions } from '../mixins/ApiItemContainerMixin';
@@ -116,4 +117,11 @@ export class ApiClass extends ApiItemContainerMixin(ApiNameMixin(ApiTypeParamete
116117

117118
jsonObject.implementsTokenRanges = this.implementsTypes.map(x => x.excerpt.tokenRange);
118119
}
120+
121+
/** @beta @override */
122+
public buildCanonicalReference(): DeclarationReference {
123+
return (this.parent ? this.parent.canonicalReference : DeclarationReference.empty())
124+
.addNavigationStep(Navigation.Exports, this.name)
125+
.withMeaning(Meaning.Class);
126+
}
119127
}

apps/api-extractor-model/src/model/ApiConstructSignature.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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.
33

4+
import { DeclarationReference, Meaning, Navigation } from '@microsoft/tsdoc/lib/beta/DeclarationReference';
45
import { ApiItemKind } from '../items/ApiItem';
56
import { IApiDeclaredItemOptions, ApiDeclaredItem } from '../items/ApiDeclaredItem';
67
import { IApiParameterListMixinOptions, ApiParameterListMixin } from '../mixins/ApiParameterListMixin';
@@ -82,4 +83,12 @@ export class ApiConstructSignature extends ApiTypeParameterListMixin(ApiParamete
8283
public get containerKey(): string {
8384
return ApiConstructSignature.getContainerKey(this.overloadIndex);
8485
}
86+
87+
/** @beta @override */
88+
public buildCanonicalReference(): DeclarationReference {
89+
return (this.parent ? this.parent.canonicalReference : DeclarationReference.empty())
90+
.addNavigationStep(Navigation.Members, '')
91+
.withMeaning(Meaning.Signature)
92+
.withOverloadIndex(this.overloadIndex);
93+
}
8594
}

apps/api-extractor-model/src/model/ApiConstructor.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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.
33

4+
import { DeclarationReference, Meaning, Navigation } from '@microsoft/tsdoc/lib/beta/DeclarationReference';
45
import { ApiItemKind } from '../items/ApiItem';
56
import { IApiDeclaredItemOptions, ApiDeclaredItem } from '../items/ApiDeclaredItem';
67
import { IApiParameterListMixinOptions, ApiParameterListMixin } from '../mixins/ApiParameterListMixin';
@@ -62,4 +63,12 @@ export class ApiConstructor extends ApiParameterListMixin(ApiReleaseTagMixin(Api
6263
public get containerKey(): string {
6364
return ApiConstructor.getContainerKey(this.overloadIndex);
6465
}
66+
67+
/** @beta @override */
68+
public buildCanonicalReference(): DeclarationReference {
69+
return (this.parent ? this.parent.canonicalReference : DeclarationReference.empty())
70+
.addNavigationStep(Navigation.Members, '')
71+
.withMeaning(Meaning.Constructor)
72+
.withOverloadIndex(this.overloadIndex);
73+
}
6574
}

apps/api-extractor-model/src/model/ApiEntryPoint.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
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, ModuleSource } from '@microsoft/tsdoc/lib/beta/DeclarationReference';
45
import { ApiItem, ApiItemKind } from '../items/ApiItem';
56
import { ApiItemContainerMixin, IApiItemContainerMixinOptions } from '../mixins/ApiItemContainerMixin';
67
import { IApiNameMixinOptions, ApiNameMixin } from '../mixins/ApiNameMixin';
8+
import { ApiPackage } from './ApiPackage';
79

810
/**
911
* Constructor options for {@link ApiEntryPoint}.
@@ -51,4 +53,23 @@ export class ApiEntryPoint extends ApiItemContainerMixin(ApiNameMixin(ApiItem))
5153
// No prefix needed, because ApiEntryPoint is the only possible member of an ApiPackage
5254
return this.name;
5355
}
56+
57+
/** @beta @override */
58+
public buildCanonicalReference(): DeclarationReference {
59+
let modulePath: string = '';
60+
61+
if (this.parent instanceof ApiPackage) {
62+
modulePath = this.parent.name;
63+
}
64+
65+
if (this.name) {
66+
modulePath += this.name;
67+
}
68+
69+
if (modulePath) {
70+
return new DeclarationReference(new ModuleSource(modulePath));
71+
}
72+
73+
return DeclarationReference.empty();
74+
}
5475
}

apps/api-extractor-model/src/model/ApiEnum.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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.
33

4+
import { DeclarationReference, Meaning, Navigation } from '@microsoft/tsdoc/lib/beta/DeclarationReference';
45
import { ApiItemKind } from '../items/ApiItem';
56
import { ApiDeclaredItem, IApiDeclaredItemOptions } from '../items/ApiDeclaredItem';
67
import { ApiReleaseTagMixin, IApiReleaseTagMixinOptions } from '../mixins/ApiReleaseTagMixin';
@@ -71,4 +72,11 @@ export class ApiEnum extends ApiItemContainerMixin(ApiNameMixin(ApiReleaseTagMix
7172
}
7273
super.addMember(member);
7374
}
75+
76+
/** @beta @override */
77+
public buildCanonicalReference(): DeclarationReference {
78+
return (this.parent ? this.parent.canonicalReference : DeclarationReference.empty())
79+
.addNavigationStep(Navigation.Exports, this._getCanonicalReferenceName())
80+
.withMeaning(Meaning.Enum);
81+
}
7482
}

apps/api-extractor-model/src/model/ApiEnumMember.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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.
33

4+
import { DeclarationReference, Meaning, Navigation } from '@microsoft/tsdoc/lib/beta/DeclarationReference';
45
import { ApiItemKind } from '../items/ApiItem';
56
import { ApiDeclaredItem, IApiDeclaredItemOptions, IApiDeclaredItemJson } from '../items/ApiDeclaredItem';
67
import { ApiReleaseTagMixin, IApiReleaseTagMixinOptions } from '../mixins/ApiReleaseTagMixin';
@@ -86,4 +87,11 @@ export class ApiEnumMember extends ApiNameMixin(ApiReleaseTagMixin(ApiDeclaredIt
8687

8788
jsonObject.initializerTokenRange = this.initializerExcerpt.tokenRange;
8889
}
90+
91+
/** @beta @override */
92+
public buildCanonicalReference(): DeclarationReference {
93+
return (this.parent ? this.parent.canonicalReference : DeclarationReference.empty())
94+
.addNavigationStep(Navigation.Exports, this._getCanonicalReferenceName())
95+
.withMeaning(Meaning.EnumMember);
96+
}
8997
}

apps/api-extractor-model/src/model/ApiFunction.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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.
33

4+
import { DeclarationReference, Meaning, Navigation } from '@microsoft/tsdoc/lib/beta/DeclarationReference';
45
import { ApiItemKind } from '../items/ApiItem';
56
import { IApiDeclaredItemOptions, ApiDeclaredItem } from '../items/ApiDeclaredItem';
67
import { IApiParameterListMixinOptions, ApiParameterListMixin } from '../mixins/ApiParameterListMixin';
@@ -63,4 +64,12 @@ export class ApiFunction extends ApiNameMixin(ApiTypeParameterListMixin(ApiParam
6364
public get containerKey(): string {
6465
return ApiFunction.getContainerKey(this.name, this.overloadIndex);
6566
}
67+
68+
/** @beta @override */
69+
public buildCanonicalReference(): DeclarationReference {
70+
return (this.parent ? this.parent.canonicalReference : DeclarationReference.empty())
71+
.addNavigationStep(Navigation.Exports, this._getCanonicalReferenceName())
72+
.withMeaning(Meaning.Function)
73+
.withOverloadIndex(this.overloadIndex);
74+
}
6675
}

apps/api-extractor-model/src/model/ApiIndexSignature.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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.
33

4+
import { DeclarationReference, Meaning, Navigation } from '@microsoft/tsdoc/lib/beta/DeclarationReference';
45
import { ApiItemKind } from '../items/ApiItem';
56
import { IApiDeclaredItemOptions, ApiDeclaredItem } from '../items/ApiDeclaredItem';
67
import { IApiParameterListMixinOptions, ApiParameterListMixin } from '../mixins/ApiParameterListMixin';
@@ -59,4 +60,12 @@ export class ApiIndexSignature extends ApiParameterListMixin(ApiReleaseTagMixin(
5960
public get containerKey(): string {
6061
return ApiIndexSignature.getContainerKey(this.overloadIndex);
6162
}
63+
64+
/** @beta @override */
65+
public buildCanonicalReference(): DeclarationReference {
66+
return (this.parent ? this.parent.canonicalReference : DeclarationReference.empty())
67+
.addNavigationStep(Navigation.Members, '')
68+
.withMeaning(Meaning.Signature)
69+
.withOverloadIndex(this.overloadIndex);
70+
}
6271
}

0 commit comments

Comments
 (0)