Hide Object.assign polyfill behind a module

Because the JS community's polyfilling infrastructure sucks and we'll
have to fix it for them before we require this.

JSX spread uses React.__spread
(which might get special behavior for key/ref, not sure yet)

This never uses the native implementation and throws for prototype chains.
Once the native implementations are faster, we'll start using them.
This commit is contained in:
Sebastian Markbage 2014-10-16 09:21:10 -07:00
parent 5263f61c87
commit 8210beeef4
41 changed files with 182 additions and 128 deletions

View File

@ -1,3 +1 @@
__DEV__ = true;
require.requireActual('../src/vendor/polyfill/Object.es6.js');

View File

@ -14,6 +14,8 @@
var React = require('React');
var assign = require('Object.assign');
var ReactTransitionGroup = React.createFactory(
require('ReactTransitionGroup')
);
@ -54,7 +56,7 @@ var ReactCSSTransitionGroup = React.createClass({
render: function() {
return (
ReactTransitionGroup(
Object.assign({}, this.props, {childFactory: this._wrapChild})
assign({}, this.props, {childFactory: this._wrapChild})
)
);
}

View File

@ -14,6 +14,7 @@
var React = require('React');
var ReactTransitionChildMapping = require('ReactTransitionChildMapping');
var assign = require('Object.assign');
var cloneWithProps = require('cloneWithProps');
var emptyFunction = require('emptyFunction');
@ -151,7 +152,7 @@ var ReactTransitionGroup = React.createClass({
// This entered again before it fully left. Add it again.
this.performEnter(key);
} else {
var newChildren = Object.assign({}, this.state.children);
var newChildren = assign({}, this.state.children);
delete newChildren[key];
this.setState({children: newChildren});
}

View File

@ -11,6 +11,7 @@
"use strict";
var assign = require('Object.assign');
var keyOf = require('keyOf');
var invariant = require('invariant');
@ -18,7 +19,7 @@ function shallowCopy(x) {
if (Array.isArray(x)) {
return x.concat();
} else if (x && typeof x === 'object') {
return Object.assign(new x.constructor(), x);
return assign(new x.constructor(), x);
} else {
return x;
}
@ -98,7 +99,7 @@ function update(value, spec) {
COMMAND_MERGE,
nextValue
);
Object.assign(nextValue, spec[COMMAND_MERGE]);
assign(nextValue, spec[COMMAND_MERGE]);
}
if (spec.hasOwnProperty(COMMAND_PUSH)) {

View File

@ -18,6 +18,7 @@ var EventPluginRegistry = require('EventPluginRegistry');
var ReactEventEmitterMixin = require('ReactEventEmitterMixin');
var ViewportMetrics = require('ViewportMetrics');
var assign = require('Object.assign');
var isEventSupported = require('isEventSupported');
/**
@ -147,7 +148,7 @@ function getListeningForDocument(mountAt) {
*
* @internal
*/
var ReactBrowserEventEmitter = Object.assign({}, ReactEventEmitterMixin, {
var ReactBrowserEventEmitter = assign({}, ReactEventEmitterMixin, {
/**
* Injectable event backend

View File

@ -14,11 +14,13 @@
var PooledClass = require('PooledClass');
var ReactBrowserEventEmitter = require('ReactBrowserEventEmitter');
var assign = require('Object.assign');
function ReactPutListenerQueue() {
this.listenersToPut = [];
}
Object.assign(ReactPutListenerQueue.prototype, {
assign(ReactPutListenerQueue.prototype, {
enqueuePutListener: function(rootNodeID, propKey, propValue) {
this.listenersToPut.push({
rootNodeID: rootNodeID,

View File

@ -19,6 +19,8 @@ var ReactInputSelection = require('ReactInputSelection');
var ReactPutListenerQueue = require('ReactPutListenerQueue');
var Transaction = require('Transaction');
var assign = require('Object.assign');
/**
* Ensures that, when possible, the selection range (currently selected text
* input) is not disturbed by performing the transaction.
@ -165,7 +167,7 @@ var Mixin = {
};
Object.assign(ReactReconcileTransaction.prototype, Transaction.Mixin, Mixin);
assign(ReactReconcileTransaction.prototype, Transaction.Mixin, Mixin);
PooledClass.addPoolingTo(ReactReconcileTransaction);

View File

@ -16,6 +16,7 @@ var DOMPropertyOperations = require('DOMPropertyOperations');
var ReactComponent = require('ReactComponent');
var ReactElement = require('ReactElement');
var assign = require('Object.assign');
var escapeTextForBrowser = require('escapeTextForBrowser');
/**
@ -37,7 +38,7 @@ var ReactTextComponent = function(props) {
// This constructor and it's argument is currently used by mocks.
};
Object.assign(ReactTextComponent.prototype, ReactComponent.Mixin, {
assign(ReactTextComponent.prototype, ReactComponent.Mixin, {
/**
* Creates the markup for this text node. This node is not intended to have

View File

@ -17,6 +17,7 @@ var CallbackQueue = require('CallbackQueue');
var ReactPutListenerQueue = require('ReactPutListenerQueue');
var Transaction = require('Transaction');
var assign = require('Object.assign');
var emptyFunction = require('emptyFunction');
/**
@ -99,7 +100,7 @@ var Mixin = {
};
Object.assign(
assign(
ReactServerRenderingTransaction.prototype,
Transaction.Mixin,
Mixin

View File

@ -14,6 +14,7 @@
var PooledClass = require('PooledClass');
var assign = require('Object.assign');
var emptyFunction = require('emptyFunction');
var getEventTarget = require('getEventTarget');
@ -82,7 +83,7 @@ function SyntheticEvent(dispatchConfig, dispatchMarker, nativeEvent) {
this.isPropagationStopped = emptyFunction.thatReturnsFalse;
}
Object.assign(SyntheticEvent.prototype, {
assign(SyntheticEvent.prototype, {
preventDefault: function() {
this.defaultPrevented = true;
@ -140,11 +141,11 @@ SyntheticEvent.augmentClass = function(Class, Interface) {
var Super = this;
var prototype = Object.create(Super.prototype);
Object.assign(prototype, Class.prototype);
assign(prototype, Class.prototype);
Class.prototype = prototype;
Class.prototype.constructor = Class;
Class.Interface = Object.assign({}, Super.Interface, Interface);
Class.Interface = assign({}, Super.Interface, Interface);
Class.augmentClass = Super.augmentClass;
PooledClass.addPoolingTo(Class, PooledClass.threeArgumentPooler);

View File

@ -11,10 +11,6 @@
"use strict";
// TODO: Move this elsewhere - it only exists in open source until a better
// solution is found.
require('Object.es6');
var DOMPropertyOperations = require('DOMPropertyOperations');
var EventPluginUtils = require('EventPluginUtils');
var ReactChildren = require('ReactChildren');
@ -36,6 +32,7 @@ var ReactPropTypes = require('ReactPropTypes');
var ReactServerRendering = require('ReactServerRendering');
var ReactTextComponent = require('ReactTextComponent');
var assign = require('Object.assign');
var deprecated = require('deprecated');
var onlyChild = require('onlyChild');
@ -84,6 +81,9 @@ var React = {
isValidElement: ReactElement.isValidElement,
withContext: ReactContext.withContext,
// Hook for JSX spread, don't use this for anything else.
__spread: assign,
// Deprecations (remove for 0.13)
renderComponent: deprecated(
'React',

View File

@ -22,6 +22,7 @@ var ReactMount = require('ReactMount');
var ReactMultiChild = require('ReactMultiChild');
var ReactPerf = require('ReactPerf');
var assign = require('Object.assign');
var escapeTextForBrowser = require('escapeTextForBrowser');
var invariant = require('invariant');
var isEventSupported = require('isEventSupported');
@ -213,7 +214,7 @@ ReactDOMComponent.Mixin = {
} else {
if (propKey === STYLE) {
if (propValue) {
propValue = props.style = Object.assign({}, props.style);
propValue = props.style = assign({}, props.style);
}
propValue = CSSPropertyOperations.createMarkupForStyles(propValue);
}
@ -362,7 +363,7 @@ ReactDOMComponent.Mixin = {
}
if (propKey === STYLE) {
if (nextProp) {
nextProp = nextProps.style = Object.assign({}, nextProp);
nextProp = nextProps.style = assign({}, nextProp);
}
if (lastProp) {
// Unset styles on `lastProp` but not on `nextProp`.
@ -471,7 +472,7 @@ ReactDOMComponent.Mixin = {
};
Object.assign(
assign(
ReactDOMComponent.prototype,
ReactComponent.Mixin,
ReactDOMComponent.Mixin,

View File

@ -19,6 +19,7 @@ var ReactInstanceHandles = require('ReactInstanceHandles');
var ReactMount = require('ReactMount');
var ReactUpdates = require('ReactUpdates');
var assign = require('Object.assign');
var getEventTarget = require('getEventTarget');
var getUnboundedScrollPosition = require('getUnboundedScrollPosition');
@ -46,7 +47,7 @@ function TopLevelCallbackBookKeeping(topLevelType, nativeEvent) {
this.nativeEvent = nativeEvent;
this.ancestors = [];
}
Object.assign(TopLevelCallbackBookKeeping.prototype, {
assign(TopLevelCallbackBookKeeping.prototype, {
destructor: function() {
this.topLevelType = null;
this.nativeEvent = null;

View File

@ -13,6 +13,7 @@
"use strict";
var assign = require('Object.assign');
var mocks = require('mocks');
describe('ReactDOMComponent', function() {
@ -223,7 +224,7 @@ describe('ReactDOMComponent', function() {
this.props = initialProps || {};
this._rootNodeID = 'test';
};
Object.assign(NodeStub.prototype, ReactDOMComponent.Mixin);
assign(NodeStub.prototype, ReactDOMComponent.Mixin);
genMarkup = function(props) {
var transaction = new ReactReconcileTransaction();
@ -273,7 +274,7 @@ describe('ReactDOMComponent', function() {
this.props = initialProps || {};
this._rootNodeID = 'test';
};
Object.assign(NodeStub.prototype, ReactDOMComponent.Mixin);
assign(NodeStub.prototype, ReactDOMComponent.Mixin);
genMarkup = function(props) {
var transaction = new ReactReconcileTransaction();
@ -310,9 +311,9 @@ describe('ReactDOMComponent', function() {
var StubNativeComponent = function(element) {
ReactComponent.Mixin.construct.call(this, element);
};
Object.assign(StubNativeComponent.prototype, ReactComponent.Mixin);
Object.assign(StubNativeComponent.prototype, ReactDOMComponent.Mixin);
Object.assign(StubNativeComponent.prototype, ReactMultiChild.Mixin);
assign(StubNativeComponent.prototype, ReactComponent.Mixin);
assign(StubNativeComponent.prototype, ReactDOMComponent.Mixin);
assign(StubNativeComponent.prototype, ReactMultiChild.Mixin);
mountComponent = function(props) {
var transaction = new ReactReconcileTransaction();

View File

@ -21,6 +21,7 @@ var ReactDOM = require('ReactDOM');
var ReactMount = require('ReactMount');
var ReactUpdates = require('ReactUpdates');
var assign = require('Object.assign');
var invariant = require('invariant');
// Store a reference to the <input> `ReactDOMComponent`. TODO: use string
@ -66,7 +67,7 @@ var ReactDOMInput = ReactCompositeComponent.createClass({
render: function() {
// Clone `this.props` so we don't mutate the input.
var props = Object.assign({}, this.props);
var props = assign({}, this.props);
props.defaultChecked = null;
props.defaultValue = null;

View File

@ -19,6 +19,8 @@ var ReactElement = require('ReactElement');
var ReactDOM = require('ReactDOM');
var ReactUpdates = require('ReactUpdates');
var assign = require('Object.assign');
// Store a reference to the <select> `ReactDOMComponent`. TODO: use string
var select = ReactElement.createFactory(ReactDOM.select.type);
@ -129,7 +131,7 @@ var ReactDOMSelect = ReactCompositeComponent.createClass({
render: function() {
// Clone `this.props` so we don't mutate the input.
var props = Object.assign({}, this.props);
var props = assign({}, this.props);
props.onChange = this._handleChange;
props.value = null;

View File

@ -20,6 +20,7 @@ var ReactElement = require('ReactElement');
var ReactDOM = require('ReactDOM');
var ReactUpdates = require('ReactUpdates');
var assign = require('Object.assign');
var invariant = require('invariant');
var warning = require('warning');
@ -95,7 +96,7 @@ var ReactDOMTextarea = ReactCompositeComponent.createClass({
render: function() {
// Clone `this.props` so we don't mutate the input.
var props = Object.assign({}, this.props);
var props = assign({}, this.props);
invariant(
props.dangerouslySetInnerHTML == null,

View File

@ -15,6 +15,7 @@ var ReactElement = require('ReactElement');
var ReactOwner = require('ReactOwner');
var ReactUpdates = require('ReactUpdates');
var assign = require('Object.assign');
var invariant = require('invariant');
var keyMirror = require('keyMirror');
@ -143,7 +144,7 @@ var ReactComponent = {
// element props.
var element = this._pendingElement || this._currentElement;
this.replaceProps(
Object.assign({}, element.props, partialProps),
assign({}, element.props, partialProps),
callback
);
},
@ -192,7 +193,7 @@ var ReactComponent = {
var element = this._pendingElement || this._currentElement;
this._pendingElement = ReactElement.cloneAndReplaceProps(
element,
Object.assign({}, element.props, partialProps)
assign({}, element.props, partialProps)
);
ReactUpdates.enqueueUpdate(this, callback);
},

View File

@ -26,6 +26,7 @@ var ReactPropTypeLocations = require('ReactPropTypeLocations');
var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');
var ReactUpdates = require('ReactUpdates');
var assign = require('Object.assign');
var instantiateReactComponent = require('instantiateReactComponent');
var invariant = require('invariant');
var keyMirror = require('keyMirror');
@ -340,7 +341,7 @@ var RESERVED_SPEC_KEYS = {
childContextTypes,
ReactPropTypeLocations.childContext
);
Constructor.childContextTypes = Object.assign(
Constructor.childContextTypes = assign(
{},
Constructor.childContextTypes,
childContextTypes
@ -352,7 +353,7 @@ var RESERVED_SPEC_KEYS = {
contextTypes,
ReactPropTypeLocations.context
);
Constructor.contextTypes = Object.assign(
Constructor.contextTypes = assign(
{},
Constructor.contextTypes,
contextTypes
@ -378,7 +379,7 @@ var RESERVED_SPEC_KEYS = {
propTypes,
ReactPropTypeLocations.prop
);
Constructor.propTypes = Object.assign(
Constructor.propTypes = assign(
{},
Constructor.propTypes,
propTypes
@ -866,7 +867,7 @@ var ReactCompositeComponentMixin = {
}
// Merge with `_pendingState` if it exists, otherwise with existing state.
this.replaceState(
Object.assign({}, this._pendingState || this.state, partialState),
assign({}, this._pendingState || this.state, partialState),
callback
);
},
@ -954,7 +955,7 @@ var ReactCompositeComponentMixin = {
name
);
}
return Object.assign({}, currentContext, childContext);
return assign({}, currentContext, childContext);
}
return currentContext;
},
@ -1332,7 +1333,7 @@ var ReactCompositeComponentMixin = {
};
var ReactCompositeComponentBase = function() {};
Object.assign(
assign(
ReactCompositeComponentBase.prototype,
ReactComponent.Mixin,
ReactOwner.Mixin,

View File

@ -11,6 +11,8 @@
"use strict";
var assign = require('Object.assign');
/**
* Keeps track of the current context.
*
@ -44,7 +46,7 @@ var ReactContext = {
withContext: function(newContext, scopedCallback) {
var result;
var previousContext = ReactContext.current;
ReactContext.current = Object.assign({}, previousContext, newContext);
ReactContext.current = assign({}, previousContext, newContext);
try {
result = scopedCallback();
} finally {

View File

@ -14,6 +14,7 @@
var ReactUpdates = require('ReactUpdates');
var Transaction = require('Transaction');
var assign = require('Object.assign');
var emptyFunction = require('emptyFunction');
var RESET_BATCHED_UPDATES = {
@ -34,7 +35,7 @@ function ReactDefaultBatchingStrategyTransaction() {
this.reinitializeTransaction();
}
Object.assign(
assign(
ReactDefaultBatchingStrategyTransaction.prototype,
Transaction.Mixin,
{

View File

@ -11,6 +11,7 @@
"use strict";
var assign = require('Object.assign');
var invariant = require('invariant');
var genericComponentClass = null;
@ -26,7 +27,7 @@ var ReactNativeComponentInjection = {
// This accepts a keyed object with classes as values. Each key represents a
// tag. That particular tag will use this class instead of the generic one.
injectComponentClasses: function(componentClasses) {
Object.assign(tagToComponentClass, componentClasses);
assign(tagToComponentClass, componentClasses);
}
};

View File

@ -11,6 +11,7 @@
"use strict";
var assign = require('Object.assign');
var emptyFunction = require('emptyFunction');
var invariant = require('invariant');
var joinClasses = require('joinClasses');
@ -39,7 +40,7 @@ var transferStrategyMerge = createTransferStrategy(function(a, b) {
// `merge` overrides the first object's (`props[key]` above) keys using the
// second object's (`value`) keys. An object's style's existing `propA` would
// get overridden. Flip the order here.
return Object.assign({}, b, a);
return assign({}, b, a);
});
/**
@ -105,7 +106,7 @@ var ReactPropTransferer = {
* @return {object} a new object containing both sets of props merged.
*/
mergeProps: function(oldProps, newProps) {
return transferInto(Object.assign({}, oldProps), newProps);
return transferInto(assign({}, oldProps), newProps);
},
/**

View File

@ -17,6 +17,7 @@ var ReactCurrentOwner = require('ReactCurrentOwner');
var ReactPerf = require('ReactPerf');
var Transaction = require('Transaction');
var assign = require('Object.assign');
var invariant = require('invariant');
var warning = require('warning');
@ -72,7 +73,7 @@ function ReactUpdatesFlushTransaction() {
ReactUpdates.ReactReconcileTransaction.getPooled();
}
Object.assign(
assign(
ReactUpdatesFlushTransaction.prototype,
Transaction.Mixin, {
getTransactionWrappers: function() {

View File

@ -11,6 +11,8 @@
"use strict";
var assign = require('Object.assign');
describe('EventPluginRegistry', function() {
var EventPluginRegistry;
var createPlugin;
@ -20,7 +22,7 @@ describe('EventPluginRegistry', function() {
EventPluginRegistry._resetEventPlugins();
createPlugin = function(properties) {
return Object.assign({extractEvents: function() {}}, properties);
return assign({extractEvents: function() {}}, properties);
};
});

View File

@ -9,6 +9,8 @@
* @providesModule ReactDefaultPerfAnalysis
*/
var assign = require('Object.assign');
// Don't try to save users less than 1.2ms (a number I made up)
var DONT_CARE_THRESHOLD = 1.2;
var DOM_OPERATION_TYPES = {
@ -62,7 +64,7 @@ function getExclusiveSummary(measurements) {
for (var i = 0; i < measurements.length; i++) {
var measurement = measurements[i];
var allIDs = Object.assign(
var allIDs = assign(
{},
measurement.exclusive,
measurement.inclusive
@ -114,7 +116,7 @@ function getInclusiveSummary(measurements, onlyClean) {
for (var i = 0; i < measurements.length; i++) {
var measurement = measurements[i];
var allIDs = Object.assign(
var allIDs = assign(
{},
measurement.exclusive,
measurement.inclusive
@ -173,7 +175,7 @@ function getUnchangedComponents(measurement) {
// the amount of time it took to render the entire subtree.
var cleanComponents = {};
var dirtyLeafIDs = Object.keys(measurement.writes);
var allIDs = Object.assign({}, measurement.exclusive, measurement.inclusive);
var allIDs = assign({}, measurement.exclusive, measurement.inclusive);
for (var id in allIDs) {
var isDirty = false;

View File

@ -22,6 +22,8 @@ var ReactTextComponent = require('ReactTextComponent');
var ReactUpdates = require('ReactUpdates');
var SyntheticEvent = require('SyntheticEvent');
var assign = require('Object.assign');
var topLevelTypes = EventConstants.topLevelTypes;
function Event(suffix) {}
@ -318,7 +320,7 @@ function makeSimulator(eventType) {
ReactMount.getID(node),
fakeNativeEvent
);
Object.assign(event, eventData);
assign(event, eventData);
EventPropagators.accumulateTwoPhaseDispatches(event);
ReactUpdates.batchedUpdates(function() {
@ -374,7 +376,7 @@ buildSimulators();
function makeNativeSimulator(eventType) {
return function(domComponentOrNode, nativeEventData) {
var fakeNativeEvent = new Event(eventType);
Object.assign(fakeNativeEvent, nativeEventData);
assign(fakeNativeEvent, nativeEventData);
if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
ReactTestUtils.simulateNativeEventOnDOMComponent(
eventType,

View File

@ -14,6 +14,8 @@
var ReactTestUtils = require('ReactTestUtils');
var assign = require('Object.assign');
function reactComponentExpect(instance) {
if (instance instanceof reactComponentExpect) {
return instance;
@ -29,7 +31,7 @@ function reactComponentExpect(instance) {
expect(ReactTestUtils.isElement(instance)).toBe(false);
}
Object.assign(reactComponentExpect.prototype, {
assign(reactComponentExpect.prototype, {
// Getters -------------------------------------------------------------------
/**

View File

@ -13,6 +13,7 @@
var PooledClass = require('PooledClass');
var assign = require('Object.assign');
var invariant = require('invariant');
/**
@ -31,7 +32,7 @@ function CallbackQueue() {
this._contexts = null;
}
Object.assign(CallbackQueue.prototype, {
assign(CallbackQueue.prototype, {
/**
* Enqueues a callback to be invoked when `notifyAll` is invoked.

View File

@ -12,6 +12,7 @@
"use strict";
var assign = require('Object.assign');
var invariant = require('invariant');
var isNode = require('isNode');
var mergeHelpers = require('mergeHelpers');
@ -42,7 +43,7 @@ if (__DEV__) {
* @constructor
*/
LegacyImmutableObject = function LegacyImmutableObject(initialProperties) {
Object.assign(this, initialProperties);
assign(this, initialProperties);
deepFreeze(this);
};
@ -91,7 +92,7 @@ if (__DEV__) {
*/
LegacyImmutableObject.set = function(immutableObject, put) {
assertLegacyImmutableObject(immutableObject);
var totalNewFields = Object.assign({}, immutableObject, put);
var totalNewFields = assign({}, immutableObject, put);
return new LegacyImmutableObject(totalNewFields);
};
@ -103,7 +104,7 @@ if (__DEV__) {
* @constructor
*/
LegacyImmutableObject = function LegacyImmutableObject(initialProperties) {
Object.assign(this, initialProperties);
assign(this, initialProperties);
};
/**
@ -117,7 +118,7 @@ if (__DEV__) {
LegacyImmutableObject.set = function(immutableObject, put) {
assertLegacyImmutableObject(immutableObject);
var newMap = new LegacyImmutableObject(immutableObject);
Object.assign(newMap, put);
assign(newMap, put);
return newMap;
};
}

View File

@ -11,6 +11,7 @@
"use strict";
var assign = require('Object.assign');
var invariant = require('invariant');
var PREFIX = 'key:';
@ -475,7 +476,7 @@ var OrderedMapMethods = {
}
};
Object.assign(OrderedMapImpl.prototype, OrderedMapMethods);
assign(OrderedMapImpl.prototype, OrderedMapMethods);
var OrderedMap = {
from: function(orderedMap) {

View File

@ -11,6 +11,8 @@
"use strict";
var assign = require('Object.assign');
var Transaction;
var INIT_ERRORED = 'initErrored'; // Just a dummy value to check for.
@ -44,7 +46,7 @@ describe('Transaction', function() {
this.secondCloseParam = INIT_ERRORED; // WILL be set to something else
this.lastCloseParam = INIT_ERRORED; // WON'T be set to something else
};
Object.assign(TestTransaction.prototype, Transaction.Mixin);
assign(TestTransaction.prototype, Transaction.Mixin);
TestTransaction.prototype.getTransactionWrappers = function() {
return [
{
@ -93,7 +95,7 @@ describe('Transaction', function() {
this.secondCloseParam = INIT_ERRORED; // WILL be set to something else
this.lastCloseParam = INIT_ERRORED; // WILL be set to something else
};
Object.assign(TestTransaction.prototype, Transaction.Mixin);
assign(TestTransaction.prototype, Transaction.Mixin);
TestTransaction.prototype.getTransactionWrappers = function() {
return [
{
@ -154,7 +156,7 @@ describe('Transaction', function() {
this.secondCloseParam = INIT_ERRORED; // WILL be set to something else
this.lastCloseParam = INIT_ERRORED; // WILL be set to something else
};
Object.assign(TestTransaction.prototype, Transaction.Mixin);
assign(TestTransaction.prototype, Transaction.Mixin);
// Now, none of the close/inits throw, but the operation we wrap will throw.
TestTransaction.prototype.getTransactionWrappers = function() {
return [
@ -218,7 +220,7 @@ describe('Transaction', function() {
var TestTransaction = function() {
this.reinitializeTransaction();
};
Object.assign(TestTransaction.prototype, Transaction.Mixin);
assign(TestTransaction.prototype, Transaction.Mixin);
var exceptionMsg = 'This exception should throw.';
TestTransaction.prototype.getTransactionWrappers = function() {
return [
@ -247,7 +249,7 @@ describe('Transaction', function() {
this.reinitializeTransaction();
this.firstCloseParam = INIT_ERRORED; // WILL be set to something else
};
Object.assign(TestTransaction.prototype, Transaction.Mixin);
assign(TestTransaction.prototype, Transaction.Mixin);
TestTransaction.prototype.getTransactionWrappers = function() {
return [
{
@ -275,7 +277,7 @@ describe('Transaction', function() {
var NestedTransaction = function() {
this.reinitializeTransaction();
};
Object.assign(NestedTransaction.prototype, Transaction.Mixin);
assign(NestedTransaction.prototype, Transaction.Mixin);
NestedTransaction.prototype.getTransactionWrappers = function() {
return [{
initialize: function() {

View File

@ -9,6 +9,7 @@
* @providesModule deprecated
*/
var assign = require('Object.assign');
var warning = require('warning');
/**
@ -37,7 +38,7 @@ function deprecated(namespace, oldName, newName, ctx, fn) {
newFn.displayName = `${namespace}_${oldName}`;
// We need to make sure all properties of the original fn are copied over.
// In particular, this is needed to support PropTypes
return Object.assign(newFn, fn);
return assign(newFn, fn);
}
return fn;

View File

@ -22,13 +22,11 @@ function makeEmptyFunction(arg) {
*/
function emptyFunction() {}
Object.assign(emptyFunction, {
thatReturns: makeEmptyFunction,
thatReturnsFalse: makeEmptyFunction(false),
thatReturnsTrue: makeEmptyFunction(true),
thatReturnsNull: makeEmptyFunction(null),
thatReturnsThis: function() { return this; },
thatReturnsArgument: function(arg) { return arg; }
});
emptyFunction.thatReturns = makeEmptyFunction;
emptyFunction.thatReturnsFalse = makeEmptyFunction(false);
emptyFunction.thatReturnsTrue = makeEmptyFunction(true);
emptyFunction.thatReturnsNull = makeEmptyFunction(null);
emptyFunction.thatReturnsThis = function() { return this; };
emptyFunction.thatReturnsArgument = function(arg) { return arg; };
module.exports = emptyFunction;

View File

@ -9,6 +9,7 @@
* @providesModule Immutable
*/
var assign = require('Object.assign');
var invariant = require('invariant');
var isNode = require('isNode');
var keyOf = require('keyOf');
@ -59,7 +60,7 @@ class Immutable {
static mergeAllPropertiesInto(destination, propertyObjects) {
var argLength = propertyObjects.length;
for (var i = 0; i < argLength; i++) {
Object.assign(destination, propertyObjects[i]);
assign(destination, propertyObjects[i]);
}
}

View File

@ -1,45 +0,0 @@
/**
* Copyright 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule Object.es6
* @polyfill
*/
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.assign
if (!Object.assign) {
Object.assign = function(target, sources) {
if (target == null) {
throw new TypeError('Object.assign target cannot be null or undefined');
}
var to = Object(target);
var hasOwnProperty = Object.prototype.hasOwnProperty;
for (var nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
var nextSource = arguments[nextIndex];
if (nextSource == null) {
continue;
}
var from = Object(nextSource);
// We don't currently support accessors nor proxies. Therefore this
// copy cannot throw. If we ever supported this then we must handle
// exceptions and side-effects.
for (var key in from) {
if (hasOwnProperty.call(from, key)) {
to[key] = from[key];
}
}
}
return to;
};
}

57
src/vendor/stubs/Object.assign.js vendored Normal file
View File

@ -0,0 +1,57 @@
/**
* Copyright 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule Object.assign
*/
// This is an optimized version that fails on hasOwnProperty checks
// and non objects. It's not spec-compliant. It's a perf optimization.
var hasOwnProperty = Object.prototype.hasOwnProperty;
function assign(target, sources) {
if (__DEV__) {
if (target == null) {
throw new TypeError('Object.assign target cannot be null or undefined');
}
if (typeof target !== 'object' && typeof target !== 'function') {
throw new TypeError(
'In this environment the target of assign MUST be an object. ' +
'This error is a performance optimization and not spec compliant.'
);
}
}
for (var nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
var nextSource = arguments[nextIndex];
if (nextSource == null) {
continue;
}
// We don't currently support accessors nor proxies. Therefore this
// copy cannot throw. If we ever supported this then we must handle
// exceptions and side-effects.
for (var key in nextSource) {
if (__DEV__) {
if (!hasOwnProperty.call(nextSource, key)) {
throw new TypeError(
'One of the sources to assign has an enumerable key on the ' +
'prototype chain. This is an edge case that we do not support. ' +
'This error is a performance optimization and not spec compliant.'
);
}
}
target[key] = nextSource[key];
}
}
return target;
};
module.exports = assign;

View File

@ -11,6 +11,8 @@
"use strict";
var assign = require('Object.assign');
/**
* Shallow merges two structures into a return value, without mutating either.
*
@ -19,7 +21,7 @@
* @return {object} The shallow extension of one by two.
*/
var merge = function(one, two) {
return Object.assign({}, one, two);
return assign({}, one, two);
};
module.exports = merge;

View File

@ -12,7 +12,9 @@
"use strict";
module.exports = Object.assign;
var assign = require('Object.assign');
module.exports = assign;
// deprecation notice
console.warn(

View File

@ -42,7 +42,7 @@ describe('react jsx', function() {
var Component = jest.genMockFunction();
var Child = jest.genMockFunction();
var objectAssignMock = jest.genMockFunction();
Object.assign = objectAssignMock;
React.__spread = objectAssignMock;
eval(transform(code).code);
return expect(objectAssignMock);
}
@ -309,12 +309,12 @@ describe('react jsx', function() {
expect(() => transform(code)).toThrow();
});
it('wraps props in Object.assign for spread attributes', function() {
it('wraps props in React.__spread for spread attributes', function() {
var code =
'<Component { ... x } y\n' +
'={2 } z />';
var result =
'React.createElement(Component, Object.assign({}, x , {y: \n' +
'React.createElement(Component, React.__spread({}, x , {y: \n' +
'2, z: true}))';
expect(transform(code).code).toBe(result);
@ -326,7 +326,7 @@ describe('react jsx', function() {
' {...this.props}\n' +
' sound="moo" />';
var result =
'React.createElement(Component, Object.assign({}, \n' +
'React.createElement(Component, React.__spread({}, \n' +
' this.props, \n' +
' {sound: "moo"}))';
@ -340,7 +340,7 @@ describe('react jsx', function() {
expect(transform(code).code).toBe(result);
});
it('does not call Object.assign when there are no spreads', function() {
it('does not call React.__spread when there are no spreads', function() {
expectObjectAssign(
'<Component x={y} />'
).not.toBeCalled();
@ -376,13 +376,13 @@ describe('react jsx', function() {
).toBeCalledWith({x: 1}, y);
});
it('passes the same value multiple times to Object.assign', function() {
it('passes the same value multiple times to React.__spread', function() {
expectObjectAssign(
'<Component x={1} y="2" {...z} {...z}><Child /></Component>'
).toBeCalledWith({x: 1, y: "2"}, z, z);
});
it('evaluates sequences before passing them to Object.assign', function() {
it('evaluates sequences before passing them to React.__spread', function() {
expectObjectAssign(
'<Component x="1" {...(z = { y: 2 }, z)} z={3}>Text</Component>'
).toBeCalledWith({x: "1"}, { y: 2 }, {z: 3});

View File

@ -89,7 +89,7 @@ function visitReactTag(traverse, object, path, state) {
// if we don't have any attributes, pass in null
if (hasAtLeastOneSpreadProperty) {
utils.append('Object.assign({', state);
utils.append('React.__spread({', state);
} else if (hasAttributes) {
utils.append('{', state);
} else {