Skip to content

Commit fd7170f

Browse files
committed
Improve how undefined values are stored in the cache
1 parent 8fa2599 commit fd7170f

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

‎libraries/api-extractor/src/PackageJsonLookup.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface IPackageJson {
1616
* and retrieving the name of the package. The results are cached.
1717
*/
1818
export default class PackageJsonLookup {
19-
private _tryFindPackagePathUpwardsCache: Map<string, string>;
19+
private _tryFindPackagePathUpwardsCache: Map<string, string | undefined>;
2020
private _readPackageNameCache: Map<string, string>;
2121

2222
constructor() {
@@ -32,14 +32,15 @@ export default class PackageJsonLookup {
3232
* @param currentPath - a path (relative or absolute) of the current location
3333
* @returns a relative path to the package folder
3434
*/
35-
public tryFindPackagePathUpwards(currentPath: string): string {
36-
// Since Map cannot store an undefined value, we use the weird "null" object to
37-
// represent "undefined".
38-
let result: string = this._tryFindPackagePathUpwardsCache.get(currentPath);
39-
if (result !== undefined) {
40-
return result === null ? undefined : result; // tslint:disable-line: no-null-keyword
35+
public tryFindPackagePathUpwards(currentPath: string): string | undefined {
36+
// Two lookups are required, because get() cannot distinguish the undefined value
37+
// versus a missing key.
38+
if (this._tryFindPackagePathUpwardsCache.has(currentPath)) {
39+
return this._tryFindPackagePathUpwardsCache.get(currentPath);
4140
}
4241

42+
let result: string | undefined;
43+
4344
const parentFolder: string = path.dirname(currentPath);
4445
if (!parentFolder || parentFolder === currentPath) {
4546
result = undefined;
@@ -49,8 +50,7 @@ export default class PackageJsonLookup {
4950
result = this.tryFindPackagePathUpwards(parentFolder);
5051
}
5152

52-
// tslint:disable-next-line: no-null-keyword
53-
this._tryFindPackagePathUpwardsCache.set(currentPath, result === undefined ? null : result);
53+
this._tryFindPackagePathUpwardsCache.set(currentPath, result);
5454
return result;
5555
}
5656

0 commit comments

Comments
 (0)