8000 Fixes to ApiDeclaredItem · syengineering/rushstack@0154e43 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0154e43

Browse files
committed
Fixes to ApiDeclaredItem
1 parent 1361612 commit 0154e43

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export class ApiDeclaredItem extends ApiDocumentedItem {
101101
buildExcerpt(tokenRange: IExcerptTokenRange): Excerpt;
102102
get excerpt(): Excerpt;
103103
get excerptTokens(): ReadonlyArray<ExcerptToken>;
104+
get fileUrlPath(): string | undefined;
104105
getExcerptWithModifiers(): string;
105106
// Warning: (ae-forgotten-export) The symbol "IApiDeclaredItemJson" needs to be exported by the entry point index.d.ts
106107
//
@@ -1044,7 +1045,6 @@ export namespace ReleaseTag {
10441045
export class SourceLocation {
10451046
constructor(options: ISourceLocationOptions);
10461047
get fileUrl(): string | undefined;
1047-
get fileUrlPath(): string | undefined;
10481048
}
10491049

10501050
// @public

libraries/api-extractor-model/src/items/ApiDeclaredItem.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import { DeclarationReference } from '@microsoft/tsdoc/lib-commonjs/beta/DeclarationReference';
55
import { ApiDocumentedItem, IApiDocumentedItemJson, IApiDocumentedItemOptions } from './ApiDocumentedItem';
6+
import { ApiItem } from './ApiItem';
67
import { Excerpt, ExcerptToken, IExcerptTokenRange, IExcerptToken } from '../mixins/Excerpt';
78
import { DeserializerContext } from '../model/DeserializerContext';
89
import { SourceLocation } from '../model/SourceLocation';
@@ -38,7 +39,8 @@ export interface IApiDeclaredItemJson extends IApiDocumentedItemJson {
3839
export class ApiDeclaredItem extends ApiDocumentedItem {
3940
private _excerptTokens: ExcerptToken[];
4041
private _excerpt: Excerpt;
41-
private _sourceLocation: SourceLocation;
42+
private _fileUrlPath?: string;
43+
private _sourceLocation?: SourceLocation;
4244

4345
public constructor(options: IApiDeclaredItemOptions) {
4446
super(options);
@@ -51,14 +53,7 @@ export class ApiDeclaredItem extends ApiDocumentedItem {
5153
return new ExcerptToken(token.kind, token.text, canonicalReference);
5254
});
5355
this._excerpt = new Excerpt(this.excerptTokens, { startIndex: 0, endIndex: this.excerptTokens.length });
54-
55-
const projectFolderUrl: string | undefined = this.getAssociatedPackage()?.projectFolderUrl;
56-
const fileUrlPath: string | undefined = options.fileUrlPath || this._parentSourceLocation?.fileUrlPath;
57-
58-
this._sourceLocation = new SourceLocation({
59-
projectFolderUrl: projectFolderUrl,
60-
fileUrlPath: fileUrlPath
61-
});
56+
this._fileUrlPath = options.fileUrlPath;
6257
}
6358

6459
/** @override */
@@ -88,9 +83,21 @@ export class ApiDeclaredItem extends ApiDocumentedItem {
8883
}
8984

9085
/**
91-
* The source location where the API item is declared.
86+
* The file URL path relative to the `projectFolder` and `projectFolderURL` fields
87+
* as defined in the `api-extractor.json` config. Is `undefined` if the path is
88+
* the same as the parent API item's.
89+
*/
90+
public get fileUrlPath(): string | undefined {
91+
return this._fileUrlPath;
92+
}
93+
94+
/**
95+
* Returns the source location where the API item is declared.
9296
*/
9397
public get sourceLocation(): SourceLocation {
98+
if (!this._sourceLocation) {
99+
this._sourceLocation = this._buildSourceLocation();
100+
}
94101
return this._sourceLocation;
95102
}
96103

@@ -137,11 +144,10 @@ export class ApiDeclaredItem extends ApiDocumentedItem {
137144

138145
// Only serialize this API item's file URL path if it exists and it's different from its parent's
139146
// (a little optimization to keep the doc model succinct).
140-
if (
141-
this._sourceLocation.fileUrlPath &&
142-
this._sourceLocation.fileUrlPath !== this._parentSourceLocation?.fileUrlPath
143-
) {
144-
jsonObject.fileUrlPath = this._sourceLocation.fileUrlPath;
147+
if (this.fileUrlPath) {
148+
if (!(this.parent instanceof ApiDeclaredItem) || this.fileUrlPath !== this.parent.fileUrlPath) {
149+
jsonObject.fileUrlPath = this.fileUrlPath;
150+
}
145151
}
146152
}
147153

@@ -152,7 +158,23 @@ export class ApiDeclaredItem extends ApiDocumentedItem {
152158
return new Excerpt(this.excerptTokens, tokenRange);
153159
}
154160

155-
private get _parentSourceLocation(): SourceLocation | undefined {
156-
return this.parent instanceof ApiDeclaredItem ? this.parent.sourceLocation : undefined;
161+
/**
162+
* Builds the cached object used by the `sourceLocation` property.
163+
*/
164+
private _buildSourceLocation(): SourceLocation {
165+
const projectFolderUrl: string | undefined = this.getAssociatedPackage()?.projectFolderUrl;
166+
167+
let fileUrlPath: string | undefined;
168+
for (let current: ApiItem | undefined = this; current !== undefined; current = current.parent) {
169+
if (current instanceof ApiDeclaredItem && current.fileUrlPath) {
170+
fileUrlPath = current.fileUrlPath;
171+
break;
172+
}
173+
}
174+
175+
return new SourceLocation({
176+
projectFolderUrl: projectFolderUrl,
177+
fileUrlPath: fileUrlPath
178+
});
157179
}
158180
}

libraries/api-extractor-model/src/model/SourceLocation.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ export class SourceLocation {
4040
this._fileUrlPath = options.fileUrlPath;
4141
}
4242

43-
/** {@inheritDoc ISourceLocationOptions.fileUrlPath} */
44-
public get fileUrlPath(): string | undefined {
45-
return this._fileUrlPath;
46-
}
47-
4843
/**
4944
* Returns the file URL to the given source location. Returns `undefined` if the file URL
5045
* cannot be determined.

0 commit comments

Comments
 (0)
0