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

Fix immutability helper to check hasOwnProperty safely #3563

Merged
merged 1 commit into from
Apr 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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