Skip to content

Commit 29054cc

Browse files
fix freezing of Map values during finalization
Map keys are not enumerable properties of the Map object, so the propertyIsEnumerable check does not apply to them. This commit adds a condition to detect Map objects and uses Map.prototype.has to check for key existence. Fixes immerjs#1119.
1 parent 19cbe47 commit 29054cc

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

__tests__/frozen.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,12 @@ function runTests(name) {
147147

148148
const res = produce(base, draft => {
149149
draft.set("a", 1)
150+
draft.set("o", {b: 1})
150151
})
151152
expect(() => res.set("b", 2)).toThrowErrorMatchingSnapshot()
152153
expect(() => res.clear()).toThrowErrorMatchingSnapshot()
153154
expect(() => res.delete("b")).toThrowErrorMatchingSnapshot()
155+
expect(Object.isFrozen(res.get("o"))).toBe(true)
154156

155157
// In draft, still editable
156158
expect(produce(res, d => void d.set("a", 2))).not.toBe(res)

src/core/finalize.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
getPlugin,
1616
die,
1717
revokeScope,
18-
isFrozen
18+
isFrozen,
19+
isMap
1920
} from "../internal"
2021

2122
export function processResult(result: any, scope: ImmerScope) {
@@ -151,7 +152,9 @@ function finalizeProperty(
151152
if (
152153
(!parentState || !parentState.scope_.parent_) &&
153154
typeof prop !== "symbol" &&
154-
Object.prototype.propertyIsEnumerable.call(targetObject, prop)
155+
(isMap(targetObject)
156+
? targetObject.has(prop)
157+
: Object.prototype.propertyIsEnumerable.call(targetObject, prop))
155158
)
156159
maybeFreeze(rootScope, childValue)
157160
}

0 commit comments

Comments
 (0)