Inline fbjs/lib/emptyFunction (#13054)

This commit is contained in:
Dan Abramov
2018-06-15 18:45:14 +01:00
committed by GitHub
parent 72434a7686
commit ae14317d68
8 changed files with 33 additions and 37 deletions
+20 -11
View File
@@ -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', () => {}),
);
}
},
+1 -1
View File
@@ -9,7 +9,7 @@
'use strict';
const emptyFunction = require('fbjs/lib/emptyFunction');
function emptyFunction() {}
describe('ReactDOMInput', () => {
let React;
+1 -1
View File
@@ -9,7 +9,7 @@
'use strict';
const emptyFunction = require('fbjs/lib/emptyFunction');
function emptyFunction() {}
describe('ReactDOMTextarea', () => {
let React;
+4 -3
View File
@@ -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(
+1 -2
View File
@@ -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:
+2 -3
View File
@@ -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<Frame>) => {};
+1 -2
View File
@@ -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
+3 -14
View File
@@ -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;
}