Skip to content

Commit

Permalink
chore: address code review
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Nov 5, 2020
1 parent 6434455 commit 0b30bc2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/internal/source_map/source_map_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function sourceMapCacheToObject() {
}

function appendCJSCache(obj) {
for (const [, value] of cjsSourceMapCache) {
for (const value of cjsSourceMapCache) {
obj[ObjectGetValueSafe(value, 'filename')] = {
lineLengths: ObjectGetValueSafe(value, 'lineLengths'),
data: ObjectGetValueSafe(value, 'data'),
Expand All @@ -230,7 +230,7 @@ function findSourceMap(sourceURL) {
}
let sourceMap = esmSourceMapCache.get(sourceURL);
if (sourceMap === undefined) {
for (const [, value] of cjsSourceMapCache) {
for (const value of cjsSourceMapCache) {
const filename = ObjectGetValueSafe(value, 'filename');
if (sourceURL === filename) {
sourceMap = {
Expand Down
13 changes: 7 additions & 6 deletions lib/internal/util/iterable_weak_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
makeSafe,
Object,
SafeSet,
SafeWeakMap,
Symbol,
Expand Down Expand Up @@ -49,6 +50,10 @@ class IterableWeakMap {
return this.#weakMap.get(key)?.value;
}

has(key) {
return this.#weakMap.has(key);
}

delete(key) {
const entry = this.#weakMap.get(key);
if (!entry) {
Expand All @@ -65,12 +70,6 @@ class IterableWeakMap {
const key = ref.deref();
if (!key) continue;
const { value } = this.#weakMap.get(key);
yield [key, value];
}
}

*values() {
for (const [, value] of this) {
yield value;
}
}
Expand All @@ -80,6 +79,8 @@ function cleanup({ set, ref }) {
set.delete(ref);
}

Object.freeze(IterableWeakMap.prototype);

module.exports = {
IterableWeakMap
};
18 changes: 14 additions & 4 deletions test/parallel/test-internal-iterable-weak-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const { IterableWeakMap } = require('internal/util/iterable_weak_map');
setImmediate(() => {
_cache;
globalThis.gc();
const values = [...wm.values()];
const values = [...wm];
deepStrictEqual(values, ['hello', 'goodbye']);
});
}
Expand All @@ -35,7 +35,7 @@ const { IterableWeakMap } = require('internal/util/iterable_weak_map');
wm.set(_cache.moduleA, 'hello');
wm.set(_cache.moduleB, 'goodbye');
wm.set(_cache.moduleB, 'goodnight');
const values = [...wm.values()];
const values = [...wm];
deepStrictEqual(values, ['hello', 'goodnight']);
}

Expand All @@ -51,7 +51,7 @@ const { IterableWeakMap } = require('internal/util/iterable_weak_map');
wm.set(_cache.moduleB, 'discard');
wm.set(_cache.moduleC, 'goodbye');
wm.delete(_cache.moduleB);
const values = [...wm.values()];
const values = [...wm];
deepStrictEqual(values, ['hello', 'goodbye']);
}

Expand All @@ -66,7 +66,7 @@ const { IterableWeakMap } = require('internal/util/iterable_weak_map');
wm.set(_cache.moduleA, 'hello');
wm.set(_cache.moduleC, 'goodbye');
wm.delete(_cache.moduleB);
const values = [...wm.values()];
const values = [...wm];
deepStrictEqual(values, ['hello', 'goodbye']);
}

Expand All @@ -83,3 +83,13 @@ const { IterableWeakMap } = require('internal/util/iterable_weak_map');
wm.set(_cache.moduleC, 'goodbye');
strictEqual(wm.get(_cache.moduleB), 'discard');
}

// It returns true for has() if key exists.
{
const wm = new IterableWeakMap();
const _cache = {
moduleA: {},
};
wm.set(_cache.moduleA, 'hello');
strictEqual(wm.has(_cache.moduleA), true);
}

0 comments on commit 0b30bc2

Please sign in to comment.