Inline fbjs/lib/emptyFunction (#13054)
Dan Abramov committed
Jun 15, 2018 at 18:45 UTC
ae14317d6810d5b07d01308a2116a707e5e09983
8 files changed
+33
-37
packages/events/SyntheticEvent.js
+20
-11
@@ -7,7 +7,6 @@
7
8
/* eslint valid-typeof: 0 */
9
10
-import emptyFunction from 'fbjs/lib/emptyFunction';
10
import invariant from 'fbjs/lib/invariant';
11
import warning from 'fbjs/lib/warning';
12
@@ -32,7 +31,9 @@ const EventInterface = {
31
type: null,
32
target: null,
33
// currentTarget is set when dispatching; no use in copying it here
35
- currentTarget: emptyFunction.thatReturnsNull,
34
+ currentTarget: function() {
35
+ return null;
36
+ },
37
eventPhase: null,
38
bubbles: null,
39
cancelable: null,
@@ -43,6 +44,14 @@ const EventInterface = {
44
isTrusted: null,
45
};
46
47
+function functionThatReturnsTrue() {
48
+ return true;
49
+}
50
+
51
+function functionThatReturnsFalse() {
52
+ return false;
53
+}
54
+
55
/**
56
* Synthetic events are dispatched by event plugins, typically in response to a
57
* top-level event delegation handler.
@@ -103,11 +112,11 @@ function SyntheticEvent(
112
? nativeEvent.defaultPrevented
113
: nativeEvent.returnValue === false;
114
if (defaultPrevented) {
106
- this.isDefaultPrevented = emptyFunction.thatReturnsTrue;
115
+ this.isDefaultPrevented = functionThatReturnsTrue;
116
} else {
108
- this.isDefaultPrevented = emptyFunction.thatReturnsFalse;
117
+ this.isDefaultPrevented = functionThatReturnsFalse;
118
}
110
- this.isPropagationStopped = emptyFunction.thatReturnsFalse;
119
+ this.isPropagationStopped = functionThatReturnsFalse;
120
return this;
121
}
122
@@ -124,7 +133,7 @@ Object.assign(SyntheticEvent.prototype, {
133
} else if (typeof event.returnValue !== 'unknown') {
134
event.returnValue = false;
135
}
127
- this.isDefaultPrevented = emptyFunction.thatReturnsTrue;
136
+ this.isDefaultPrevented = functionThatReturnsTrue;
137
},
138
139
stopPropagation: function() {
@@ -144,7 +153,7 @@ Object.assign(SyntheticEvent.prototype, {
153
event.cancelBubble = true;
154
}
155
147
- this.isPropagationStopped = emptyFunction.thatReturnsTrue;
156
+ this.isPropagationStopped = functionThatReturnsTrue;
157
},
158
159
/**
@@ -153,7 +162,7 @@ Object.assign(SyntheticEvent.prototype, {
162
* won't be added back into the pool.
163
*/
164
persist: function() {
156
- this.isPersistent = emptyFunction.thatReturnsTrue;
165
+ this.isPersistent = functionThatReturnsTrue;
166
},
167
168
/**
@@ -161,7 +170,7 @@ Object.assign(SyntheticEvent.prototype, {
170
*
171
* @return {boolean} True if this should not be released, false otherwise.
172
*/
164
- isPersistent: emptyFunction.thatReturnsFalse,
173
+ isPersistent: functionThatReturnsFalse,
174
175
/**
176
* `PooledClass` looks for `destructor` on each instance it releases.
@@ -191,12 +200,12 @@ Object.assign(SyntheticEvent.prototype, {
200
Object.defineProperty(
201
this,
202
'preventDefault',
194
- getPooledWarningPropertyDefinition('preventDefault', emptyFunction),
203
+ getPooledWarningPropertyDefinition('preventDefault', () => {}),
204
);
205
Object.defineProperty(
206
this,
207
'stopPropagation',
199
- getPooledWarningPropertyDefinition('stopPropagation', emptyFunction),
208
+ getPooledWarningPropertyDefinition('stopPropagation', () => {}),
209
);
210
}
211
},
packages/react-dom/src/__tests__/ReactDOMInput-test.js
+1
-1
@@ -9,7 +9,7 @@
9
10
'use strict';
11
12
-const emptyFunction = require('fbjs/lib/emptyFunction');
12
+function emptyFunction() {}
13
14
describe('ReactDOMInput', () => {
15
let React;
packages/react-dom/src/__tests__/ReactDOMTextarea-test.js
+1
-1
@@ -9,7 +9,7 @@
9
10
'use strict';
11
12
-const emptyFunction = require('fbjs/lib/emptyFunction');
12
+function emptyFunction() {}
13
14
describe('ReactDOMTextarea', () => {
15
let React;
packages/react-dom/src/client/ReactDOMFiberComponent.js
+4
-3
@@ -10,7 +10,6 @@
10
// TODO: direct imports like some-package/src/* are bad. Fix me.
11
import ReactDebugCurrentFiber from 'react-reconciler/src/ReactDebugCurrentFiber';
12
import {registrationNameModules} from 'events/EventPluginRegistry';
13
-import emptyFunction from 'fbjs/lib/emptyFunction';
13
import warning from 'fbjs/lib/warning';
14
15
import * as DOMPropertyOperations from './DOMPropertyOperations';
@@ -63,7 +62,7 @@ const HTML = '__html';
62
63
const {html: HTML_NAMESPACE} = Namespaces;
64
66
-let getStack = emptyFunction.thatReturns('');
65
+let getStack = () => '';
66
67
let warnedUnknownTags;
68
let suppressHydrationWarning;
@@ -232,6 +231,8 @@ function getOwnerDocumentFromRootContainer(
231
: rootContainerElement.ownerDocument;
232
}
233
234
+function noop() {}
235
+
236
function trapClickOnNonInteractiveElement(node: HTMLElement) {
237
// Mobile Safari does not fire properly bubble click events on
238
// non-interactive elements, which means delegated click listeners do not
@@ -242,7 +243,7 @@ function trapClickOnNonInteractiveElement(node: HTMLElement) {
243
// bookkeeping for it. Not sure if we need to clear it when the listener is
244
// removed.
245
// TODO: Only do this for the relevant Safaris maybe?
245
- node.onclick = emptyFunction;
246
+ node.onclick = noop;
247
}
248
249
function setInitialDOMProperties(
packages/react-dom/src/client/validateDOMNesting.js
+1
-2
@@ -5,13 +5,12 @@
5
* LICENSE file in the root directory of this source tree.
6
*/
7
8
-import emptyFunction from 'fbjs/lib/emptyFunction';
8
import warning from 'fbjs/lib/warning';
9
// TODO: direct imports like some-package/src/* are bad. Fix me.
10
import ReactDebugCurrentFiber from 'react-reconciler/src/ReactDebugCurrentFiber';
11
12
const {getCurrentFiberStackAddendum} = ReactDebugCurrentFiber;
14
-let validateDOMNesting = emptyFunction;
13
+let validateDOMNesting = () => {};
14
15
if (__DEV__) {
16
// This validation code was written based on the HTML5 parsing spec:
packages/react-dom/src/server/ReactPartialRenderer.js
+2
-3
@@ -15,7 +15,6 @@ import type {
15
} from 'shared/ReactTypes';
16
17
import React from 'react';
18
-import emptyFunction from 'fbjs/lib/emptyFunction';
18
import emptyObject from 'fbjs/lib/emptyObject';
19
import invariant from 'fbjs/lib/invariant';
20
import lowPriorityWarning from 'shared/lowPriorityWarning';
@@ -66,8 +65,8 @@ const toArray = ((React.Children.toArray: any): toArrayType);
65
let currentDebugStack;
66
let currentDebugElementStack;
67
69
-let getStackAddendum = emptyFunction.thatReturns('');
70
-let describeStackFrame = emptyFunction.thatReturns('');
68
+let getStackAddendum = () => '';
69
+let describeStackFrame = element => '';
70
71
let validatePropertiesInDevelopment = (type, props) => {};
72
let setCurrentDebugStack = (stack: Array<Frame>) => {};
packages/react-dom/src/shared/warnValidStyle.js
+1
-2
@@ -5,10 +5,9 @@
5
* LICENSE file in the root directory of this source tree.
6
*/
7
8
-import emptyFunction from 'fbjs/lib/emptyFunction';
8
import warning from 'fbjs/lib/warning';
9
11
-let warnValidStyle = emptyFunction;
10
+let warnValidStyle = () => {};
11
12
if (__DEV__) {
13
// 'msTransform' is correct, but the other prefixes should be capitalized
packages/react/src/ReactChildren.js
+3
-14
@@ -5,7 +5,6 @@
5
* LICENSE file in the root directory of this source tree.
6
*/
7
8
-import emptyFunction from 'fbjs/lib/emptyFunction';
8
import invariant from 'fbjs/lib/invariant';
9
import warning from 'fbjs/lib/warning';
10
import {
@@ -292,12 +291,7 @@ function mapSingleChildIntoContext(bookKeeping, child, childKey) {
291
292
let mappedChild = func.call(context, child, bookKeeping.count++);
293
if (Array.isArray(mappedChild)) {
295
- mapIntoWithKeyPrefixInternal(
296
- mappedChild,
297
- result,
298
- childKey,
299
- emptyFunction.thatReturnsArgument,
300
- );
294
+ mapIntoWithKeyPrefixInternal(mappedChild, result, childKey, c => c);
295
} else if (mappedChild != null) {
296
if (isValidElement(mappedChild)) {
297
mappedChild = cloneAndReplaceKey(
@@ -362,7 +356,7 @@ function mapChildren(children, func, context) {
356
* @return {number} The number of children.
357
*/
358
function countChildren(children) {
365
- return traverseAllChildren(children, emptyFunction.thatReturnsNull, null);
359
+ return traverseAllChildren(children, () => null, null);
360
}
361
362
/**
@@ -373,12 +367,7 @@ function countChildren(children) {
367
*/
368
function toArray(children) {
369
const result = [];
376
- mapIntoWithKeyPrefixInternal(
377
- children,
378
- result,
379
- null,
380
- emptyFunction.thatReturnsArgument,
381
- );
370
+ mapIntoWithKeyPrefixInternal(children, result, null, child => child);
371
return result;
372
}
373