Skip to content

Commit ae14317

Browse files
authored
Inline fbjs/lib/emptyFunction (facebook#13054)
1 parent 72434a7 commit ae14317

File tree

8 files changed

+33
-37
lines changed

8 files changed

+33
-37
lines changed

packages/events/SyntheticEvent.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
/* eslint valid-typeof: 0 */
99

10-
import emptyFunction from 'fbjs/lib/emptyFunction';
1110
import invariant from 'fbjs/lib/invariant';
1211
import warning from 'fbjs/lib/warning';
1312

@@ -32,7 +31,9 @@ const EventInterface = {
3231
type: null,
3332
target: null,
3433
// currentTarget is set when dispatching; no use in copying it here
35-
currentTarget: emptyFunction.thatReturnsNull,
34+
currentTarget: function() {
35+
return null;
36+
},
3637
eventPhase: null,
3738
bubbles: null,
3839
cancelable: null,
@@ -43,6 +44,14 @@ const EventInterface = {
4344
isTrusted: null,
4445
};
4546

47+
function functionThatReturnsTrue() {
48+
return true;
49+
}
50+
51+
function functionThatReturnsFalse() {
52+
return false;
53+
}
54+
4655
/**
4756
* Synthetic events are dispatched by event plugins, typically in response to a
4857
* top-level event delegation handler.
@@ -103,11 +112,11 @@ function SyntheticEvent(
103112
? nativeEvent.defaultPrevented
104113
: nativeEvent.returnValue === false;
105114
if (defaultPrevented) {
106-
this.isDefaultPrevented = emptyFunction.thatReturnsTrue;
115+
this.isDefaultPrevented = functionThatReturnsTrue;
107116
} else {
108-
this.isDefaultPrevented = emptyFunction.thatReturnsFalse;
117+
this.isDefaultPrevented = functionThatReturnsFalse;
109118
}
110-
this.isPropagationStopped = emptyFunction.thatReturnsFalse;
119+
this.isPropagationStopped = functionThatReturnsFalse;
111120
return this;
112121
}
113122

@@ -124,7 +133,7 @@ Object.assign(SyntheticEvent.prototype, {
124133
} else if (typeof event.returnValue !== 'unknown') {
125134
event.returnValue = false;
126135
}
127-
this.isDefaultPrevented = emptyFunction.thatReturnsTrue;
136+
this.isDefaultPrevented = functionThatReturnsTrue;
128137
},
129138

130139
stopPropagation: function() {
@@ -144,7 +153,7 @@ Object.assign(SyntheticEvent.prototype, {
144153
event.cancelBubble = true;
145154
}
146155

147-
this.isPropagationStopped = emptyFunction.thatReturnsTrue;
156+
this.isPropagationStopped = functionThatReturnsTrue;
148157
},
149158

150159
/**
@@ -153,15 +162,15 @@ Object.assign(SyntheticEvent.prototype, {
153162
* won't be added back into the pool.
154163
*/
155164
persist: function() {
156-
this.isPersistent = emptyFunction.thatReturnsTrue;
165+
this.isPersistent = functionThatReturnsTrue;
157166
},
158167

159168
/**
160169
* Checks if this event should be released back into the pool.
161170
*
162171
* @return {boolean} True if this should not be released, false otherwise.
163172
*/
164-
isPersistent: emptyFunction.thatReturnsFalse,
173+
isPersistent: functionThatReturnsFalse,
165174

166175
/**
167176
* `PooledClass` looks for `destructor` on each instance it releases.
@@ -191,12 +200,12 @@ Object.assign(SyntheticEvent.prototype, {
191200
Object.defineProperty(
192201
this,
193202
'preventDefault',
194-
getPooledWarningPropertyDefinition('preventDefault', emptyFunction),
203+
getPooledWarningPropertyDefinition('preventDefault', () => {}),
195204
);
196205
Object.defineProperty(
197206
this,
198207
'stopPropagation',
199-
getPooledWarningPropertyDefinition('stopPropagation', emptyFunction),
208+
getPooledWarningPropertyDefinition('stopPropagation', () => {}),
200209
);
201210
}
202211
},

packages/react-dom/src/__tests__/ReactDOMInput-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
const emptyFunction = require('fbjs/lib/emptyFunction');
12+
function emptyFunction() {}
1313

1414
describe('ReactDOMInput', () => {
1515
let React;

packages/react-dom/src/__tests__/ReactDOMTextarea-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
const emptyFunction = require('fbjs/lib/emptyFunction');
12+
function emptyFunction() {}
1313

1414
describe('ReactDOMTextarea', () => {
1515
let React;

packages/react-dom/src/client/ReactDOMFiberComponent.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// TODO: direct imports like some-package/src/* are bad. Fix me.
1111
import ReactDebugCurrentFiber from 'react-reconciler/src/ReactDebugCurrentFiber';
1212
import {registrationNameModules} from 'events/EventPluginRegistry';
13-
import emptyFunction from 'fbjs/lib/emptyFunction';
1413
import warning from 'fbjs/lib/warning';
1514

1615
import * as DOMPropertyOperations from './DOMPropertyOperations';
@@ -63,7 +62,7 @@ const HTML = '__html';
6362

6463
const {html: HTML_NAMESPACE} = Namespaces;
6564

66-
let getStack = emptyFunction.thatReturns('');
65+
let getStack = () => '';
6766

6867
let warnedUnknownTags;
6968
let suppressHydrationWarning;
@@ -232,6 +231,8 @@ function getOwnerDocumentFromRootContainer(
232231
: rootContainerElement.ownerDocument;
233232
}
234233

234+
function noop() {}
235+
235236
function trapClickOnNonInteractiveElement(node: HTMLElement) {
236237
// Mobile Safari does not fire properly bubble click events on
237238
// non-interactive elements, which means delegated click listeners do not
@@ -242,7 +243,7 @@ function trapClickOnNonInteractiveElement(node: HTMLElement) {
242243
// bookkeeping for it. Not sure if we need to clear it when the listener is
243244
// removed.
244245
// TODO: Only do this for the relevant Safaris maybe?
245-
node.onclick = emptyFunction;
246+
node.onclick = noop;
246247
}
247248

248249
function setInitialDOMProperties(

packages/react-dom/src/client/validateDOMNesting.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import emptyFunction from 'fbjs/lib/emptyFunction';
98
import warning from 'fbjs/lib/warning';
109
// TODO: direct imports like some-package/src/* are bad. Fix me.
1110
import ReactDebugCurrentFiber from 'react-reconciler/src/ReactDebugCurrentFiber';
1211

1312
const {getCurrentFiberStackAddendum} = ReactDebugCurrentFiber;
14-
let validateDOMNesting = emptyFunction;
13+
let validateDOMNesting = () => {};
1514

1615
if (__DEV__) {
1716
// This validation code was written based on the HTML5 parsing spec:

packages/react-dom/src/server/ReactPartialRenderer.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import type {
1515
} from 'shared/ReactTypes';
1616

1717
import React from 'react';
18-
import emptyFunction from 'fbjs/lib/emptyFunction';
1918
import emptyObject from 'fbjs/lib/emptyObject';
2019
import invariant from 'fbjs/lib/invariant';
2120
import lowPriorityWarning from 'shared/lowPriorityWarning';
@@ -66,8 +65,8 @@ const toArray = ((React.Children.toArray: any): toArrayType);
6665
let currentDebugStack;
6766
let currentDebugElementStack;
6867

69-
let getStackAddendum = emptyFunction.thatReturns('');
70-
let describeStackFrame = emptyFunction.thatReturns('');
68+
let getStackAddendum = () => '';
69+
let describeStackFrame = element => '';
7170

7271
let validatePropertiesInDevelopment = (type, props) => {};
7372
let setCurrentDebugStack = (stack: Array<Frame>) => {};

packages/react-dom/src/shared/warnValidStyle.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import emptyFunction from 'fbjs/lib/emptyFunction';
98
import warning from 'fbjs/lib/warning';
109

11-
let warnValidStyle = emptyFunction;
10+
let warnValidStyle = () => {};
1211

1312
if (__DEV__) {
1413
// 'msTransform' is correct, but the other prefixes should be capitalized

packages/react/src/ReactChildren.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import emptyFunction from 'fbjs/lib/emptyFunction';
98
import invariant from 'fbjs/lib/invariant';
109
import warning from 'fbjs/lib/warning';
1110
import {
@@ -292,12 +291,7 @@ function mapSingleChildIntoContext(bookKeeping, child, childKey) {
292291

293292
let mappedChild = func.call(context, child, bookKeeping.count++);
294293
if (Array.isArray(mappedChild)) {
295-
mapIntoWithKeyPrefixInternal(
296-
mappedChild,
297-
result,
298-
childKey,
299-
emptyFunction.thatReturnsArgument,
300-
);
294+
mapIntoWithKeyPrefixInternal(mappedChild, result, childKey, c => c);
301295
} else if (mappedChild != null) {
302296
if (isValidElement(mappedChild)) {
303297
mappedChild = cloneAndReplaceKey(
@@ -362,7 +356,7 @@ function mapChildren(children, func, context) {
362356
* @return {number} The number of children.
363357
*/
364358
function countChildren(children) {
365-
return traverseAllChildren(children, emptyFunction.thatReturnsNull, null);
359+
return traverseAllChildren(children, () => null, null);
366360
}
367361

368362
/**
@@ -373,12 +367,7 @@ function countChildren(children) {
373367
*/
374368
function toArray(children) {
375369
const result = [];
376-
mapIntoWithKeyPrefixInternal(
377-
children,
378-
result,
379-
null,
380-
emptyFunction.thatReturnsArgument,
381-
);
370+
mapIntoWithKeyPrefixInternal(children, result, null, child => child);
382371
return result;
383372
}
384373

0 commit comments

Comments
 (0)