-
Notifications
You must be signed in to change notification settings - Fork 195
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
Detect cycles during development #119
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -497,7 +497,7 @@ | |
return makeImmutable(obj, mutatingObjectMethods); | ||
} | ||
|
||
function Immutable(obj, options) { | ||
function Immutable(obj, options, stackRemaining) { | ||
if (isImmutable(obj)) { | ||
return obj; | ||
} else if (obj instanceof Array) { | ||
|
@@ -512,9 +512,22 @@ | |
instantiatePlainObject : (function() { return Object.create(prototype); }); | ||
var clone = instantiateEmptyObject(); | ||
|
||
if (process.env.NODE_ENV !== "production") { | ||
/*jshint eqnull:true */ | ||
if (stackRemaining == null) { | ||
stackRemaining = 64; | ||
} | ||
if (stackRemaining <= 0) { | ||
throw new ImmutableError("Attempt to construct Immutable from a deeply nested object was detected." + | ||
" Have you tried to wrap an object with circular references (e.g. React element)?" + | ||
" See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details."); | ||
} | ||
stackRemaining -= 1; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Increment? |
||
|
||
for (var key in obj) { | ||
if (Object.getOwnPropertyDescriptor(obj, key)) { | ||
clone[key] = Immutable(obj[key]); | ||
clone[key] = Immutable(obj[key], undefined, stackRemaining); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,31 @@ var getTestUtils = require("./TestUtils.js"); | |
}); | ||
}); | ||
}); | ||
|
||
it("detects cycles", function() { | ||
var obj = {}; | ||
obj.prop = obj; | ||
|
||
if (config.id === 'prod') { | ||
assert.throws(function() { Immutable(obj); }, RangeError); | ||
} else { | ||
assert.throws(function() { Immutable(obj); }, /deeply nested/); | ||
} | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assert.throws(Immutable.bind(null, obj), config.id === 'prod' ? RangeError : /deeply nested/); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice suggestion, thank you! But I find my version a little easier to follow. |
||
|
||
it("can configure stackRemaining", function() { | ||
var mutable = {bottom: true}; | ||
_.range(65).forEach(function() { | ||
mutable = {prop: mutable}; | ||
}); | ||
|
||
if (config.id === 'prod') { | ||
TestUtils.assertJsonEqual(mutable, Immutable(mutable)); | ||
} else { | ||
assert.throws(function() { Immutable(mutable); }, /deeply nested/); | ||
TestUtils.assertJsonEqual(mutable, Immutable(mutable, null, 66)); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.