Skip to content

Commit ce1a273

Browse files
committed
Fix memoization
1 parent 0102f9b commit ce1a273

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

src/cache/clone.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
// Clone cached versions to prevent mutations
22
// TODO: use `structuredClone()` after dropping support for Node <17.0.0
33
export const cloneCachedVersions = function ({ versions, majors }) {
4-
return { versions: [...versions], majors: majors.map(shallowCloneObject) }
4+
return {
5+
versions: shallowCloneObjectsArray(versions),
6+
majors: shallowCloneObjectsArray(majors),
7+
}
8+
}
9+
10+
const shallowCloneObjectsArray = function (arrayOfObjects) {
11+
return arrayOfObjects.map(shallowCloneObject)
512
}
613

714
const shallowCloneObject = function (object) {

test/cache.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,13 @@ each(
7979

8080
test.serial(`Process cached files cannot be mutated`, async (t) => {
8181
const { versions } = await allNodeVersions({ fetch: false })
82-
const [firstVersion] = versions
82+
const [{ node: firstVersion }] = versions
83+
// eslint-disable-next-line fp/no-mutation
84+
versions[0].node = 'test'
8385
// eslint-disable-next-line fp/no-mutating-methods
8486
versions.reverse()
85-
const { versions: versionsAgain } = await allNodeVersions({ fetch: false })
86-
t.is(versionsAgain[0], firstVersion)
87+
const {
88+
versions: [{ node: firstVersionAgain }],
89+
} = await allNodeVersions({ fetch: false })
90+
t.is(firstVersionAgain, firstVersion)
8791
})

test/helpers/cache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const writeCacheFile = async function (oldCacheFile = false) {
1919
const cacheFile = await getCacheFile()
2020
const lastUpdate = oldCacheFile ? 0 : Date.now()
2121
const versionsInfo = {
22-
versions: ['cached'],
22+
versions: [{ node: 'cached' }],
2323
majors: [{ major: 1, latest: 'cached' }],
2424
}
2525
const cacheContent = { lastUpdate, ...versionsInfo }

test/helpers/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import allNodeVersions from 'all-node-versions'
33
// Retrieve latest Node.js version
44
export const getLatestVersion = async function (opts) {
55
const {
6-
versions: [version],
6+
versions: [{ node }],
77
} = await allNodeVersions(opts)
8-
return version
8+
return node
99
}

0 commit comments

Comments
 (0)