From ae14317d6810d5b07d01308a2116a707e5e09983 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 15 Jun 2018 18:45:14 +0100 Subject: [PATCH] Inline fbjs/lib/emptyFunction (#13054) --- packages/events/SyntheticEvent.js | 31 ++++++++++++------- .../src/__tests__/ReactDOMInput-test.js | 2 +- .../src/__tests__/ReactDOMTextarea-test.js | 2 +- .../src/client/ReactDOMFiberComponent.js | 7 +++-- .../src/client/validateDOMNesting.js | 3 +- .../src/server/ReactPartialRenderer.js | 5 ++- .../react-dom/src/shared/warnValidStyle.js | 3 +- packages/react/src/ReactChildren.js | 17 ++-------- 8 files changed, 33 insertions(+), 37 deletions(-) diff --git a/packages/events/SyntheticEvent.js b/packages/events/SyntheticEvent.js index beaaa5947e..b8693d8d37 100644 --- a/packages/events/SyntheticEvent.js +++ b/packages/events/SyntheticEvent.js @@ -7,7 +7,6 @@ /* eslint valid-typeof: 0 */ -import emptyFunction from 'fbjs/lib/emptyFunction'; import invariant from 'fbjs/lib/invariant'; import warning from 'fbjs/lib/warning'; @@ -32,7 +31,9 @@ const EventInterface = { type: null, target: null, // currentTarget is set when dispatching; no use in copying it here - currentTarget: emptyFunction.thatReturnsNull, + currentTarget: function() { + return null; + }, eventPhase: null, bubbles: null, cancelable: null, @@ -43,6 +44,14 @@ const EventInterface = { isTrusted: null, }; +function functionThatReturnsTrue() { + return true; +} + +function functionThatReturnsFalse() { + return false; +} + /** * Synthetic events are dispatched by event plugins, typically in response to a * top-level event delegation handler. @@ -103,11 +112,11 @@ function SyntheticEvent( ? nativeEvent.defaultPrevented : nativeEvent.returnValue === false; if (defaultPrevented) { - this.isDefaultPrevented = emptyFunction.thatReturnsTrue; + this.isDefaultPrevented = functionThatReturnsTrue; } else { - this.isDefaultPrevented = emptyFunction.thatReturnsFalse; + this.isDefaultPrevented = functionThatReturnsFalse; } - this.isPropagationStopped = emptyFunction.thatReturnsFalse; + this.isPropagationStopped = functionThatReturnsFalse; return this; } @@ -124,7 +133,7 @@ Object.assign(SyntheticEvent.prototype, { } else if (typeof event.returnValue !== 'unknown') { event.returnValue = false; } - this.isDefaultPrevented = emptyFunction.thatReturnsTrue; + this.isDefaultPrevented = functionThatReturnsTrue; }, stopPropagation: function() { @@ -144,7 +153,7 @@ Object.assign(SyntheticEvent.prototype, { event.cancelBubble = true; } - this.isPropagationStopped = emptyFunction.thatReturnsTrue; + this.isPropagationStopped = functionThatReturnsTrue; }, /** @@ -153,7 +162,7 @@ Object.assign(SyntheticEvent.prototype, { * won't be added back into the pool. */ persist: function() { - this.isPersistent = emptyFunction.thatReturnsTrue; + this.isPersistent = functionThatReturnsTrue; }, /** @@ -161,7 +170,7 @@ Object.assign(SyntheticEvent.prototype, { * * @return {boolean} True if this should not be released, false otherwise. */ - isPersistent: emptyFunction.thatReturnsFalse, + isPersistent: functionThatReturnsFalse, /** * `PooledClass` looks for `destructor` on each instance it releases. @@ -191,12 +200,12 @@ Object.assign(SyntheticEvent.prototype, { Object.defineProperty( this, 'preventDefault', - getPooledWarningPropertyDefinition('preventDefault', emptyFunction), + getPooledWarningPropertyDefinition('preventDefault', () => {}), ); Object.defineProperty( this, 'stopPropagation', - getPooledWarningPropertyDefinition('stopPropagation', emptyFunction), + getPooledWarningPropertyDefinition('stopPropagation', () => {}), ); } }, diff --git a/packages/react-dom/src/__tests__/ReactDOMInput-test.js b/packages/react-dom/src/__tests__/ReactDOMInput-test.js index ca4b999f5f..578e5b100e 100644 --- a/packages/react-dom/src/__tests__/ReactDOMInput-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMInput-test.js @@ -9,7 +9,7 @@ 'use strict'; -const emptyFunction = require('fbjs/lib/emptyFunction'); +function emptyFunction() {} describe('ReactDOMInput', () => { let React; diff --git a/packages/react-dom/src/__tests__/ReactDOMTextarea-test.js b/packages/react-dom/src/__tests__/ReactDOMTextarea-test.js index 879be14fec..aa5714ded3 100644 --- a/packages/react-dom/src/__tests__/ReactDOMTextarea-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMTextarea-test.js @@ -9,7 +9,7 @@ 'use strict'; -const emptyFunction = require('fbjs/lib/emptyFunction'); +function emptyFunction() {} describe('ReactDOMTextarea', () => { let React; diff --git a/packages/react-dom/src/client/ReactDOMFiberComponent.js b/packages/react-dom/src/client/ReactDOMFiberComponent.js index e089b33098..4975e55f67 100644 --- a/packages/react-dom/src/client/ReactDOMFiberComponent.js +++ b/packages/react-dom/src/client/ReactDOMFiberComponent.js @@ -10,7 +10,6 @@ // TODO: direct imports like some-package/src/* are bad. Fix me. import ReactDebugCurrentFiber from 'react-reconciler/src/ReactDebugCurrentFiber'; import {registrationNameModules} from 'events/EventPluginRegistry'; -import emptyFunction from 'fbjs/lib/emptyFunction'; import warning from 'fbjs/lib/warning'; import * as DOMPropertyOperations from './DOMPropertyOperations'; @@ -63,7 +62,7 @@ const HTML = '__html'; const {html: HTML_NAMESPACE} = Namespaces; -let getStack = emptyFunction.thatReturns(''); +let getStack = () => ''; let warnedUnknownTags; let suppressHydrationWarning; @@ -232,6 +231,8 @@ function getOwnerDocumentFromRootContainer( : rootContainerElement.ownerDocument; } +function noop() {} + function trapClickOnNonInteractiveElement(node: HTMLElement) { // Mobile Safari does not fire properly bubble click events on // non-interactive elements, which means delegated click listeners do not @@ -242,7 +243,7 @@ function trapClickOnNonInteractiveElement(node: HTMLElement) { // bookkeeping for it. Not sure if we need to clear it when the listener is // removed. // TODO: Only do this for the relevant Safaris maybe? - node.onclick = emptyFunction; + node.onclick = noop; } function setInitialDOMProperties( diff --git a/packages/react-dom/src/client/validateDOMNesting.js b/packages/react-dom/src/client/validateDOMNesting.js index 9341bd1e3f..8f843f7a2f 100644 --- a/packages/react-dom/src/client/validateDOMNesting.js +++ b/packages/react-dom/src/client/validateDOMNesting.js @@ -5,13 +5,12 @@ * LICENSE file in the root directory of this source tree. */ -import emptyFunction from 'fbjs/lib/emptyFunction'; import warning from 'fbjs/lib/warning'; // TODO: direct imports like some-package/src/* are bad. Fix me. import ReactDebugCurrentFiber from 'react-reconciler/src/ReactDebugCurrentFiber'; const {getCurrentFiberStackAddendum} = ReactDebugCurrentFiber; -let validateDOMNesting = emptyFunction; +let validateDOMNesting = () => {}; if (__DEV__) { // This validation code was written based on the HTML5 parsing spec: diff --git a/packages/react-dom/src/server/ReactPartialRenderer.js b/packages/react-dom/src/server/ReactPartialRenderer.js index 39bec1d2cf..ebe3ac8512 100644 --- a/packages/react-dom/src/server/ReactPartialRenderer.js +++ b/packages/react-dom/src/server/ReactPartialRenderer.js @@ -15,7 +15,6 @@ import type { } from 'shared/ReactTypes'; import React from 'react'; -import emptyFunction from 'fbjs/lib/emptyFunction'; import emptyObject from 'fbjs/lib/emptyObject'; import invariant from 'fbjs/lib/invariant'; import lowPriorityWarning from 'shared/lowPriorityWarning'; @@ -66,8 +65,8 @@ const toArray = ((React.Children.toArray: any): toArrayType); let currentDebugStack; let currentDebugElementStack; -let getStackAddendum = emptyFunction.thatReturns(''); -let describeStackFrame = emptyFunction.thatReturns(''); +let getStackAddendum = () => ''; +let describeStackFrame = element => ''; let validatePropertiesInDevelopment = (type, props) => {}; let setCurrentDebugStack = (stack: Array) => {}; diff --git a/packages/react-dom/src/shared/warnValidStyle.js b/packages/react-dom/src/shared/warnValidStyle.js index 0b91118722..0231d589bb 100644 --- a/packages/react-dom/src/shared/warnValidStyle.js +++ b/packages/react-dom/src/shared/warnValidStyle.js @@ -5,10 +5,9 @@ * LICENSE file in the root directory of this source tree. */ -import emptyFunction from 'fbjs/lib/emptyFunction'; import warning from 'fbjs/lib/warning'; -let warnValidStyle = emptyFunction; +let warnValidStyle = () => {}; if (__DEV__) { // 'msTransform' is correct, but the other prefixes should be capitalized diff --git a/packages/react/src/ReactChildren.js b/packages/react/src/ReactChildren.js index b99056b439..e01f765c9b 100644 --- a/packages/react/src/ReactChildren.js +++ b/packages/react/src/ReactChildren.js @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import emptyFunction from 'fbjs/lib/emptyFunction'; import invariant from 'fbjs/lib/invariant'; import warning from 'fbjs/lib/warning'; import { @@ -292,12 +291,7 @@ function mapSingleChildIntoContext(bookKeeping, child, childKey) { let mappedChild = func.call(context, child, bookKeeping.count++); if (Array.isArray(mappedChild)) { - mapIntoWithKeyPrefixInternal( - mappedChild, - result, - childKey, - emptyFunction.thatReturnsArgument, - ); + mapIntoWithKeyPrefixInternal(mappedChild, result, childKey, c => c); } else if (mappedChild != null) { if (isValidElement(mappedChild)) { mappedChild = cloneAndReplaceKey( @@ -362,7 +356,7 @@ function mapChildren(children, func, context) { * @return {number} The number of children. */ function countChildren(children) { - return traverseAllChildren(children, emptyFunction.thatReturnsNull, null); + return traverseAllChildren(children, () => null, null); } /** @@ -373,12 +367,7 @@ function countChildren(children) { */ function toArray(children) { const result = []; - mapIntoWithKeyPrefixInternal( - children, - result, - null, - emptyFunction.thatReturnsArgument, - ); + mapIntoWithKeyPrefixInternal(children, result, null, child => child); return result; }