mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Fix immutability helper to check hasOwnProperty safely
This makes `update({}, {'hasOwnProperty': {$set: 'yolo'}})` work.
This commit is contained in:
@@ -93,4 +93,10 @@ describe('update', function() {
|
||||
'$apply. Did you forget to include {$set: ...}?'
|
||||
);
|
||||
});
|
||||
|
||||
it('should perform safe hasOwnProperty check', function() {
|
||||
expect(update({}, {'hasOwnProperty': {$set: 'a'}})).toEqual({
|
||||
'hasOwnProperty': 'a'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,11 +9,14 @@
|
||||
* @providesModule update
|
||||
*/
|
||||
|
||||
/* global hasOwnProperty:true */
|
||||
|
||||
'use strict';
|
||||
|
||||
var assign = require('Object.assign');
|
||||
var keyOf = require('keyOf');
|
||||
var invariant = require('invariant');
|
||||
var hasOwnProperty = {}.hasOwnProperty;
|
||||
|
||||
function shallowCopy(x) {
|
||||
if (Array.isArray(x)) {
|
||||
@@ -73,7 +76,7 @@ function update(value, spec) {
|
||||
COMMAND_SET
|
||||
);
|
||||
|
||||
if (spec.hasOwnProperty(COMMAND_SET)) {
|
||||
if (hasOwnProperty.call(spec, COMMAND_SET)) {
|
||||
invariant(
|
||||
Object.keys(spec).length === 1,
|
||||
'Cannot have more than one key in an object with %s',
|
||||
@@ -85,7 +88,7 @@ function update(value, spec) {
|
||||
|
||||
var nextValue = shallowCopy(value);
|
||||
|
||||
if (spec.hasOwnProperty(COMMAND_MERGE)) {
|
||||
if (hasOwnProperty.call(spec, COMMAND_MERGE)) {
|
||||
var mergeObj = spec[COMMAND_MERGE];
|
||||
invariant(
|
||||
mergeObj && typeof mergeObj === 'object',
|
||||
@@ -102,21 +105,21 @@ function update(value, spec) {
|
||||
assign(nextValue, spec[COMMAND_MERGE]);
|
||||
}
|
||||
|
||||
if (spec.hasOwnProperty(COMMAND_PUSH)) {
|
||||
if (hasOwnProperty.call(spec, COMMAND_PUSH)) {
|
||||
invariantArrayCase(value, spec, COMMAND_PUSH);
|
||||
spec[COMMAND_PUSH].forEach(function(item) {
|
||||
nextValue.push(item);
|
||||
});
|
||||
}
|
||||
|
||||
if (spec.hasOwnProperty(COMMAND_UNSHIFT)) {
|
||||
if (hasOwnProperty.call(spec, COMMAND_UNSHIFT)) {
|
||||
invariantArrayCase(value, spec, COMMAND_UNSHIFT);
|
||||
spec[COMMAND_UNSHIFT].forEach(function(item) {
|
||||
nextValue.unshift(item);
|
||||
});
|
||||
}
|
||||
|
||||
if (spec.hasOwnProperty(COMMAND_SPLICE)) {
|
||||
if (hasOwnProperty.call(spec, COMMAND_SPLICE)) {
|
||||
invariant(
|
||||
Array.isArray(value),
|
||||
'Expected %s target to be an array; got %s',
|
||||
@@ -142,7 +145,7 @@ function update(value, spec) {
|
||||
});
|
||||
}
|
||||
|
||||
if (spec.hasOwnProperty(COMMAND_APPLY)) {
|
||||
if (hasOwnProperty.call(spec, COMMAND_APPLY)) {
|
||||
invariant(
|
||||
typeof spec[COMMAND_APPLY] === 'function',
|
||||
'update(): expected spec of %s to be a function; got %s.',
|
||||
|
||||
Reference in New Issue
Block a user