Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Freezing of Map values during finalization #1150

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 #1119.
  • Loading branch information
chetan-satpute committed Nov 15, 2024
commit 29054cc0482d5cc3539aea462bb52818949f4910
2 changes: 2 additions & 0 deletions __tests__/frozen.js
Original file line number Diff line number Diff line change
@@ -147,10 +147,12 @@ function runTests(name) {

const res = produce(base, draft => {
draft.set("a", 1)
draft.set("o", {b: 1})
})
expect(() => res.set("b", 2)).toThrowErrorMatchingSnapshot()
expect(() => res.clear()).toThrowErrorMatchingSnapshot()
expect(() => res.delete("b")).toThrowErrorMatchingSnapshot()
expect(Object.isFrozen(res.get("o"))).toBe(true)

// In draft, still editable
expect(produce(res, d => void d.set("a", 2))).not.toBe(res)
7 changes: 5 additions & 2 deletions src/core/finalize.ts
Original file line number Diff line number Diff line change
@@ -15,7 +15,8 @@ import {
getPlugin,
die,
revokeScope,
isFrozen
isFrozen,
isMap
} from "../internal"

export function processResult(result: any, scope: ImmerScope) {
@@ -151,7 +152,9 @@ function finalizeProperty(
if (
(!parentState || !parentState.scope_.parent_) &&
typeof prop !== "symbol" &&
Object.prototype.propertyIsEnumerable.call(targetObject, prop)
(isMap(targetObject)
? targetObject.has(prop)
: Object.prototype.propertyIsEnumerable.call(targetObject, prop))
)
maybeFreeze(rootScope, childValue)
}
Loading
Oops, something went wrong.