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

Modified deepFreezeAndThrowOnMutationInDev to use Object.prototype.ha… #19598

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ describe('deepFreezeAndThrowOnMutationInDev', function() {
expect(() => deepFreezeAndThrowOnMutationInDev()).not.toThrow();
});

it('should not throw on object without prototype', () => {
__DEV__ = true;
var o = Object.create(null);
o.key = 'Value';
expect(() => deepFreezeAndThrowOnMutationInDev(o)).not.toThrow();
});

it('should throw on mutation in dev with strict', () => {
'use strict';
__DEV__ = true;
Expand Down
13 changes: 9 additions & 4 deletions Libraries/Utilities/deepFreezeAndThrowOnMutationInDev.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ function deepFreezeAndThrowOnMutationInDev<T: Object>(object: T): T {
}

const keys = Object.keys(object);
const hasOwnProperty = Object.prototype.hasOwnProperty;

for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (object.hasOwnProperty(key)) {
object.__defineGetter__(key, identity.bind(null, object[key]));
object.__defineSetter__(key, throwOnImmutableMutation.bind(null, key));
if (hasOwnProperty.call(object, key)) {
Object.defineProperty(object, key, {
get: identity.bind(null, object[key]),
});
Object.defineProperty(object, key, {
set: throwOnImmutableMutation.bind(null, key),
});
}
}

Expand All @@ -53,7 +58,7 @@ function deepFreezeAndThrowOnMutationInDev<T: Object>(object: T): T {

for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (object.hasOwnProperty(key)) {
if (hasOwnProperty.call(object, key)) {
deepFreezeAndThrowOnMutationInDev(object[key]);
}
}
Expand Down