Skip to content

Commit 4caa6d3

Browse files
author
Keith Brown
committed
Cherry-pick PR facebook#19598 to patch deepFreezeAndThrowOnMutationInDev fix into 0.49-stable
1 parent 312d37f commit 4caa6d3

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Libraries/Utilities/__tests__/deepFreezeAndThrowOnMutationInDev-test.js

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ describe('deepFreezeAndThrowOnMutationInDev', function() {
2626
expect(() => deepFreezeAndThrowOnMutationInDev()).not.toThrow();
2727
});
2828

29+
it('should not throw on object without prototype', () => {
30+
__DEV__ = true;
31+
var o = Object.create(null);
32+
o.key = 'Value';
33+
expect(() => deepFreezeAndThrowOnMutationInDev(o)).not.toThrow();
34+
});
35+
2936
it('should throw on mutation in dev with strict', () => {
3037
'use strict';
3138
__DEV__ = true;

Libraries/Utilities/deepFreezeAndThrowOnMutationInDev.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,18 @@ function deepFreezeAndThrowOnMutationInDev(object: Object) {
3838
return;
3939
}
4040

41-
var keys = Object.keys(object);
41+
const keys = Object.keys(object);
42+
const hasOwnProperty = Object.prototype.hasOwnProperty;
4243

4344
for (var i = 0; i < keys.length; i++) {
4445
var key = keys[i];
45-
if (object.hasOwnProperty(key)) {
46-
object.__defineGetter__(key, identity.bind(null, object[key]));
47-
object.__defineSetter__(key, throwOnImmutableMutation.bind(null, key));
46+
if (hasOwnProperty.call(object, key)) {
47+
Object.defineProperty(object, key, {
48+
get: identity.bind(null, object[key]),
49+
});
50+
Object.defineProperty(object, key, {
51+
set: throwOnImmutableMutation.bind(null, key),
52+
});
4853
}
4954
}
5055

@@ -53,7 +58,7 @@ function deepFreezeAndThrowOnMutationInDev(object: Object) {
5358

5459
for (var i = 0; i < keys.length; i++) {
5560
var key = keys[i];
56-
if (object.hasOwnProperty(key)) {
61+
if (hasOwnProperty.call(object, key)) {
5762
deepFreezeAndThrowOnMutationInDev(object[key]);
5863
}
5964
}

0 commit comments

Comments
 (0)