react-native/Libraries/Utilities
Keith Brown 02d1bcc047 Modified deepFreezeAndThrowOnMutationInDev to use Object.prototype.ha… (#19598)
Summary:
This PR fixes a bug in `deepFreezeAndThrowOnMutationInDev` which did not take into account that objects passed to it may have been created with `Object.create(null)` and thus may not have a prototype. Such objects don't have the methods `hasOwnProperty`, `__defineGetter__`, or `__defineSetter__` on the instance.

I ran into an unrecoverable error in React Native when passing this type of object across the bridge because `deepFreezeAndThrowOnMutationInDev` attempts to call `object.hasOwnProperty(key)`, `object.__defineGetter__` and `object__defineSetter__` on objects passed to it. But my object instance does not have these prototype methods.

Changes:
* Defined `Object.prototype.hasOwnProperty` as a `const` (pattern used elsewhere in React Native)
* Modified calls to `object.hasOwnProperty(key)` to use `hasOwnProperty.call(object, key)` (Per ESLint rule [here](https://eslint.org/docs/rules/no-prototype-builtins))
* Modified calls to deprecated methods `object.__defineGetter__` and `object.__defineSetter__` to instead use `Object.defineProperty` to define get and set methods on the object. (Per guidance on [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineGetter__))
* Added a new test to `deepFreezeAndThrowOnMutationInDev-test` to verify the fix.

I tried to create a reproducible example to post to Snack by passing prototype-less objects to a `Text` component, in various ways, but they appear to be converted to plain objects before crossing the bridge and therefore they do not throw an error.

However, I was able to create a new test to reproduce the issue. I added the following test to `deepFreezeAndThrowOnMutationInDev-test`:

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

The changes in this PR include this new test.

ESLint test produced no change in Error count (3) or Warnings (671)

N/A
Other areas with _possibly_ the same issue:
c6b96c0df7/Libraries/vendor/core/mergeInto.js (L50)
8dc3ba0444/Libraries/ReactNative/requireNativeComponent.js (L134)

 [GENERAL] [BUGFIX] [Libraries/Utilities/deepFreezeAndThrowOnMutationInDev] -Fix for compatibility with objects without a prototype.
Closes https://github.com/facebook/react-native/pull/19598

Differential Revision: D8310845

Pulled By: TheSavior

fbshipit-source-id: 020c414a1062a637e97f9ee99bf8e5ba2d1fcf4f
2018-06-07 02:44:27 -07:00
..
__mocks__ Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
__tests__ Modified deepFreezeAndThrowOnMutationInDev to use Object.prototype.ha… (#19598) 2018-06-07 02:44:27 -07:00
differ Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
BackAndroid.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
BackHandler.android.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
BackHandler.ios.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
DeviceInfo.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
Dimensions.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
HMRClient.js Remove @providesModule from all modules 2018-04-25 07:37:10 -07:00
HMRLoadingView.android.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
HMRLoadingView.ios.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
HeapCapture.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
JSDevSupportModule.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
MatrixMath.js Bump Prettier to 1.13.4 on xplat 2018-06-06 05:32:06 -07:00
PerformanceLogger.js Use timestamps from QPL by default 2018-05-31 02:39:21 -07:00
PixelRatio.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
Platform.android.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
Platform.ios.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
PlatformOS.android.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
PlatformOS.ios.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
PolyfillFunctions.js Remove @providesModule from all modules 2018-04-25 07:37:10 -07:00
RCTLog.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
SceneTracker.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
binaryToBase64.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
buildStyleInterpolator.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
clamp.js Clamp typechecks -> Flow 2018-06-01 10:25:30 -07:00
createStrictShapeTypeChecker.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
deepFreezeAndThrowOnMutationInDev.js Modified deepFreezeAndThrowOnMutationInDev to use Object.prototype.ha… (#19598) 2018-06-07 02:44:27 -07:00
defineLazyObjectProperty.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
deprecatedPropType.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
dismissKeyboard.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
groupByEveryN.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
infoLog.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
logError.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
mapWithSeparator.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
mergeIntoFast.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
stringifySafe.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00
truncate.js Prettier React Native Libraries 2018-05-10 19:10:38 -07:00