Skip to content

Commit 9c6603e

Browse files
committed
fix: throw on circular references
The most basic case; when an object references itself directly.
1 parent 9a2e756 commit 9c6603e

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/immer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ export class Immer {
168168

169169
const {onAssign} = this
170170
const finalizeProperty = (prop, value, parent) => {
171-
// Only `root` can be a draft in here.
171+
if (value === parent) {
172+
throw Error("Immer forbids circular references")
173+
}
174+
175+
// The only possible draft (in the scope of a `finalizeTree` call) is the `root` object.
172176
const inDraft = !!state && parent === root
173177

174178
if (isDraft(value)) {

0 commit comments

Comments
 (0)