Convert usage of String and Boolean constructors
Andrew Clark committed
Feb 28, 2017 at 13:20 UTC
1e9812e31b77df43bfcc3a6c531d44d56f053bd1
18 files changed
+47
-40
src/renderers/dom/fiber/ReactDOMFiber.js
+6
-4
@@ -196,8 +196,9 @@ var DOMRenderer = ReactFiberReconciler({
196
typeof props.children === 'string' ||
197
typeof props.children === 'number'
198
) {
199
+ const string = '' + props.children;
200
const ownAncestorInfo = updatedAncestorInfo(hostContextDev.ancestorInfo, type, null);
200
- validateDOMNesting(null, String(props.children), null, ownAncestorInfo);
201
+ validateDOMNesting(null, string, null, ownAncestorInfo);
202
}
203
parentNamespace = hostContextDev.namespace;
204
} else {
@@ -237,8 +238,9 @@ var DOMRenderer = ReactFiberReconciler({
238
typeof newProps.children === 'string' ||
239
typeof newProps.children === 'number'
240
)) {
241
+ const string = '' + newProps.children;
242
const ownAncestorInfo = updatedAncestorInfo(hostContextDev.ancestorInfo, type, null);
241
- validateDOMNesting(null, String(newProps.children), null, ownAncestorInfo);
243
+ validateDOMNesting(null, string, null, ownAncestorInfo);
244
}
245
}
246
return diffProperties(domElement, type, oldProps, newProps, rootContainerInstance);
@@ -411,9 +413,9 @@ var ReactDOM = {
413
}
414
415
if (__DEV__) {
414
- const isRootRenderedBySomeReact = Boolean(container._reactRootContainer);
416
+ const isRootRenderedBySomeReact = !!container._reactRootContainer;
417
const rootEl = getReactRootElementInContainer(container);
416
- const hasNonRootReactChild = Boolean(rootEl && ReactDOMComponentTree.getInstanceFromNode(rootEl));
418
+ const hasNonRootReactChild = !!(rootEl && ReactDOMComponentTree.getInstanceFromNode(rootEl));
419
420
warning(
421
!hasNonRootReactChild ||
src/renderers/dom/fiber/wrappers/ReactDOMFiberSelect.js
+10
-10
@@ -134,7 +134,7 @@ var ReactDOMSelect = {
134
var value = props.value;
135
node._wrapperState = {
136
initialValue: value != null ? value : props.defaultValue,
137
- wasMultiple: Boolean(props.multiple),
137
+ wasMultiple: !!props.multiple,
138
};
139
140
if (
@@ -153,11 +153,11 @@ var ReactDOMSelect = {
153
didWarnValueDefaultValue = true;
154
}
155
156
- node.multiple = Boolean(props.multiple);
156
+ node.multiple = !!props.multiple;
157
if (value != null) {
158
- updateOptions(node, Boolean(props.multiple), value);
158
+ updateOptions(node, !!props.multiple, value);
159
} else if (props.defaultValue != null) {
160
- updateOptions(node, Boolean(props.multiple), props.defaultValue);
160
+ updateOptions(node, !!props.multiple, props.defaultValue);
161
}
162
},
163
@@ -168,18 +168,18 @@ var ReactDOMSelect = {
168
node._wrapperState.initialValue = undefined;
169
170
var wasMultiple = node._wrapperState.wasMultiple;
171
- node._wrapperState.wasMultiple = Boolean(props.multiple);
171
+ node._wrapperState.wasMultiple = !!props.multiple;
172
173
var value = props.value;
174
if (value != null) {
175
- updateOptions(node, Boolean(props.multiple), value);
176
- } else if (wasMultiple !== Boolean(props.multiple)) {
175
+ updateOptions(node, !!props.multiple, value);
176
+ } else if (wasMultiple !== !!props.multiple) {
177
// For simplicity, reapply `defaultValue` if `multiple` is toggled.
178
if (props.defaultValue != null) {
179
- updateOptions(node, Boolean(props.multiple), props.defaultValue);
179
+ updateOptions(node, !!props.multiple, props.defaultValue);
180
} else {
181
// Revert the select back to its default unselected state.
182
- updateOptions(node, Boolean(props.multiple), props.multiple ? [] : '');
182
+ updateOptions(node, !!props.multiple, props.multiple ? [] : '');
183
}
184
}
185
},
@@ -189,7 +189,7 @@ var ReactDOMSelect = {
189
var value = props.value;
190
191
if (value != null) {
192
- updateOptions(node, Boolean(props.multiple), value);
192
+ updateOptions(node, !!props.multiple, value);
193
}
194
},
195
};
src/renderers/dom/shared/ReactBrowserEventEmitter.js
+1
-1
@@ -154,7 +154,7 @@ var topEventMapping = {
154
/**
155
* To ensure no conflicts with other potential React instances on the page
156
*/
157
-var topListenersIDKey = '_reactListenersID' + String(Math.random()).slice(2);
157
+var topListenersIDKey = '_reactListenersID' + ('' + Math.random()).slice(2);
158
159
function getListeningForDocument(mountAt) {
160
// In IE8, `mountAt` is a host object and doesn't have `hasOwnProperty`
src/renderers/dom/shared/ReactDOMComponentTree.js
+1
-1
@@ -31,7 +31,7 @@ var internalEventHandlersKey = '__reactEventHandlers$' + randomKey;
31
*/
32
function shouldPrecacheNode(node, nodeID) {
33
return (node.nodeType === 1 &&
34
- node.getAttribute(ATTR_NAME) === String(nodeID)) ||
34
+ node.getAttribute(ATTR_NAME) === ('' + nodeID)) ||
35
(node.nodeType === 8 &&
36
node.nodeValue === ' react-text: ' + nodeID + ' ') ||
37
(node.nodeType === 8 &&
src/renderers/dom/shared/validateDOMNesting.js
+1
-1
@@ -400,7 +400,7 @@ if (__DEV__) {
400
childTag,
401
ancestorInstance,
402
ancestorTag,
403
- Boolean(invalidParent)
403
+ !!invalidParent
404
) + '.';
405
} else {
406
addendum = getCurrentFiberStackAddendum();
src/renderers/dom/stack/client/ReactDOMComponent.js
+1
-1
@@ -185,7 +185,7 @@ if (__DEV__) {
185
return;
186
}
187
188
- validateDOMNesting(null, String(content), this, this._ancestorInfo);
188
+ validateDOMNesting(null, '' + content, this, this._ancestorInfo);
189
this._contentDebugID = contentDebugID;
190
if (hasExistingContent) {
191
ReactInstrumentation.debugTool.onBeforeUpdateComponent(contentDebugID, content);
src/renderers/dom/stack/client/ReactMount.js
+1
-1
@@ -440,7 +440,7 @@ var ReactMount = {
440
callback === null || typeof callback === 'function',
441
'render(...): Expected the last optional `callback` argument to be a ' +
442
'function. Instead received: %s.',
443
- String(callback)
443
+ '' + callback
444
);
445
}
446
if (!React.isValidElement(nextElement)) {
src/renderers/dom/stack/client/wrappers/ReactDOMSelect.js
+7
-7
@@ -135,7 +135,7 @@ var ReactDOMSelect = {
135
inst._wrapperState = {
136
initialValue: value != null ? value : props.defaultValue,
137
listeners: null,
138
- wasMultiple: Boolean(props.multiple),
138
+ wasMultiple: !!props.multiple,
139
};
140
141
if (
@@ -169,18 +169,18 @@ var ReactDOMSelect = {
169
inst._wrapperState.initialValue = undefined;
170
171
var wasMultiple = inst._wrapperState.wasMultiple;
172
- inst._wrapperState.wasMultiple = Boolean(props.multiple);
172
+ inst._wrapperState.wasMultiple = !!props.multiple;
173
174
var value = props.value;
175
if (value != null) {
176
- updateOptions(inst, Boolean(props.multiple), value);
177
- } else if (wasMultiple !== Boolean(props.multiple)) {
176
+ updateOptions(inst, !!props.multiple, value);
177
+ } else if (wasMultiple !== !!props.multiple) {
178
// For simplicity, reapply `defaultValue` if `multiple` is toggled.
179
if (props.defaultValue != null) {
180
- updateOptions(inst, Boolean(props.multiple), props.defaultValue);
180
+ updateOptions(inst, !!props.multiple, props.defaultValue);
181
} else {
182
// Revert the select back to its default unselected state.
183
- updateOptions(inst, Boolean(props.multiple), props.multiple ? [] : '');
183
+ updateOptions(inst, !!props.multiple, props.multiple ? [] : '');
184
}
185
}
186
},
@@ -191,7 +191,7 @@ var ReactDOMSelect = {
191
var value = props.value;
192
193
if (value != null) {
194
- updateOptions(inst, Boolean(props.multiple), value);
194
+ updateOptions(inst, !!props.multiple, value);
195
}
196
}
197
},
src/renderers/shared/fiber/ReactChildFiber.js
+3
-2
@@ -95,7 +95,7 @@ function coerceRef(current: Fiber | null, element: ReactElement) {
95
'bug in React. Please file an issue.',
96
mixedRef
97
);
98
- const stringRef = String(mixedRef);
98
+ const stringRef = '' + mixedRef;
99
// Check if previous string ref matches new string ref
100
if (current !== null && current.ref !== null && current.ref._stringRef === stringRef) {
101
return current.ref;
@@ -117,7 +117,8 @@ function coerceRef(current: Fiber | null, element: ReactElement) {
117
118
function throwOnInvalidObjectType(returnFiber : Fiber, newChild : Object) {
119
if (returnFiber.type !== 'textarea') {
120
- const childrenString = String(newChild);
120
+ // $FlowFixMe - Intentional cast to string
121
+ const childrenString = '' + newChild;
122
let addendum = '';
123
if (__DEV__) {
124
addendum =
src/renderers/shared/fiber/ReactFiberClassComponent.js
+2
-1
@@ -47,7 +47,8 @@ if (__DEV__) {
47
'%s(...): Expected the last optional `callback` argument to be a ' +
48
'function. Instead received: %s.',
49
callerName,
50
- String(callback)
50
+ // $FlowFixMe - Intentional cast to string
51
+ '' + callback
52
);
53
};
54
}
src/renderers/shared/fiber/ReactFiberReconciler.js
+2
-1
@@ -167,7 +167,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
167
callback === null || typeof callback === 'function',
168
'render(...): Expected the last optional `callback` argument to be a ' +
169
'function. Instead received: %s.',
170
- String(callback)
170
+ // $FlowFixMe - Intentional cast to string
171
+ '' + callback
172
);
173
}
174
addTopLevelUpdate(current, nextState, callback, priorityLevel);
src/renderers/shared/fiber/ReactFiberScheduler.js
+3
-3
@@ -776,7 +776,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
776
'by a bug in React. Please file an issue.'
777
);
778
isPerformingWork = true;
779
- const isPerformingDeferredWork = Boolean(deadline);
779
+ const isPerformingDeferredWork = !!deadline;
780
781
// This outer loop exists so that we can restart the work loop after
782
// catching an error. It also lets us flush Task work at the end of a
@@ -1034,7 +1034,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
1034
function hasCapturedError(fiber : Fiber) : boolean {
1035
// TODO: capturedErrors should store the boundary instance, to avoid needing
1036
// to check the alternate.
1037
- return Boolean(
1037
+ return (
1038
capturedErrors !== null &&
1039
(capturedErrors.has(fiber) || (fiber.alternate !== null && capturedErrors.has(fiber.alternate)))
1040
);
@@ -1043,7 +1043,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
1043
function isFailedBoundary(fiber : Fiber) : boolean {
1044
// TODO: failedBoundaries should store the boundary instance, to avoid
1045
// needing to check the alternate.
1046
- return Boolean(
1046
+ return (
1047
failedBoundaries !== null &&
1048
(failedBoundaries.has(fiber) || (fiber.alternate !== null && failedBoundaries.has(fiber.alternate)))
1049
);
src/renderers/shared/fiber/ReactFiberUpdateQueue.js
+3
-2
@@ -341,7 +341,7 @@ function addTopLevelUpdate(
341
callback : Callback | null,
342
priorityLevel : PriorityLevel
343
) : void {
344
- const isTopLevelUnmount = Boolean(
344
+ const isTopLevelUnmount = !!(
345
partialState &&
346
partialState.element === null
347
);
@@ -470,7 +470,8 @@ function commitCallbacks(finishedWork : Fiber, queue : UpdateQueue, context : mi
470
typeof callback === 'function',
471
'Invalid argument passed as callback. Expected a function. Instead ' +
472
'received: %s',
473
- String(callback)
473
+ // $FlowFixMe - Intentional cast to string
474
+ '' + callback
475
);
476
callback.call(context);
477
}
src/renderers/shared/stack/reconciler/ReactCompositeComponent.js
+1
-1
@@ -1002,7 +1002,7 @@ var ReactCompositeComponent = {
1002
) {
1003
var inst = this._instance;
1004
1005
- var hasComponentDidUpdate = Boolean(inst.componentDidUpdate);
1005
+ var hasComponentDidUpdate = !!inst.componentDidUpdate;
1006
var prevProps;
1007
var prevState;
1008
var prevContext;
src/renderers/shared/stack/reconciler/ReactUpdateQueue.js
+1
-1
@@ -24,7 +24,7 @@ if (__DEV__) {
24
'%s(...): Expected the last optional `callback` argument to be a ' +
25
'function. Instead received: %s.',
26
callerName,
27
- String(callback)
27
+ '' + callback
28
);
29
};
30
}
src/renderers/shared/utils/validateCallback.js
+2
-1
@@ -19,7 +19,8 @@ function validateCallback(callback: ?Function) {
19
!callback || typeof callback === 'function',
20
'Invalid argument passed as callback. Expected a function. Instead ' +
21
'received: %s',
22
- String(callback)
22
+ // $FlowFixMe - Intentional cast to string
23
+ '' + callback
24
);
25
}
26
src/shared/utils/traverseAllChildren.js
+1
-1
@@ -157,7 +157,7 @@ function traverseAllChildrenImpl(
157
}
158
}
159
}
160
- var childrenString = String(children);
160
+ var childrenString = '' + children;
161
invariant(
162
false,
163
'Objects are not valid as a React child (found: %s).%s',
src/test/reactComponentExpect.js
+1
-1
@@ -212,7 +212,7 @@ Object.assign(reactComponentExpectInternal.prototype, {
212
expect(this._instance.tag === HostText).toBe(true);
213
var actualVal = this._instance.memoizedProps;
214
expect(typeof actualVal === 'string' || typeof actualVal === 'number').toBe(true);
215
- expect(String(actualVal)).toBe(val);
215
+ expect('' + actualVal).toBe(val);
216
} else {
217
// Fiber reconciler
218
var elementType = typeof this._instance._currentElement;