Skip to content

Commit

Permalink
Use shared/assign instead of Object.assign in code
Browse files Browse the repository at this point in the history
This is so that we have one cached local instance in the bundle.

Ideally we should have a compile do this for us but we already follow
this pattern with hasOwnProperty, isArray, Object.is etc.
  • Loading branch information
sebmarkbage committed Feb 23, 2022
1 parent 38623ae commit 42c6a76
Show file tree
Hide file tree
Showing 18 changed files with 64 additions and 30 deletions.
3 changes: 2 additions & 1 deletion packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
} from 'react-reconciler/src/ReactInternalTypes';

import ErrorStackParser from 'error-stack-parser';
import assign from 'shared/assign';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {
FunctionComponent,
Expand Down Expand Up @@ -720,7 +721,7 @@ function inspectHooksOfForwardRef<Props, Ref>(
function resolveDefaultProps(Component, baseProps) {
if (Component && Component.defaultProps) {
// Resolve default props. Taken from ReactElement
const props = Object.assign({}, baseProps);
const props = assign({}, baseProps);
const defaultProps = Component.defaultProps;
for (const propName in defaultProps) {
if (props[propName] === undefined) {
Expand Down
3 changes: 2 additions & 1 deletion packages/react-dom/src/client/ReactDOMInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {getToStringValue, toString} from './ToStringValue';
import {checkControlledValueProps} from '../shared/ReactControlledValuePropTypes';
import {updateValueIfChanged} from './inputValueTracking';
import getActiveElement from './getActiveElement';
import assign from 'shared/assign';
import {disableInputAttributeSyncing} from 'shared/ReactFeatureFlags';
import {checkAttributeStringCoercion} from 'shared/CheckStringCoercion';

Expand Down Expand Up @@ -62,7 +63,7 @@ export function getHostProps(element: Element, props: Object) {
const node = ((element: any): InputWithWrapperState);
const checked = props.checked;

const hostProps = Object.assign({}, props, {
const hostProps = assign({}, props, {
defaultChecked: undefined,
defaultValue: undefined,
value: undefined,
Expand Down
3 changes: 2 additions & 1 deletion packages/react-dom/src/client/ReactDOMSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {getCurrentFiberOwnerNameInDevOrNull} from 'react-reconciler/src/ReactCur

import {checkControlledValueProps} from '../shared/ReactControlledValuePropTypes';
import {getToStringValue, toString} from './ToStringValue';
import assign from 'shared/assign';
import isArray from 'shared/isArray';

let didWarnValueDefaultValue;
Expand Down Expand Up @@ -134,7 +135,7 @@ function updateOptions(
*/

export function getHostProps(element: Element, props: Object) {
return Object.assign({}, props, {
return assign({}, props, {
value: undefined,
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/react-dom/src/events/SyntheticEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/* eslint valid-typeof: 0 */

import assign from 'shared/assign';
import getEventCharCode from './getEventCharCode';

type EventInterfaceType = {
Expand Down Expand Up @@ -78,7 +79,7 @@ function createSyntheticEvent(Interface: EventInterfaceType) {
return this;
}

Object.assign(SyntheticBaseEvent.prototype, {
assign(SyntheticBaseEvent.prototype, {
preventDefault: function() {
this.defaultPrevented = true;
const event = this.nativeEvent;
Expand Down
21 changes: 11 additions & 10 deletions packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import warnValidStyle from '../shared/warnValidStyle';
import {validateProperties as validateARIAProperties} from '../shared/ReactDOMInvalidARIAHook';
import {validateProperties as validateInputProperties} from '../shared/ReactDOMNullInputValuePropHook';
import {validateProperties as validateUnknownProperties} from '../shared/ReactDOMUnknownPropertyHook';
import assign from 'shared/assign';
import hasOwnProperty from 'shared/hasOwnProperty';

// Based on reading the React.Children implementation. TODO: type this somewhere?
Expand Down Expand Up @@ -563,7 +564,7 @@ function resolve(
}

if (partialState != null) {
inst.state = Object.assign({}, inst.state, partialState);
inst.state = assign({}, inst.state, partialState);
}
}
} else {
Expand Down Expand Up @@ -695,9 +696,9 @@ function resolve(
if (partialState != null) {
if (dontMutate) {
dontMutate = false;
nextState = Object.assign({}, nextState, partialState);
nextState = assign({}, nextState, partialState);
} else {
Object.assign(nextState, partialState);
assign(nextState, partialState);
}
}
}
Expand Down Expand Up @@ -745,7 +746,7 @@ function resolve(
}
}
if (childContext) {
context = Object.assign({}, context, childContext);
context = assign({}, context, childContext);
}
}
}
Expand Down Expand Up @@ -1192,7 +1193,7 @@ class ReactDOMServerRenderer {
const nextChildren = [
React.createElement(
elementType.type,
Object.assign({ref: element.ref}, element.props),
assign({ref: element.ref}, element.props),
),
];
const frame: Frame = {
Expand Down Expand Up @@ -1291,7 +1292,7 @@ class ReactDOMServerRenderer {
const nextChildren = [
React.createElement(
result,
Object.assign({ref: element.ref}, element.props),
assign({ref: element.ref}, element.props),
),
];
const frame: Frame = {
Expand Down Expand Up @@ -1413,7 +1414,7 @@ class ReactDOMServerRenderer {
}
}

props = Object.assign(
props = assign(
{
type: undefined,
},
Expand Down Expand Up @@ -1485,7 +1486,7 @@ class ReactDOMServerRenderer {
if (__DEV__) {
checkFormFieldValueStringCoercion(initialValue);
}
props = Object.assign({}, props, {
props = assign({}, props, {
value: undefined,
children: '' + initialValue,
});
Expand Down Expand Up @@ -1531,7 +1532,7 @@ class ReactDOMServerRenderer {
}
this.currentSelectValue =
props.value != null ? props.value : props.defaultValue;
props = Object.assign({}, props, {
props = assign({}, props, {
value: undefined,
});
} else if (tag === 'option') {
Expand Down Expand Up @@ -1577,7 +1578,7 @@ class ReactDOMServerRenderer {
selected = '' + selectValue === value;
}

props = Object.assign(
props = assign(
{
selected: undefined,
},
Expand Down
3 changes: 2 additions & 1 deletion packages/react-dom/src/test-utils/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
rethrowCaughtError,
invokeGuardedCallbackAndCatchFirstError,
} from 'shared/ReactErrorUtils';
import assign from 'shared/assign';
import isArray from 'shared/isArray';

// Keep in sync with ReactDOM.js:
Expand Down Expand Up @@ -596,7 +597,7 @@ function makeSimulator(eventType) {
// Since we aren't using pooling, always persist the event. This will make
// sure it's marked and won't warn when setting additional properties.
event.persist();
Object.assign(event, eventData);
assign(event, eventData);

if (directDispatchEventTypes.has(eventType)) {
accumulateDirectDispatchesSingle(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/* eslint valid-typeof: 0 */

import assign from 'shared/assign';

const EVENT_POOL_SIZE = 10;

/**
Expand Down Expand Up @@ -110,7 +112,7 @@ function SyntheticEvent(
return this;
}

Object.assign(SyntheticEvent.prototype, {
assign(SyntheticEvent.prototype, {
preventDefault: function() {
this.defaultPrevented = true;
const event = this.nativeEvent;
Expand Down Expand Up @@ -236,11 +238,11 @@ SyntheticEvent.extend = function(Interface) {
function Class() {
return Super.apply(this, arguments);
}
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.extend = Super.extend;
addEventPoolingTo(Class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {get as getInstance, set as setInstance} from 'shared/ReactInstanceMap';
import shallowEqual from 'shared/shallowEqual';
import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
import getComponentNameFromType from 'shared/getComponentNameFromType';
import assign from 'shared/assign';
import isArray from 'shared/isArray';
import {REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE} from 'shared/ReactSymbols';

Expand Down Expand Up @@ -186,7 +187,7 @@ function applyDerivedStateFromProps(
const memoizedState =
partialState === null || partialState === undefined
? prevState
: Object.assign({}, prevState, partialState);
: assign({}, prevState, partialState);
workInProgress.memoizedState = memoizedState;

// Once the update queue is empty, persist the derived state onto the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {get as getInstance, set as setInstance} from 'shared/ReactInstanceMap';
import shallowEqual from 'shared/shallowEqual';
import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
import getComponentNameFromType from 'shared/getComponentNameFromType';
import assign from 'shared/assign';
import isArray from 'shared/isArray';
import {REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE} from 'shared/ReactSymbols';

Expand Down Expand Up @@ -186,7 +187,7 @@ function applyDerivedStateFromProps(
const memoizedState =
partialState === null || partialState === undefined
? prevState
: Object.assign({}, prevState, partialState);
: assign({}, prevState, partialState);
workInProgress.memoizedState = memoizedState;

// Once the update queue is empty, persist the derived state onto the
Expand Down
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiberLazyComponent.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
* @flow
*/

import assign from 'shared/assign';

export function resolveDefaultProps(Component: any, baseProps: Object): Object {
if (Component && Component.defaultProps) {
// Resolve default props. Taken from ReactElement
const props = Object.assign({}, baseProps);
const props = assign({}, baseProps);
const defaultProps = Component.defaultProps;
for (const propName in defaultProps) {
if (props[propName] === undefined) {
Expand Down
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiberLazyComponent.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
* @flow
*/

import assign from 'shared/assign';

export function resolveDefaultProps(Component: any, baseProps: Object): Object {
if (Component && Component.defaultProps) {
// Resolve default props. Taken from ReactElement
const props = Object.assign({}, baseProps);
const props = assign({}, baseProps);
const defaultProps = Component.defaultProps;
for (const propName in defaultProps) {
if (props[propName] === undefined) {
Expand Down
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactUpdateQueue.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ import {
import {pushInterleavedQueue} from './ReactFiberInterleavedUpdates.new';
import {setIsStrictModeForDevtools} from './ReactFiberDevToolsHook.new';

import assign from 'shared/assign';

export type Update<State> = {|
// TODO: Temporary field. Will remove this by storing a map of
// transition -> event time on the root.
Expand Down Expand Up @@ -442,7 +444,7 @@ function getStateFromUpdate<State>(
return prevState;
}
// Merge the partial state and the previous state.
return Object.assign({}, prevState, partialState);
return assign({}, prevState, partialState);
}
case ForceUpdate: {
hasForceUpdate = true;
Expand Down
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactUpdateQueue.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ import {
import {pushInterleavedQueue} from './ReactFiberInterleavedUpdates.old';
import {setIsStrictModeForDevtools} from './ReactFiberDevToolsHook.old';

import assign from 'shared/assign';

export type Update<State> = {|
// TODO: Temporary field. Will remove this by storing a map of
// transition -> event time on the root.
Expand Down Expand Up @@ -442,7 +444,7 @@ function getStateFromUpdate<State>(
return prevState;
}
// Merge the partial state and the previous state.
return Object.assign({}, prevState, partialState);
return assign({}, prevState, partialState);
}
case ForceUpdate: {
hasForceUpdate = true;
Expand Down
7 changes: 4 additions & 3 deletions packages/react-server/src/ReactFizzClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import {get as getInstance, set as setInstance} from 'shared/ReactInstanceMap';
import getComponentNameFromType from 'shared/getComponentNameFromType';
import {REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE} from 'shared/ReactSymbols';
import assign from 'shared/assign';
import isArray from 'shared/isArray';

const didWarnAboutNoopUpdateForComponent = {};
Expand Down Expand Up @@ -161,7 +162,7 @@ function applyDerivedStateFromProps(
const newState =
partialState === null || partialState === undefined
? prevState
: Object.assign({}, prevState, partialState);
: assign({}, prevState, partialState);
return newState;
}

Expand Down Expand Up @@ -602,9 +603,9 @@ function processUpdateQueue(
if (partialState != null) {
if (dontMutate) {
dontMutate = false;
nextState = Object.assign({}, nextState, partialState);
nextState = assign({}, nextState, partialState);
} else {
Object.assign(nextState, partialState);
assign(nextState, partialState);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/react-server/src/ReactFizzServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ import {
enableSuspenseAvoidThisFallbackFizz,
} from 'shared/ReactFeatureFlags';

import assign from 'shared/assign';
import getComponentNameFromType from 'shared/getComponentNameFromType';
import isArray from 'shared/isArray';

Expand Down Expand Up @@ -837,7 +838,7 @@ function validateFunctionComponentInDev(Component: any): void {
function resolveDefaultProps(Component: any, baseProps: Object): Object {
if (Component && Component.defaultProps) {
// Resolve default props. Taken from ReactElement
const props = Object.assign({}, baseProps);
const props = assign({}, baseProps);
const defaultProps = Component.defaultProps;
for (const propName in defaultProps) {
if (props[propName] === undefined) {
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/ReactBaseClasses.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import ReactNoopUpdateQueue from './ReactNoopUpdateQueue';
import assign from 'shared/assign';

const emptyObject = {};
if (__DEV__) {
Expand Down Expand Up @@ -139,7 +140,7 @@ function PureComponent(props, context, updater) {
const pureComponentPrototype = (PureComponent.prototype = new ComponentDummy());
pureComponentPrototype.constructor = PureComponent;
// Avoid an extra prototype jump for these methods.
Object.assign(pureComponentPrototype, Component.prototype);
assign(pureComponentPrototype, Component.prototype);
pureComponentPrototype.isPureReactComponent = true;

export {Component, PureComponent};
3 changes: 2 additions & 1 deletion packages/react/src/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import getComponentNameFromType from 'shared/getComponentNameFromType';
import {REACT_ELEMENT_TYPE} from 'shared/ReactSymbols';
import assign from 'shared/assign';
import hasOwnProperty from 'shared/hasOwnProperty';
import {checkKeyStringCoercion} from 'shared/CheckStringCoercion';

Expand Down Expand Up @@ -492,7 +493,7 @@ export function cloneElement(element, config, children) {
let propName;

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

// Reserved names are extracted
let key = element.key;
Expand Down
12 changes: 12 additions & 0 deletions packages/shared/assign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

const assign = Object.assign;

export default assign;

0 comments on commit 42c6a76

Please sign in to comment.