Skip to content

Commit

Permalink
Use Object.assign directly and inject object-assign at compile
Browse files Browse the repository at this point in the history
  • Loading branch information
zpao committed Apr 4, 2016
1 parent 2811262 commit 38fcebf
Show file tree
Hide file tree
Showing 42 changed files with 104 additions and 102 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"transform-es2015-block-scoping",
"transform-es2015-modules-commonjs",
"transform-es3-member-expression-literals",
"transform-es3-property-literals"
"transform-es3-property-literals",
"./scripts/babel/transform-object-assign-require"
]
}
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var babelOpts = {
{},
require('fbjs/module-map'),
{
'Object.assign': 'object-assign',
'object-assign': 'object-assign',
}
),
}],
Expand Down
40 changes: 40 additions & 0 deletions scripts/babel/transform-object-assign-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2013-present, 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.
*/

'use strict';

module.exports = function autoImporter(babel) {
const t = babel.types;

return {
pre: function() {
// map from module to generated identifier
this.id = null;
},

visitor: {
CallExpression: function(path, file) {
if (path.get('callee').matchesPattern('Object.assign')) {
// generate identifier and require if it hasn't been already
if (!this.id) {
this.id = path.scope.generateUidIdentifier('assign');
path.scope.getProgramParent().push({
id: this.id,
init: t.callExpression(
t.identifier('require'),
[t.stringLiteral('object-assign')]
),
});
}
path.node.callee = this.id;
}
},
},
};
};
2 changes: 1 addition & 1 deletion scripts/jest/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var babelOptions = {
{},
moduleMap,
{
'Object.assign': 'object-assign',
'object-assign': 'object-assign',
}
),
}],
Expand Down
4 changes: 1 addition & 3 deletions src/addons/transitions/ReactCSSTransitionGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

var React = require('React');

var assign = require('Object.assign');

var ReactTransitionGroup = require('ReactTransitionGroup');
var ReactCSSTransitionGroupChild = require('ReactCSSTransitionGroupChild');

Expand Down Expand Up @@ -87,7 +85,7 @@ var ReactCSSTransitionGroup = React.createClass({
render: function() {
return React.createElement(
ReactTransitionGroup,
assign({}, this.props, {childFactory: this._wrapChild})
Object.assign({}, this.props, {childFactory: this._wrapChild})
);
},
});
Expand Down
3 changes: 1 addition & 2 deletions src/addons/transitions/ReactTransitionGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
var React = require('React');
var ReactTransitionChildMapping = require('ReactTransitionChildMapping');

var assign = require('Object.assign');
var emptyFunction = require('emptyFunction');

var ReactTransitionGroup = React.createClass({
Expand Down Expand Up @@ -193,7 +192,7 @@ var ReactTransitionGroup = React.createClass({
this.performEnter(key);
} else {
this.setState(function(state) {
var newChildren = assign({}, state.children);
var newChildren = Object.assign({}, state.children);
delete newChildren[key];
return {children: newChildren};
});
Expand Down
5 changes: 2 additions & 3 deletions src/addons/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

'use strict';

var assign = require('Object.assign');
var keyOf = require('keyOf');
var invariant = require('invariant');
var hasOwnProperty = {}.hasOwnProperty;
Expand All @@ -22,7 +21,7 @@ function shallowCopy(x) {
if (Array.isArray(x)) {
return x.concat();
} else if (x && typeof x === 'object') {
return assign(new x.constructor(), x);
return Object.assign(new x.constructor(), x);
} else {
return x;
}
Expand Down Expand Up @@ -102,7 +101,7 @@ function update(value, spec) {
COMMAND_MERGE,
nextValue
);
assign(nextValue, spec[COMMAND_MERGE]);
Object.assign(nextValue, spec[COMMAND_MERGE]);
}

if (hasOwnProperty.call(spec, COMMAND_PUSH)) {
Expand Down
3 changes: 1 addition & 2 deletions src/isomorphic/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var ReactElementValidator = require('ReactElementValidator');
var ReactPropTypes = require('ReactPropTypes');
var ReactVersion = require('ReactVersion');

var assign = require('Object.assign');
var onlyChild = require('onlyChild');

var createElement = ReactElement.createElement;
Expand Down Expand Up @@ -68,7 +67,7 @@ var React = {
version: ReactVersion,

// Hook for JSX spread, don't use this for anything else.
__spread: assign,
__spread: Object.assign,
};

module.exports = React;
9 changes: 4 additions & 5 deletions src/isomorphic/classic/class/ReactClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var ReactPropTypeLocations = require('ReactPropTypeLocations');
var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');
var ReactNoopUpdateQueue = require('ReactNoopUpdateQueue');

var assign = require('Object.assign');
var emptyObject = require('emptyObject');
var invariant = require('invariant');
var keyMirror = require('keyMirror');
Expand Down Expand Up @@ -331,7 +330,7 @@ var RESERVED_SPEC_KEYS = {
ReactPropTypeLocations.childContext
);
}
Constructor.childContextTypes = assign(
Constructor.childContextTypes = Object.assign(
{},
Constructor.childContextTypes,
childContextTypes
Expand All @@ -345,7 +344,7 @@ var RESERVED_SPEC_KEYS = {
ReactPropTypeLocations.context
);
}
Constructor.contextTypes = assign(
Constructor.contextTypes = Object.assign(
{},
Constructor.contextTypes,
contextTypes
Expand Down Expand Up @@ -373,7 +372,7 @@ var RESERVED_SPEC_KEYS = {
ReactPropTypeLocations.prop
);
}
Constructor.propTypes = assign(
Constructor.propTypes = Object.assign(
{},
Constructor.propTypes,
propTypes
Expand Down Expand Up @@ -726,7 +725,7 @@ var ReactClassMixin = {
};

var ReactClassComponent = function() {};
assign(
Object.assign(
ReactClassComponent.prototype,
ReactComponent.prototype,
ReactClassMixin
Expand Down
3 changes: 1 addition & 2 deletions src/isomorphic/classic/element/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

var ReactCurrentOwner = require('ReactCurrentOwner');

var assign = require('Object.assign');
var warning = require('warning');
var canDefineProperty = require('canDefineProperty');

Expand Down Expand Up @@ -253,7 +252,7 @@ ReactElement.cloneElement = function(element, config, children) {
var propName;

// Original props are copied
var props = assign({}, element.props);
var props = Object.assign({}, element.props);

// Reserved names are extracted
var key = element.key;
Expand Down
3 changes: 1 addition & 2 deletions src/isomorphic/deprecated/OrderedMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

'use strict';

var assign = require('Object.assign');
var invariant = require('invariant');

var PREFIX = 'key:';
Expand Down Expand Up @@ -475,7 +474,7 @@ var OrderedMapMethods = {
},
};

assign(OrderedMapImpl.prototype, OrderedMapMethods);
Object.assign(OrderedMapImpl.prototype, OrderedMapMethods);

var OrderedMap = {
from: function(orderedMap) {
Expand Down
5 changes: 2 additions & 3 deletions src/isomorphic/deprecated/ReactPropTransferer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

'use strict';

var assign = require('Object.assign');
var emptyFunction = require('emptyFunction');
var joinClasses = require('joinClasses');

Expand All @@ -36,7 +35,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 assign({}, b, a);
return Object.assign({}, b, a);
});

/**
Expand Down Expand Up @@ -100,7 +99,7 @@ var ReactPropTransferer = {
* @return {object} a new object containing both sets of props merged.
*/
mergeProps: function(oldProps, newProps) {
return transferInto(assign({}, oldProps), newProps);
return transferInto(Object.assign({}, oldProps), newProps);
},

};
Expand Down
3 changes: 1 addition & 2 deletions src/renderers/dom/client/ReactBrowserEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var EventPluginRegistry = require('EventPluginRegistry');
var ReactEventEmitterMixin = require('ReactEventEmitterMixin');
var ViewportMetrics = require('ViewportMetrics');

var assign = require('Object.assign');
var getVendorPrefixedEventName = require('getVendorPrefixedEventName');
var isEventSupported = require('isEventSupported');

Expand Down Expand Up @@ -175,7 +174,7 @@ function getListeningForDocument(mountAt) {
*
* @internal
*/
var ReactBrowserEventEmitter = assign({}, ReactEventEmitterMixin, {
var ReactBrowserEventEmitter = Object.assign({}, ReactEventEmitterMixin, {

/**
* Injectable event backend
Expand Down
3 changes: 1 addition & 2 deletions src/renderers/dom/client/ReactEventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var PooledClass = require('PooledClass');
var ReactDOMComponentTree = require('ReactDOMComponentTree');
var ReactUpdates = require('ReactUpdates');

var assign = require('Object.assign');
var getEventTarget = require('getEventTarget');
var getUnboundedScrollPosition = require('getUnboundedScrollPosition');

Expand All @@ -44,7 +43,7 @@ function TopLevelCallbackBookKeeping(topLevelType, nativeEvent) {
this.nativeEvent = nativeEvent;
this.ancestors = [];
}
assign(TopLevelCallbackBookKeeping.prototype, {
Object.assign(TopLevelCallbackBookKeeping.prototype, {
destructor: function() {
this.topLevelType = null;
this.nativeEvent = null;
Expand Down
3 changes: 1 addition & 2 deletions src/renderers/dom/client/ReactReconcileTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var ReactBrowserEventEmitter = require('ReactBrowserEventEmitter');
var ReactInputSelection = require('ReactInputSelection');
var Transaction = require('Transaction');

var assign = require('Object.assign');

/**
* Ensures that, when possible, the selection range (currently selected text
Expand Down Expand Up @@ -160,7 +159,7 @@ var Mixin = {
};


assign(ReactReconcileTransaction.prototype, Transaction.Mixin, Mixin);
Object.assign(ReactReconcileTransaction.prototype, Transaction.Mixin, Mixin);

PooledClass.addPoolingTo(ReactReconcileTransaction);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

var PooledClass = require('PooledClass');

var assign = require('Object.assign');
var getTextContentAccessor = require('getTextContentAccessor');

/**
Expand All @@ -33,7 +32,7 @@ function FallbackCompositionState(root) {
this._fallbackText = null;
}

assign(FallbackCompositionState.prototype, {
Object.assign(FallbackCompositionState.prototype, {
destructor: function() {
this._root = null;
this._startText = null;
Expand Down
7 changes: 3 additions & 4 deletions src/renderers/dom/client/syntheticEvents/SyntheticEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

var PooledClass = require('PooledClass');

var assign = require('Object.assign');
var emptyFunction = require('emptyFunction');
var warning = require('warning');

Expand Down Expand Up @@ -111,7 +110,7 @@ function SyntheticEvent(dispatchConfig, targetInst, nativeEvent, nativeEventTarg
return this;
}

assign(SyntheticEvent.prototype, {
Object.assign(SyntheticEvent.prototype, {

preventDefault: function() {
this.defaultPrevented = true;
Expand Down Expand Up @@ -229,11 +228,11 @@ SyntheticEvent.augmentClass = function(Class, Interface) {
E.prototype = Super.prototype;
var prototype = new E();

assign(prototype, Class.prototype);
Object.assign(prototype, Class.prototype);
Class.prototype = prototype;
Class.prototype.constructor = Class;

Class.Interface = assign({}, Super.Interface, Interface);
Class.Interface = Object.assign({}, Super.Interface, Interface);
Class.augmentClass = Super.augmentClass;

PooledClass.addPoolingTo(Class, PooledClass.fourArgumentPooler);
Expand Down
3 changes: 1 addition & 2 deletions src/renderers/dom/client/validateDOMNesting.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

'use strict';

var assign = require('Object.assign');
var emptyFunction = require('emptyFunction');
var warning = require('warning');

Expand Down Expand Up @@ -76,7 +75,7 @@ if (__DEV__) {
};

var updatedAncestorInfo = function(oldInfo, tag, instance) {
var ancestorInfo = assign({}, oldInfo || emptyAncestorInfo);
var ancestorInfo = Object.assign({}, oldInfo || emptyAncestorInfo);
var info = {tag: tag, instance: instance};

if (inScopeTags.indexOf(tag) !== -1) {
Expand Down
3 changes: 1 addition & 2 deletions src/renderers/dom/client/wrappers/ReactDOMInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var LinkedValueUtils = require('LinkedValueUtils');
var ReactDOMComponentTree = require('ReactDOMComponentTree');
var ReactUpdates = require('ReactUpdates');

var assign = require('Object.assign');
var invariant = require('invariant');
var warning = require('warning');

Expand Down Expand Up @@ -69,7 +68,7 @@ var ReactDOMInput = {
var value = LinkedValueUtils.getValue(props);
var checked = LinkedValueUtils.getChecked(props);

var nativeProps = assign({
var nativeProps = Object.assign({
// Make sure we set .type before any other properties (setting .value
// before .type means .value is lost in IE11 and below)
type: undefined,
Expand Down
3 changes: 1 addition & 2 deletions src/renderers/dom/client/wrappers/ReactDOMOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
var ReactChildren = require('ReactChildren');
var ReactDOMSelect = require('ReactDOMSelect');

var assign = require('Object.assign');
var warning = require('warning');

/**
Expand Down Expand Up @@ -59,7 +58,7 @@ var ReactDOMOption = {
},

getNativeProps: function(inst, props) {
var nativeProps = assign({selected: undefined, children: undefined}, props);
var nativeProps = Object.assign({selected: undefined, children: undefined}, props);

// Read state only from initial mount because <select> updates value
// manually; we need the initial state only for server rendering
Expand Down
Loading

0 comments on commit 38fcebf

Please sign in to comment.