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 b0d1e16 commit 1573baa
Show file tree
Hide file tree
Showing 45 changed files with 123 additions and 150 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"

This comment was marked as spam.

Copy link
@NeoSithole

NeoSithole Apr 10, 2022

Rewrite row 24.

]
}
10 changes: 9 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ var paths = {

var babelOpts = {
plugins: [
[babelPluginModules, { map: require('fbjs/module-map') }],
[babelPluginModules, {
map: Object.assign(
{},
require('fbjs/module-map'),
{
'object-assign': 'object-assign',
}
),
}],
],
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"gzip-js": "~0.3.2",
"jest-cli": "^0.9.0",
"loose-envify": "^1.1.0",
"object-assign": "^4.0.1",
"platform": "^1.1.0",
"run-sequence": "^1.1.4",
"through2": "^2.0.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
},
"dependencies": {
"fbjs": "^0.8.0-alpha.2",
"loose-envify": "^1.1.0"
"loose-envify": "^1.1.0",
"object-assign": "^4.0.1"
},
"browserify": {
"transform": [
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) {

This comment has been minimized.

Copy link
@aleclarson

aleclarson Sep 20, 2016

Contributor

@zpao Should this detect when the visited module is object-assign itself? Just ran into this because object-assign was in react/node_modules and had this transform applied to it.

This comment has been minimized.

Copy link
@zpao

zpao Sep 20, 2016

Author Member

You're seeing this when building React? I didn't think we were running this transform on any deps.

This comment has been minimized.

Copy link
@aleclarson

aleclarson Sep 20, 2016

Contributor

It seems the React Native packager is reading react/.babelrc

I assume babel plugins shouldn't be used inside node_modules?
If so, there must be an issue with the packager.

Note: My project has a symlink at node_modules/react that points to build/packages/react (in my react fork). It's possible that when the packager runs babel, it finds the .babelrc in my react fork and uses it on node_modules/object-assign.

react v15.3.2
react-native v0.34.0

/cc @davidaurelio

This comment has been minimized.

Copy link
@zpao

zpao Sep 20, 2016

Author Member

That's a pretty esoteric setup but still surprising (I wouldn't expect the packager to walk up the directory beyond the symlink - .babelrc isn't in build/*). Feel free to do something in the packager but I don't think there's anything for us to do in React.

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;
}
},
},
};
};
10 changes: 9 additions & 1 deletion scripts/jest/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ var pathToBabelrc = path.join(__dirname, '..', '..', '.babelrc');
// TODO: make sure this stays in sync with gulpfile
var babelOptions = {
plugins: [
[babelPluginModules, { map: moduleMap }],
[babelPluginModules, {
map: Object.assign(
{},
moduleMap,
{
'object-assign': 'object-assign',
}
),
}],
],
retainLines: true,
};
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
Loading

0 comments on commit 1573baa

Please sign in to comment.