Skip to content

Commit

Permalink
Merge pull request facebook#3563 from quizlet/fix-addons-hasownproperty
Browse files Browse the repository at this point in the history
Fix immutability helper to check hasOwnProperty safely
  • Loading branch information
zpao committed Apr 13, 2015
1 parent cb6d98a commit 631887d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/addons/__tests__/update-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
});
});
15 changes: 9 additions & 6 deletions src/addons/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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.',
Expand Down

0 comments on commit 631887d

Please sign in to comment.